This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

一小波更新

master
yutent 2023-07-31 12:12:29 +08:00
parent 613d8070ed
commit a08e1750e6
2 changed files with 121 additions and 35 deletions

View File

@ -167,7 +167,7 @@ window.native = new EventEmitter()
Object.assign(native, { Object.assign(native, {
quit() { quit() {
return handler('quit') return handler('quit', {}, null)
}, },
fs: { fs: {
read(filepath) {} read(filepath) {}
@ -267,5 +267,49 @@ Object.assign(native, {
return handler('tray', { action: 'set_status', value: status }) return handler('tray', { action: 'set_status', value: status })
} }
}, },
window: {
isVisible() {
return handler('window', { action: 'is_visible' })
},
toggleVisible() {
handler('window', { action: 'toggle_visible' }, null)
},
hide() {
handler('window', { action: 'hide' }, null)
},
show() {
handler('window', { action: 'show' }, null)
},
fullscreen() {
handler('window', { action: 'fullscreen' }, null)
},
unfullscreen() {
handler('window', { action: 'unfullscreen' }, null)
},
maximize() {
handler('window', { action: 'maximize' }, null)
},
unmaximize() {
handler('window', { action: 'unmaximize' }, null)
},
setTitle(title = '') {
handler('window', { action: 'set_title', value: title }, null)
},
resize(width = 0, height = 0) {
handler('window', { action: 'resize', value: { width, height } }, null)
},
move(x = 0, y = 0) {
handler('window', { action: 'resize', value: { x, y } }, null)
},
setOpacity(opacity = 1) {
handler('window', { action: 'set_opacity', value: opacity }, null)
},
alwayOnTop(setting = true) {
handler('window', { action: 'set_keep_above', value: setting }, null)
},
alwayOnBotttom(setting = true) {
handler('window', { action: 'set_keep_below', value: setting }, null)
}
},
handler handler
}) })

108
main.py
View File

