__init__.py
Python script, ASCII text executable
1
"""
2
Clock applet for the Panorama panel.
3
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public Licence for more details.
14
15
You should have received a copy of the GNU General Public Licence
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
"""
18
19
import os
20
import locale
21
from pathlib import Path
22
import panorama_panel
23
24
import gi
25
gi.require_version("Gtk", "4.0")
26
27
from gi.repository import Gtk, GLib, Gio, Gdk
28
29
30
SECOND_PLACEHOLDERS = ("%c", "%s", "%S", "%T", "%X")
31
32
33
module_directory = Path(__file__).resolve().parent
34
locale.bindtextdomain("panorama-panel-clock", module_directory / "locale")
35
_ = lambda x: locale.dgettext("panorama-panel-clock", x)
36
37
38
@Gtk.Template(filename=str(module_directory / "panorama-clock-options.ui"))
39
class ClockOptions(Gtk.Window):
40
__gtype_name__ = "ClockOptions"
41
format_entry: Gtk.Entry = Gtk.Template.Child()
42
43
def __init__(self, **kwargs):
44
super().__init__(**kwargs)
45
46
self.connect("close-request", lambda *args: self.destroy())
47
48
49
class ClockApplet(panorama_panel.Applet):
50
name = _("Clock")
51
description = _("Read the current time and date")
52
53
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
54
super().__init__(orientation=orientation, config=config)
55
if config is None:
56
config = {}
57
self.button = Gtk.MenuButton()
58
self.button.set_has_frame(False) # flat look
59
self.label = Gtk.Label()
60
self.button.set_child(self.label)
61
62
# Create the monthly calendar
63
self.popover = Gtk.Popover()
64
panorama_panel.track_popover(self.popover)
65
self.calendar = Gtk.Calendar()
66
self.calendar.set_show_week_numbers(True)
67
self.popover.set_child(self.calendar)
68
self.button.set_popover(self.popover)
69
70
self.append(self.button)
71
72
self.formatting = config.get("formatting", "%c")
73
# Some placeholders require second precision, but not all of them. If not required,
74
# use minute precision
75
self.has_second_precision = any(placeholder in self.formatting for placeholder in SECOND_PLACEHOLDERS)
76
self.next_update = None
77
self.set_time()
78
79
self.context_menu = self.make_context_menu()
80
panorama_panel.track_popover(self.context_menu)
81
82
right_click_controller = Gtk.GestureClick()
83
right_click_controller.set_button(3)
84
right_click_controller.connect("pressed", self.show_context_menu)
85
86
self.add_controller(right_click_controller)
87
88
action_group = Gio.SimpleActionGroup()
89
options_action = Gio.SimpleAction.new("options", None)
90
options_action.connect("activate", self.show_options)
91
action_group.add_action(options_action)
92
self.insert_action_group("applet", action_group)
93
94
self.options_window = None
95
96
def make_context_menu(self):
97
menu = Gio.Menu()
98
menu.append(_("Clock _options"), "applet.options")
99
context_menu = Gtk.PopoverMenu.new_from_model(menu)
100
context_menu.set_has_arrow(False)
101
context_menu.set_parent(self)
102
context_menu.set_halign(Gtk.Align.START)
103
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
104
return context_menu
105
106
def show_context_menu(self, gesture, n_presses, x, y):
107
rect = Gdk.Rectangle()
108
rect.x = int(x)
109
rect.y = int(y)
110
rect.width = 1
111
rect.height = 1
112
113
self.context_menu.set_pointing_to(rect)
114
self.context_menu.popup()
115
116
def update_formatting(self, entry):
117
self.formatting = entry.get_text()
118
# Some placeholders require second precision, but not all of them. If not required,
119
# use minute precision
120
self.has_second_precision = any(placeholder in self.formatting for placeholder in SECOND_PLACEHOLDERS)
121
if self.next_update is not None:
122
GLib.source_remove(self.next_update)
123
self.next_update = None
124
self.set_time()
125
self.emit("config-changed")
126
127
def show_options(self, _0=None, _1=None):
128
if self.options_window is None:
129
self.options_window = ClockOptions()
130
self.options_window.format_entry.set_text(self.formatting)
131
self.options_window.format_entry.connect("changed", self.update_formatting)
132
133
def reset_window(*args):
134
self.options_window = None
135
136
self.options_window.connect("close-request", reset_window)
137
self.options_window.present()
138
139
def set_time(self):
140
datetime = GLib.DateTime.new_now_local()
141
formatted_time = datetime.format(self.formatting)
142
if formatted_time is not None:
143
self.label.set_text(datetime.format(self.formatting))
144
else:
145
self.label.set_text(_("Invalid time formatting"))
146
return False
147
148
if self.has_second_precision:
149
current_ms = GLib.DateTime.new_now_local().get_microsecond() // 1000
150
self.next_update = GLib.timeout_add(1000 - current_ms + 1, self.set_time) # 1ms is added to ensure the clock is updated
151
else:
152
now = GLib.DateTime.new_now_local()
153
current_ms = now.get_second() * 1000 + now.get_microsecond() // 1000
154
self.next_update = GLib.timeout_add(60000 - current_ms + 1, self.set_time)
155
return False # Do not rerun the current timeout; a new one has been scheduled
156
157
def get_config(self):
158
return {"formatting": self.formatting}
159
160
def set_panel_position(self, position):
161
self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position])
162
self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]])
163