From a08e1750e6f568bde4d7a1ca13e850b65c70d802 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 31 Jul 2023 12:12:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=B0=8F=E6=B3=A2=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inject.js | 46 ++++++++++++++++++++++- main.py | 110 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 121 insertions(+), 35 deletions(-) diff --git a/inject.js b/inject.js index fc6c358..e5479a5 100644 --- a/inject.js +++ b/inject.js @@ -167,7 +167,7 @@ window.native = new EventEmitter() Object.assign(native, { quit() { - return handler('quit') + return handler('quit', {}, null) }, fs: { read(filepath) {} @@ -267,5 +267,49 @@ Object.assign(native, { 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 }) diff --git a/main.py b/main.py index cb21d1b..c81f86a 100755 --- a/main.py +++ b/main.py @@ -84,7 +84,7 @@ class WebKitWindow(Gtk.Window): def create_tray(self): - if AppIndicator3 : + if not AppIndicator3 : indicator = AppIndicator3.Indicator.new( "youtube", "youtube", @@ -98,23 +98,29 @@ class WebKitWindow(Gtk.Window): # indicator.set_ordering_index(99) indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) # 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: # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) - indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) + # indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) # linux - indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + # indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + # return indicator + indicator = Gtk.StatusIcon.new_from_icon_name('youtube') + indicator.connect('activate', self.toggle_visible) return indicator - # indicator = Gtk.StatusIcon.new_from_icon_name('youtube') - # indicator.connect('activate', self.toggle_visible) - # return indicator def toggle_visible(self, icon): - # trayIcon - print(icon) - if self.is_active(): + if self.is_visible(): self.hide() else: self.present() @@ -138,6 +144,7 @@ class WebKitWindow(Gtk.Window): event = data.get('event') callback = data.get('callback') params = data.get('data') + output = None match event: @@ -145,8 +152,6 @@ class WebKitWindow(Gtk.Window): pass case 'clipboard': - output = None - # 读文本 if params['action'] == 'wait_for_text': output = self.clipboard.wait_for_text() @@ -176,9 +181,6 @@ class WebKitWindow(Gtk.Window): elif params['action'] == 'clear': self.clipboard.clear() - # 回调给前端 - if callback: - self.call_js(callback, output) # 退出app case 'quit': @@ -188,9 +190,7 @@ class WebKitWindow(Gtk.Window): case 'image': filename = params['value'] pixbuf = Pixbuf.new_from_file(filename) - image = pixbuf_to_dict(pixbuf, filename) - - self.call_js(callback, image) + output = pixbuf_to_dict(pixbuf, filename) case 'monitor': @@ -198,26 +198,20 @@ class WebKitWindow(Gtk.Window): display = Gdk.Display.get_default() monitor_num = display.get_n_monitors() 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': display = Gdk.Display.get_default() monitor = display.get_primary_monitor() - info = get_monitor_info(monitor) - - self.call_js(callback, info) + output = get_monitor_info(monitor) case 'keybinder': - output = None keymap = params.get('value') shortcut_callback = params.get('shortcut_callback') or '' if params['action'] == 'register': # 绑定之前, 先解绑, 避免被重复绑定 Keybinder.unbind(keymap) - output = Keybinder.bind( keymap, lambda km : self.call_js(shortcut_callback) @@ -230,10 +224,6 @@ class WebKitWindow(Gtk.Window): elif params['action'] == 'supported': output = Keybinder.supported() - # 有回调则返回结果 - if callback: - self.call_js(callback, output) - case 'tray': if params['action'] == 'create': @@ -242,13 +232,65 @@ class WebKitWindow(Gtk.Window): elif params['action'] == 'remove': 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 _: - if callback : - res = {"foo": 123, "bar": (11,22,33)} - self.call_js(callback, res) + output = {"foo": 123, "bar": (11,22,33)} + # 有回调则返回结果 + if callback : + self.call_js(callback, output) def all_quit(win):