aboutsummaryrefslogtreecommitdiff
path: root/src/utils.py
blob: 5ef3f37e36de9f15cb99284c2c1361425337fc69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()