From ccd47e6dc9fd72e124f0ac4072b9873856333548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Mon, 27 May 2024 15:38:13 +0800 Subject: [PATCH] arc --- LunaTranslator/LunaTranslator/gui/settin.py | 8 +- .../gui/settingpage_xianshishezhi.py | 19 ++- .../LunaTranslator/gui/translatorUI.py | 12 +- .../LunaTranslator/myutils/Acrylic.py | 151 ++++++++++++++++++ .../files/defaultconfig/config.json | 1 + plugins/CMakeLists.txt | 2 +- 6 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 LunaTranslator/LunaTranslator/myutils/Acrylic.py diff --git a/LunaTranslator/LunaTranslator/gui/settin.py b/LunaTranslator/LunaTranslator/gui/settin.py index ebdd8b55..7e2e45c4 100644 --- a/LunaTranslator/LunaTranslator/gui/settin.py +++ b/LunaTranslator/LunaTranslator/gui/settin.py @@ -32,6 +32,7 @@ from gui.setting_proxy import setTab_proxy from gui.settingpage7 import setTab7, settab7direct from gui.settingpage_about import setTab_about, setTab_about_dicrect from gui.usefulwidget import closeashidewindow, tabadd_lazy +from myutils.Acrylic import WindowEffect class gridwidget(QWidget): @@ -259,11 +260,16 @@ class Settin(closeashidewindow): for widget in QApplication.topLevelWidgets(): if widget.testAttribute(Qt.WA_TranslucentBackground): + if globalconfig["WindowEffect"] == 0: + WindowEffect().clear(int(widget.winId())) + elif globalconfig["WindowEffect"] == 1: + WindowEffect().setAcrylicEffect(int(widget.winId())) + elif globalconfig["WindowEffect"] == 2: + WindowEffect().setAeroEffect(int(widget.winId())) continue winsharedutils.SetTheme( int(widget.winId()), dark, globalconfig["WindowBackdrop"] ) - try: idx = globalconfig[darklight + "theme"] - int(not dark) if idx == -1: diff --git a/LunaTranslator/LunaTranslator/gui/settingpage_xianshishezhi.py b/LunaTranslator/LunaTranslator/gui/settingpage_xianshishezhi.py index 398781bd..44372142 100644 --- a/LunaTranslator/LunaTranslator/gui/settingpage_xianshishezhi.py +++ b/LunaTranslator/LunaTranslator/gui/settingpage_xianshishezhi.py @@ -245,7 +245,8 @@ def setTabThree_lazy(self): functools.partial(changeHorizontal, self) ) self.horizontal_slider_label = QLabel() - self.horizontal_slider_label.setText("{}%".format(globalconfig["transparent_tool"])) + self.horizontal_slider_label.setText("{}%".format(globalconfig["transparent"])) + self.horizontal_slider_tool = QSlider() self.horizontal_slider_tool.setMaximum(100) self.horizontal_slider_tool.setMinimum(1) @@ -259,6 +260,7 @@ def setTabThree_lazy(self): self.horizontal_slider_tool_label.setText( "{}%".format(globalconfig["transparent_tool"]) ) + self.font_comboBox = QFontComboBox() self.font_comboBox.activated[str].connect( lambda x: globalconfig.__setitem__("fonttype", x) @@ -644,6 +646,21 @@ def setTabThree_lazy(self): 5, ), ], + [ + ("WindowEffect", 6), + ( + getsimplecombobox( + ["Disable", "Acrylic", "Aero"], + globalconfig, + "WindowEffect", + callback=lambda _: [ + self.setstylesheet(), + gobject.baseobject.translation_ui.set_color_transparency(), + ], + ), + 5, + ), + ], [ ("WindowBackdrop", 6), ( diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index b43e885e..ede48a6b 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -669,7 +669,7 @@ class QUnFrameWindow(resizableframeless): return "border-radius:%spx" % r def set_color_transparency(self): - + rate = int(globalconfig["WindowEffect"] == 0) use_r1 = min( self.translate_text.height() // 2, self.translate_text.width() // 2, @@ -681,13 +681,13 @@ class QUnFrameWindow(resizableframeless): globalconfig["yuanjiao_r"], ) topr = self.createborderradiusstring( - use_r1, + rate * use_r1, globalconfig["yuanjiao_merge"] and self._TitleLabel.isVisible(), False, ) - bottomr2 = self.createborderradiusstring(use_r2, False) + bottomr3 = self.createborderradiusstring(use_r2, False) bottomr = self.createborderradiusstring( - use_r2, globalconfig["yuanjiao_merge"], True + rate * use_r2, globalconfig["yuanjiao_merge"], True ) self.translate_text.setStyleSheet( @@ -724,12 +724,12 @@ class QUnFrameWindow(resizableframeless): font: 100 10pt; } """ % ( - bottomr2, + bottomr3, ( globalconfig["button_color_normal"], globalconfig["button_color_close"], )[_type - 1], - bottomr2, + bottomr3, ) for btn in self.stylebuttons[_type]: diff --git a/LunaTranslator/LunaTranslator/myutils/Acrylic.py b/LunaTranslator/LunaTranslator/myutils/Acrylic.py new file mode 100644 index 00000000..1acad55f --- /dev/null +++ b/LunaTranslator/LunaTranslator/myutils/Acrylic.py @@ -0,0 +1,151 @@ +# coding:utf-8 + +from ctypes import POINTER, Structure +from ctypes.wintypes import DWORD, HWND, ULONG +from enum import Enum + +# coding:utf-8 + +from ctypes import POINTER, c_bool, sizeof, windll, pointer, c_int +from ctypes.wintypes import DWORD, HWND, ULONG + + +class WINDOWCOMPOSITIONATTRIB(Enum): + WCA_UNDEFINED = (0,) + WCA_NCRENDERING_ENABLED = (1,) + WCA_NCRENDERING_POLICY = (2,) + WCA_TRANSITIONS_FORCEDISABLED = (3,) + WCA_ALLOW_NCPAINT = (4,) + WCA_CAPTION_BUTTON_BOUNDS = (5,) + WCA_NONCLIENT_RTL_LAYOUT = (6,) + WCA_FORCE_ICONIC_REPRESENTATION = (7,) + WCA_EXTENDED_FRAME_BOUNDS = (8,) + WCA_HAS_ICONIC_BITMAP = (9,) + WCA_THEME_ATTRIBUTES = (10,) + WCA_NCRENDERING_EXILED = (11,) + WCA_NCADORNMENTINFO = (12,) + WCA_EXCLUDED_FROM_LIVEPREVIEW = (13,) + WCA_VIDEO_OVERLAY_ACTIVE = (14,) + WCA_FORCE_ACTIVEWINDOW_APPEARANCE = (15,) + WCA_DISALLOW_PEEK = (16,) + WCA_CLOAK = (17,) + WCA_CLOAKED = (18,) + WCA_ACCENT_POLICY = (19,) + WCA_FREEZE_REPRESENTATION = (20,) + WCA_EVER_UNCLOAKED = (21,) + WCA_VISUAL_OWNER = (22,) + WCA_LAST = 23 + + +class ACCENT_STATE(Enum): + """客户区状态枚举类""" + + ACCENT_DISABLED = (0,) + ACCENT_ENABLE_GRADIENT = (1,) + ACCENT_ENABLE_TRANSPARENTGRADIENT = (2,) + ACCENT_ENABLE_BLURBEHIND = (3,) # Aero效果 + ACCENT_ENABLE_ACRYLICBLURBEHIND = (4,) # 亚克力效果 + ACCENT_INVALID_STATE = 5 + + +class ACCENT_POLICY(Structure): + """设置客户区的具体属性""" + + _fields_ = [ + ("AccentState", DWORD), + ("AccentFlags", DWORD), + ("GradientColor", DWORD), + ("AnimationId", DWORD), + ] + + +class WINDOWCOMPOSITIONATTRIBDATA(Structure): + _fields_ = [ + ("Attribute", DWORD), + ( + "Data", + POINTER(ACCENT_POLICY), + ), # POINTER()接收任何ctypes类型,并返回一个指针类型 + ("SizeOfData", ULONG), + ] + + +class WindowEffect: + """调用windows api实现窗口效果""" + + def __init__(self): + # 调用api + self.SetWindowCompositionAttribute = windll.user32.SetWindowCompositionAttribute + self.SetWindowCompositionAttribute.restype = c_bool + self.SetWindowCompositionAttribute.argtypes = [ + c_int, + POINTER(WINDOWCOMPOSITIONATTRIBDATA), + ] + # 初始化结构体 + self.accentPolicy = ACCENT_POLICY() + self.winCompAttrData = WINDOWCOMPOSITIONATTRIBDATA() + self.winCompAttrData.Attribute = ( + WINDOWCOMPOSITIONATTRIB.WCA_ACCENT_POLICY.value[0] + ) + self.winCompAttrData.SizeOfData = sizeof(self.accentPolicy) + self.winCompAttrData.Data = pointer(self.accentPolicy) + + def clear(self, hWnd): + self.accentPolicy.AccentState = ACCENT_STATE.ACCENT_DISABLED.value[0] + self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData)) + + def setAcrylicEffect( + self, + hWnd: int, + gradientColor: str = "F2F2F230", + isEnableShadow: bool = True, + animationId: int = 0, + ): + """开启亚克力效果 + + Parameters + ---------- + hWnd: int + 窗口句柄 + + gradientColor: str + 十六进制亚克力混合色,对应 RGBA 四个分量 + + isEnableShadow: bool + 是否启用窗口阴影 + + animationId: int + 控制磨砂动画 + """ + # 亚克力混合色 + gradientColor = ( + gradientColor[6:] + + gradientColor[4:6] + + gradientColor[2:4] + + gradientColor[:2] + ) + gradientColor = DWORD(int(gradientColor, base=16)) + # 磨砂动画 + animationId = DWORD(animationId) + # 窗口阴影 + accentFlags = DWORD(0x20 | 0x40 | 0x80 | 0x100) if isEnableShadow else DWORD(0) + self.accentPolicy.AccentState = ( + ACCENT_STATE.ACCENT_ENABLE_ACRYLICBLURBEHIND.value[0] + ) + self.accentPolicy.GradientColor = gradientColor + self.accentPolicy.AccentFlags = accentFlags + self.accentPolicy.AnimationId = animationId + # 开启亚克力 + self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData)) + + def setAeroEffect(self, hWnd: int): + """开启 Aero 效果 + + Parameter + ---------- + hWnd: int + 窗口句柄 + """ + self.accentPolicy.AccentState = ACCENT_STATE.ACCENT_ENABLE_BLURBEHIND.value[0] + # 开启Aero + self.SetWindowCompositionAttribute(hWnd, pointer(self.winCompAttrData)) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index beb8a8ab..4c0b0a0d 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -8,6 +8,7 @@ "showrangeafterrangeselect": true, "autodisappear": false, "WindowBackdrop": 0, + "WindowEffect": 0, "read_raw": true, "read_trans": false, "read_translator": 0, diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index e37ce83d..d5c13815 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -29,7 +29,7 @@ include(generate_product_version) set(VERSION_MAJOR 2) set(VERSION_MINOR 53) -set(VERSION_PATCH 0) +set(VERSION_PATCH 1) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h)