-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lee Jun Kit
committed
Jun 19, 2021
1 parent
c3f7bd3
commit 14cd2c7
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// SearchField.swift | ||
// Spottie | ||
// | ||
// Created by Lee Jun Kit on 19/6/21. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// https://stackoverflow.com/questions/64376948/swiftui-how-to-use-nssearchtoolbaritem-on-macos-11 | ||
struct SearchField: NSViewRepresentable { | ||
class Coordinator: NSObject, NSSearchFieldDelegate { | ||
var parent: SearchField | ||
|
||
init(_ parent: SearchField) { | ||
self.parent = parent | ||
} | ||
|
||
func controlTextDidChange(_ notification: Notification) { | ||
guard let searchField = notification.object as? NSSearchField else { | ||
print("Unexpected control in update notification") | ||
return | ||
} | ||
self.parent.search = searchField.stringValue | ||
} | ||
|
||
} | ||
|
||
@Binding var search: String | ||
|
||
func makeNSView(context: Context) -> NSSearchField { | ||
NSSearchField(frame: .zero) | ||
} | ||
|
||
func updateNSView(_ searchField: NSSearchField, context: Context) { | ||
searchField.stringValue = search | ||
searchField.delegate = context.coordinator | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
return Coordinator(self) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters