This commit is contained in:
恍兮惚兮 2024-06-05 02:27:47 +08:00
parent 3c1103ab7e
commit 3db17ea5aa
10 changed files with 158 additions and 10 deletions

2
.gitignore vendored
View File

@ -21,7 +21,7 @@ Thumbs.db
*.wav
build/
plugins/libs/webview2
plugins/builds
plugins/build
temp/

View File

@ -1,7 +1,7 @@
from qtsymbols import *
import os, platform, functools, threading, time, inspect
from traceback import print_exc
import windows, qtawesome
import windows, qtawesome, winsharedutils
from webviewpy import (
webview_native_handle_kind_t,
Webview,
@ -629,8 +629,12 @@ def getscaledrect(size: QSize):
class WebivewWidget(QWidget):
on_load = pyqtSignal(str)
on_ZoomFactorChanged = pyqtSignal(float)
html_limit = 2 * 1024 * 1024 # 2mb
def __del__(self):
winsharedutils.remove_ZoomFactorChanged(self.__token)
def __init__(self, parent=None, debug=False) -> None:
super().__init__(parent)
declare_library_path(
@ -644,14 +648,50 @@ class WebivewWidget(QWidget):
)
self.webview = None
self.webview = Webview(debug=debug, window=int(self.winId()))
# 直接写在这里了
self._putzoom()
self.on_ZoomFactorChanged.connect(self._zoomchanged)
#
zoomfunc = winsharedutils.add_ZoomFactorChanged_CALLBACK(self.ZoomFactorChanged)
self.__token = winsharedutils.add_ZoomFactorChanged(
self.webview.get_native_handle(
webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER
),
zoomfunc,
)
self.keepref = [zoomfunc]
self.webview.bind("__on_load", self._on_load)
self.webview.init("""window.__on_load(window.location.href)""")
def _putzoom(self):
self.put_ZoomFactor(globalconfig["webview2"]["ZoomFactor"])
def _zoomchanged(self, zoom):
globalconfig["webview2"]["ZoomFactor"] = zoom
def ZoomFactorChanged(self, zoom):
self.on_ZoomFactorChanged.emit(zoom)
def put_ZoomFactor(self, zoom):
winsharedutils.put_ZoomFactor(
self.webview.get_native_handle(
webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER
),
zoom,
)
def get_ZoomFactor(self, zoom):
return winsharedutils.get_ZoomFactor(
self.webview.get_native_handle(
webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER
)
)
def _on_load(self, href):
self.on_load.emit(href)
def navigate(self, url):
self._putzoom()
self.webview.navigate(url)
def resizeEvent(self, a0: QResizeEvent) -> None:
@ -663,6 +703,7 @@ class WebivewWidget(QWidget):
windows.MoveWindow(hwnd, 0, 0, size[0], size[1], True)
def setHtml(self, html):
self._putzoom()
self.webview.set_html(html)
def parsehtml(self, html):
@ -753,6 +794,15 @@ class QWebWrap(QWidget):
lambda qurl: self.on_load.emit(qurl.url())
)
self.internal.setZoomFactor(globalconfig["QWebEngineView"]["ZoomFactor"])
t = QTimer(self)
t.setInterval(1000)
t.timeout.connect(self.__getzoomfactor)
t.start(0)
def __getzoomfactor(self):
globalconfig["QWebEngineView"]["ZoomFactor"] = self.internal.zoomFactor()
def navigate(self, url: str):
from PyQt5.QtCore import QUrl
@ -1101,7 +1151,7 @@ def openfiledirectory(directory,multi, edit, isdir, filter1="*.*", callback=None
if len(res) == 0:
return
edit.setText('|'.join(res) if multi else res)
edit.setText("|".join(res) if multi else res)
if callback:
callback(res)
@ -1117,8 +1167,8 @@ def getsimplepatheditor(
lay = QHBoxLayout()
lay.setContentsMargins(0, 0, 0, 0)
director=(text[0] if len(text) else '') if multi else text
e = QLineEdit('|'.join(text) if multi else text)
director = (text[0] if len(text) else "") if multi else text
e = QLineEdit("|".join(text) if multi else text)
e.setReadOnly(True)
if useiconbutton:
bu = getcolorbutton("", "", None, icon="fa.gear", constcolor="#FF69B4")
@ -1126,7 +1176,13 @@ def getsimplepatheditor(
bu = QPushButton(_TR("选择" + ("文件夹" if isdir else "文件")))
bu.clicked.connect(
functools.partial(
openfiledirectory,director, multi, e, isdir, "" if isdir else filter1, callback
openfiledirectory,
director,
multi,
e,
isdir,
"" if isdir else filter1,
callback,
)
)
lay.addWidget(e)

View File

@ -19,6 +19,7 @@ from ctypes import (
c_float,
c_double,
c_char,
CFUNCTYPE,
)
from ctypes.wintypes import WORD, HANDLE, HWND, LONG, DWORD, RECT, BYTE
from windows import WINDOWPLACEMENT
@ -399,3 +400,15 @@ setAcrylicEffect = utilsdll.setAcrylicEffect
setAcrylicEffect.argtypes = (HWND,)
clearEffect = utilsdll.clearEffect
clearEffect.argtypes = (HWND,)
add_ZoomFactorChanged_CALLBACK = CFUNCTYPE(None, c_double)
add_ZoomFactorChanged = utilsdll.add_ZoomFactorChanged
add_ZoomFactorChanged.argtypes = (c_void_p, c_void_p)
add_ZoomFactorChanged.restype = c_void_p
remove_ZoomFactorChanged = utilsdll.remove_ZoomFactorChanged
remove_ZoomFactorChanged.argtypes = c_void_p, c_void_p
get_ZoomFactor = utilsdll.get_ZoomFactor
get_ZoomFactor.argtypes = (c_void_p,)
get_ZoomFactor.restype = c_double
put_ZoomFactor = utilsdll.put_ZoomFactor
put_ZoomFactor.argtypes = c_void_p, c_double

View File

@ -1638,6 +1638,12 @@
"name": "实时编辑"
}
},
"webview2":{
"ZoomFactor": 1
},
"QWebEngineView":{
"ZoomFactor": 1
},
"realtime_edit_target": "realtime_edit",
"magpiepath": "",
"minifollow": false,

View File

@ -11,6 +11,7 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/wil/include)
include_directories(${CMAKE_CURRENT_LIST_DIR}/miniaudio)
include_directories(${CMAKE_CURRENT_LIST_DIR}/tinymp3)
include_directories(${CMAKE_CURRENT_LIST_DIR}/webview2/Microsoft.Web.WebView2.1.0.1150.38/build/native/include)
if(${CMAKE_SIZEOF_VOID_P} EQUAL 4)
set(LTLPlatform "Win32")

