-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiply.py
executable file
·284 lines (198 loc) · 6.76 KB
/
hiply.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
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
#!/usr/bin/env python3
## https://github.com/samwhelp/util-prototype/blob/master/app/hiply/hiply.py
## http://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html
## https://lazka.github.io/pgi-docs/index.html#Gtk-3.0
## https://lazka.github.io/pgi-docs/index.html#Keybinder-3.0
## https://lazka.github.io/pgi-docs/index.html#AppIndicator3-0.1
## https://lazka.github.io/pgi-docs/index.html#WebKit2-4.0
## install debian package
## $ sudo apt-get install gir1.2-gtk-3.0 gir1.2-keybinder-3.0 gir1.2-appindicator3-0.1 gir1.2-webkit2-4.0
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib
gi.require_version('Keybinder', '3.0')
from gi.repository import Keybinder
gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as AppIndicator
gi.require_version('WebKit2', '4.0')
from gi.repository import WebKit2 as WebKit
import signal
import subprocess
class App:
app = None
box = None
webview = None
app_name = 'hiply'
app_title = 'Hiply'
win_width = 600
win_height = 800
state_fullscreen = False
state_activate = True
uri_default = 'http://hichannel.hinet.net/radio/index.do'
icon_name_app = 'video'
icon_name_on_win_activate = 'video'
icon_name_on_win_deactivate = 'video-display'
icon_name_btn_app_quit = 'application-exit'
keybind_accelerator_name_activate = '<Super>a'
keybind_accelerator_name_fullscreen = 'F11'
keybind_accelerator_name_play_icrt = 'F5'
#def __init__ (self):
# pass
def init (self):
self.init_signal()
self.init_keybind()
self.init_menu()
self.init_win()
def init_signal (self):
## https://docs.python.org/3/library/signal.html
signal.signal(signal.SIGINT, signal.SIG_DFL)
def init_keybind (self):
Keybinder.init()
Keybinder.bind(self.keybind_accelerator_name_activate, self.on_key_activate_win)
def init_menu (self):
self.menu = menu = Gtk.Menu()
item = Gtk.MenuItem.new_with_label('開啟應用視窗 (%s)' % self.keybind_accelerator_name_activate)
item.connect('activate', self.on_activate_win)
menu.append(item)
item = Gtk.MenuItem.new_with_label('全螢幕 (%s)' % self.keybind_accelerator_name_fullscreen)
item.connect('activate', self.on_fullscreen_win)
menu.append(item)
item = Gtk.MenuItem.new_with_label('播放ICRT (%s)' % self.keybind_accelerator_name_play_icrt)
item.connect('activate', self.on_play_icrt)
menu.append(item)
item = Gtk.MenuItem.new_with_label('目前頁面網址')
item.connect('activate', self.on_get_uri)
menu.append(item)
item = Gtk.MenuItem.new_with_label('關於 %s' % self.app_title)
item.connect('activate', self.on_show_about)
menu.append(item)
img = Gtk.Image.new_from_icon_name(self.icon_name_btn_app_quit, 16)
item = Gtk.ImageMenuItem.new_with_label('離開')
item.connect('activate', self.on_quit_app)
item.set_image(img)
menu.append(item)
menu.show_all()
self.indicator = indicator = AppIndicator.Indicator.new(
self.app_name,
self.icon_name_on_win_activate,
AppIndicator.IndicatorCategory.APPLICATION_STATUS
)
indicator.set_menu(menu)
indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
def init_win (self):
self.win = win = Gtk.Window()
win.set_title(self.app_title)
win.set_icon_name(self.icon_name_app)
win.connect('delete-event', self.on_close_win)
win.connect('key-press-event', self.on_key_press)
#win.connect('key-release-event', self.on_key_release)
self.box = box = Gtk.Box(spacing=6)
win.add(box)
self.init_webview()
box.pack_start(self.webview, True, True, 0)
win.resize(self.win_width, self.win_height)
win.show_all()
def init_webview (self):
self.webview = webview = WebKit.WebView()
self.go_load_default()
webview.connect('button-press-event', self.on_button_press)
#webview.connect('button-release-event', self.on_button_release)
def on_button_press (self, win, evt):
## on mouse left button double click
if evt.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
self.go_switch_fullscreen()
return True
return False
def on_button_release (self, win, evt):
return False
def on_key_press (self, win, evt):
accelerator_name = Gtk.accelerator_name(evt.keyval, evt.state)
## accelerator_label = Gtk.accelerator_get_label(evt.keyval, evt.state)
if accelerator_name == self.keybind_accelerator_name_fullscreen:
self.go_switch_fullscreen()
return True
elif accelerator_name == self.keybind_accelerator_name_play_icrt:
self.go_play_icrt()
return True
return False
def on_key_release (self, win, evt):
accelerator_name = Gtk.accelerator_name(evt.keyval, evt.state)
## accelerator_label = Gtk.accelerator_get_label(evt.keyval, evt.state)
return False
def on_show_about (self, menu_item):
self.go_show_about()
def on_quit_app (self, menu_item):
self.go_quit()
def on_activate_win (self, menu_item):
self.go_activate()
def on_fullscreen_win (self, menu_item):
self.go_fullscreen()
def on_get_uri (self, menu_item):
print('current_uri:', self.go_get_uri())
def on_play_icrt (self, menu_item):
self.go_play_icrt()
def on_key_activate_win (self, accelerator_name):
self.go_switch_activate()
def on_close_win (self, win, evt):
self.go_deactivate()
return True
def go_load_default (self):
self.webview.load_uri(self.uri_default)
def go_get_uri (self):
return self.webview.get_uri()
def go_play_icrt (self):
js = ''
js += "var proxy=document.getElementById('proxy');\n"
js += "proxy.init('MTU0MDE3NzAzMjI')\n"
self.webview.run_javascript(js);
## fullscreen
def is_fullscreen (self):
return self.state_fullscreen
def set_state_fullscreen (self, val):
self.state_fullscreen = val
def go_switch_fullscreen (self):
if self.is_fullscreen():
self.go_unfullscreen()
else:
self.go_fullscreen()
def go_fullscreen (self):
self.set_state_fullscreen(True)
self.win.fullscreen()
self.go_activate()
def go_unfullscreen (self):
self.set_state_fullscreen(False)
self.win.unfullscreen()
## activate
def is_activate (self):
return self.state_activate
def set_state_activate (self, val):
self.state_activate = val
def go_switch_activate (self):
if self.is_activate():
self.go_deactivate()
else:
self.go_activate()
def go_activate (self):
self.set_state_activate(True)
self.win.present()
self.go_switch_icon_on_win_activate()
def go_deactivate (self):
self.set_state_activate(False)
self.win.hide()
self.go_switch_icon_on_win_deactivate()
def go_switch_icon_on_win_activate (self):
self.indicator.set_icon(self.icon_name_on_win_activate)
def go_switch_icon_on_win_deactivate (self):
self.indicator.set_icon(self.icon_name_on_win_deactivate)
def go_show_about (self):
print('hiply:')
def go_quit (self):
Gtk.main_quit()
def run (self):
Gtk.main()
def main ():
app = App()
app.init()
app.run()
if __name__ == '__main__':
main()