Skip to content

Commit 5b0f24c

Browse files
authored
Faster and more accurate toolbarEffectView search (#112)
* Faster and more accurate toolbarEffectView search * Fix comment
1 parent 172f9ff commit 5b0f24c

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

MarkEditMac/Modules/Sources/AppKitExtensions/UI/NSWindow+Extension.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ public extension NSWindow {
1212
}
1313

1414
var toolbarEffectView: NSVisualEffectView? {
15-
var result: NSVisualEffectView?
16-
rootView?.enumerateChildren { (view: NSVisualEffectView) in
17-
// Blindly consider a full-width NSVisualEffectView the toolbar,
18-
// a more accurate way would be relying on NSThemeFrame, which is private.
19-
if abs(view.frame.width - frame.width) < .ulpOfOne {
20-
result = view
15+
guard let rootView else {
16+
assertionFailure("Missing rootView from window to proceed")
17+
return nil
18+
}
19+
20+
var stack = [rootView]
21+
while !stack.isEmpty {
22+
let node = stack.removeLast()
23+
if node is NSVisualEffectView && node.superview?.className.hasPrefix("NSTitlebar") == true {
24+
// What we want is an NSVisualEffectView child of an NSTitlebarView
25+
return node as? NSVisualEffectView
2126
}
27+
28+
// Depth-first search
29+
stack.append(contentsOf: node.subviews)
2230
}
2331

24-
assert(result != nil, "Failed to find NSVisualEffectView in toolbar")
25-
return result
32+
assertionFailure("Failed to find NSVisualEffectView in toolbar")
33+
return nil
2634
}
2735

2836
/// Change the frame size, treat the top-left corner as the anchor point.

MarkEditMac/Sources/Editor/EditorWindow.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,11 @@ final class EditorWindow: NSWindow {
4747

4848
// Slightly change the toolbar effect to match editor better
4949
if let view = toolbarEffectView {
50-
// Blend the color of contents behind the window
51-
view.blendingMode = .behindWindow
50+
view.alphaValue = effectiveAppearance.isDarkMode ? 0.3 : 0.7
5251
view.isHidden = reduceTransparency == true
5352

54-
// When there's a dimmed view from save panel, always make it fully visible,
55-
// otherwise, a layered background appears.
56-
if NSApp.keyWindow is NSSavePanel {
57-
view.alphaValue = 1.0
58-
} else {
59-
view.alphaValue = effectiveAppearance.isDarkMode ? 0.3 : 0.7
60-
}
53+
// Blend the color of contents behind the window
54+
view.blendingMode = .behindWindow
6155
}
6256
}
6357
}

0 commit comments

Comments
 (0)