View File

@ -1,3 +1,4 @@
python fetchwebview2.py
cmake ../CMakeLists.txt -G "Visual Studio 17 2022" -A win32 -T host=x86 -B ../build/x86
cmake --build ../build/x86 --config Release --target ALL_BUILD -j 14
python copytarget.py 1

View File

@ -1,3 +1,4 @@
python fetchwebview2.py
cmake ../CMakeLists.txt -G "Visual Studio 17 2022" -A x64 -T host=x64 -B ../build/x64
cmake --build ../build/x64 --config Release --target ALL_BUILD -j 14
python copytarget.py 0

View File

@ -0,0 +1,22 @@
mswebview2_version = "1.0.1150.38"
import os, subprocess
target = os.path.normpath(os.path.join(os.path.dirname(__file__), r"..\libs\webview2"))
os.makedirs(target, exist_ok=True)
nuget_exe = os.path.join(target, "nuget.exe")
print(nuget_exe)
if os.path.exists(nuget_exe) == False:
os.system(
rf'curl -sSLo "{nuget_exe}" https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
)
mswebview2_dir = os.path.join(target, f"Microsoft.Web.WebView2.{mswebview2_version}")
if os.path.exists(mswebview2_dir) == False:
os.mkdir(mswebview2_dir)
print(
rf""""{nuget_exe}" install Microsoft.Web.Webview2 -Verbosity quiet -Version "{mswebview2_version}" -OutputDirectory {target}"""
)
subprocess.run(
rf'"{nuget_exe}" install Microsoft.Web.Webview2 -Verbosity quiet -Version "{mswebview2_version}" -OutputDirectory {target}'
)

View File

@ -11,7 +11,7 @@ generate_product_version(
VERSION_PATCH ${VERSION_PATCH}
)
add_library(winsharedutils MODULE AreoAcrylic.cpp screenshot.cpp audio.cpp ../implsapi.cpp hwnd.cpp darklistener.cpp theme.cpp version.cpp otsu.cpp cinterface.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp maglistener.cpp ${versioninfo})
add_library(winsharedutils MODULE webview2_extra.cpp AreoAcrylic.cpp screenshot.cpp audio.cpp ../implsapi.cpp hwnd.cpp darklistener.cpp theme.cpp version.cpp otsu.cpp cinterface.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp maglistener.cpp ${versioninfo})
target_precompile_headers(winsharedutils REUSE_FROM pch)
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
set_target_properties(winsharedutils PROPERTIES OUTPUT_NAME "winsharedutils64")

View File

@ -0,0 +1,48 @@
#include "define.h"
#include <wrl.h>
#include <wil/com.h>
#include <wil/result.h>
#include <wil/com.h>
#include <wrl/implements.h>
using namespace Microsoft::WRL;
#include <WebView2.h>
DECLARE void *add_ZoomFactorChanged(void *m_host, void (*signal)(double))
{
EventRegistrationToken *m_zoomFactorChangedToken = new EventRegistrationToken;
// Register a handler for the ZoomFactorChanged event.
// This handler just announces the new level of zoom on the window's title bar.
reinterpret_cast<ICoreWebView2Controller *>(m_host)->add_ZoomFactorChanged(
Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
[signal](ICoreWebView2Controller *sender, IUnknown *args) -> HRESULT
{
double zoomFactor;
sender->get_ZoomFactor(&zoomFactor);
signal(zoomFactor);
// std::wstring message = L"WebView2APISample (Zoom: " +
// std::to_wstring(int(zoomFactor * 100)) + L"%)";
// SetWindowText(m_appWindow->GetMainWindow(), message.c_str());
return S_OK;
})
.Get(),
m_zoomFactorChangedToken);
return m_zoomFactorChangedToken;
}
DECLARE void remove_ZoomFactorChanged(void *m_host, void *m_zoomFactorChangedToken)
{
reinterpret_cast<ICoreWebView2Controller *>(m_host)->remove_ZoomFactorChanged(*reinterpret_cast<EventRegistrationToken *>(m_zoomFactorChangedToken));
delete m_zoomFactorChangedToken;
}
DECLARE double get_ZoomFactor(void *m_host)
{
double zoomFactor;
reinterpret_cast<ICoreWebView2Controller *>(m_host)->get_ZoomFactor(&zoomFactor);
return zoomFactor;
}
DECLARE void put_ZoomFactor(void *m_host, double zoomFactor)
{
reinterpret_cast<ICoreWebView2Controller *>(m_host)->put_ZoomFactor(zoomFactor);
}