From 26f3f081061f385f02942b926e8fb256fafacb04 Mon Sep 17 00:00:00 2001 From: Stuart Langridge Date: Thu, 6 Jun 2019 11:43:11 +0100 Subject: Keyboard shortcuts for zooming in and out Adds support for minus and plus to zoom out and in. Uses Keybinder. This will need to be added as a dependency to the deb. Partial completion of #4 and the remainder of that issue can also maybe be dealt with here. --- magnus | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/magnus b/magnus index 8fb8f1a..c039c94 100755 --- a/magnus +++ b/magnus @@ -8,7 +8,9 @@ import sys from functools import lru_cache import gi gi.require_version('Gtk', '3.0') -from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, Gio # noqa: E402 +gi.require_version('Keybinder', '3.0') +from gi.repository import \ + Gtk, Gdk, GLib, GdkPixbuf, Gio, Keybinder # noqa: E402 __VERSION__ = "1.0.1" @@ -97,6 +99,12 @@ class Main(object): scrolled_window.add(self.img) self.w.add(scrolled_window) + # bind the zoom keyboard shortcuts + Keybinder.init() + if Keybinder.supported(): + Keybinder.bind("plus", self.zoom_in, zoom) + Keybinder.bind("minus", self.zoom_out, zoom) + # and, go self.w.show_all() @@ -111,6 +119,19 @@ class Main(object): GLib.idle_add(self.load_config) + def zoom_out(self, keypress, zoom): + current_index = zoom.get_active() + if current_index == 0: return + zoom.set_active(current_index - 1) + self.set_zoom(zoom) + + def zoom_in(self, keypress, zoom): + current_index = zoom.get_active() + size = zoom.get_model().iter_n_children(None) + if current_index == size - 1: return + zoom.set_active(current_index + 1) + self.set_zoom(zoom) + def read_window_decorations_size(self, win, alloc): sz = self.w.get_size() self.decorations_width = alloc.width - sz.width @@ -118,6 +139,7 @@ class Main(object): def set_zoom(self, zoom): self.zoomlevel = int(zoom.get_active_text()[0]) + self.poll(force_refresh=True) def read_window_size(self, *args): loc = self.w.get_size() @@ -173,12 +195,13 @@ class Main(object): arr, GdkPixbuf.Colorspace.RGB, True, 8, width, height, width * len(light)) - def poll(self): + def poll(self, force_refresh=False): display = Gdk.Display.get_default() (screen, x, y, modifier) = display.get_pointer() if x == self.last_x and y == self.last_y: # bail if nothing would be different - return True + if not force_refresh: + return True self.last_x = x self.last_y = y if (x > self.window_x and -- cgit