Skip to content

Commit d659baa

Browse files
committed
Update package
1 parent 346138c commit d659baa

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

Sources/Intramodular/Search/SearchBar.swift

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public struct SearchBar: DefaultTextInputType {
4747
private var textContentType: UITextContentType? = nil
4848
private var keyboardType: UIKeyboardType?
4949
#endif
50-
50+
51+
#if os(macOS)
52+
private var isBezeled: Bool = true
53+
private var focusRingType: NSFocusRingType = .default
54+
#endif
55+
5156
public init<S: StringProtocol>(
5257
_ title: S,
5358
text: Binding<String>,
@@ -253,16 +258,13 @@ extension SearchBar: NSViewRepresentable {
253258

254259
public func makeNSView(context: Context) -> NSViewType {
255260
let nsView = NSViewType(string: placeholder ?? "")
256-
261+
257262
nsView.delegate = context.coordinator
258263
nsView.target = context.coordinator
259264
nsView.action = #selector(context.coordinator.performAction(_:))
260-
261-
nsView.bezelStyle = .roundedBezel
265+
262266
nsView.cell?.sendsActionOnEndEditing = false
263-
nsView.isBordered = false
264-
nsView.isBezeled = true
265-
267+
266268
return nsView
267269
}
268270

@@ -271,14 +273,20 @@ extension SearchBar: NSViewRepresentable {
271273
context.coordinator.view = nsView
272274

273275
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+
275285
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)
281287
}
288+
289+
assignIfNotEqual(text, to: &nsView.stringValue)
282290
}
283291

284292
final public class Coordinator: NSObject, NSSearchFieldDelegate {
@@ -304,8 +312,6 @@ extension SearchBar: NSViewRepresentable {
304312

305313
public func controlTextDidEndEditing(_ notification: Notification) {
306314
base.onEditingChanged(false)
307-
308-
// _ = view?.resignFirstResponder()
309315
}
310316

311317
@objc
@@ -362,15 +368,21 @@ extension SearchBar {
362368
@available(tvOSApplicationExtension, unavailable)
363369
extension SearchBar {
364370
#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
365-
public func placeholder(_ placeholder: String) -> Self {
371+
public func placeholder(_ placeholder: String?) -> Self {
366372
then({ $0.placeholder = placeholder })
367373
}
368374
#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 {
371378
then({ $0.appKitOrUIKitFont = font })
372379
}
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+
374386
public func foregroundColor(_ foregroundColor: AppKitOrUIKitColor) -> Self {
375387
then({ $0.appKitOrUIKitForegroundColor = foregroundColor })
376388
}
@@ -426,6 +438,16 @@ extension SearchBar {
426438
then({ $0.keyboardType = keyboardType })
427439
}
428440
#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
429451
}
430452

431453
// MARK: - Auxiliary Implementation -

Sources/Intramodular/Typography/FontFamily.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,12 @@ fileprivate struct SetFontWithLineHeight<F: FontFamily>: ViewModifier {
123123
}
124124
}
125125
}
126+
#endif
126127

128+
#if os(macOS)
129+
extension NSFont {
130+
public convenience init?<F: FontFamily>(_ family: F, size: CGFloat) {
131+
self.init(name: family.rawValue, size: size)!
132+
}
133+
}
127134
#endif

0 commit comments

Comments
 (0)