[meld] ui.statusbar: Move line-column display, and use that UI for go-to-line



commit a110252c4dcdfcb3b2c4a94cc75b4599228f0d8b
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Fri Nov 24 07:34:20 2017 +1000

    ui.statusbar: Move line-column display, and use that UI for go-to-line
    
    This isn't hooked up to FileDiff yet, but everything else is ready.

 meld/filediff.py     |   11 ++------
 meld/ui/statusbar.py |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 8 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 234b93a..9cbd052 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -218,9 +218,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         for statusbar, buf in zip(self.statusbar, self.textbuffer):
             overwrite_label = Gtk.Label()
             overwrite_label.show()
-            cursor_label = Gtk.Label()
-            cursor_label.show()
-            pane_labels = [overwrite_label, cursor_label]
+            pane_labels = [overwrite_label]
             self.status_info_labels.append(pane_labels)
 
             statusbar.set_info_box(pane_labels)
@@ -353,8 +351,6 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
 
     # Abbreviations for insert and overwrite that fit in the status bar
     _insert_overwrite_text = (_("INS"), _("OVR"))
-    # Abbreviation for line, column so that it will fit in the status bar
-    _line_column_text = _("Ln %i, Col %i")
 
     def on_cursor_position_changed(self, buf, pspec, force=False):
         pane = self.textbuffer.index(buf)
@@ -368,10 +364,9 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         line = cursor_it.get_line()
 
         insert_overwrite = self._insert_overwrite_text[self.textview_overwrite]
-        line_column = self._line_column_text % (line + 1, offset + 1)
-        overwrite_label, cursor_label = self.status_info_labels[pane]
+        self.statusbar[pane].props.cursor_position = (line, offset)
+        overwrite_label = self.status_info_labels[pane][0]
         overwrite_label.set_text(insert_overwrite)
-        cursor_label.set_text(line_column)
 
         if line != self.cursor.line or force:
             chunk, prev, next_ = self.linediffer.locate_chunk(pane, line)
diff --git a/meld/ui/statusbar.py b/meld/ui/statusbar.py
index f9c0620..27570bd 100644
--- a/meld/ui/statusbar.py
+++ b/meld/ui/statusbar.py
@@ -18,6 +18,7 @@ from gi.repository import Gtk
 from gi.repository import GtkSource
 from gi.repository import Pango
 
+from meld.conf import _
 from meld.ui.bufferselectors import EncodingSelector
 from meld.ui.bufferselectors import SourceLangSelector
 
@@ -102,10 +103,18 @@ class MeldStatusBar(Gtk.Statusbar):
     __gtype_name__ = "MeldStatusBar"
 
     __gsignals__ = {
+        'go-to-line': (
+            GObject.SignalFlags.RUN_FIRST, None, (int,)),
         'encoding-changed': (
             GObject.SignalFlags.RUN_FIRST, None, (GtkSource.Encoding,)),
     }
 
+    cursor_position = GObject.property(
+        type=object,
+        nick="The position of the cursor displayed in the status bar",
+        default=None,
+    )
+
     source_encoding = GObject.property(
         type=GtkSource.Encoding,
         nick="The file encoding displayed in the status bar",
@@ -118,6 +127,9 @@ class MeldStatusBar(Gtk.Statusbar):
         default=None,
     )
 
+    # Abbreviation for line, column so that it will fit in the status bar
+    _line_column_text = _("Ln %i, Col %i")
+
     def __init__(self):
         GObject.GObject.__init__(self)
         self.props.margin = 0
@@ -140,10 +152,64 @@ class MeldStatusBar(Gtk.Statusbar):
 
         self.box_box = Gtk.HBox(homogeneous=False, spacing=6)
         self.pack_end(self.box_box, False, True, 0)
+        self.box_box.pack_end(self.construct_line_display(), False, True, 0)
         self.box_box.pack_end(self.construct_highlighting_selector(), False, True, 0)
         self.box_box.pack_end(self.construct_encoding_selector(), False, True, 0)
         self.box_box.show_all()
 
+    def construct_line_display(self):
+
+        # Note that we're receiving one-based line numbers from the
+        # user and storing and emitting zero-base line numbers.
+
+        def line_entry_mapped(entry):
+            line, offset = self.props.cursor_position
+            entry.set_text(str(line + 1))
+
+        def line_entry_insert_text(entry, new_text, length, position):
+            if not new_text.isdigit():
+                GObject.signal_stop_emission_by_name(entry, 'insert-text')
+                return
+
+        def line_entry_activated(entry):
+            try:
+                line = int(entry.get_text())
+            except ValueError:
+                return
+            self.emit('go-to-line', max(0, line - 1))
+
+        entry = Gtk.Entry()
+        entry.set_icon_from_icon_name(
+            Gtk.EntryIconPosition.PRIMARY, 'go-jump-symbolic')
+        entry.set_icon_activatable(Gtk.EntryIconPosition.PRIMARY, False)
+        entry.set_icon_from_icon_name(
+            Gtk.EntryIconPosition.SECONDARY, 'edit-clear-symbolic')
+        entry.set_input_purpose(Gtk.InputPurpose.DIGITS)
+        entry.connect('map', line_entry_mapped)
+        entry.connect('insert-text', line_entry_insert_text)
+        entry.connect('activate', line_entry_activated)
+
+        selector = Gtk.Grid()
+        selector.add(entry)
+        selector.show_all()
+
+        pop = Gtk.Popover()
+        pop.set_position(Gtk.PositionType.TOP)
+        pop.add(selector)
+
+        def format_cursor_position(binding, cursor):
+            line, offset = cursor
+            return self._line_column_text % (line + 1, offset + 1)
+
+        button = MeldStatusMenuButton()
+        self.bind_property(
+            'cursor_position', button, 'label', GObject.BindingFlags.DEFAULT,
+            format_cursor_position)
+        button.set_popover(pop)
+        button.show()
+
+        return button
+
     def construct_encoding_selector(self):
         def change_encoding(selector, encoding):
             self.props.source_encoding = encoding


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]