Commit
Commit copied to clipboard
Limit width of TextView
Follow up from https://github.com/sonnyp/Commit/pull/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>