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