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 • 1.94 kiB
Python script, ASCII text executable
        
            
1
import panorama_panel
2
3
import gi
4
gi.require_version("Gtk", "4.0")
5
6
from gi.repository import Gtk, GLib
7
8
9
SECOND_PLACEHOLDERS = ("%c", "%s", "%S", "%T", "%X")
10
11
12
class ClockApplet(panorama_panel.Applet):
13
name = "Clock"
14
description = "Read the current time and date"
15
16
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
17
super().__init__(orientation=orientation, config=config)
18
if config is None:
19
config = {}
20
self.button = Gtk.MenuButton()
21
self.button.set_has_frame(False) # flat look
22
self.label = Gtk.Label()
23
self.button.set_child(self.label)
24
25
# Create the monthly calendar
26
self.popover = Gtk.Popover()
27
self.calendar = Gtk.Calendar()
28
self.calendar.set_show_week_numbers(True)
29
self.popover.set_child(self.calendar)
30
self.button.set_popover(self.popover)
31
32
self.append(self.button)
33
34
self.formatting = config.get("formatting", "%c")
35
# Some placeholders require second precision, but not all of them. If not required,
36
# use minute precision
37
self.has_second_precision = any(placeholder in self.formatting for placeholder in SECOND_PLACEHOLDERS)
38
self.set_time()
39
40
def set_time(self):
41
datetime = GLib.DateTime.new_now_local()
42
self.label.set_text(datetime.format(self.formatting))
43
44
if self.has_second_precision:
45
current_ms = GLib.DateTime.new_now_local().get_microsecond() // 1000
46
GLib.timeout_add(1000 - current_ms + 1, self.set_time) # 1ms is added to ensure the clock is updated
47
else:
48
now = GLib.DateTime.new_now_local()
49
current_ms = now.get_second() * 1000 + now.get_microsecond() // 1000
50
GLib.timeout_add(60000 - current_ms + 1, self.set_time)
51
return False # Do not rerun the current timeout; a new one has been scheduled
52
53
def get_config(self):
54
return {"text": self.label.get_text()}
55