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

增加系统通知; 调整fs以支持二进制数据写入

master
yutent 2023-08-01 14:20:51 +08:00
parent 5735427f19
commit 031255857d
4 changed files with 105 additions and 23 deletions

View File

@ -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
})

41
main.py
View File

@ -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:
# 调整以支持二进制数据写入
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(params['content'])
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,15 +382,7 @@ 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:
@ -382,12 +391,8 @@ class WebKitWindow(Gtk.Window):
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()

39
notes/notify.py Normal file
View File

@ -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()

View File

@ -82,13 +82,36 @@ class App extends Component {
</button>
<button
@click=${async function () {
native.notify({ title: 'hello', icon: 'chrome' })
native.notify({
title: 'hello',
icon: 'chrome',
callback() {
console.log('<><><><>, 通知被点击了')
}
})
}}
>
通知
</button>
<img style="width:100px;border:1px solid #09f;" src=${this.img} />
<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
)
}
}}
/>
`
}