From 031255857d8c4ef30d6b6ea1f0ce34f787eb3286 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 1 Aug 2023 14:20:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B3=BB=E7=BB=9F=E9=80=9A?= =?UTF-8?q?=E7=9F=A5;=20=E8=B0=83=E6=95=B4fs=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=95=B0=E6=8D=AE=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inject.js | 19 +++++++++++++++++-- main.py | 45 +++++++++++++++++++++++++-------------------- notes/notify.py | 39 +++++++++++++++++++++++++++++++++++++++ webview/app.js | 25 ++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 notes/notify.py diff --git a/inject.js b/inject.js index 1173a4b..ae4bf10 100644 --- a/inject.js +++ b/inject.js @@ -214,6 +214,12 @@ Object.assign(native, { }, copy(filepath, target) { return handler('fs', { action: 'copy', filepath, target }) + }, + async upload(file) { + return handler('fs', { + action: 'upload', + file: new Uint8Array(await file.arrayBuffer()) + }) } }, image(filepath) { @@ -356,8 +362,17 @@ Object.assign(native, { handler('window', { action: 'set_keep_below', value: setting }, null) } }, - notify({ title, summary, icon, progress = 0, urgency = 0 }) { - handler('notify', { title, summary, icon, progress, urgency }) + notify({ title, summary, icon, progress = 0, urgency = 0, callback }) { + let eventName + if (callback) { + eventName = rand() + native.$once(eventName, callback) + } + handler( + 'notify', + { title, summary, icon, progress, urgency, callback: eventName }, + null + ) }, handler }) diff --git a/main.py b/main.py index b91f9fa..2e397e8 100755 --- a/main.py +++ b/main.py @@ -6,13 +6,14 @@ import gi, json, os, shutil gi.require_version("Gtk", "3.0") gi.require_version("WebKit2", "4.1") gi.require_version("Keybinder", "3.0") -gi.require_version('Notify', '0.7') # gir1.2-notify-0.7 -from gi.repository import Gtk, Gdk, WebKit2, GLib, Gio, Keybinder, Notify + +from gi.repository import Gtk, Gdk, WebKit2, GLib, Gio, Keybinder from gi.repository.GdkPixbuf import Pixbuf from notes.utils import * from notes.mimetypes import get_mimetype +from notes.notify import Notification # 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon try: @@ -24,7 +25,7 @@ except (ValueError, ImportError): # 初始化 Keybinder Keybinder.init() -Notify.init(Notify.get_app_name() or 'webapp') + # im_context = Gtk.IMContext() @@ -32,8 +33,12 @@ Notify.init(Notify.get_app_name() or 'webapp') class WebKitWindow(Gtk.Window): def __init__(self): + Gtk.Window.__init__(self, title="WebKit Example") + self.notify = Notification(self) + # Notification.__init__(self.notify, ) + self.set_default_size(800, 600) settings = WebKit2.Settings() @@ -73,6 +78,8 @@ class WebKitWindow(Gtk.Window): context = self.webview.get_context() context.register_uri_scheme('app', self.resource_request_callback) + # 允许网页通知权限 + context.initialize_notification_permissions([WebKit2.SecurityOrigin.new_for_uri('app:///index.html')], []) im = self.webview.get_input_method_context() @@ -187,15 +194,23 @@ class WebKitWindow(Gtk.Window): match event: case 'fs': - filepath = params['filepath'] + filepath = params.get('filepath') + if params['action'] == 'read': file = open(filepath) output = file.read() if params['action'] == 'write': try: - file = open(filepath, 'a' if params['append'] else 'w') - output = file.write(params['content']) + # 调整以支持二进制数据写入 + buff = params['content'] + if type(buff) == list: + buff = bytes(buff) + file = open(filepath, 'ab' if params['append'] else 'wb') + else: + file = open(filepath, 'a' if params['append'] else 'w') + + output = file.write(buff) finally: if file: file.close() @@ -227,6 +242,8 @@ class WebKitWindow(Gtk.Window): if params['action'] == 'isdir': output = os.path.isdir(filepath) + if params['action'] == 'upload': + print(params) @@ -365,29 +382,17 @@ class WebKitWindow(Gtk.Window): progress = params.get('progress') urgency = params.get('urgency') - notify = Notify.Notification.new(title, summary, icon) - - if progress: - notify.set_hint('value', progress) - - notify.set_urgency(urgency) - notify.add_action("click", "Click Me!", my_callback_func) - - notify.show() + self.notify.create(title, summary, icon, progress, urgency, params.get('callback')) # 有回调则返回结果 - if callback : + if callback: self.call_js(callback, output) def all_quit(win): print('朕要休息了~~~') - Notify.uninit() Gtk.main_quit() -def my_callback_func(notification, action_name, user_data): - print("Button clicked! Action name: %s" % action_name) - notification.close() diff --git a/notes/notify.py b/notes/notify.py new file mode 100644 index 0000000..f4824b0 --- /dev/null +++ b/notes/notify.py @@ -0,0 +1,39 @@ + +import gi + +gi.require_version('Notify', '0.7') # gir1.2-notify-0.7 + +from gi.repository import Notify + + +class Notification(): + + def __init__(self, window): + + Notify.init(Notify.get_app_name() or 'webapp') + + self.window = window + self.notify = Notify.Notification() + + + def create(self, title, summary, icon, progress = 0, urgency = 0, callback = None): + + self.notify.clear_actions() + self.notify.clear_hints() + + self.notify.update(title, summary, icon) + + if progress: + self.notify.set_hint('value', progress) + + self.notify.set_urgency(urgency) + + if callback: + self.notify.add_action("click", "click", self.action_callback, callback) + + self.notify.show() + + def action_callback(self, instance, action, callback): + if callback: + self.window.call_js(callback) + instance.close() \ No newline at end of file diff --git a/webview/app.js b/webview/app.js index 990277f..6261834 100644 --- a/webview/app.js +++ b/webview/app.js @@ -82,13 +82,36 @@ class App extends Component { +
+
+ ` }