@@ -29,9 +29,9 @@ public struct SearchBar: DefaultTextInputType {
29
29
30
30
private var placeholder : String ?
31
31
32
+ private var appKitOrUIKitFont : AppKitOrUIKitFont ?
33
+ private var appKitOrUIKitForegroundColor : AppKitOrUIKitColor ?
32
34
#if os(iOS) || targetEnvironment(macCatalyst)
33
- private var appKitOrUIKitFont : UIFont ?
34
- private var appKitOrUIKitForegroundColor : UIColor ?
35
35
private var appKitOrUIKitSearchFieldBackgroundColor : UIColor ?
36
36
private var searchBarStyle : UISearchBar . Style = . minimal
37
37
private var iconImageConfiguration : [ UISearchBar . Icon : AppKitOrUIKitImage ] = [ : ]
@@ -231,10 +231,41 @@ extension SearchBar: UIViewRepresentable {
231
231
@available ( iOSApplicationExtension, unavailable)
232
232
@available ( tvOSApplicationExtension, unavailable)
233
233
extension SearchBar : NSViewRepresentable {
234
- public typealias NSViewType = NSSearchField
235
-
234
+ public final class NSViewType : NSSearchField {
235
+ class ClosureResponder : NSView {
236
+ override func becomeFirstResponder( ) -> Bool {
237
+ print ( " Foo " )
238
+
239
+ return true
240
+ }
241
+
242
+ override func resignFirstResponder( ) -> Bool {
243
+ return true
244
+ }
245
+ }
246
+
247
+ var isFirstResponderBinding : Binding < Bool > ?
248
+ var closureResponder = ClosureResponder ( )
249
+
250
+ override public func becomeFirstResponder( ) -> Bool {
251
+ let result = super. becomeFirstResponder ( )
252
+
253
+ isFirstResponderBinding? . wrappedValue = result
254
+
255
+ return result
256
+ }
257
+
258
+ override public func resignFirstResponder( ) -> Bool {
259
+ let result = super. resignFirstResponder ( )
260
+
261
+ isFirstResponderBinding? . wrappedValue = !result
262
+
263
+ return result
264
+ }
265
+ }
266
+
236
267
public func makeNSView( context: Context ) -> NSViewType {
237
- let nsView = NSSearchField ( string: placeholder ?? " " )
268
+ let nsView = NSViewType ( string: placeholder ?? " " )
238
269
239
270
nsView. delegate = context. coordinator
240
271
nsView. target = context. coordinator
@@ -245,11 +276,21 @@ extension SearchBar: NSViewRepresentable {
245
276
nsView. isBordered = false
246
277
nsView. isBezeled = true
247
278
279
+ nsView. nextResponder = nsView. closureResponder
280
+ nsView. nextKeyView = nsView. closureResponder
281
+
248
282
return nsView
249
283
}
250
284
251
- public func updateNSView( _ nsView: NSSearchField , context: Context ) {
285
+ public func updateNSView( _ nsView: NSViewType , context: Context ) {
252
286
context. coordinator. base = self
287
+ context. coordinator. view = nsView
288
+
289
+ nsView. isFirstResponderBinding = isFocused
290
+
291
+ if let appKitOrUIKitFont = appKitOrUIKitFont {
292
+ nsView. font = appKitOrUIKitFont
293
+ }
253
294
254
295
if nsView. stringValue != text {
255
296
nsView. stringValue = text
@@ -259,6 +300,8 @@ extension SearchBar: NSViewRepresentable {
259
300
final public class Coordinator : NSObject , NSSearchFieldDelegate {
260
301
var base : SearchBar
261
302
303
+ weak var view : NSViewType ?
304
+
262
305
init ( base: SearchBar ) {
263
306
self . base = base
264
307
}
@@ -277,6 +320,8 @@ extension SearchBar: NSViewRepresentable {
277
320
278
321
public func controlTextDidEndEditing( _ notification: Notification ) {
279
322
base. onEditingChanged ( false )
323
+
324
+ // _ = view?.resignFirstResponder()
280
325
}
281
326
282
327
@objc
@@ -338,15 +383,15 @@ extension SearchBar {
338
383
}
339
384
#endif
340
385
341
- #if os(iOS) || targetEnvironment(macCatalyst)
342
- public func font( _ font: UIFont ) -> Self {
386
+ public func font( _ font: AppKitOrUIKitFont ) -> Self {
343
387
then ( { $0. appKitOrUIKitFont = font } )
344
388
}
345
389
346
390
public func foregroundColor( _ foregroundColor: AppKitOrUIKitColor ) -> Self {
347
391
then ( { $0. appKitOrUIKitForegroundColor = foregroundColor } )
348
392
}
349
393
394
+ #if os(iOS) || targetEnvironment(macCatalyst)
350
395
@_disfavoredOverload
351
396
public func foregroundColor( _ foregroundColor: Color ) -> Self {
352
397
then ( { $0. appKitOrUIKitForegroundColor = foregroundColor. toUIColor ( ) } )
0 commit comments