-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_app.py
executable file
·175 lines (150 loc) · 6.98 KB
/
time_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
#
# Copyright 2014 Paul Donohue <Tray_Apps@PaulSD.com>
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program. If
# not, see <http://www.gnu.org/licenses/>.
#
#
# Prerequisites:
# Install GtkTrayIcon (from the gtktrayicon/ subdirectory)
# sudo apt-get install --no-install-recommends libgirepository1.0-dev gobject-introspection \
# gir1.2-gtk-3.0
#
# Use `None` for a transparent background.
# In Ubuntu 18.04 (trayer 1.1.7, gtk 3.22.30), transparency worked fine. However, in Ubuntu 20.04
# (trayer 1.1.8, gtk 3.24.20), transparency does not work. Specifically, the visual area of the
# icon is never cleared, so at startup any existing icon that was moved to make space for the new
# icon will remain visible in the new icon's background, and any updates to the icon text will draw
# over the previous text. I'm not sure what is causing it, but a simple fix is to set a background
# color instead of using a transparent background.
#background_color = None
background_color = '#9A9A9A'
import gi
gi.require_version('Gtkti', '3.0')
from gi.repository import Gtkti, Gtk, Gdk, GLib
import signal, sys, os
import threading
import datetime
# In Python 2, threading.Event().wait() wakes up frequently and burns a lot of CPU.
# This does not happen in Python 3, so I'm simply using Python 3 instead of Python 2 for this app.
# See: http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
# I don't know if there are any work-arounds for this issue in Python 2.
# WARNING: Variable scope for Python inline functions and lambdas does not work like other
# languages! To ensure that definition-scope variables are passed into the function/lambda's scope
# as expected, explicitly add 'var=var' (optional/defaulted) parameters to the end of the function/
# lambda's parameter list.
class TimeApp:
def __init__(self):
self.show_date = True
self.prefix = ''
#self.date_format = '%x ' # Locale-appropriate date format
self.date_format = '%Y.%m.%d '
self.time_format = '%H:%M'
self.show_seconds = False
self.seconds_format = ':%S'
self.suffix = ' '
self.time_fudge = datetime.timedelta(seconds=.25)
self.build_ui()
self.gtk_update_ui()
self.start_update_thread()
def build_ui(self):
self.tray = tray = Gtkti.TrayIcon()
eventbox = Gtk.EventBox()
if background_color:
css = Gtk.CssProvider()
css.load_from_data(('* { background-color: '+background_color+'; }').encode())
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
tray.add(eventbox)
self.tray_label = tray_label = Gtk.Label(label=self.prefix+self.suffix)
eventbox.add(tray_label)
tray.show_all()
menu = Gtk.Menu()
item_show_date = Gtk.CheckMenuItem(label='Show Date')
item_show_date.set_active(self.show_date)
def toggle_date(item_show_date, self=self):
self.show_date = item_show_date.get_active()
self.gtk_update_ui()
item_show_date.connect('toggled', toggle_date)
menu.append(item_show_date)
item_show_seconds = Gtk.CheckMenuItem(label='Show Seconds')
item_show_seconds.set_active(self.show_seconds)
self.toggle_seconds_event = threading.Event()
def toggle_seconds(item_show_seconds, self=self):
self.show_seconds = item_show_seconds.get_active()
# Wake the update thread, which will update the UI, then sleep again
self.toggle_seconds_event.set()
item_show_seconds.connect('toggled', toggle_seconds)
menu.append(item_show_seconds)
item_quit = Gtk.MenuItem(label='Quit')
def quit(menu_item):
if sys.version_info < (3, 0):
os.kill(os.getpid(), signal.SIGINT)
else:
Gtk.main_quit()
item_quit.connect('activate', quit)
menu.append(item_quit)
menu.show_all()
def button_pressed(eventbox, event, menu=menu):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
menu.popup(None, None, None, None, event.button, event.time)
eventbox.connect('button-press-event', button_pressed)
# Update the UI (thread-safe)
def update_ui(self):
GLib.idle_add(self.gtk_update_ui)
# Update the UI (within the GTK main thread ; not thread-safe)
def gtk_update_ui(self):
fmt = self.prefix
if self.show_date: fmt += self.date_format
fmt += self.time_format
if self.show_seconds: fmt += self.seconds_format
fmt += self.suffix
# Update events should fire as close as possible to the second or minute boundary, but if they
# fire early, the previous time will incorrectly be displayed until the next update. Fudge the
# time to make sure the display is incremented even if the event fires slightly early.
now = datetime.datetime.now() + self.time_fudge
self.tray_label.set_text(now.strftime(fmt))
# Return false to unregister this method as a GLib idle handler
return False
def start_update_thread(self):
def run_in_thread(self=self):
while True:
fired_update = datetime.datetime.utcnow()
self.update_ui()
now = datetime.datetime.utcnow()
if self.show_seconds:
time_to_next_update = 1 - now.microsecond/1000000.0
if (1000000.0 - fired_update.microsecond) < self.time_fudge.microseconds:
time_to_next_update += 1
else:
time_to_next_update = 60 - now.second - now.microsecond/1000000.0
if time_to_next_update < 1 and (1000000.0 - fired_update.microsecond) < self.time_fudge.microseconds:
time_to_next_update += 60
self.toggle_seconds_event.wait(time_to_next_update) ; self.toggle_seconds_event.clear()
thread = threading.Thread(target=run_in_thread)
thread.daemon = True
thread.start()
if __name__ == '__main__':
TimeApp()
def on_sigint(_signum, _frame):
Gtk.main_quit()
signal.signal(signal.SIGINT, on_sigint)
# If the main thread is running C code (such as Gtk.main()), then Python signal handlers will not
# run until that C code returns. To work around this, run the C code in a separate thread, then
# sleep the main thread. Unfortunately, threading.Thread().join() and threading.Event().wait() in
# Python 2.X (but not 3.X) also block signal handlers (see http://bugs.python.org/issue1167930).
# To work around this, sleep the main thread using signal.pause(), and wake it from the 'Quit'
# menu item above using `os.kill(os.getpid(), signal.SIGINT)`.
thread = threading.Thread(target=Gtk.main)
thread.start()
if sys.version_info < (3, 0):
signal.pause()
thread.join()