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 | 83 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/core/contexts/cocoawindowcontext.mm b/src/core/contexts/cocoawindowcontext.mm index faa75bb..eed5bfd 100644 --- a/src/core/contexts/cocoawindowcontext.mm +++ b/src/core/contexts/cocoawindowcontext.mm @@ -118,6 +118,12 @@ namespace QWK { struct NSWindowProxy : public QWK_NSWindowDelegate { + enum class BlurMode { + Dark, + Light, + None, + }; + NSWindowProxy(NSWindow *macWindow) { nswindow = macWindow; g_proxyIndexes->insert(nswindow, this); @@ -127,6 +133,7 @@ g_proxyIndexes->remove(nswindow); } + // Delegate void windowWillEnterFullScreen() override { } @@ -161,6 +168,7 @@ updateSystemButtonRect(); } + // System buttons visibility void setSystemButtonVisible(bool visible) { systemButtonVisible = visible; for (const auto &button : systemButtons()) { @@ -173,6 +181,7 @@ updateSystemButtonRect(); } + // System buttons area void setSystemButtonRect(const QRect &rect) { systemButtonRect = rect; @@ -225,6 +234,49 @@ return {closeBtn, minimizeBtn, zoomBtn}; } + // 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); + } + } + 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 { + effectView.material = NSVisualEffectMaterialUnderWindowBackground; + effectView.blendingMode = NSVisualEffectBlendingModeBehindWindow; + effectView.state = NSVisualEffectStateFollowsWindowActiveState; + + if (mode == BlurMode::Dark) { + effectView.appearance = + [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantDark"]; + } else { + effectView.appearance = + [NSAppearance appearanceNamed:@"NSAppearanceNameVibrantLight"]; + } + } + return true; + } + + // System title bar void setSystemTitleBarVisible(const bool visible) { NSView *nsview = [nswindow contentView]; if (!nsview) { @@ -311,6 +363,8 @@ windowObserver = nil; } + static inline const Class windowClass = [NSWindow class]; + protected: static BOOL canBecomeKeyWindow(id obj, SEL sel) { if (g_proxyIndexes->contains(reinterpret_cast<NSWindow *>(obj))) { @@ -376,8 +430,6 @@ } #endif } - - static inline const Class windowClass = [NSWindow class]; private: Q_DISABLE_COPY(NSWindowProxy) @@ -622,6 +674,33 @@ ensureWindowProxy(windowId)->setSystemButtonVisible(!attribute.toBool()); return true; } + + if (key == QStringLiteral("blur-effect")) { + auto mode = NSWindowProxy::BlurMode::None; + if (attribute.type() == QVariant::Bool) { + if (attribute.toBool()) { + NSString *osxMode = + [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; + mode = [osxMode isEqualToString:@"Dark"] ? NSWindowProxy::BlurMode::Dark + : NSWindowProxy::BlurMode::Light; + } + } else if (attribute.type() == QVariant::String) { + auto value = attribute.toString(); + if (value == QStringLiteral("dark")) { + mode = NSWindowProxy::BlurMode::Dark; + } else if (value == QStringLiteral("light")) { + mode = NSWindowProxy::BlurMode::Light; + } else if (value == QStringLiteral("none")) { + // ... + } else { + return false; + } + } else { + return false; + } + return ensureWindowProxy(windowId)->setBlurEffect(mode); + } + return false; } -- Gitblit v1.9.1