修复窗口移动API; 增加新窗口打开的支持

master
yutent 2023-09-07 19:58:17 +08:00
parent ed5dcf1245
commit 1f2273150c
6 changed files with 105 additions and 20 deletions

View File

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

View File

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

View File

@ -6,8 +6,8 @@
import gi, os
gi.require_version("WebKit2", "4.1")
from gi.repository import WebKit2
from ._version import version

View File

@ -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'],

View File

@ -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 set_root(self, root):
# 手动创建窗口
def new_window_by_custom(self, options = {}):
return create_custom_window(self, options)
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)

View File

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