forked from fogleman/FeedNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopups.py
More file actions
410 lines (311 loc) · 9.67 KB
/
popups.py
File metadata and controls
410 lines (311 loc) · 9.67 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""[summary]
Returns:
[type] -- [description]
"""
import webbrowser
import wx
import theme_default
from settings import settings
BLANK = 'about:blank'
COMMAND_CLOSE = 'http://close/'
COMMAND_NEXT = 'http://next/'
COMMAND_PREVIOUS = 'http://previous/'
COMMAND_FIRST = 'http://first/'
COMMAND_LAST = 'http://last/'
COMMAND_PLAY = 'http://play/'
COMMAND_PAUSE = 'http://pause/'
def position_window(window):
"""[summary]
Arguments:
window {[type]} -- [description]
"""
index = settings.POPUP_DISPLAY
if index >= wx.Display_GetCount():
index = 0
display = wx.Display(index)
x, y, w, h = display.GetClientArea()
cw, ch = window.GetSize()
pad = 10
x1 = x + pad
y1 = y + pad
x2 = x + w - cw - pad
y2 = y + h - ch - pad
x3 = x + w / 2 - cw / 2
y3 = y + h / 2 - ch / 2
lookup = {
(-1, -1): (x1, y1),
(1, -1): (x2, y1),
(-1, 1): (x1, y2),
(1, 1): (x2, y2),
(0, 0): (x3, y3),
}
window.SetPosition(lookup[settings.POPUP_POSITION])
class Event(wx.PyEvent):
"""[summary]
Arguments:
wx {[type]} -- [description]
"""
def __init__(self, event_object, type):
"""[summary]
Arguments:
event_object {[type]} -- [description]
type {[type]} -- [description]
"""
super(Event, self).__init__()
self.SetEventType(type.typeId)
self.SetEventObject(event_object)
EVT_LINK = wx.PyEventBinder(wx.NewEventType())
EVT_POPUP_CLOSE = wx.PyEventBinder(wx.NewEventType())
EVT_POPUP_ENTER = wx.PyEventBinder(wx.NewEventType())
EVT_POPUP_LEAVE = wx.PyEventBinder(wx.NewEventType())
class PopupManager(wx.EvtHandler):
"""[summary]
Arguments:
wx {[type]} -- [description]
"""
def __init__(self):
"""[summary]
"""
super(PopupManager, self).__init__()
self.timer = None
self.auto = settings.POPUP_AUTO_PLAY
self.cache = {}
self.hover_count = 0
def set_items(self, items, index=0, focus=False):
"""[summary]
Arguments:
items {[type]} -- [description]
Keyword Arguments:
index {int} -- [description] (default: {0})
focus {bool} -- [description] (default: {False})
"""
self.items = list(items)
self.index = index
self.count = len(self.items)
self.clear_cache(keep_current_item=True)
self.update(focus)
self.set_timer()
def update(self, focus=False):
"""[summary]
Keyword Arguments:
focus {bool} -- [description] (default: {False})
"""
item = self.items[self.index]
if item in self.cache:
self.show_frame(focus)
self.update_cache()
else:
self.update_cache(True)
self.show_frame(focus)
self.update_cache()
def update_cache(self, current_only=False):
"""[summary]
Keyword Arguments:
current_only {bool} -- [description] (default: {False})
"""
indexes = set()
indexes.add(self.index)
if not current_only:
indexes.add(self.index - 1)
indexes.add(self.index + 1)
# indexes.add(0)
# indexes.add(self.count - 1)
items = set(self.items[index]
for index in indexes if index >= 0 and index < self.count)
for item in items:
if item in self.cache:
continue
frame = self.create_frame(item)
self.cache[item] = frame
for item, frame in list(self.cache.items()):
if item not in items:
frame.Close()
del self.cache[item]
def clear_cache(self, keep_current_item=False):
"""[summary]
Keyword Arguments:
keep_current_item {bool} -- [description] (default: {False})
"""
current_item = self.items[self.index]
for item, frame in list(self.cache.items()):
if keep_current_item and item == current_item:
continue
frame.Close()
del self.cache[item]
def show_frame(self, focus=False):
"""[summary]
Keyword Arguments:
focus {bool} -- [description] (default: {False})
"""
current_item = self.items[self.index]
current_item.read = True
for item, frame in list(self.cache.items()):
if item == current_item:
if focus:
frame.Show()
else:
frame.Disable()
frame.Show()
frame.Enable()
frame.Update()
if settings.POPUP_TRANSPARENCY < 255:
frame.SetTransparent(settings.POPUP_TRANSPARENCY)
for item, frame in list(self.cache.items()):
if item != current_item:
frame.Hide()
def create_frame(self, item):
"""[summary]
Arguments:
item {[type]} -- [description]
Returns:
[type] -- [description]
"""
if True: # settings.POPUP_THEME == 'default':
# import theme_default # FIXME: delete this
context = self.create_context(item)
frame = theme_default.Frame(item, context)
frame.Bind(EVT_LINK, self.on_link)
frame.Bind(EVT_POPUP_ENTER, self.on_enter)
frame.Bind(EVT_POPUP_LEAVE, self.on_leave)
position_window(frame)
if settings.POPUP_TRANSPARENCY < 255:
frame.SetTransparent(0)
return frame
def create_context(self, item):
"""[summary]
Arguments:
item {[type]} -- [description]
Returns:
[type] -- [description]
"""
context = {}
count = str(self.count)
index = str(self.items.index(item) + 1)
index = '%s%s' % ('0' * (len(count) - len(index)), index)
context['item_index'] = index
context['item_count'] = count
context['is_playing'] = self.auto
context['is_paused'] = not self.auto
context['POPUP_WIDTH'] = settings.POPUP_WIDTH
context['COMMAND_CLOSE'] = COMMAND_CLOSE
context['COMMAND_NEXT'] = COMMAND_NEXT
context['COMMAND_PREVIOUS'] = COMMAND_PREVIOUS
context['COMMAND_FIRST'] = COMMAND_FIRST
context['COMMAND_LAST'] = COMMAND_LAST
context['COMMAND_PLAY'] = COMMAND_PLAY
context['COMMAND_PAUSE'] = COMMAND_PAUSE
return context
def set_timer(self):
"""[summary]
"""
if self.timer and self.timer.IsRunning():
return
duration = settings.POPUP_DURATION * 1000
self.timer = wx.CallLater(duration, self.on_timer)
def stop_timer(self):
"""[summary]
"""
if self.timer and self.timer.IsRunning():
self.timer.Stop()
self.timer = None
def on_enter(self, event):
"""[summary]
Arguments:
event {[type]} -- [description]
"""
event.Skip()
self.hover_count += 1
def on_leave(self, event):
"""[summary]
Arguments:
event {[type]} -- [description]
"""
event.Skip()
self.hover_count -= 1
def on_link(self, event):
"""[summary]
Arguments:
event {[type]} -- [description]
"""
link = event.link
# track the click
item = self.items[self.index]
feed = item.feed
if link == item.link or link == feed.link:
feed.clicks += 1
# handle the click
if link == BLANK:
event.Skip()
elif link == COMMAND_CLOSE:
self.on_close()
elif link == COMMAND_FIRST:
self.auto = False
self.on_first()
elif link == COMMAND_LAST:
self.auto = False
self.on_last()
elif link == COMMAND_NEXT:
self.auto = False
self.on_next()
elif link == COMMAND_PREVIOUS:
self.auto = False
self.on_previous()
elif link == COMMAND_PLAY:
if not self.auto:
self.auto = True
self.stop_timer()
self.on_timer()
elif link == COMMAND_PAUSE:
self.auto = False
else:
webbrowser.open(link)
def on_first(self):
"""[summary]
"""
self.index = 0
self.update(True)
def on_last(self):
"""[summary]
"""
self.index = self.count - 1
self.update(True)
def on_next(self, focus=True):
"""[summary]
Keyword Arguments:
focus {bool} -- [description] (default: {True})
"""
if self.index < self.count - 1:
self.index += 1
self.update(focus)
else:
self.on_close()
def on_previous(self):
"""[summary]
"""
if self.index > 0:
self.index -= 1
self.update(True)
def on_close(self):
"""[summary]
"""
self.stop_timer()
self.clear_cache()
event = Event(self, EVT_POPUP_CLOSE)
wx.PostEvent(self, event)
def on_timer(self):
"""[summary]
"""
self.timer = None
set_timer = False
if self.hover_count and settings.POPUP_WAIT_ON_HOVER:
set_timer = True
elif self.auto:
if self.index == self.count - 1:
self.on_close()
else:
self.on_next(False)
set_timer = True
if set_timer:
self.set_timer()
# EOF