From 591c05641b436f5d6c308a2ee79f9689ecd54d35 Mon Sep 17 00:00:00 2001 From: Pete Schaffner Date: Wed, 7 Feb 2024 21:39:36 +0100 Subject: [PATCH] Use shallow search to improve performance --- .../Features/Terminal/TerminalWindow.swift | 19 +++++++++---------- macos/Sources/Helpers/NSView+Extension.swift | 11 ----------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalWindow.swift b/macos/Sources/Features/Terminal/TerminalWindow.swift index c70755727..050fbce69 100644 --- a/macos/Sources/Features/Terminal/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/TerminalWindow.swift @@ -37,9 +37,9 @@ class TerminalWindow: NSWindow { override func updateConstraintsIfNeeded() { super.updateConstraintsIfNeeded() - guard let titlebarContainer = contentView?.superview?.firstSubview(withClassName: "NSTitlebarContainerView") else { - return - } + guard let titlebarContainer = contentView?.superview?.subviews.first(where: { + $0.className == "NSTitlebarContainerView" + }) else { return } for v in titlebarContainer.subviews(withClassName: "NSTitlebarSeparatorView") { v.isHidden = true @@ -82,9 +82,9 @@ class TerminalWindow: NSWindow { func setTitlebarBackground(_ color: CGColor) { storedTitlebarBackgroundColor = color - guard let titlebarContainer = contentView?.superview?.firstSubview(withClassName: "NSTitlebarContainerView") else { - return - } + guard let titlebarContainer = contentView?.superview?.subviews.first(where: { + $0.className == "NSTitlebarContainerView" + }) else { return } titlebarContainer.wantsLayer = true titlebarContainer.layer?.backgroundColor = color @@ -146,11 +146,10 @@ class TerminalWindow: NSWindow { guard let accessoryClipView = accessoryView.superview else { return } guard let titlebarView = accessoryClipView.superview else { return } guard titlebarView.className == "NSTitlebarView" else { return } + guard let toolbarView = titlebarView.subviews.first(where: { + $0.className == "NSToolbarView" + }) else { return } - guard let toolbarView = titlebarView.firstSubview(withClassName: "NSToolbarView") else { - return - } - addWindowButtonsBackdrop(titlebarView: titlebarView, toolbarView: toolbarView) guard let windowButtonsBackdrop = windowButtonsBackdrop else { return } diff --git a/macos/Sources/Helpers/NSView+Extension.swift b/macos/Sources/Helpers/NSView+Extension.swift index 94cbba3a7..744517c23 100644 --- a/macos/Sources/Helpers/NSView+Extension.swift +++ b/macos/Sources/Helpers/NSView+Extension.swift @@ -1,17 +1,6 @@ import AppKit extension NSView { - func firstSubview(withClassName name: String) -> NSView? { - for subview in subviews { - if String(describing: type(of: subview)) == name { - return subview - } else if let found = subview.firstSubview(withClassName: name) { - return found - } - } - - return nil - } func subviews(withClassName name: String) -> [NSView] { var result = [NSView]()