From 96552dfcb531053f4a331c2c80ecf5d8cb2aaf36 Mon Sep 17 00:00:00 2001
From: 某莔 <seiuryuu@gmail.com>
Date: 周四, 28 12月 2023 22:55:40 +0800
Subject: [PATCH] fix: NSVisualEffectView

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

diff --git a/src/core/contexts/cocoawindowcontext.mm b/src/core/contexts/cocoawindowcontext.mm
index 54a3dbe..a84e7dd 100644
--- a/src/core/contexts/cocoawindowcontext.mm
+++ b/src/core/contexts/cocoawindowcontext.mm
@@ -130,9 +130,6 @@
         }
 
         ~NSWindowProxy() override {
-            if (blurEffect) {
-                setBlurEffect(BlurMode::None);
-            }
             g_proxyIndexes->remove(nswindow);
         }
 
@@ -164,10 +161,6 @@
         }
 
         void windowDidResize() override {
-            if (blurEffect) {
-                updateBlurEffectSize();
-            }
-
             if (systemButtonRect.isEmpty() || !systemButtonVisible) {
                 return;
             }
@@ -239,44 +232,35 @@
         }
 
         void setBlurEffect(BlurMode option) {
-            if (option == BlurMode::None) {
-                if (!blurEffect) {
-                    return;
-                }
-                [blurEffect removeFromSuperview];
-                [blurEffect release];
-                blurEffect = 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;
-
-#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"];
-                } else {
-                    view.appearance =
-                        [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantLight"];
+            NSVisualEffectView *effectView = nil;
+            NSView *const view = [nswindow contentView];
+            for (NSView *subview in [[view superview] subviews]) {
+                if ([subview isKindOfClass:visualEffectViewClass]) {
+                    effectView = subview;
                 }
             }
-        }
+            if (effectView == nil) { return; }
 
-        void updateBlurEffectSize() {
-            const NSView *const view = [nswindow contentView];
-            blurEffect.frame = view.frame;
+            static const auto originalMaterial = effectView.material;
+            static const auto originalBlendingMode = effectView.blendingMode;
+            static const auto originalState = effectView.state;
+
+            if (option == BlurMode::None) {
+                effectView.material = originalMaterial;
+                effectView.blendingMode = originalBlendingMode;
+                effectView.state = originalState;
+            } else {
+                effectView.material = NSVisualEffectMaterialUnderWindowBackground;
+                effectView.blendingMode = NSVisualEffectBlendingModeBehindWindow;
+                effectView.state = NSVisualEffectStateFollowsWindowActiveState;
+
+                // if (option == BlurMode::Dark) {
+                //     view.appearance = [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantDark"];
+                // } else {
+                //     view.appearance =
+                //         [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantLight"];
+                // }
+            }
         }
 
         void setSystemTitleBarVisible(const bool visible) {
@@ -439,7 +423,6 @@
         Q_DISABLE_COPY(NSWindowProxy)
 
         NSWindow *nswindow = nil;
-        NSView *blurEffect = nil;
 
         bool systemButtonVisible = true;
         QRect systemButtonRect;

--
Gitblit v1.9.1