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