Compare commits

...

10 Commits

9 changed files with 602 additions and 473 deletions

View File

@ -22,6 +22,7 @@ sed -i "s/{{size}}/${_size}/" DEBIAN/control
sed -i "s/{{version}}/${version}/" DEBIAN/control sed -i "s/{{version}}/${version}/" DEBIAN/control
cd .. cd ..
sudo chown -R root:root unpack/
dpkg-deb -b unpack/ "python3-webengine-gtk3-${version}.deb" dpkg-deb -b unpack/ "python3-webengine-gtk3-${version}.deb"
sudo rm -rf unpack sudo rm -rf unpack

2
debian/control vendored
View File

@ -4,7 +4,7 @@ Section: develop
Priority: optional Priority: optional
Maintainer: Yutent <yutent.io@gmail.com> Maintainer: Yutent <yutent.io@gmail.com>
Architecture: all Architecture: all
Depends: python3 (>=3.10), python3-gi, gir1.2-gtk-3.0, python3-pil, python3-gi-cairo, gir1.2-gdkpixbuf-2.0 Depends: python3 (>=3.10), python3-gi, gir1.2-webkit2-4.1, gir1.2-gtk-3.0, python3-pil, python3-gi-cairo, gir1.2-gdkpixbuf-2.0
Recommends: gir1.2-ayatanaappindicator3-0.1, gir1.2-notify-0.7, gir1.2-keybinder-3.0 Recommends: gir1.2-ayatanaappindicator3-0.1, gir1.2-notify-0.7, gir1.2-keybinder-3.0
Installed-Size: {{size}} Installed-Size: {{size}}
Homepage: https://git.wkit.fun/appcat/python3-webengine-gtk3 Homepage: https://git.wkit.fun/appcat/python3-webengine-gtk3

View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
import os
home_dir = os.getenv('HOME')
env = {
"HOME_DIR": home_dir,
"CONFIG_DIR": os.path.join(home_dir, '.config'),
"CACHE_DIR": os.path.join(home_dir, '.cache'),
"LANG": os.environ['LANG'] or "en_US.UTF-8"
}

View File

@ -15,11 +15,16 @@ class Inject:
self.manager = webview.get_user_content_manager() self.manager = webview.get_user_content_manager()
script_data = open(self.abspath('./inject.js'), 'r').read() code = open(self.abspath('./inject.js'), 'r').read()
frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES frame = WebKit2.UserContentInjectedFrames.ALL_FRAMES
time = WebKit2.UserScriptInjectionTime.END time = WebKit2.UserScriptInjectionTime.START
script_data = script_data.replace("'{{env}}'", json.dumps(env)).replace("{{uuid}}", webview.uuid)
script = WebKit2.UserScript(script_data, frame, time, None, None) code = code.replace("'{{env}}'", json.dumps(env))
code = code.replace("{{uuid}}", webview.uuid)
code = code.replace("{{app_name}}", webview.app_name)
code = code.replace("{{app_version}}", webview.app_version)
script = WebKit2.UserScript(code, frame, time, None, None)
self.manager.add_script(script) self.manager.add_script(script)

View File

@ -22,7 +22,7 @@ class Settings(WebKit2.Settings):
self.set_javascript_can_open_windows_automatically(True) self.set_javascript_can_open_windows_automatically(True)
self.set_user_agent_with_application_details('WebEngine', version) self.set_useragent_with_app('WebEngine', version)
# indexedDB 和 localStorage 和 离线缓存 # indexedDB 和 localStorage 和 离线缓存
@ -43,6 +43,9 @@ class Settings(WebKit2.Settings):
self.set_media_playback_allows_inline(True) self.set_media_playback_allows_inline(True)
def set_useragent_with_app(self, name, ver):
self.set_user_agent_with_application_details(name, ver)
def set_useragent(self, str): def set_useragent(self, str):
self.set_user_agent(str) self.set_user_agent(str)
@ -75,6 +78,9 @@ def create_setting(options = None):
setting.enable_devtools() setting.enable_devtools()
setting.mock_devices() setting.mock_devices()
if options.get('app_name') and options.get('app_version'):
setting.set_useragent_with_app(options['app_name'], options['app_version'])
if options.get('useragent'): if options.get('useragent'):
setting.set_useragent(options.get('useragent')) setting.set_useragent(options.get('useragent'))
@ -86,6 +92,9 @@ def create_setting(options = None):
def wrapper(app, extra = None): def wrapper(app, extra = None):
if options is not None:
app.app_name = options.get('app_name')
app.app_version = options.get('app_version')
app.set_settings(setting) app.set_settings(setting)
return wrapper return wrapper

