@@ -13,11 +13,7 @@ struct FindModePicker: NSViewRepresentable {
13
13
@Environment ( \. controlActiveState) var activeState
14
14
let onToggleWrapAround : ( ) -> Void
15
15
16
- func makeNSView( context: Context ) -> NSView {
17
- let container = NSView ( )
18
- container. wantsLayer = true
19
-
20
- // Create the magnifying glass button
16
+ private func createSymbolButton( context: Context ) -> NSButton {
21
17
let button = NSButton ( frame: . zero)
22
18
button. bezelStyle = . regularSquare
23
19
button. isBordered = false
@@ -26,29 +22,33 @@ struct FindModePicker: NSViewRepresentable {
26
22
. withSymbolConfiguration ( . init( pointSize: 12 , weight: . regular) )
27
23
button. imagePosition = . imageOnly
28
24
button. target = context. coordinator
25
+ button. action = nil
26
+ button. sendAction ( on: . leftMouseDown)
27
+ button. target = context. coordinator
29
28
button. action = #selector( Coordinator . openMenu ( _: ) )
29
+ return button
30
+ }
30
31
31
- // Create the popup button
32
+ private func createPopupButton ( context : Context ) -> NSPopUpButton {
32
33
let popup = NSPopUpButton ( frame: . zero, pullsDown: false )
33
34
popup. bezelStyle = . regularSquare
34
35
popup. isBordered = false
35
36
popup. controlSize = . small
36
37
popup. font = . systemFont( ofSize: NSFont . systemFontSize ( for: . small) )
37
38
popup. autoenablesItems = false
39
+ return popup
40
+ }
38
41
39
- // Calculate the required width
40
- let font = NSFont . systemFont ( ofSize: NSFont . systemFontSize ( for: . small) )
41
- let maxWidth = FindPanelMode . allCases. map { mode in
42
- mode. displayName. size ( withAttributes: [ . font: font] ) . width
43
- } . max ( ) ?? 0
44
- let totalWidth = maxWidth + 28 // Add padding for the chevron and spacing
45
-
46
- // Create menu
42
+ private func createMenu( context: Context ) -> NSMenu {
47
43
let menu = NSMenu ( )
48
44
49
45
// Add mode items
50
46
FindPanelMode . allCases. forEach { mode in
51
- let item = NSMenuItem ( title: mode. displayName, action: #selector( Coordinator . modeSelected ( _: ) ) , keyEquivalent: " " )
47
+ let item = NSMenuItem (
48
+ title: mode. displayName,
49
+ action: #selector( Coordinator . modeSelected ( _: ) ) ,
50
+ keyEquivalent: " "
51
+ )
52
52
item. target = context. coordinator
53
53
item. tag = mode == . find ? 0 : 1
54
54
menu. addItem ( item)
@@ -58,19 +58,19 @@ struct FindModePicker: NSViewRepresentable {
58
58
menu. addItem ( . separator( ) )
59
59
60
60
// Add wrap around item
61
- let wrapItem = NSMenuItem ( title: " Wrap Around " , action: #selector( Coordinator . toggleWrapAround ( _: ) ) , keyEquivalent: " " )
61
+ let wrapItem = NSMenuItem (
62
+ title: " Wrap Around " ,
63
+ action: #selector( Coordinator . toggleWrapAround ( _: ) ) ,
64
+ keyEquivalent: " "
65
+ )
62
66
wrapItem. target = context. coordinator
63
67
wrapItem. state = wrapAround ? . on : . off
64
68
menu. addItem ( wrapItem)
65
69
66
- popup. menu = menu
67
- popup. selectItem ( at: mode == . find ? 0 : 1 )
68
-
69
- // Add subviews
70
- container. addSubview ( button)
71
- container. addSubview ( popup)
70
+ return menu
71
+ }
72
72
73
- // Set up constraints
73
+ private func setupConstraints ( container : NSView , button : NSButton , popup : NSPopUpButton , totalWidth : CGFloat ) {
74
74
button. translatesAutoresizingMaskIntoConstraints = false
75
75
popup. translatesAutoresizingMaskIntoConstraints = false
76
76
@@ -86,6 +86,30 @@ struct FindModePicker: NSViewRepresentable {
86
86
popup. bottomAnchor. constraint ( equalTo: container. bottomAnchor) ,
87
87
popup. widthAnchor. constraint ( equalToConstant: totalWidth)
88
88
] )
89
+ }
90
+
91
+ func makeNSView( context: Context ) -> NSView {
92
+ let container = NSView ( )
93
+ container. wantsLayer = true
94
+
95
+ let button = createSymbolButton ( context: context)
96
+ let popup = createPopupButton ( context: context)
97
+
98
+ // Calculate the required width
99
+ let font = NSFont . systemFont ( ofSize: NSFont . systemFontSize ( for: . small) )
100
+ let maxWidth = FindPanelMode . allCases. map { mode in
101
+ mode. displayName. size ( withAttributes: [ . font: font] ) . width
102
+ } . max ( ) ?? 0
103
+ let totalWidth = maxWidth + 28 // Add padding for the chevron and spacing
104
+
105
+ popup. menu = createMenu ( context: context)
106
+ popup. selectItem ( at: mode == . find ? 0 : 1 )
107
+
108
+ // Add subviews
109
+ container. addSubview ( button)
110
+ container. addSubview ( popup)
111
+
112
+ setupConstraints ( container: container, button: button, popup: popup, totalWidth: totalWidth)
89
113
90
114
return container
91
115
}
0 commit comments