增加系统通知; 调整fs以支持二进制数据写入
parent
5735427f19
commit
031255857d
19
inject.js
19
inject.js
|
@ -214,6 +214,12 @@ Object.assign(native, {
|
||||||
},
|
},
|
||||||
copy(filepath, target) {
|
copy(filepath, target) {
|
||||||
return handler('fs', { action: '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) {
|
image(filepath) {
|
||||||
|
@ -356,8 +362,17 @@ Object.assign(native, {
|
||||||
handler('window', { action: 'set_keep_below', value: setting }, null)
|
handler('window', { action: 'set_keep_below', value: setting }, null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
notify({ title, summary, icon, progress = 0, urgency = 0 }) {
|
notify({ title, summary, icon, progress = 0, urgency = 0, callback }) {
|
||||||
handler('notify', { title, summary, icon, progress, urgency })
|
let eventName
|
||||||
|
if (callback) {
|
||||||
|
eventName = rand()
|
||||||
|
native.$once(eventName, callback)
|
||||||
|
}
|
||||||
|
handler(
|
||||||
|
'notify',
|
||||||
|
{ title, summary, icon, progress, urgency, callback: eventName },
|
||||||
|
null
|
||||||
|
)
|
||||||
},
|
},
|
||||||
handler
|
handler
|
||||||
})
|
})
|
||||||
|
|
41
main.py
41
main.py
|
@ -6,13 +6,14 @@ import gi, json, os, shutil
|
||||||
gi.require_version("Gtk", "3.0")
|
gi.require_version("Gtk", "3.0")
|
||||||
gi.require_version("WebKit2", "4.1")
|
gi.require_version("WebKit2", "4.1")
|
||||||
gi.require_version("Keybinder", "3.0")
|
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 gi.repository.GdkPixbuf import Pixbuf
|
||||||
|
|
||||||
from notes.utils import *
|
from notes.utils import *
|
||||||
from notes.mimetypes import get_mimetype
|
from notes.mimetypes import get_mimetype
|
||||||
|
from notes.notify import Notification
|
||||||
|
|
||||||
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
|
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
|
||||||
try:
|
try:
|
||||||
|
@ -24,7 +25,7 @@ except (ValueError, ImportError):
|
||||||
|
|
||||||
# 初始化 Keybinder
|
# 初始化 Keybinder
|
||||||
Keybinder.init()
|
Keybinder.init()
|
||||||
Notify.init(Notify.get_app_name() or 'webapp')
|
|
||||||
|
|
||||||
# im_context = Gtk.IMContext()
|
# im_context = Gtk.IMContext()
|
||||||
|
|
||||||
|
@ -32,8 +33,12 @@ Notify.init(Notify.get_app_name() or 'webapp')
|
||||||
class WebKitWindow(Gtk.Window):
|
class WebKitWindow(Gtk.Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
|
|
||||||
Gtk.Window.__init__(self, title="WebKit Example")
|
Gtk.Window.__init__(self, title="WebKit Example")
|
||||||
|
|
||||||
|
self.notify = Notification(self)
|
||||||
|
# Notification.__init__(self.notify, )
|
||||||
|
|
||||||
self.set_default_size(800, 600)
|
self.set_default_size(800, 600)
|
||||||
|
|
||||||
settings = WebKit2.Settings()
|
settings = WebKit2.Settings()
|
||||||
|
@ -73,6 +78,8 @@ class WebKitWindow(Gtk.Window):
|
||||||
|
|
||||||
context = self.webview.get_context()
|
context = self.webview.get_context()
|
||||||
context.register_uri_scheme('app', self.resource_request_callback)
|
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()
|
im = self.webview.get_input_method_context()
|
||||||
|
@ -187,15 +194,23 @@ class WebKitWindow(Gtk.Window):
|
||||||
|
|
||||||
match event:
|
match event:
|
||||||
case 'fs':
|
case 'fs':
|
||||||
filepath = params['filepath']
|
filepath = params.get('filepath')
|
||||||
|
|
||||||
if params['action'] == 'read':
|
if params['action'] == 'read':
|
||||||
file = open(filepath)
|
file = open(filepath)
|
||||||
output = file.read()
|
output = file.read()
|
||||||
|
|
||||||
if params['action'] == 'write':
|
if params['action'] == 'write':
|
||||||
try:
|
try:
|
||||||
|
# 调整以支持二进制数据写入
|
||||||
|
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')
|
file = open(filepath, 'a' if params['append'] else 'w')
|
||||||
output = file.write(params['content'])
|
|
||||||
|
output = file.write(buff)
|
||||||
finally:
|
finally:
|
||||||
if file:
|
if file:
|
||||||
file.close()
|
file.close()
|
||||||
|
@ -227,6 +242,8 @@ class WebKitWindow(Gtk.Window):
|
||||||
if params['action'] == 'isdir':
|
if params['action'] == 'isdir':
|
||||||
output = os.path.isdir(filepath)
|
output = os.path.isdir(filepath)
|
||||||
|
|
||||||
|
if params['action'] == 'upload':
|
||||||
|
print(params)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -365,15 +382,7 @@ class WebKitWindow(Gtk.Window):
|
||||||
progress = params.get('progress')
|
progress = params.get('progress')
|
||||||
urgency = params.get('urgency')
|
urgency = params.get('urgency')
|
||||||
|
|
||||||
notify = Notify.Notification.new(title, summary, icon)
|
self.notify.create(title, summary, icon, progress, urgency, params.get('callback'))
|
||||||
|
|
||||||
if progress:
|
|
||||||
notify.set_hint('value', progress)
|
|
||||||
|
|
||||||
notify.set_urgency(urgency)
|
|
||||||
notify.add_action("click", "Click Me!", my_callback_func)
|
|
||||||
|
|
||||||
notify.show()
|
|
||||||
|
|
||||||
# 有回调则返回结果
|
# 有回调则返回结果
|
||||||
if callback:
|
if callback:
|
||||||
|
@ -382,12 +391,8 @@ class WebKitWindow(Gtk.Window):
|
||||||
|
|
||||||
def all_quit(win):
|
def all_quit(win):
|
||||||
print('朕要休息了~~~')
|
print('朕要休息了~~~')
|
||||||
Notify.uninit()
|
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|
||||||
def my_callback_func(notification, action_name, user_data):
|
|
||||||
print("Button clicked! Action name: %s" % action_name)
|
|
||||||
notification.close()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
|
@ -82,13 +82,36 @@ class App extends Component {
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
@click=${async function () {
|
@click=${async function () {
|
||||||
native.notify({ title: 'hello', icon: 'chrome' })
|
native.notify({
|
||||||
|
title: 'hello',
|
||||||
|
icon: 'chrome',
|
||||||
|
callback() {
|
||||||
|
console.log('<><><><>, 通知被点击了')
|
||||||
|
}
|
||||||
|
})
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
通知
|
通知
|
||||||
</button>
|
</button>
|
||||||
<img style="width:100px;border:1px solid #09f;" src=${this.img} />
|
<img style="width:100px;border:1px solid #09f;" src=${this.img} />
|
||||||
<textarea @paste=${this.pasteImg}></textarea>
|
<textarea @paste=${this.pasteImg}></textarea>
|
||||||
|
<br />
|
||||||
|
<hr />
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
@change=${async function (ev) {
|
||||||
|
let file = ev.target.files[0]
|
||||||
|
let u8 = new Uint8Array(await file.arrayBuffer())
|
||||||
|
console.log(u8)
|
||||||
|
for (let i = 0; i <= u8.length; i += 2048) {
|
||||||
|
await native.fs.write(
|
||||||
|
'/code/gtk/webkit/demo.png',
|
||||||
|
Array.from(u8.slice(i, i + 2048)),
|
||||||
|
true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue