Skip to content

Commit 26023b1

Browse files
committed
Better way to hack AppKitSearchField
1 parent 618d3bd commit 26023b1

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

MarkEditMac/Modules/Sources/AppKitControls/LabeledSearchField.swift

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,13 @@ public final class LabeledSearchField: NSSearchField {
6565
}
6666

6767
// To completely remove the unnecessary capsule-style border
68-
if modernStyle {
69-
#if DEBUG
70-
var hasAppKitSearchField = false
71-
#endif
72-
73-
enumerateDescendants { view in
74-
if view.className.contains("AppKitSearchField") {
75-
#if DEBUG
76-
hasAppKitSearchField = true
77-
#endif
78-
renderCustomIcons(modernBezel: view)
79-
}
80-
}
81-
82-
#if DEBUG
83-
assert(hasAppKitSearchField, "Missing AppKitSearchField in NSSearchField")
84-
#endif
68+
if modernStyle, let view = modernBezelView {
69+
renderCustomIcons(modernBezel: view)
8570
}
71+
72+
#if DEBUG
73+
assert(!modernStyle || modernBezelView != nil, "Missing AppKitSearchField in NSSearchField")
74+
#endif
8675
}
8776

8877
override public func draw(_ dirtyRect: NSRect) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public extension NSSearchField {
1212
subviews.first { $0.className.hasSuffix("FocusClipView") }
1313
}
1414

15+
var modernBezelView: NSView? {
16+
firstDescendant {
17+
$0.className.contains("AppKitSearchField")
18+
}
19+
}
20+
1521
var searchButtonCell: NSButtonCell? {
1622
(cell as? NSSearchFieldCell)?.searchButtonCell
1723
}

MarkEditMac/Modules/Tests/RuntimeTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ final class RuntimeTests: XCTestCase {
6363
testExistenceOfSelector(object: NSImage(), selector: "_setTintColor:")
6464
}
6565

66+
func testExistenceOfAppKitSearchField() {
67+
let window = NSWindow()
68+
window.makeKeyAndOrderFront(nil)
69+
70+
let searchField = NSSearchField()
71+
window.contentView?.addSubview(searchField)
72+
73+
let expectation = XCTestExpectation()
74+
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
75+
expectation.fulfill()
76+
}
77+
78+
wait(for: [expectation])
79+
XCTAssertNotNil(searchField.modernBezelView)
80+
}
81+
6682
func testRetrievingPopover() {
6783
class ContentViewController: NSViewController {
6884
override func loadView() {

0 commit comments

Comments
 (0)