Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit width of TextView #57

Open
sonnyp opened this issue Mar 17, 2022 · 0 comments
Open

Limit width of TextView #57

sonnyp opened this issue Mar 17, 2022 · 0 comments

Comments

@sonnyp
Copy link
Owner

sonnyp commented Mar 17, 2022

Follow up from #56

I've been experimenting with setting the TextView width to the desired max body length.
The benefit is that if you make Commit window bigger you can see what the text will wrap.
It would probably be a max with thought or making the window smaller / mobile device will have a scrolled area.

An other option would be to make the Window big enough to fit the textview - only if the screen is wider than the width of course.

The TextView should be centered too.

We can also add a ruler.

Something like this seems to work well - tested in Workbench

import Pango from "gi://Pango";

const textview = workbench.builder.get_object("textview");
const metrics = textview.get_pango_context()?.get_metrics(null, null);
const text =
  "foo sdfs fas fas df as dfa sdf sdf sd fs ldflasd falsdfkasdf kasdfk asdlkfask dflaksd fkals dfk asdkfl asdklf aslkd f dsf flkasd flskd flaksd faslkd f ";
textview.get_buffer().text = text;

const max_body_length = 75;
const character_width = metrics.get_approximate_char_width() / Pango.SCALE;

const total_width =
  character_width * max_body_length +
  1 +
  textview.right_margin +
  textview.left_margin;

textview.width_request = Math.ceil(total_width);
<?xml version="1.0" encoding="UTF-8" ?>
<interface>
  <object class="GtkBox" id="welcome">
    <property name="orientation">vertical</property>
    <property name="hexpand">true</property>
    <property name="vexpand">true</property>
    <child>
      <object class="GtkTextView" id="textview">
        <property name="left-margin">0</property>
        <property name="right-margin">0</property>
        <property name="halign">start</property>
        <property name="monospace">true</property>
        <property name="hexpand">true</property>
        <property name="vexpand">true</property>
        <property name="wrap-mode">word-char</property>
      </object>
    </child>
  </object>
</interface>
@sonnyp sonnyp changed the title Limit width of textview to body wrap setting Limit width of TextView Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant