File: accel_manager.py

package info (click to toggle)
gnome-feeds 2.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,520 kB
  • sloc: python: 5,369; sh: 93; xml: 28; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,266 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
from typing import Callable, List
from gi.repository import Gtk


class Accelerator:
    def __init__(self, combo: str, cb: Callable):
        self.combo = combo
        self.cb = cb


def add_accelerators(
        window: Gtk.Window,
        shortcuts_l: List[Accelerator]
) -> Gtk.ShortcutController:
    shortcut_controller = Gtk.ShortcutController()
    shortcut_controller.set_scope(Gtk.ShortcutScope.GLOBAL)
    for s in shortcuts_l:
        __add_accelerator(
            shortcut_controller, s.combo, s.cb
        )
    window.add_controller(shortcut_controller)
    return shortcut_controller


def __add_accelerator(
        controller: Gtk.ShortcutController, shortcut: str, callback: Callable
):
    if shortcut:
        # res is bool, don't know what it is
        _, key, mod = Gtk.accelerator_parse(shortcut)
        gshcut = Gtk.Shortcut(
            trigger=Gtk.KeyvalTrigger(keyval=key, modifiers=mod),
            action=Gtk.CallbackAction.new(callback)
        )
        controller.add_shortcut(gshcut)


def add_mouse_button_accel(
        widget: Gtk.Widget, function: Callable,
        propagation: Gtk.PropagationPhase = Gtk.PropagationPhase.BUBBLE
) -> Gtk.GestureClick:
    '''Adds an accelerator for mouse btn press for widget to function.
    NOTE: this returns the Gtk.Gesture, you need to keep this around or it
    won't work. Assign it to some random variable and don't let it go out of
    scope'''

    gesture = Gtk.GestureClick.new()
    gesture.set_button(0)
    gesture.set_propagation_phase(propagation)
    gesture.connect('pressed', function)
    widget.add_controller(gesture)
    return gesture


def add_longpress_accel(
        widget: Gtk.Widget, function: Callable,
        propagation: Gtk.PropagationPhase = Gtk.PropagationPhase.BUBBLE
) -> Gtk.GestureLongPress:
    '''Adds an accelerator for mouse btn press for widget to function.
    NOTE: this returns the Gtk.Gesture, you need to keep this around or it
    won't work. Assign it to some random variable and don't let it go out of
    scope'''

    gesture = Gtk.GestureLongPress.new()
    gesture.set_propagation_phase(propagation)
    gesture.set_touch_only(False)
    gesture.connect('pressed', function)
    widget.add_controller(gesture)
    return gesture