@@ -13,11 +13,7 @@ struct FindModePicker: NSViewRepresentable {
1313 @Environment ( \. controlActiveState) var activeState
1414 let onToggleWrapAround : ( ) -> Void
1515
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 {
2117 let button = NSButton ( frame: . zero)
2218 button. bezelStyle = . regularSquare
2319 button. isBordered = false
@@ -26,29 +22,33 @@ struct FindModePicker: NSViewRepresentable {
2622 . withSymbolConfiguration ( . init( pointSize: 12 , weight: . regular) )
2723 button. imagePosition = . imageOnly
2824 button. target = context. coordinator
25+ button. action = nil
26+ button. sendAction ( on: . leftMouseDown)
27+ button. target = context. coordinator
2928 button. action = #selector( Coordinator . openMenu ( _: ) )
29+ return button
30+ }
3031
31- // Create the popup button
32+ private func createPopupButton ( context : Context ) -> NSPopUpButton {
3233 let popup = NSPopUpButton ( frame: . zero, pullsDown: false )
3334 popup. bezelStyle = . regularSquare
3435 popup. isBordered = false
3536 popup. controlSize = . small
3637 popup. font = . systemFont( ofSize: NSFont . systemFontSize ( for: . small) )
3738 popup. autoenablesItems = false
39+ return popup
40+ }
3841
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 {
4743 let menu = NSMenu ( )
4844
4945 // Add mode items
5046 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+ )
5252 item. target = context. coordinator
5353 item. tag = mode == . find ? 0 : 1
5454 menu. addItem ( item)
@@ -58,19 +58,19 @@ struct FindModePicker: NSViewRepresentable {
5858 menu. addItem ( . separator( ) )
5959
6060 // 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+ )
6266 wrapItem. target = context. coordinator
6367 wrapItem. state = wrapAround ? . on : . off
6468 menu. addItem ( wrapItem)
6569
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+ }
7272
73- // Set up constraints
73+ private func setupConstraints ( container : NSView , button : NSButton , popup : NSPopUpButton , totalWidth : CGFloat ) {
7474 button. translatesAutoresizingMaskIntoConstraints = false
7575 popup. translatesAutoresizingMaskIntoConstraints = false
7676
@@ -86,6 +86,30 @@ struct FindModePicker: NSViewRepresentable {
8686 popup. bottomAnchor. constraint ( equalTo: container. bottomAnchor) ,
8787 popup. widthAnchor. constraint ( equalToConstant: totalWidth)
8888 ] )
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)
89113
90114 return container
91115 }
0 commit comments