View File

@ -3,9 +3,12 @@
# @author yutent<yutent.io@gmail.com> # @author yutent<yutent.io@gmail.com>
# @date 2023/07/28 14:39:33 # @date 2023/07/28 14:39:33
import gi import gi, threading
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf, GObject
def noop():
pass
def get_monitor_info(monitor): def get_monitor_info(monitor):
return { return {
@ -63,4 +66,34 @@ def dict_to_pixbuf(data):
else: else:
image = None image = None
return image return image
# 定义一个异步修饰器, 用于在子线程中运行一些会阻塞主线程的任务
def run_async(func):
def wrapper(*args, **kwargs):
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
return wrapper
# 类型js的settimeout的修饰器
def set_timeout(timeout = 0.5):
def decorator(callback):
def wrapper(*args):
t = threading.Timer(timeout, callback, args=args)
t.start()
return t
return wrapper
return decorator
# 定义一个修饰器, 用于将当前方法转到主线程中运行 (子线程中调用方法时)
def idle(func):
def wrapper(*args):
GObject.idle_add(func, *args)
return wrapper

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
build = (0, 6, 0) build = (0, 7, 0)
version = '.'.join(map(str, build)) version = '.'.join(map(str, build))

View File

@ -3,7 +3,8 @@
# @date 2023/08/08 14:07:26 # @date 2023/08/08 14:07:26
import gi, os, json, shutil, hashlib, random import gi, os, sys, json
import webbrowser, shutil, hashlib, random
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version("WebKit2", "4.1") gi.require_version("WebKit2", "4.1")
@ -31,27 +32,13 @@ try:
except: except:
Keybinder = None Keybinder = None
from ._env import env
from ._version import version
from ._settings import create_setting from ._settings import create_setting
from ._protocal import create_protocal from ._protocal import create_protocal
from ._notify import create_notify from ._notify import create_notify
from ._inject import Inject from ._inject import Inject
from ._utils import get_monitor_info, pixbuf_to_dict, dict_to_pixbuf from ._utils import noop, get_monitor_info, pixbuf_to_dict, dict_to_pixbuf, run_async, idle
env = {
"HOME_DIR": os.getenv('HOME'),
"CONFIG_DIR": os.path.join(os.getenv('HOME'), '.config'),
"CACHE_DIR": os.path.join(os.getenv('HOME'), '.cache')
}
def noop():
pass
@ -60,7 +47,8 @@ class WebEngine(WebKit2.WebView):
__gsignals__ = { __gsignals__ = {
'quit': (GObject.SignalFlags.RUN_FIRST, None, ()) 'quit': (GObject.SignalFlags.RUN_FIRST, None, ())
} }
app_name = 'WebEngine'
app_version = version
uuid = None uuid = None
root = None root = None
window = None window = None
@ -84,7 +72,7 @@ class WebEngine(WebKit2.WebView):
opener.children.add(self) opener.children.add(self)
if win.get_title() is None: if win.get_title() is None:
win.set_title('WebEngine') win.set_title(self.app_name)
if win.get_icon() is None and win.get_icon_name() is None: if win.get_icon() is None and win.get_icon_name() is None:
win.set_icon_name('web-browser') win.set_icon_name('web-browser')
@ -108,7 +96,11 @@ class WebEngine(WebKit2.WebView):
# 允许前端 widnow.close() 关闭窗口 # 允许前端 widnow.close() 关闭窗口
self.connect('close', self.close_window) self.connect('close', self.close_window)
win.connect("destroy", self.remove_from_opener) # 通过外部关闭窗口时从父级中移除
# 通过外部关闭窗口时从父级中移除
win.connect("destroy", self.remove_from_opener)
win.connect('hide', lambda w: self.call_js('hide'))
win.connect('show', lambda w: self.call_js('show'))
def remove_from_opener(self, win = None): def remove_from_opener(self, win = None):
@ -163,14 +155,14 @@ class WebEngine(WebKit2.WebView):
raise ValueError('url must starts with "/" or "app://"') raise ValueError('url must starts with "/" or "app://"')
@idle
def call_js(self, method, data = None, err = None): def call_js(self, method, data = None, err = None):
if err is not None: if err is not None:
err = str(err) err = str(err)
scripts = 'native.$emit("' + method + '", ' + json.dumps(err) + ', ' + json.dumps(data) + ')' scripts = 'native.$emit("' + method + '", ' + json.dumps(err) + ', ' + json.dumps(data) + ')'
self.evaluate_javascript(scripts, -1) self.evaluate_javascript(scripts, -1)
@run_async
def called_by_js(self, webview, message): def called_by_js(self, webview, message):
data = json.loads(message.get_js_value().to_json(0)) data = json.loads(message.get_js_value().to_json(0))
@ -183,22 +175,17 @@ class WebEngine(WebKit2.WebView):
match event: match event:
case 'init':
output = env case 'app':
_error, output = self._app_handler(params)
case 'fs': case 'fs':
_error, output = self._fs_setting(params) _error, output = self._fs_handler(params)
case 'clipboard': case 'clipboard':
_error, output = self._clipboard_setting(params) _error, output = self._clipboard_handler(params)
# 退出app
case 'quit':
self.close_window()
self.emit('quit')
# 读取图片, 返回图片像素数据 # 读取图片, 返回图片像素数据
case 'image': case 'image':
filename = params['value'] filename = params['value']
@ -217,7 +204,7 @@ class WebEngine(WebKit2.WebView):
case 'keybinder': case 'keybinder':
_error, output = self._keybinder_setting(params) _error, output = self._keybinder_handler(params)
case 'tray': case 'tray':
@ -257,7 +244,7 @@ class WebEngine(WebKit2.WebView):
return return
case 'window': case 'window':
_error, output = self._window_setting(params) _error, output = self._window_handler(params)
case 'notify': case 'notify':
@ -274,25 +261,61 @@ class WebEngine(WebKit2.WebView):
case 'proxy': case 'proxy':
_error, output = self._proxy_setting(params) _error, output = self._proxy_handler(params)
case 'md5': case 'md5':
output = hashlib.md5(str(params.get('value'))).hexdigest() output = hashlib.md5(str(params.get('value'))).hexdigest()
case _: case _:
if self.custom_bridge is None: if self.custom_bridge is not None:
pass try:
else: _error, output = self.custom_bridge(event, params) or (None, None)
_error, output = self.custom_bridge(event, params) except Exception as err:
print(err)
_error = err
# 有回调则返回结果 # 有回调则返回结果
if callback: if callback:
self.call_js(callback, output, _error) self.call_js(callback, output, _error)
def _app_handler(self, params = {}):
_error = None
output = None
match(params.get('action')):
# 退出app
case 'quit':
self.close_window()
self.emit('quit')
def _fs_setting(self, params = {}): case 'relaunch':
py = sys.executable
os.execl(py, py, *sys.argv)
return (_error, output)
def _shell_handler(self, params = {}):
_error = None
output = None
path = params.get('path')
match(params.get('action')):
case 'openExternal':
webbrowser.open(params.get('url'))
case 'showItemInFolder':
os.system(f"xdg-open '{path}'")
case 'openPath':
os.system(f"xdg-open '{path}'")
case 'trashItem':
pass
def _fs_handler(self, params = {}):
_error = None _error = None
output = None output = None
filepath = params.get('filepath') filepath = params.get('filepath')
@ -362,11 +385,14 @@ class WebEngine(WebKit2.WebView):
case 'isdir': case 'isdir':
output = os.path.isdir(filepath) output = os.path.isdir(filepath)
case 'mkdir':
output = os.makedirs(filepath)
return (_error, output) return (_error, output)
def _clipboard_setting(self, params = {}): def _clipboard_handler(self, params = {}):
_error = None _error = None
output = None output = None
@ -403,7 +429,7 @@ class WebEngine(WebKit2.WebView):
return (_error, output) return (_error, output)
def _keybinder_setting(self, params = {}): def _keybinder_handler(self, params = {}):
_error = None _error = None
output = None output = None
keymap = params.get('value') keymap = params.get('value')
@ -434,7 +460,7 @@ class WebEngine(WebKit2.WebView):
def _window_setting(self, params = {}): def _window_handler(self, params = {}):
_error = None _error = None
output = None output = None
@ -496,7 +522,7 @@ class WebEngine(WebKit2.WebView):
def _proxy_setting(self, params = {}): def _proxy_handler(self, params = {}):
dm = self.get_website_data_manager() dm = self.get_website_data_manager()
output = True output = True
_error = None _error = None

View File

@ -3,467 +3,509 @@
* @author yutent<yutent.io@gmail.com> * @author yutent<yutent.io@gmail.com>
* @date 2023/07/21 17:38:11 * @date 2023/07/21 17:38:11
*/ */
!(function () {
const MIME_TYPES = {
html: 'text/html',
json: 'application/json',
js: 'application/javascript',
htm: 'text/html',
txt: 'text/plain',
css: 'text/css',
webp: 'image/webp',
jpg: 'image/jpg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
svg: 'image/svg+xml',
ico: 'image/ico',
mp3: 'audio/mpeg',
ogg: 'audio/ogg',
m4a: 'audio/m4a',
amr: 'audio/amr',
mp4: 'video/mp4',
webm: 'video/webm',
wasm: 'application/wasm',
asm: 'application/asm',
zip: 'application/zip',
'7z': 'application/x-7z-compressed',
eot: 'application/vnd.ms-fontobject',
ttf: 'font/ttf',
otf: 'font/otf',
woff: 'font/woff',
woff2: 'font/woff2',
xls: 'application/vnd.ms-excel',
doc: 'application/msword',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}
const KEYS_MAP = {
shift: '<Shift>',
ctrl: '<Ctrl>',
alt: '<Alt>',
super: '<Super>'
}
const MIME_TYPES = { const NO_CALLBACK = false
html: 'text/html', const CALL_ONCE = true
json: 'application/json',
js: 'application/javascript',
htm: 'text/html',
txt: 'text/plain',
css: 'text/css',
webp: 'image/webp',
jpg: 'image/jpg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
svg: 'image/svg+xml',
ico: 'image/ico',
mp3: 'audio/mpeg',
ogg: 'audio/ogg',
m4a: 'audio/m4a',
amr: 'audio/amr',
mp4: 'video/mp4',
webm: 'video/webm',
wasm: 'application/wasm',
asm: 'application/asm',
zip: 'application/zip',
'7z': 'application/x-7z-compressed',
eot: 'application/vnd.ms-fontobject',
ttf: 'font/ttf',
otf: 'font/otf',
woff: 'font/woff',
woff2: 'font/woff2',
xls: 'application/vnd.ms-excel',
doc: 'application/msword',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}
const KEYS_MAP = {
shift: '<Shift>',
ctrl: '<Ctrl>',
alt: '<Alt>',
super: '<Super>'
}
const NO_CALLBACK = false const __events__ = Symbol('events')
const CALL_ONCE = true
function defer() { function defer() {
let obj = {} let obj = {}
obj.promise = new Promise((resolve, reject) => { obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve obj.resolve = resolve
obj.reject = reject obj.reject = reject
}) })
return obj return obj
} }
function rand(prefix = 'cb_') { function rand(prefix = 'cb_') {
return prefix + Math.random().toString().slice(2) return prefix + Math.random().toString().slice(2)
} }
function handler(event, data = {}, need = CALL_ONCE) { function handler(event, data = {}, need = CALL_ONCE) {
let _ = defer() let _ = defer()
let callback let callback
if (need === NO_CALLBACK) { if (need === NO_CALLBACK) {
_.resolve(true) _.resolve(true)
} else { } else {
callback = rand() callback = rand()
native.$once(callback, (err, res) => { native.$once(callback, (err, res) => {
if (err) { if (err) {
_.reject(err) _.reject(new Error(err))
} else { } else {
_.resolve(res) _.resolve(res)
}
})
}
window.webkit.messageHandlers.app.postMessage({
event,
data,
callback
})
return _.promise
}
function base64(str = '') {
return btoa(str).replace(/[+=\/]/g, '')
}
function _postMessage(data = {}, uuid = null) {
let ev = new Event('message')
Object.assign(ev, {
data,
source: {
postMessage(msg) {
native.children.postMessage(msg, uuid)
}
} }
}) })
window.dispatchEvent(ev)
}
function readonly(obj, key, value) {
Object.defineProperty(obj, key, {
get() {
return value
},
enumerable: false
})
} }
window.webkit.messageHandlers.app.postMessage({ function _extend(origin, options = {}) {
event, for (let k in options) {
data, readonly(origin, k, options[k])
callback }
}) }
return _.promise
}
function base64(str = '') { class NativeImage {
return btoa(str).replace(/[+=\/]/g, '') #origin
}
function _postMessage(data = {}, uuid = null) { constructor(obj) {
let ev = new Event('message') this.#origin = obj
Object.assign(ev, { this.width = obj.width
data, this.height = obj.height
source: { this.type = MIME_TYPES[obj.filepath.split('.').pop()]
postMessage(msg) { }
native.children.postMessage(msg, uuid)
toJSON() {
return this.#origin
}
export(type, base64) {
let _ = defer()
let canvas = document.createElement('canvas')
canvas.width = this.width
canvas.height = this.height
let ctx = canvas.getContext('2d')
let imgData = ctx.getImageData(0, 0, this.width, this.height)
let data = imgData.data
for (let i = 0; i < this.#origin.bytes.length; i += 4) {
imgData.data[i] = this.#origin.bytes[i]
imgData.data[i + 1] = this.#origin.bytes[i + 1]
imgData.data[i + 2] = this.#origin.bytes[i + 2]
imgData.data[i + 3] = this.#origin.bytes[i + 3]
} }
}
})
window.dispatchEvent(ev)
}
class NativeImage { ctx.putImageData(imgData, 0, 0)
#origin
constructor(obj) { if (base64) {
this.#origin = obj return canvas.toDataURL(type || this.type, 1)
this.width = obj.width
this.height = obj.height
this.type = MIME_TYPES[obj.filepath.split('.').pop()]
}
toJSON() {
return this.#origin
}
export(type, base64) {
let _ = defer()
let canvas = document.createElement('canvas')
canvas.width = this.width
canvas.height = this.height
let ctx = canvas.getContext('2d')
let imgData = ctx.getImageData(0, 0, this.width, this.height)
let data = imgData.data
for (let i = 0; i < this.#origin.bytes.length; i += 4) {
imgData.data[i] = this.#origin.bytes[i]
imgData.data[i + 1] = this.#origin.bytes[i + 1]
imgData.data[i + 2] = this.#origin.bytes[i + 2]
imgData.data[i + 3] = this.#origin.bytes[i + 3]
}
ctx.putImageData(imgData, 0, 0)
if (base64) {
return canvas.toDataURL(type || this.type, 1)
} else {
canvas.toBlob(_.resolve, type || this.type, 1)
return _.promise
}
}
toPNG() {
return this.export('image/png')
}
toJPEG() {
return this.export('image/jpeg')
}
toDataURL(type) {
return this.export(type, true)
}
}
class EventEmitter {
//
__events__ = Object.create(null)
$on(name, fn) {
if (this.__events__[name]) {
this.__events__[name].push(fn)
} else {
this.__events__[name] = [fn]
}
}
$once(name, fn) {
fn.__once__ = true
this.$on(name, fn)
}
$off(name, fn) {
if (this.__events__[name]) {
if (fn) {
this.__events__[name] = this.__events__[name].filter(it => it !== fn)
} else { } else {
this.__events__[name] = [] canvas.toBlob(_.resolve, type || this.type, 1)
return _.promise
} }
} }
toPNG() {
return this.export('image/png')
}
toJPEG() {
return this.export('image/jpeg')
}
toDataURL(type) {
return this.export(type, true)
}
} }
$emit(name, ...args) { class Native {
if (this.__events__[name]) { //
for (let fn of this.__events__[name]) { [__events__] = Object.create(null)
try {
fn.apply(this, args) $on(name, fn) {
if (fn.__once__) { if (this[__events__][name]) {
this.$off(name, fn) this[__events__][name].push(fn)
} } else {
} catch (e) { this[__events__][name] = [fn]
console.error(e) }
}
$once(name, fn) {
fn.__once__ = true
this.$on(name, fn)
}
$off(name, fn) {
if (this[__events__][name]) {
if (fn) {
this[__events__][name] = this[__events__][name].filter(
it => it !== fn
)
} else {
this[__events__][name] = []
} }
} }
} }
}
$destroy() { $emit(name, ...args) {
this.__events__ = Object.create(null) if (this[__events__][name]) {
} for (let fn of this[__events__][name]) {
} try {
fn.apply(this, args)
window.native = new EventEmitter() if (fn.__once__) {
this.$off(name, fn)
native.$on('opener_message', (data, uuid) => _postMessage(data, uuid)) }
} catch (e) {
Object.assign(native, { console.error(e)
env: '{{env}}', }
}
quit() {
return handler('quit', {}, NO_CALLBACK)
},
fs: {
access(filepath, mode = 'r') {
return handler('fs', { action: 'access', mode, filepath })
},
read(filepath, mode = 'r') {
return handler('fs', { action: 'read', mode, filepath }).then(r =>
mode.includes('b') ? new Uint8Array(r) : r
)
},
write(filepath, content = '', mode = 'w') {
return handler('fs', {
action: 'write',
mode,
filepath,
content
})
},
append(filepath, content = '', mode = 'a+') {
return handler('fs', {
action: 'write',
mode,
filepath,
content
})
},
exists(filepath) {
return handler('fs', { action: 'exists', filepath })
},
list(filepath) {
return handler('fs', { action: 'list', filepath })
},
isfile(filepath) {
return handler('fs', { action: 'isfile', filepath })
},
isdir(filepath) {
return handler('fs', { action: 'isdir', filepath })
},
remove(filepath) {
return handler('fs', { action: 'remove', filepath })
},
rename(filepath, target) {
return handler('fs', { action: 'rename', filepath, target })
},
copy(filepath, target) {
return handler('fs', { action: 'copy', filepath, target })
}
},
image(filepath) {
return handler('image', { value: filepath }).then(r => new NativeImage(r))
},
clipboard: {
readText() {
return handler('clipboard', { action: 'wait_for_text' })
},
writeText(value) {
return handler('clipboard', { action: 'set_text', value }, NO_CALLBACK)
},
readImage() {
return handler('clipboard', { action: 'wait_for_image' }).then(r =>
r ? new NativeImage(r) : r
)
},
writeImage(value) {
// 暂不知原因, postMessage传给Gtk后, JSON.stringify()并未读取toJSON的结果
if (typeof value === 'object') {
value = value.toJSON()
}
return handler('clipboard', { action: 'set_image', value }, NO_CALLBACK)
},
clear() {
return handler('clipboard', { action: 'clear' }, NO_CALLBACK)
}
},
screen: {
getAllDisplays() {
return handler('monitor', { action: 'get-all' })
},
getPrimaryDisplay() {
return handler('monitor', { action: 'get-primary' })
}
},
globalShortcut: {
get enabled() {
return handler('keybinder', { action: 'supported' })
},
register(keyMap, callback) {
let shortcut_callback = base64(keyMap)
native.$off(shortcut_callback)
native.$on(shortcut_callback, callback)
return handler('keybinder', {
action: 'register',
value: keyMap,
shortcut_callback
})
},
unregister(keyMap) {
let shortcut_callback = base64(keyMap)
native.$off(shortcut_callback)
return handler('keybinder', { action: 'unregister', value: keyMap })
},
unregisterAll(keyMaps) {
for (let it of keyMaps) {
this.unregister(it)
} }
} }
},
tray: {
create() {
//
},
remove() {
//
},
/** $destroy() {
* 设置普通状态的tray图标, 只需要传名称, 自动会去当前主题下去找 this[__events__] = Object.create(null)
*/
set_icon(name) {
return handler('tray', { action: 'set_icon', value: name })
},
/**
* 设置警示图标, 同上
*/
set_attention_icon(name) {
return handler('tray', { action: 'set_attention_icon', value: name })
},
/**
*
*/
set_title(title) {
return handler('tray', { action: 'set_title', value: title })
},
/**
* 修改tray图标状态
* @param status <Number> 0: 隐藏, 1: 显示, 2: 重要(对应上面的attention_icon)
*/
set_status(status) {
return handler('tray', { action: 'set_status', value: status })
} }
}, }
opener: {
postMessage(data = {}) {
return handler('opener', { action: 'postmessage', data }, NO_CALLBACK)
}
},
children: {
postMessage(data = {}, uuid = null) {
return handler(
'children',
{ action: 'postmessage', data, uuid },
NO_CALLBACK
)
}
},
window: {
uuid: '{{uuid}}',
create(options = {}) {
return handler('window', { action: 'create', options })
},
close() { readonly(window, 'native', new Native())
return handler('window', { action: 'close' })
},
isVisible() { native.$on('opener_message', (data, uuid) => _postMessage(data, uuid))
return handler('window', { action: 'is_visible' })
_extend(native, {
env: '{{env}}',
app: {
name: '{{app_name}}',
version: '{{app_version}}',
quit() {
return handler('app', { action: 'quit' }, NO_CALLBACK)
},
relaunch() {
return handler('app', { action: 'relaunch' }, NO_CALLBACK)
},
getLocale() {
return native.env.LANG
}
}, },
toggleVisible() { shell: {
handler('window', { action: 'toggle_visible' }, NO_CALLBACK) openExternal(url) {},
showItemInFolder(path) {},
openPath(path) {},
trashItem(path) {}
}, },
hide() { fs: {
handler('window', { action: 'hide' }, NO_CALLBACK) access(filepath, mode = 'r') {
return handler('fs', { action: 'access', mode, filepath })
},
read(filepath, mode = 'r') {
return handler('fs', { action: 'read', mode, filepath }).then(r =>
mode.includes('b') ? new Uint8Array(r) : r
)
},
write(filepath, content = '', mode = 'w') {
return handler('fs', {
action: 'write',
mode,
filepath,
content
})
},
append(filepath, content = '', mode = 'a+') {
return handler('fs', {
action: 'write',
mode,
filepath,
content
})
},
exists(filepath) {
return handler('fs', { action: 'exists', filepath })
},
list(filepath) {
return handler('fs', { action: 'list', filepath })
},
isfile(filepath) {
return handler('fs', { action: 'isfile', filepath })
},
isdir(filepath) {
return handler('fs', { action: 'isdir', filepath })
},
remove(filepath) {
return handler('fs', { action: 'remove', filepath })
},
rename(filepath, target) {
return handler('fs', { action: 'rename', filepath, target })
},
copy(filepath, target) {
return handler('fs', { action: 'copy', filepath, target })
},
mkdir(filepath) {
return handler('fs', { action: 'mkdir', filepath })
}
}, },
show() { image(filepath) {
handler('window', { action: 'show' }, NO_CALLBACK) return handler('image', { value: filepath }).then(r => new NativeImage(r))
}, },
fullscreen() { clipboard: {
handler('window', { action: 'fullscreen' }, NO_CALLBACK) readText() {
return handler('clipboard', { action: 'wait_for_text' })
},
writeText(value) {
return handler('clipboard', { action: 'set_text', value }, NO_CALLBACK)
},
readImage() {
return handler('clipboard', { action: 'wait_for_image' }).then(r =>
r ? new NativeImage(r) : r
)
},
writeImage(value) {
// 暂不知原因, postMessage传给Gtk后, JSON.stringify()并未读取toJSON的结果
if (typeof value === 'object') {
value = value.toJSON()
}
return handler('clipboard', { action: 'set_image', value }, NO_CALLBACK)
},
clear() {
return handler('clipboard', { action: 'clear' }, NO_CALLBACK)
}
}, },
unfullscreen() { screen: {
handler('window', { action: 'unfullscreen' }, NO_CALLBACK) getAllDisplays() {
return handler('monitor', { action: 'get-all' })
},
getPrimaryDisplay() {
return handler('monitor', { action: 'get-primary' })
}
}, },
maximize() { globalShortcut: {
handler('window', { action: 'maximize' }, NO_CALLBACK) get enabled() {
return handler('keybinder', { action: 'supported' })
},
register(keyMap, callback) {
let shortcut_callback = base64(keyMap)
native.$off(shortcut_callback)
native.$on(shortcut_callback, callback)
return handler('keybinder', {
action: 'register',
value: keyMap,
shortcut_callback
})
},
unregister(keyMap) {
let shortcut_callback = base64(keyMap)
native.$off(shortcut_callback)
return handler('keybinder', { action: 'unregister', value: keyMap })
},
unregisterAll(keyMaps) {
for (let it of keyMaps) {
this.unregister(it)
}
}
}, },
unmaximize() { tray: {
handler('window', { action: 'unmaximize' }, NO_CALLBACK) create() {
//
},
remove() {
//
},
/**
* 设置普通状态的tray图标, 只需要传名称, 自动会去当前主题下去找
*/
set_icon(name) {
return handler('tray', { action: 'set_icon', value: name })
},
/**
* 设置警示图标, 同上
*/
set_attention_icon(name) {
return handler('tray', { action: 'set_attention_icon', value: name })
},
/**
*
*/
set_title(title) {
return handler('tray', { action: 'set_title', value: title })
},
/**
* 修改tray图标状态
* @param status <Number> 0: 隐藏, 1: 显示, 2: 重要(对应上面的attention_icon)
*/
set_status(status) {
return handler('tray', { action: 'set_status', value: status })
}
}, },
setTitle(title = '') { opener: {
handler('window', { action: 'set_title', value: title }, NO_CALLBACK) postMessage(data = {}) {
return handler('opener', { action: 'postmessage', data }, NO_CALLBACK)
}
}, },
resize(width = 0, height = 0) { children: {
postMessage(data = {}, uuid = null) {
return handler(
'children',
{ action: 'postmessage', data, uuid },
NO_CALLBACK
)
}
},
window: {
uuid: '{{uuid}}',
create(options = {}) {
return handler('window', { action: 'create', options })
},
close() {
return handler('window', { action: 'close' })
},
isVisible() {
return handler('window', { action: 'is_visible' })
},
toggleVisible() {
handler('window', { action: 'toggle_visible' }, NO_CALLBACK)
},
hide() {
handler('window', { action: 'hide' }, NO_CALLBACK)
},
show() {
handler('window', { action: 'show' }, NO_CALLBACK)
},
fullscreen() {
handler('window', { action: 'fullscreen' }, NO_CALLBACK)
},
unfullscreen() {
handler('window', { action: 'unfullscreen' }, NO_CALLBACK)
},
maximize() {
handler('window', { action: 'maximize' }, NO_CALLBACK)
},
unmaximize() {
handler('window', { action: 'unmaximize' }, NO_CALLBACK)
},
setTitle(title = '') {
handler('window', { action: 'set_title', value: title }, NO_CALLBACK)
},
resize(width = 0, height = 0) {
handler(
'window',
{ action: 'resize', value: { width, height } },
NO_CALLBACK
)
},
move(x = 0, y = 0) {
handler('window', { action: 'move', value: { x, y } }, NO_CALLBACK)
},
setOpacity(opacity = 1) {
handler(
'window',
{ action: 'set_opacity', value: opacity },
NO_CALLBACK
)
},
alwayOnTop(setting = true) {
handler(
'window',
{ action: 'set_keep_above', value: setting },
NO_CALLBACK
)
},
alwayOnBotttom(setting = true) {
handler(
'window',
{ action: 'set_keep_below', value: setting },
NO_CALLBACK
)
}
},
notify({ title, summary, icon, progress = 0, urgency = 0, callback }) {
let eventName
if (callback) {
eventName = rand()
native.$once(eventName, callback)
}
handler( handler(
'window', 'notify',
{ action: 'resize', value: { width, height } }, { title, summary, icon, progress, urgency, callback: eventName },
NO_CALLBACK NO_CALLBACK
) )
}, },
move(x = 0, y = 0) {
handler('window', { action: 'move', value: { x, y } }, NO_CALLBACK)
},
setOpacity(opacity = 1) {
handler('window', { action: 'set_opacity', value: opacity }, NO_CALLBACK)
},
alwayOnTop(setting = true) {
handler(
'window',
{ action: 'set_keep_above', value: setting },
NO_CALLBACK
)
},
alwayOnBotttom(setting = true) {
handler(
'window',
{ action: 'set_keep_below', value: setting },
NO_CALLBACK
)
}
},
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 },
NO_CALLBACK
)
},
md5(value = '') { md5(value = '') {
return handler('md5', { value }) return handler('md5', { value })
},
proxy: {
disable() {
return handler('proxy', { action: 'disable' }, NO_CALLBACK)
}, },
system() {
return handler('proxy', { action: 'system' }, NO_CALLBACK)
},
custom(url = '', ignore = null) {
return handler('proxy', { action: 'enable', url, ignore }, NO_CALLBACK)
}
},
handler proxy: {
}) disable() {
return handler('proxy', { action: 'disable' }, NO_CALLBACK)
},
system() {
return handler('proxy', { action: 'system' }, NO_CALLBACK)
},
custom(url = '', ignore = null) {
return handler('proxy', { action: 'enable', url, ignore }, NO_CALLBACK)
}
},
handler
})
})()