Saturday, 25 September 2021

Why do empty lines in Gtk.TextView misalign with line numbers but lines with text don't?

I have a Gtk application written in Python 3, and I want to make a line numbers widget. My current tactic is to use two Gtk.TextViews in a Gtk.HBox which is in a Gtk.ScrolledWindow. I have a css theme loaded that sets the TextViews' font to monospace, but there's a problem.

When I type lines of text in the main TextView, the line numbers line up with the text I'm typing. However, when I type a bunch of empty lines, the line numbers gradually get misaligned with the text.

Here's the code I'm using:

import gi

gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk

class Window(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("delete-event", Gtk.main_quit)

        self.sw = Gtk.ScrolledWindow()
        self.add(self.sw)

        self.sw_box = Gtk.HBox()
        self.sw.add(self.sw_box)

        self.t1 = Gtk.TextView()
        self.t1.do_insert_at_cursor(self.t1, "1\n" * 50)
        self.t1.set_editable(False)
        self.sw_box.pack_start(self.t1, False, False, 0)
        
        self.t2 = Gtk.TextView()
        self.sw_box.pack_start(self.t2, True, True, 0)

        self.load_theme()

        self.show_all()

    def load_theme(self):
        """Load the theme."""
        provider = Gtk.CssProvider.new()
        display = Gdk.Display.get_default()
        screen = display.get_default_screen()
        Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
        provider.load_from_path("theme.css")

if __name__ == "__main__":
    w = Window()
    Gtk.main()

theme.css

* {
    background-clip: padding-box;
    -GtkToolButton-icon-spacing: 4;
    -GtkTextView-error-underline-color: #FC4138;
    -GtkScrolledWindow-scrollbar-spacing: 0;
    -GtkToolItemGroup-expander-size: 11;
    -GtkWidget-text-handle-width: 20;
    -GtkWidget-text-handle-height: 20;
    -GtkDialog-button-spacing: 4;
    -GtkDialog-action-area-border: 0;
    outline-color: alpha(currentColor,0.3);
    outline-style: dashed;
    outline-offset: -3px;
    outline-width: 1px;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
    -gtk-outline-radius: 2px;
}

.gtkstyle-fallback:selected {
    background: #ff00ff;
}

.background {
    color: #D3DAE3;
    background: #000000;
}

*:selected {
    color: #000000;
    background-color: #ffff00;
}
*:disabled {
    -gtk-icon-effect: dim;
    background: #000000;
}

button {
    background-image: none;
    border-color: #000000;
}
button:active {
    border-color: #ffff00;
}

menuitem {
    background: #000000;
    box-shadow: none;
    border: 1px solid;
    border-color: #000000;
}
menuitem:hover {
    color: #ffffff;
    border: 1px solid;
    border-color: #ffff00;
}

messagedialog .titlebar {
    background: #000000;
}

notebook tabs{
    background: #000000;
}
notebook tab:checked {
    border: 1px solid;
    border-color: #ffff00;
    box-shadow: none;
}

scrollbar slider {
    background-color: #aaaa00;
}
scrollbar slider:active {
    background-color: #ffff00;
}

scrolledwindow {
    color: #ffffff;
    border: 1px solid;
    border-color: #ffff00;
}

textview {
    font-family: monospace;
}
textview text {
    color: #ffffff;
    background-color: #000000;
}
textview text selection {
    color: #000000;
    background-color: #ffffff;
}

The text is aligned when there are no empty lines The text is misaligned when there are several empty lines

Does anyone know why this is happening, and how to prevent it?



from Why do empty lines in Gtk.TextView misalign with line numbers but lines with text don't?

No comments:

Post a Comment