更新tray的使用
parent
aba79c47b4
commit
e5642a10ac
7
app.js
7
app.js
|
@ -9,7 +9,7 @@ import { $, bind } from 'wkit'
|
||||||
|
|
||||||
async function test1() {
|
async function test1() {
|
||||||
try {
|
try {
|
||||||
var result = await window.webkit.messageHandlers.app.postMessage({
|
var result = window.webkit.messageHandlers.app.postMessage({
|
||||||
value: 'Test 1'
|
value: 'Test 1'
|
||||||
})
|
})
|
||||||
$('#output').innerHTML = 'output: ' + result
|
$('#output').innerHTML = 'output: ' + result
|
||||||
|
@ -20,14 +20,13 @@ async function test1() {
|
||||||
async function test2() {
|
async function test2() {
|
||||||
let key = 'cb_' + Math.random().toString().slice(2)
|
let key = 'cb_' + Math.random().toString().slice(2)
|
||||||
|
|
||||||
window.$on(key, function (v) {
|
native.$on(key, function (v) {
|
||||||
$('#output').innerHTML = v
|
$('#output').innerHTML = v
|
||||||
})
|
})
|
||||||
window.webkit.messageHandlers.app.postMessage({
|
await window.webkit.messageHandlers.app.postMessage({
|
||||||
value: new Date(),
|
value: new Date(),
|
||||||
key
|
key
|
||||||
})
|
})
|
||||||
$('#output').innerHTML = result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bind($('.btn1'), 'click', test1)
|
bind($('.btn1'), 'click', test1)
|
||||||
|
|
16
inject.js
16
inject.js
|
@ -4,9 +4,9 @@
|
||||||
* @date 2023/07/21 17:38:11
|
* @date 2023/07/21 17:38:11
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Object.assign(window, {
|
class EventEmitter {
|
||||||
//
|
//
|
||||||
__events__: Object.create(null),
|
__events__ = Object.create(null)
|
||||||
|
|
||||||
$on(name, fn) {
|
$on(name, fn) {
|
||||||
if (this.__events__[name]) {
|
if (this.__events__[name]) {
|
||||||
|
@ -14,12 +14,12 @@ Object.assign(window, {
|
||||||
} else {
|
} else {
|
||||||
this.__events__[name] = [fn]
|
this.__events__[name] = [fn]
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
$once(name, fn) {
|
$once(name, fn) {
|
||||||
fn.__once__ = true
|
fn.__once__ = true
|
||||||
this.$on(name, fn)
|
this.$on(name, fn)
|
||||||
},
|
}
|
||||||
|
|
||||||
$off(name, fn) {
|
$off(name, fn) {
|
||||||
if (this.__events__[name]) {
|
if (this.__events__[name]) {
|
||||||
|
@ -29,7 +29,7 @@ Object.assign(window, {
|
||||||
this.__events__[name] = []
|
this.__events__[name] = []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
$emit(name, ...args) {
|
$emit(name, ...args) {
|
||||||
if (this.__events__[name]) {
|
if (this.__events__[name]) {
|
||||||
|
@ -44,9 +44,11 @@ Object.assign(window, {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
$destroy() {
|
$destroy() {
|
||||||
this.__events__ = Object.create(null)
|
this.__events__ = Object.create(null)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
window.native = new EventEmitter()
|
||||||
|
|
36
main.py
36
main.py
|
@ -4,13 +4,16 @@ import gi, json, os
|
||||||
|
|
||||||
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("JavaScriptCore", "4.1")
|
|
||||||
|
|
||||||
from gi.repository import Gtk, WebKit2, JavaScriptCore
|
from gi.repository import Gtk, WebKit2
|
||||||
|
|
||||||
|
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
|
||||||
|
try:
|
||||||
|
gi.require_version('AyatanaAppIndicator3', '0.1')
|
||||||
|
from gi.repository import AyatanaAppIndicator3 as AppIndicator3
|
||||||
|
except (ValueError, ImportError):
|
||||||
|
AppIndicator3 = None
|
||||||
|
|
||||||
class MyScriptMessageHandler(WebKit2.ScriptMessageReply):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
class WebKitWindow(Gtk.Window):
|
class WebKitWindow(Gtk.Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -49,11 +52,26 @@ class WebKitWindow(Gtk.Window):
|
||||||
self.webview.load_uri("http://127.0.0.1:10086/index.html")
|
self.webview.load_uri("http://127.0.0.1:10086/index.html")
|
||||||
# self.webview.load_uri("https://benchmark.wkit.fun")
|
# self.webview.load_uri("https://benchmark.wkit.fun")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.add(self.webview)
|
self.add(self.webview)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_tray(self):
|
||||||
|
if AppIndicator3 :
|
||||||
|
indicator = AppIndicator3.Indicator.new(
|
||||||
|
"youtube",
|
||||||
|
"youtube",
|
||||||
|
AppIndicator3.IndicatorCategory.APPLICATION_STATUS
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
|
||||||
|
|
||||||
|
return indicator
|
||||||
|
|
||||||
|
|
||||||
def file_path(self, filepath):
|
def file_path(self, filepath):
|
||||||
root = os.path.dirname(os.path.realpath(__file__))
|
root = os.path.dirname(os.path.realpath(__file__))
|
||||||
return os.path.join(root, filepath)
|
return os.path.join(root, filepath)
|
||||||
|
@ -66,7 +84,7 @@ class WebKitWindow(Gtk.Window):
|
||||||
key = data.get('key')
|
key = data.get('key')
|
||||||
|
|
||||||
if key :
|
if key :
|
||||||
scripts = '$emit("' + key + '", "这是py返回的值 ' + data.get('value') + '")'
|
scripts = 'native.$emit("' + key + '", "这是py返回的值 ' + data.get('value') + '")'
|
||||||
self.webview.evaluate_javascript(scripts, -1)
|
self.webview.evaluate_javascript(scripts, -1)
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,4 +107,8 @@ win = WebKitWindow()
|
||||||
win.connect("destroy", Gtk.main_quit)
|
win.connect("destroy", Gtk.main_quit)
|
||||||
win.show_all()
|
win.show_all()
|
||||||
|
|
||||||
|
tray = win.create_tray()
|
||||||
|
|
||||||
|
print(tray)
|
||||||
|
|
||||||
Gtk.main()
|
Gtk.main()
|
|
@ -0,0 +1,3 @@
|
||||||
|
[tool.python3-build.dependencies]
|
||||||
|
gtk3 = ">=3.0"
|
||||||
|
webkit2 = ">=4.1"
|
22
setup.py
22
setup.py
|
@ -1,22 +0,0 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
from setuptools import setup
|
|
||||||
|
|
||||||
setup(
|
|
||||||
name='myapp',
|
|
||||||
version='0.1',
|
|
||||||
description='My GTK3 + WebKit2 application',
|
|
||||||
author='Your Name',
|
|
||||||
author_email='your@email.com',
|
|
||||||
url='https://github.com/yourusername/myapp',
|
|
||||||
py_modules=['main'],
|
|
||||||
install_requires=[
|
|
||||||
'gi',
|
|
||||||
'webkit2gtk',
|
|
||||||
],
|
|
||||||
entry_points={
|
|
||||||
'gui_scripts': [
|
|
||||||
'myapp=main:WebKitWindow'
|
|
||||||
]
|
|
||||||
},
|
|
||||||
)
|
|
Reference in New Issue