By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

View raw Download
text/x-script.python • 4.6 kiB
Python script, ASCII text executable
        
            
1
import os
2
from pathlib import Path
3
import wayfire
4
import panorama_panel
5
6
import gi
7
from wayfire import WayfireSocket
8
9
gi.require_version("Gtk", "4.0")
10
11
from gi.repository import Gtk, GLib, Gio, Gdk
12
13
14
module_directory = Path(__file__).resolve().parent
15
16
17
"""
18
@Gtk.Template(filename=str(module_directory / "panorama-clock-options.ui"))
19
class ClockOptions(Gtk.Window):
20
__gtype_name__ = "ClockOptions"
21
format_entry: Gtk.Entry = Gtk.Template.Child()
22
23
def __init__(self, **kwargs):
24
super().__init__(**kwargs)
25
26
self.connect("close-request", lambda *args: self.destroy())
27
"""
28
29
30
class WindowButton(Gtk.ToggleButton):
31
def __init__(self, window_id, window_title, **kwargs):
32
super().__init__(**kwargs)
33
34
self.window_id = window_id
35
self.window_title = window_title
36
self.set_has_frame(False)
37
38
self.set_label(self.window_title)
39
40
41
class WFWindowList(panorama_panel.Applet):
42
name = "Wayfire window list"
43
description = "Traditional window list (for Wayfire)"
44
45
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
46
super().__init__(orientation=orientation, config=config)
47
if config is None:
48
config = {}
49
50
self.socket = WayfireSocket()
51
self.socket.watch()
52
fd = self.socket.client.fileno()
53
GLib.io_add_watch(fd, GLib.IO_IN, self.on_wf_event)
54
55
self.initial_button = Gtk.ToggleButton()
56
57
self.window_buttons: dict[int, WindowButton] = {}
58
59
for view in self.socket.list_views():
60
self.create_window_button(view)
61
62
self.context_menu = self.make_context_menu()
63
panorama_panel.track_popover(self.context_menu)
64
65
right_click_controller = Gtk.GestureClick()
66
right_click_controller.set_button(3)
67
right_click_controller.connect("pressed", self.show_context_menu)
68
69
self.add_controller(right_click_controller)
70
71
action_group = Gio.SimpleActionGroup()
72
options_action = Gio.SimpleAction.new("options", None)
73
options_action.connect("activate", self.show_options)
74
action_group.add_action(options_action)
75
self.insert_action_group("applet", action_group)
76
77
self.options_window = None
78
79
def create_window_button(self, view):
80
button = WindowButton(view["id"], view["title"])
81
button.set_group(self.initial_button)
82
83
self.window_buttons[view["id"]] = button
84
self.append(button)
85
86
def remove_window_button(self, view):
87
self.remove(self.window_buttons[view["id"]])
88
self.window_buttons[view["id"]] = None
89
90
def focus_window_button(self, view):
91
self.window_buttons[view["id"]].set_active(True)
92
93
def on_wf_event(self, source, condition):
94
if condition == GLib.IO_IN:
95
try:
96
message = self.socket.read_next_event()
97
event = message.get("event")
98
view = message.get("view", {})
99
print(f"[{event}] {view.get('app-id')} - {view.get('title')}")
100
match event:
101
case "view-mapped":
102
self.create_window_button(message["view"])
103
case "view-unmapped":
104
self.remove_window_button(message["view"])
105
case "view-focused":
106
self.focus_window_button(message["view"])
107
except Exception as e:
108
print("Error reading Wayfire event:", e)
109
return True
110
111
def make_context_menu(self):
112
menu = Gio.Menu()
113
menu.append("Window list _options", "applet.options")
114
context_menu = Gtk.PopoverMenu.new_from_model(menu)
115
context_menu.set_has_arrow(False)
116
context_menu.set_parent(self)
117
context_menu.set_halign(Gtk.Align.START)
118
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
119
return context_menu
120
121
def show_context_menu(self, gesture, n_presses, x, y):
122
rect = Gdk.Rectangle()
123
rect.x = int(x)
124
rect.y = int(y)
125
rect.width = 1
126
rect.height = 1
127
128
self.context_menu.set_pointing_to(rect)
129
self.context_menu.popup()
130
131
def show_options(self, _0=None, _1=None):
132
"""
133
if self.options_window is None:
134
self.options_window = ClockOptions()
135
self.options_window.format_entry.set_text(self.formatting)
136
self.options_window.format_entry.connect("changed", self.update_formatting)
137
138
def reset_window(*args):
139
self.options_window = None
140
141
self.options_window.connect("close-request", reset_window)
142
self.options_window.present()
143
"""
144
145
def get_config(self):
146
return {}
147