Use shallow search to improve performance

This commit is contained in:
Pete Schaffner
2024-02-07 21:39:36 +01:00
parent bc946109b7
commit 591c05641b
2 changed files with 9 additions and 21 deletions

View File

@ -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 }

View File

@ -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]()