From 1f2273150c182f7894f3521c6f40013b3d03ee6e Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 7 Sep 2023 19:58:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AA=97=E5=8F=A3=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8API;=20=E5=A2=9E=E5=8A=A0=E6=96=B0=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=89=93=E5=BC=80=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webengine/gtk3/_custom_window.py | 62 +++++++++++++++++++ .../dist-packages/webengine/gtk3/_protocal.py | 5 +- .../dist-packages/webengine/gtk3/_settings.py | 2 +- .../dist-packages/webengine/gtk3/_utils.py | 5 +- .../webengine/gtk3/_webengine.py | 46 +++++++++----- .../dist-packages/webengine/gtk3/inject.js | 5 +- 6 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 usr/lib/python3/dist-packages/webengine/gtk3/_custom_window.py diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_custom_window.py b/usr/lib/python3/dist-packages/webengine/gtk3/_custom_window.py new file mode 100644 index 0000000..1307253 --- /dev/null +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_custom_window.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +import gi, os, json, shutil, hashlib, time, threading + +gi.require_version('Gtk', '3.0') +gi.require_version("WebKit2", "4.1") +from gi.repository import GObject, Gtk, Gdk, WebKit2, GLib, Gio, GdkPixbuf + + +def create_same_window(origin, req): + w, h = origin.window.get_size() + win = Gtk.Window() + win.set_default_size(w, h) + web = WebKit2.WebView.new_with_related_view(origin) + web.load_request(req) + + win.add(web) + win.show_all() + return web + + + + +def create_custom_window(origin, options): + WebEngine = type(origin) + _w, _h = origin.window.get_size() + w = options.get('width') or _w + h = options.get('height') or _h + win = Gtk.Window() + win.set_default_size(w, h) + + wmclass = options.get('wmclass') or 'WebEngine Window' + win.set_wmclass(wmclass, 'WebEngine') + win.set_title(options.get('title') or 'WebEngine') + + if options.get('frame') == False: + win.set_decorated(False) + + if options.get('x') is not None and options.get('y') is not None: + win.move(options.get('x'), options.get('y')) + + if options.get('always_on_top') == True: + win.set_keep_above(options['always_on_top']) + + if options.get('resizable') == False: + win.set_resizable(False) + + + + web = WebEngine(win) + # web = WebEngine(win, origin) + # web = WebKit2.WebView.new_with_related_view(origin) + # web = WebEngine.new_with(win, origin) + # web.window = win + web.set_root(origin.root, True) + web.set_zoom_level(origin.get_zoom_level()) + web.set_settings(origin.get_settings()) + web.load(options.get('url')) + + win.add(web) + win.show_all() + return web \ No newline at end of file diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py b/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py index e775a41..bfa17ee 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py @@ -1,6 +1,9 @@ -import os +import gi, os + +gi.require_version("WebKit2", "4.1") from gi.repository import Gio, WebKit2 + from ._mimetypes import get_mimetype from ._version import version diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_settings.py b/usr/lib/python3/dist-packages/webengine/gtk3/_settings.py index 32ac35c..228ac57 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_settings.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_settings.py @@ -6,8 +6,8 @@ import gi, os gi.require_version("WebKit2", "4.1") - from gi.repository import WebKit2 + from ._version import version diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py b/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py index 8c7f2a3..f0fb3d5 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_utils.py @@ -4,7 +4,8 @@ # @date 2023/07/28 14:39:33 import gi -from gi.repository.GdkPixbuf import Pixbuf +gi.require_version('Gtk', '3.0') +from gi.repository import GdkPixbuf def get_monitor_info(monitor): return { @@ -50,7 +51,7 @@ def pixbuf_to_dict(pixbuf, filename = ''): def dict_to_pixbuf(data): if data: - image = Pixbuf.new_from_data( + image = GdkPixbuf.Pixbuf.new_from_data( data = bytes(data['bytes']), colorspace = data['colorspace'], has_alpha = data['has_alpha'], diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py index 6a17977..f684446 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py @@ -10,6 +10,7 @@ gi.require_version("WebKit2", "4.1") from gi.repository import GObject, Gtk, Gdk, WebKit2, GLib, Gio, GdkPixbuf +from ._custom_window import create_same_window, create_custom_window # 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon @@ -65,6 +66,7 @@ def set_timeout(timeout = 0.5): return decorator + class WebEngine(WebKit2.WebView): __gsignals__ = { @@ -75,11 +77,17 @@ class WebEngine(WebKit2.WebView): window = None custom_bridge = None - def __init__(self, window): + def __init__(self, win): WebKit2.WebView.__init__(self) - self.window = window + self.window = win + + if win.get_title() is None: + win.set_title('WebEngine') + + if win.get_icon() is None and win.get_icon_name() is None: + win.set_icon_name('web-browser') self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) self.display = Gdk.Display.get_default() @@ -101,21 +109,22 @@ class WebEngine(WebKit2.WebView): + # 响应原生js的 window.open def create_new_window(self, webview, nav): req = nav.get_request() - w, h = self.window.get_size() - win = Gtk.Window() - web = WebEngine(win) - web.set_settings(self.get_settings()) - web.set_zoom_level(self.get_zoom_level()) - web.load_request(req) - win.set_default_size(w, h) - win.add(web) - win.show_all() + return create_same_window(self, req) + + + # 手动创建窗口 + def new_window_by_custom(self, options = {}): + return create_custom_window(self, options) - def set_root(self, root): + def set_root(self, root, has_protocol = False): self.root = root + + if has_protocol: + return self return self.use(create_protocal(root)) @@ -125,7 +134,7 @@ class WebEngine(WebKit2.WebView): def load(self, url = '/index.html'): - if url.startswith('http:') or url.startswith('https:'): + if url.startswith('http://') or url.startswith('https://'): self.load_uri(url) else: if self.root is None: @@ -133,8 +142,10 @@ class WebEngine(WebKit2.WebView): else: if url.startswith('/'): self.load_uri(f"app://{url}") + elif url.startswith('app://'): + self.load_uri(url) else: - raise ValueError('url must starts with "/"') + raise ValueError('url must starts with "/" or "app://"') @@ -171,7 +182,7 @@ class WebEngine(WebKit2.WebView): # 退出app case 'quit': self.window.close() - self.emit('quit') + # self.emit('quit') # 读取图片, 返回图片像素数据 case 'image': @@ -384,6 +395,10 @@ class WebEngine(WebKit2.WebView): output = None match(params.get('action')): + + case 'create': + self.new_window_by_custom(params.get('options')) + case 'fullscreen': self.window.fullscreen() @@ -451,3 +466,4 @@ class WebEngine(WebKit2.WebView): output = False return (_error, output) + diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js index 6147341..ba535f7 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js +++ b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js @@ -339,6 +339,9 @@ Object.assign(native, { } }, window: { + create(options = {}) { + return handler('window', { action: 'create', options }) + }, isVisible() { return handler('window', { action: 'is_visible' }) }, @@ -370,7 +373,7 @@ Object.assign(native, { handler('window', { action: 'resize', value: { width, height } }, null) }, move(x = 0, y = 0) { - handler('window', { action: 'resize', value: { x, y } }, null) + handler('window', { action: 'move', value: { x, y } }, null) }, setOpacity(opacity = 1) { handler('window', { action: 'set_opacity', value: opacity }, null)