@@ -47,7 +47,12 @@ public struct SearchBar: DefaultTextInputType {
47
47
private var textContentType : UITextContentType ? = nil
48
48
private var keyboardType : UIKeyboardType ?
49
49
#endif
50
-
50
+
51
+ #if os(macOS)
52
+ private var isBezeled : Bool = true
53
+ private var focusRingType : NSFocusRingType = . default
54
+ #endif
55
+
51
56
public init < S: StringProtocol > (
52
57
_ title: S ,
53
58
text: Binding < String > ,
@@ -253,16 +258,13 @@ extension SearchBar: NSViewRepresentable {
253
258
254
259
public func makeNSView( context: Context ) -> NSViewType {
255
260
let nsView = NSViewType ( string: placeholder ?? " " )
256
-
261
+
257
262
nsView. delegate = context. coordinator
258
263
nsView. target = context. coordinator
259
264
nsView. action = #selector( context. coordinator. performAction ( _: ) )
260
-
261
- nsView. bezelStyle = . roundedBezel
265
+
262
266
nsView. cell? . sendsActionOnEndEditing = false
263
- nsView. isBordered = false
264
- nsView. isBezeled = true
265
-
267
+
266
268
return nsView
267
269
}
268
270
@@ -271,14 +273,20 @@ extension SearchBar: NSViewRepresentable {
271
273
context. coordinator. view = nsView
272
274
273
275
nsView. isFirstResponderBinding = isFocused
274
-
276
+
277
+ assignIfNotEqual ( . roundedBezel, to: & nsView. bezelStyle)
278
+ assignIfNotEqual ( focusRingType, to: & nsView. focusRingType)
279
+ assignIfNotEqual ( false , to: & nsView. isBordered)
280
+ assignIfNotEqual ( isBezeled, to: & nsView. isBezeled)
281
+ assignIfNotEqual ( placeholder, to: & nsView. placeholderString)
282
+
283
+ ( nsView. cell as? NSSearchFieldCell ) ? . searchButtonCell? . isTransparent = !isBezeled
284
+
275
285
if let appKitOrUIKitFont = appKitOrUIKitFont {
276
- nsView. font = appKitOrUIKitFont
277
- }
278
-
279
- if nsView. stringValue != text {
280
- nsView. stringValue = text
286
+ assignIfNotEqual ( appKitOrUIKitFont, to: & nsView. font)
281
287
}
288
+
289
+ assignIfNotEqual ( text, to: & nsView. stringValue)
282
290
}
283
291
284
292
final public class Coordinator : NSObject , NSSearchFieldDelegate {
@@ -304,8 +312,6 @@ extension SearchBar: NSViewRepresentable {
304
312
305
313
public func controlTextDidEndEditing( _ notification: Notification ) {
306
314
base. onEditingChanged ( false )
307
-
308
- // _ = view?.resignFirstResponder()
309
315
}
310
316
311
317
@objc
@@ -362,15 +368,21 @@ extension SearchBar {
362
368
@available ( tvOSApplicationExtension, unavailable)
363
369
extension SearchBar {
364
370
#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
365
- public func placeholder( _ placeholder: String ) -> Self {
371
+ public func placeholder( _ placeholder: String ? ) -> Self {
366
372
then ( { $0. placeholder = placeholder } )
367
373
}
368
374
#endif
369
-
370
- public func font( _ font: AppKitOrUIKitFont ) -> Self {
375
+
376
+ /// Sets the default font for text in the view.
377
+ public func font( _ font: AppKitOrUIKitFont ? ) -> Self {
371
378
then ( { $0. appKitOrUIKitFont = font } )
372
379
}
373
-
380
+
381
+ /// Sets the default font for text in the view.
382
+ public func font< F: FontFamily > ( _ font: F , size: CGFloat ) -> Self {
383
+ self . font ( AppKitOrUIKitFont ( name: font. rawValue, size: size) )
384
+ }
385
+
374
386
public func foregroundColor( _ foregroundColor: AppKitOrUIKitColor ) -> Self {
375
387
then ( { $0. appKitOrUIKitForegroundColor = foregroundColor } )
376
388
}
@@ -426,6 +438,16 @@ extension SearchBar {
426
438
then ( { $0. keyboardType = keyboardType } )
427
439
}
428
440
#endif
441
+
442
+ #if os(macOS)
443
+ public func focusRingType( _ focusRingType: NSFocusRingType ) -> Self {
444
+ then ( { $0. focusRingType = focusRingType } )
445
+ }
446
+
447
+ public func isBezeled( _ isBezeled: Bool ) -> Self {
448
+ then ( { $0. isBezeled = isBezeled } )
449
+ }
450
+ #endif
429
451
}
430
452
431
453
// MARK: - Auxiliary Implementation -
0 commit comments