Skip to content

Commit ffce84c

Browse files
committed
Update package
1 parent 05f1839 commit ffce84c

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

Sources/Intermodular/Extensions/SwiftUI/Binding++.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
import Swift
66
import SwiftUI
77

8+
extension Binding {
9+
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
10+
public init(_from binding: FocusState<Value>.Binding) where Value: Hashable {
11+
self.init(get: { binding.wrappedValue }, set: { binding.wrappedValue = $0 })
12+
}
13+
}
14+
815
extension Binding {
916
@inlinable
1017
public func map<T>(_ keyPath: WritableKeyPath<Value, T>) -> Binding<T> {

Sources/Intramodular/Search/SearchBar.swift

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public struct SearchBar: DefaultTextInputType {
2929

3030
private var placeholder: String?
3131

32+
private var appKitOrUIKitFont: AppKitOrUIKitFont?
33+
private var appKitOrUIKitForegroundColor: AppKitOrUIKitColor?
3234
#if os(iOS) || targetEnvironment(macCatalyst)
33-
private var appKitOrUIKitFont: UIFont?
34-
private var appKitOrUIKitForegroundColor: UIColor?
3535
private var appKitOrUIKitSearchFieldBackgroundColor: UIColor?
3636
private var searchBarStyle: UISearchBar.Style = .minimal
3737
private var iconImageConfiguration: [UISearchBar.Icon: AppKitOrUIKitImage] = [:]
@@ -231,10 +231,41 @@ extension SearchBar: UIViewRepresentable {
231231
@available(iOSApplicationExtension, unavailable)
232232
@available(tvOSApplicationExtension, unavailable)
233233
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+
236267
public func makeNSView(context: Context) -> NSViewType {
237-
let nsView = NSSearchField(string: placeholder ?? "")
268+
let nsView = NSViewType(string: placeholder ?? "")
238269

239270
nsView.delegate = context.coordinator
240271
nsView.target = context.coordinator
@@ -245,11 +276,21 @@ extension SearchBar: NSViewRepresentable {
245276
nsView.isBordered = false
246277
nsView.isBezeled = true
247278

279+
nsView.nextResponder = nsView.closureResponder
280+
nsView.nextKeyView = nsView.closureResponder
281+
248282
return nsView
249283
}
250284

251-
public func updateNSView(_ nsView: NSSearchField, context: Context) {
285+
public func updateNSView(_ nsView: NSViewType, context: Context) {
252286
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+
}
253294

254295
if nsView.stringValue != text {
255296
nsView.stringValue = text
@@ -259,6 +300,8 @@ extension SearchBar: NSViewRepresentable {
259300
final public class Coordinator: NSObject, NSSearchFieldDelegate {
260301
var base: SearchBar
261302

303+
weak var view: NSViewType?
304+
262305
init(base: SearchBar) {
263306
self.base = base
264307
}
@@ -277,6 +320,8 @@ extension SearchBar: NSViewRepresentable {
277320

278321
public func controlTextDidEndEditing(_ notification: Notification) {
279322
base.onEditingChanged(false)
323+
324+
// _ = view?.resignFirstResponder()
280325
}
281326

282327
@objc
@@ -338,15 +383,15 @@ extension SearchBar {
338383
}
339384
#endif
340385

341-
#if os(iOS) || targetEnvironment(macCatalyst)
342-
public func font(_ font: UIFont) -> Self {
386+
public func font(_ font: AppKitOrUIKitFont) -> Self {
343387
then({ $0.appKitOrUIKitFont = font })
344388
}
345389

346390
public func foregroundColor(_ foregroundColor: AppKitOrUIKitColor) -> Self {
347391
then({ $0.appKitOrUIKitForegroundColor = foregroundColor })
348392
}
349393

394+
#if os(iOS) || targetEnvironment(macCatalyst)
350395
@_disfavoredOverload
351396
public func foregroundColor(_ foregroundColor: Color) -> Self {
352397
then({ $0.appKitOrUIKitForegroundColor = foregroundColor.toUIColor() })

Sources/Intramodular/Status Bar/StatusItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension View {
6666
let content = content()
6767

6868
return withInlineState(initialValue: UUID()) { id in
69-
statusItem(id: id.wrappedValue, image: image, content: { content })
69+
statusItem(id: id.wrappedValue, image: image, isActive: isActive, content: { content })
7070
}
7171
}
7272

0 commit comments

Comments
 (0)