Compare commits

..

2 Commits

Author SHA1 Message Date
yutent ece744aafe 增加shell操作 2023-09-12 18:55:32 +08:00
yutent b398551deb native对象及属性改为只读 2023-09-12 14:28:04 +08:00
2 changed files with 481 additions and 440 deletions

View File

@ -3,7 +3,8 @@
# @date 2023/08/08 14:07:26 # @date 2023/08/08 14:07:26
import gi, os, sys, 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")
@ -288,6 +289,25 @@ class WebEngine(WebKit2.WebView):
return (_error, output) 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 = {}): def _fs_handler(self, params = {}):
_error = None _error = None

View File

@ -3,8 +3,8 @@
* @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 = { const MIME_TYPES = {
html: 'text/html', html: 'text/html',
json: 'application/json', json: 'application/json',
js: 'application/javascript', js: 'application/javascript',
@ -37,31 +37,33 @@ const MIME_TYPES = {
doc: 'application/msword', doc: 'application/msword',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
} }
const KEYS_MAP = { const KEYS_MAP = {
shift: '<Shift>', shift: '<Shift>',
ctrl: '<Ctrl>', ctrl: '<Ctrl>',
alt: '<Alt>', alt: '<Alt>',
super: '<Super>' super: '<Super>'
} }
const NO_CALLBACK = false const NO_CALLBACK = false
const CALL_ONCE = true const CALL_ONCE = true
function defer() { const __events__ = Symbol('events')
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
@ -84,13 +86,13 @@ function handler(event, data = {}, need = CALL_ONCE) {
callback callback
}) })
return _.promise return _.promise
} }
function base64(str = '') { function base64(str = '') {
return btoa(str).replace(/[+=\/]/g, '') return btoa(str).replace(/[+=\/]/g, '')
} }
function _postMessage(data = {}, uuid = null) { function _postMessage(data = {}, uuid = null) {
let ev = new Event('message') let ev = new Event('message')
Object.assign(ev, { Object.assign(ev, {
data, data,
@ -101,18 +103,24 @@ function _postMessage(data = {}, uuid = null) {
} }
}) })
window.dispatchEvent(ev) window.dispatchEvent(ev)
} }
function readonly(obj, key, value) { function readonly(obj, key, value) {
Object.defineProperty(obj, key, { Object.defineProperty(obj, key, {
get() { get() {
return value return value
}, },
enumerable: false enumerable: false
}) })
} }
class NativeImage { function _extend(origin, options = {}) {
for (let k in options) {
readonly(origin, k, options[k])
}
}
class NativeImage {
#origin #origin
constructor(obj) { constructor(obj) {
@ -163,17 +171,17 @@ class NativeImage {
toDataURL(type) { toDataURL(type) {
return this.export(type, true) return this.export(type, true)
} }
} }
class EventEmitter { class Native {
// //
__events__ = Object.create(null) [__events__] = Object.create(null)
$on(name, fn) { $on(name, fn) {
if (this.__events__[name]) { if (this[__events__][name]) {
this.__events__[name].push(fn) this[__events__][name].push(fn)
} else { } else {
this.__events__[name] = [fn] this[__events__][name] = [fn]
} }
} }
@ -183,18 +191,20 @@ class EventEmitter {
} }
$off(name, fn) { $off(name, fn) {
if (this.__events__[name]) { if (this[__events__][name]) {
if (fn) { if (fn) {
this.__events__[name] = this.__events__[name].filter(it => it !== fn) this[__events__][name] = this[__events__][name].filter(
it => it !== fn
)
} else { } else {
this.__events__[name] = [] this[__events__][name] = []
} }
} }
} }
$emit(name, ...args) { $emit(name, ...args) {
if (this.__events__[name]) { if (this[__events__][name]) {
for (let fn of this.__events__[name]) { for (let fn of this[__events__][name]) {
try { try {
fn.apply(this, args) fn.apply(this, args)
if (fn.__once__) { if (fn.__once__) {
@ -208,15 +218,15 @@ class EventEmitter {
} }
$destroy() { $destroy() {
this.__events__ = Object.create(null) this[__events__] = Object.create(null)
}
} }
}
readonly(window, 'native', new EventEmitter()) readonly(window, 'native', new Native())
native.$on('opener_message', (data, uuid) => _postMessage(data, uuid)) native.$on('opener_message', (data, uuid) => _postMessage(data, uuid))
Object.assign(native, { _extend(native, {
env: '{{env}}', env: '{{env}}',
app: { app: {
name: '{{app_name}}', name: '{{app_name}}',
@ -231,6 +241,12 @@ Object.assign(native, {
return native.env.LANG return native.env.LANG
} }
}, },
shell: {
openExternal(url) {},
showItemInFolder(path) {},
openPath(path) {},
trashItem(path) {}
},
fs: { fs: {
access(filepath, mode = 'r') { access(filepath, mode = 'r') {
return handler('fs', { action: 'access', mode, filepath }) return handler('fs', { action: 'access', mode, filepath })
@ -440,7 +456,11 @@ Object.assign(native, {
handler('window', { action: 'move', value: { x, y } }, NO_CALLBACK) handler('window', { action: 'move', value: { x, y } }, NO_CALLBACK)
}, },
setOpacity(opacity = 1) { setOpacity(opacity = 1) {
handler('window', { action: 'set_opacity', value: opacity }, NO_CALLBACK) handler(
'window',
{ action: 'set_opacity', value: opacity },
NO_CALLBACK
)
}, },
alwayOnTop(setting = true) { alwayOnTop(setting = true) {
handler( handler(
@ -487,4 +507,5 @@ Object.assign(native, {
}, },
handler handler
}) })
})()