aboutsummaryrefslogtreecommitdiff
path: root/src/utils.py
diff options
context:
space:
mode:
authorJoris Guyonvarch2025-12-26 18:41:26 +0100
committerJoris Guyonvarch2025-12-27 20:41:44 +0100
commita110c200e86d2325af07167531fac0f61d9681a0 (patch)
tree90e843f915a2e153ba735849afd83710d90560bf /src/utils.py
parenta26d92ad5055fa057647158eb79511e7b1841162 (diff)
Switch to GUI to manage the library
Allow to regroup the CLI and the view into one unique tool.
Diffstat (limited to 'src/utils.py')
-rw-r--r--src/utils.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/utils.py b/src/utils.py
new file mode 100644
index 0000000..5ef3f37
--- /dev/null
+++ b/src/utils.py
@@ -0,0 +1,58 @@
+import gi
+gi.require_version('Gtk', '4.0')
+from gi.repository import Gtk, GLib, GdkPixbuf, Gdk
+import unicodedata
+
+def set_header_bar(window, title):
+ header_bar = Gtk.HeaderBar()
+ header_bar.set_show_title_buttons(True)
+ title_widget = Gtk.Label()
+ title_widget.set_text(title)
+ window.set_title(title)
+ header_bar.set_title_widget(title_widget)
+ window.set_titlebar(header_bar)
+
+def set_margin(widget, a, b = None, c = None, d = None):
+ if b == c == d == None:
+ b = c = d = a
+ elif c == d == None:
+ (c, d) = (a, b)
+ elif d == None:
+ d = b
+ widget.set_margin_top(a)
+ widget.set_margin_end(b)
+ widget.set_margin_bottom(c)
+ widget.set_margin_start(d)
+
+def configure_dialog(window, parent_window, title, width=600, height=400):
+ window.use_header_bar = True
+ window.set_modal(True)
+ window.set_transient_for(parent_window)
+ window.set_default_size(width or 0, height or 0)
+ set_header_bar(window, title)
+
+ control_key = Gtk.EventControllerKey.new()
+ control_key.connect('key-pressed', on_dialog_key_pressed, window)
+ window.add_controller(control_key)
+
+def on_dialog_key_pressed(a, b, key, c, window):
+ if key == 9: # Escape
+ window.close()
+
+def label(text):
+ l = Gtk.Label()
+ l.set_text(text)
+ l.set_wrap(True)
+ l.set_halign(Gtk.Align.START)
+ return l
+
+def entry():
+ return Gtk.Entry()
+
+# https://gitlab.gnome.org/GNOME/pygobject/-/issues/225
+# https://gist.github.com/mozbugbox/10cd35b2872628246140
+def image_to_pixbuf(image):
+ image = image.convert('RGB')
+ data = GLib.Bytes.new(image.tobytes())
+ pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB, False, 8, image.width, image.height, len(image.getbands())*image.width)
+ return pixbuf.copy()