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