@ -84,7 +84,7 @@ class WebKitWindow(Gtk.Window):
def create_tray(self): def create_tray(self):
if AppIndicator3 : if not AppIndicator3 :
indicator = AppIndicator3.Indicator.new( indicator = AppIndicator3.Indicator.new(
"youtube", "youtube",
"youtube", "youtube",
@ -98,23 +98,29 @@ class WebKitWindow(Gtk.Window):
# indicator.set_ordering_index(99) # indicator.set_ordering_index(99)
indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
# indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION) # indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION)
menu = Gtk.Menu()
menu_items = Gtk.MenuItem.new_with_label("Toggle Floater")
menu.append(menu_items)
menu_items.connect("activate", self.toggle_visible)
menu.show_all()
indicator.set_menu(menu)
indicator.set_secondary_activate_target(menu_items)
else: else:
# windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找)
indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) # indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32))
# linux # linux
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
return indicator
# indicator = Gtk.StatusIcon.new_from_icon_name('youtube') # indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
# indicator.connect('activate', self.toggle_visible)
# return indicator # return indicator
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
indicator.connect('activate', self.toggle_visible)
return indicator
def toggle_visible(self, icon): def toggle_visible(self, icon):
# trayIcon
print(icon)
if self.is_active(): if self.is_visible():
self.hide() self.hide()
else: else:
self.present() self.present()
@ -138,6 +144,7 @@ class WebKitWindow(Gtk.Window):
event = data.get('event') event = data.get('event')
callback = data.get('callback') callback = data.get('callback')
params = data.get('data') params = data.get('data')
output = None
match event: match event:
@ -145,8 +152,6 @@ class WebKitWindow(Gtk.Window):
pass pass
case 'clipboard': case 'clipboard':
output = None
# 读文本 # 读文本
if params['action'] == 'wait_for_text': if params['action'] == 'wait_for_text':
output = self.clipboard.wait_for_text() output = self.clipboard.wait_for_text()
@ -176,9 +181,6 @@ class WebKitWindow(Gtk.Window):
elif params['action'] == 'clear': elif params['action'] == 'clear':
self.clipboard.clear() self.clipboard.clear()
# 回调给前端
if callback:
self.call_js(callback, output)
# 退出app # 退出app
case 'quit': case 'quit':
@ -188,9 +190,7 @@ class WebKitWindow(Gtk.Window):
case 'image': case 'image':
filename = params['value'] filename = params['value']
pixbuf = Pixbuf.new_from_file(filename) pixbuf = Pixbuf.new_from_file(filename)
image = pixbuf_to_dict(pixbuf, filename) output = pixbuf_to_dict(pixbuf, filename)
self.call_js(callback, image)
case 'monitor': case 'monitor':
@ -198,26 +198,20 @@ class WebKitWindow(Gtk.Window):
display = Gdk.Display.get_default() display = Gdk.Display.get_default()
monitor_num = display.get_n_monitors() monitor_num = display.get_n_monitors()
monitors = [display.get_monitor(i) for i in range(monitor_num)] monitors = [display.get_monitor(i) for i in range(monitor_num)]
monitors = [get_monitor_info(m) for m in monitors] output = [get_monitor_info(m) for m in monitors]
self.call_js(callback, monitors)
elif params['action'] == 'get-primary': elif params['action'] == 'get-primary':
display = Gdk.Display.get_default() display = Gdk.Display.get_default()
monitor = display.get_primary_monitor() monitor = display.get_primary_monitor()
info = get_monitor_info(monitor) output = get_monitor_info(monitor)
self.call_js(callback, info)
case 'keybinder': case 'keybinder':
output = None
keymap = params.get('value') keymap = params.get('value')
shortcut_callback = params.get('shortcut_callback') or '' shortcut_callback = params.get('shortcut_callback') or ''
if params['action'] == 'register': if params['action'] == 'register':
# 绑定之前, 先解绑, 避免被重复绑定 # 绑定之前, 先解绑, 避免被重复绑定
Keybinder.unbind(keymap) Keybinder.unbind(keymap)
output = Keybinder.bind( output = Keybinder.bind(
keymap, keymap,
lambda km : self.call_js(shortcut_callback) lambda km : self.call_js(shortcut_callback)
@ -230,10 +224,6 @@ class WebKitWindow(Gtk.Window):
elif params['action'] == 'supported': elif params['action'] == 'supported':
output = Keybinder.supported() output = Keybinder.supported()
# 有回调则返回结果
if callback:
self.call_js(callback, output)
case 'tray': case 'tray':
if params['action'] == 'create': if params['action'] == 'create':
@ -242,13 +232,65 @@ class WebKitWindow(Gtk.Window):
elif params['action'] == 'remove': elif params['action'] == 'remove':
pass pass
if callback :
self.call_js(callback, True) case 'window':
if params['action'] == 'fullscreen':
self.fullscreen()
elif params['action'] == 'unfullscreen':
self.unfullscreen()
elif params['action'] == 'maximize':
self.maximize()
elif params['action'] == 'unmaximize':
self.unmaximize()
elif params['action'] == 'set_title':
self.set_title(params['value'] or '')
elif params['action'] == 'resize':
self.resize(params['value'].get('width'), params['value'].get('height'))
elif params['action'] == 'set_opacity':
self.set_opacity(params['value'])
elif params['action'] == 'set_keep_above':
self.set_keep_above(params['value'])
elif params['action'] == 'set_keep_below':
self.set_keep_below(params['value'])
elif params['action'] == 'move':
self.move(params['value'].get('x'), params['value'].get('y'))
elif params['action'] == 'toggle_visible':
if self.is_visible():
self.hide()
else:
self.present()
elif params['action'] == 'hide':
self.hide()
elif params['action'] == 'show':
self.present()
elif params['action'] == 'is_visible':
output = self.is_visible()
case _: case _:
output = {"foo": 123, "bar": (11,22,33)}
# 有回调则返回结果
if callback : if callback :
res = {"foo": 123, "bar": (11,22,33)} self.call_js(callback, output)
self.call_js(callback, res)
def all_quit(win): def all_quit(win):