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.78 kiB
Python script, ASCII text executable
        
            
1
import os
2
from pathlib import Path
3
import panorama_panel
4
5
import gi
6
gi.require_version("Gtk", "4.0")
7
8
from gi.repository import Gtk, GLib, Gio, Gdk
9
10
11
SECOND_PLACEHOLDERS = ("%c", "%s", "%S", "%T", "%X")
12
13
14
module_directory = Path(__file__).resolve().parent
15
16
17
@Gtk.Template(filename=str(module_directory / "panorama-clock-options.ui"))
18
class ClockOptions(Gtk.Window):
19
__gtype_name__ = "ClockOptions"
20
format_entry: Gtk.Entry = Gtk.Template.Child()
21
22
def __init__(self, **kwargs):
23
super().__init__(**kwargs)
24
25
self.connect("close-request", lambda *args: self.destroy())
26
27
28
class ClockApplet(panorama_panel.Applet):
29
name = "Clock"
30
description = "Read the current time and date"
31
32
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
33
super().__init__(orientation=orientation, config=config)
34
if config is None:
35
config = {}
36
self.button = Gtk.MenuButton()
37
self.button.set_has_frame(False) # flat look
38
self.label = Gtk.Label()
39
self.button.set_child(self.label)
40
41
# Create the monthly calendar
42
self.popover = Gtk.Popover()
43
self.calendar = Gtk.Calendar()
44
self.calendar.set_show_week_numbers(True)
45
self.popover.set_child(self.calendar)
46
self.button.set_popover(self.popover)
47
48
self.append(self.button)
49
50
self.formatting = config.get("formatting", "%c")
51
# Some placeholders require second precision, but not all of them. If not required,
52
# use minute precision
53
self.has_second_precision = any(placeholder in self.formatting for placeholder in SECOND_PLACEHOLDERS)
54
self.next_update = None
55
self.set_time()
56
57
self.context_menu = self.make_context_menu()
58
59
right_click_controller = Gtk.GestureClick()
60
right_click_controller.set_button(3)
61
right_click_controller.connect("pressed", self.show_context_menu)
62
63
self.add_controller(right_click_controller)
64
65
action_group = Gio.SimpleActionGroup()
66
options_action = Gio.SimpleAction.new("options", None)
67
options_action.connect("activate", self.show_options)
68
action_group.add_action(options_action)
69
self.insert_action_group("applet", action_group)
70
71
self.options_window = None
72
73
def make_context_menu(self):
74
menu = Gio.Menu()
75
menu.append("Clock _options", "applet.options")
76
context_menu = Gtk.PopoverMenu.new_from_model(menu)
77
context_menu.set_has_arrow(False)
78
context_menu.set_parent(self)
79
context_menu.set_halign(Gtk.Align.START)
80
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
81
return context_menu
82
83
def show_context_menu(self, gesture, n_presses, x, y):
84
rect = Gdk.Rectangle()
85
rect.x = int(x)
86
rect.y = int(y)
87
rect.width = 1
88
rect.height = 1
89
90
self.context_menu.set_pointing_to(rect)
91
self.context_menu.popup()
92
93
def update_formatting(self, entry):
94
self.formatting = entry.get_text()
95
# Some placeholders require second precision, but not all of them. If not required,
96
# use minute precision
97
self.has_second_precision = any(placeholder in self.formatting for placeholder in SECOND_PLACEHOLDERS)
98
if self.next_update is not None:
99
GLib.source_remove(self.next_update)
100
self.next_update = None
101
self.set_time()
102
103
def show_options(self, _0=None, _1=None):
104
if self.options_window is None:
105
self.options_window = ClockOptions()
106
self.options_window.format_entry.set_text(self.formatting)
107
self.options_window.format_entry.connect("changed", self.update_formatting)
108
109
def reset_window(*args):
110
self.options_window = None
111
112
self.options_window.connect("close-request", reset_window)
113
self.options_window.present()
114
115
def set_time(self):
116
datetime = GLib.DateTime.new_now_local()
117
formatted_time = datetime.format(self.formatting)
118
if formatted_time is not None:
119
self.label.set_text(datetime.format(self.formatting))
120
else:
121
self.label.set_text("Invalid time formatting")
122
return False
123
124
if self.has_second_precision:
125
current_ms = GLib.DateTime.new_now_local().get_microsecond() // 1000
126
self.next_update = GLib.timeout_add(1000 - current_ms + 1, self.set_time) # 1ms is added to ensure the clock is updated
127
else:
128
now = GLib.DateTime.new_now_local()
129
current_ms = now.get_second() * 1000 + now.get_microsecond() // 1000
130
self.next_update = GLib.timeout_add(60000 - current_ms + 1, self.set_time)
131
return False # Do not rerun the current timeout; a new one has been scheduled
132
133
def get_config(self):
134
return {"formatting": self.formatting}
135