8000 gtk/tk: Ensure no flicker when hovering over images. · matplotlib/matplotlib@7803062 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7803062

Browse files
committed
gtk/tk: Ensure no flicker when hovering over images.
When the mouse is over an image, the status message is two lines long. This causes the toolbar to increase size slightly. On Qt, this is ignored by a sizing policy, but there is no such thing in other toolkits. On GTK this steals space from the canvas, so it re-draws a little bit smaller, until the mouse moves off the image. On Tk, the space is added to the window, _unless_ the window is maximized, and then it's stolen from the canvas as well. As a simple workaround, we add an additional label with invisible text (spaces) on two lines. This forces the toolbar to always be two lines high and thus not flicker. If the message were 3 or more lines long, then the same thing would happen, but we don't have any of those by default. The advantage of using a label is that it saves us having to measure text in some toolkit-specific way, and thus works regardless of theme.
1 parent 98474c8 commit 7803062

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ def __init__(self, canvas, window, *, pack_toolbar=True):
512512
if tooltip_text is not None:
513513
ToolTip.createToolTip(button, tooltip_text)
514514

515+
# This filler item ensures the toolbar is always at least two text
516+
# lines high. Otherwise the canvas gets redrawn as the mouse hovers
517+
# over images because those use two-line messages which resize the
518+
# toolbar.
519+
label = tk.Label(master=self,
520+
text='\N{NO-BREAK SPACE}\n\N{NO-BREAK SPACE}')
521+
label.pack(side=tk.RIGHT)
522+
515523
self.message = tk.StringVar(master=self)
516524
self._message_label = tk.Label(master=self, textvariable=self.message)
517525
self._message_label.pack(side=tk.RIGHT)

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,17 @@ def __init__(self, canvas, window):
521521
toolitem.set_draw(False)
522522
toolitem.set_expand(True)
523523

524+
# This filler item ensures the toolbar is always at least two text
525+
# lines high. Otherwise the canvas gets redrawn as the mouse hovers
526+
# over images because those use two-line messages which resize the
527+
# toolbar.
528+
toolitem = Gtk.ToolItem()
529+
self.insert(toolitem, -1)
530+
label = Gtk.Label()
531+
label.set_markup(
532+
'<small>\N{NO-BREAK SPACE}\n\N{NO-BREAK SPACE}</small>')
533+
toolitem.add(label)
534+
524535
toolitem = Gtk.ToolItem()
525536
self.insert(toolitem, -1)
526537
self.message = Gtk.Label()
@@ -536,7 +547,8 @@ def ctx(self):
536547
return self.canvas.get_property("window").cairo_create()
537548

538549
def set_message(self, s):
539-
self.message.set_label(s)
550+
escaped = GLib.markup_escape_text(s)
551+
self.message.set_markup(f'<small>{escaped}</small>')
540552

541553
def set_cursor(self, cursor):
542554
self.canvas.get_property("window").set_cursor(cursord[cursor])

0 commit comments

Comments
 (0)
0