From 06b335edde08b91aeb5497719f2d584f98abd7c4 Mon Sep 17 00:00:00 2001
From: SineStriker <trueful@163.com>
Date: 摹曛, 28 12月 2023 23:04:49 +0800
Subject: [PATCH] clean code

---
 src/core/contexts/cocoawindowcontext.mm |   97 +++++++++++++++++++++---------------------------
 1 files changed, 43 insertions(+), 54 deletions(-)

diff --git a/src/core/contexts/cocoawindowcontext.mm b/src/core/contexts/cocoawindowcontext.mm
index 54a3dbe..eed5bfd 100644
--- a/src/core/contexts/cocoawindowcontext.mm
+++ b/src/core/contexts/cocoawindowcontext.mm
@@ -130,12 +130,10 @@
         }
 
         ~NSWindowProxy() override {
-            if (blurEffect) {
-                setBlurEffect(BlurMode::None);
-            }
             g_proxyIndexes->remove(nswindow);
         }
 
+        // Delegate
         void windowWillEnterFullScreen() override {
         }
 
@@ -164,16 +162,13 @@
         }
 
         void windowDidResize() override {
-            if (blurEffect) {
-                updateBlurEffectSize();
-            }
-
             if (systemButtonRect.isEmpty() || !systemButtonVisible) {
                 return;
             }
             updateSystemButtonRect();
         }
 
+        // System buttons visibility
         void setSystemButtonVisible(bool visible) {
             systemButtonVisible = visible;
             for (const auto &button : systemButtons()) {
@@ -186,6 +181,7 @@
             updateSystemButtonRect();
         }
 
+        // System buttons area
         void setSystemButtonRect(const QRect &rect) {
             systemButtonRect = rect;
 
@@ -238,47 +234,49 @@
             return {closeBtn, minimizeBtn, zoomBtn};
         }
 
-        void setBlurEffect(BlurMode option) {
-            if (option == BlurMode::None) {
-                if (!blurEffect) {
-                    return;
+        // Blur effect
+        bool setBlurEffect(BlurMode mode) {
+            static Class visualEffectViewClass = NSClassFromString(@"NSVisualEffectView");
+            if (!visualEffectViewClass)
+                return false;
+
+            NSVisualEffectView *effectView = nil;
+            NSView *const view = [nswindow contentView];
+            for (NSView *subview in [[view superview] subviews]) {
+                if ([subview isKindOfClass:visualEffectViewClass]) {
+                    effectView = reinterpret_cast<NSVisualEffectView *>(subview);
                 }
-                [blurEffect removeFromSuperview];
-                [blurEffect release];
-                blurEffect = nil;
+            }
+            if (effectView == nil) {
+                return false;
+            }
+
+            static const auto originalMaterial = effectView.material;
+            static const auto originalBlendingMode = effectView.blendingMode;
+            static const auto originalState = effectView.state;
+
+            if (mode == BlurMode::None) {
+                effectView.material = originalMaterial;
+                effectView.blendingMode = originalBlendingMode;
+                effectView.state = originalState;
+                effectView.appearance = nil;
             } else {
-                if (!blurEffect) {
-                    NSView *const view = [nswindow contentView];
-                    NSVisualEffectView *const blurView =
-                        [[visualEffectViewClass alloc] initWithFrame:view.bounds];
-                    blurView.material = NSVisualEffectMaterialUnderWindowBackground;
-                    blurView.blendingMode = NSVisualEffectBlendingModeBehindWindow;
-                    blurView.state = NSVisualEffectStateFollowsWindowActiveState;
+                effectView.material = NSVisualEffectMaterialUnderWindowBackground;
+                effectView.blendingMode = NSVisualEffectBlendingModeBehindWindow;
+                effectView.state = NSVisualEffectStateFollowsWindowActiveState;
 
-#if 1
-                    const NSView *const parent = [view superview];
-                    [parent addSubview:blurView positioned:NSWindowBelow relativeTo:view];
-#endif
-
-                    blurEffect = blurView;
-                    updateBlurEffectSize();
-                }
-
-                auto view = static_cast<const NSVisualEffectView *>(blurEffect);
-                if (option == BlurMode::Dark) {
-                    view.appearance = [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantDark"];
+                if (mode == BlurMode::Dark) {
+                    effectView.appearance =
+                        [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantDark"];
                 } else {
-                    view.appearance =
+                    effectView.appearance =
                         [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantLight"];
                 }
             }
+            return true;
         }
 
-        void updateBlurEffectSize() {
-            const NSView *const view = [nswindow contentView];
-            blurEffect.frame = view.frame;
-        }
-
+        // System title bar
         void setSystemTitleBarVisible(const bool visible) {
             NSView *nsview = [nswindow contentView];
             if (!nsview) {
@@ -367,8 +365,6 @@
 
         static inline const Class windowClass = [NSWindow class];
 
-        static inline const Class visualEffectViewClass = NSClassFromString(@"NSVisualEffectView");
-
     protected:
         static BOOL canBecomeKeyWindow(id obj, SEL sel) {
             if (g_proxyIndexes->contains(reinterpret_cast<NSWindow *>(obj))) {
@@ -439,7 +435,6 @@
         Q_DISABLE_COPY(NSWindowProxy)
 
         NSWindow *nswindow = nil;
-        NSView *blurEffect = nil;
 
         bool systemButtonVisible = true;
         QRect systemButtonRect;
@@ -681,25 +676,20 @@
         }
 
         if (key == QStringLiteral("blur-effect")) {
-            // Class not available
-            if (!NSWindowProxy::visualEffectViewClass) {
-                return false;
-            }
-
-            auto option = NSWindowProxy::BlurMode::None;
+            auto mode = NSWindowProxy::BlurMode::None;
             if (attribute.type() == QVariant::Bool) {
                 if (attribute.toBool()) {
                     NSString *osxMode =
                         [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
-                    option = [osxMode isEqualToString:@"Dark"] ? NSWindowProxy::BlurMode::Dark
-                                                               : NSWindowProxy::BlurMode::Light;
+                    mode = [osxMode isEqualToString:@"Dark"] ? NSWindowProxy::BlurMode::Dark
+                                                             : NSWindowProxy::BlurMode::Light;
                 }
             } else if (attribute.type() == QVariant::String) {
                 auto value = attribute.toString();
                 if (value == QStringLiteral("dark")) {
-                    option = NSWindowProxy::BlurMode::Dark;
+                    mode = NSWindowProxy::BlurMode::Dark;
                 } else if (value == QStringLiteral("light")) {
-                    option = NSWindowProxy::BlurMode::Light;
+                    mode = NSWindowProxy::BlurMode::Light;
                 } else if (value == QStringLiteral("none")) {
                     // ...
                 } else {
@@ -708,8 +698,7 @@
             } else {
                 return false;
             }
-            ensureWindowProxy(windowId)->setBlurEffect(option);
-            return true;
+            return ensureWindowProxy(windowId)->setBlurEffect(mode);
         }
 
         return false;

--
Gitblit v1.9.1