A simple NSSearchField with a localizable, managed recent searches menu and SwiftUI support.
NSSearchField
has a wonderful built-in mechanism for handling recents list which requires the developer to provide a menu to provide the recents list. This class automatically provides a suitable localizable menu structure.
DSFSearchField
inherits from NSSearchField
so search delegates, actions etc. all work as expected.
Note that currently to support localization you'll need to use the 'Direct' method below until Swift tool version 5.3 becomes more pervasive (it supports embedding localizations within modules)
Add https://github.com/dagronf/DSFSearchField
to your project.
Copy DSFSearchField.swift
(and DSFSearchField+SwiftUI.swift
) if you want SwiftUI support) to your project.
- Add a new
NSSearchField
using Interface Builder, then change the class type toDSFSearchField
. - If you want to save the recent searches list, set
Autosave
in the Attributes Inspector of Interface Builder to a unique string for your project.
let searchField = DSFSearchField(frame: rect, recentsAutosaveName: "primary-search")
This is called whenever the text changes within the search field.
searchField.searchTermChangeCallback = { [weak self] newSearchTerm in
// Do something with 'newSearchTerm'
...
}
This is called when the user presses 'return' in the search field.
searchField.searchSubmitCallback = { [weak self] newSearchTerm in
// Do something with 'newSearchTerm'
...
}
You can set the search text using stringValue
, but I personally find this scans badly when I'm trying to read and understand code. This class provides an additional property searchTerm
more descriptive property name. This property is bindable and settable.
Simple setting of the search term
searchField.searchTerm = "cats"
Bindable observation of the search term.
searchField.addObserver(
self,
forKeyPath: #keyPath(DSFSearchField.searchTerm),
options: [.new],
context: nil)
DSFSearchField
provides a basic SwiftUI interface to the search control.
DSFSearchField.SwiftUI(
text: $searchText,
placeholderText: "Search for colors…",
autosaveName: "SystemColorsSearchField"
)
The style of bezel for the search bar.
The size of the control
Note: Unfortunately, I could not figure out how to hook into SwiftUI's onSubmit
view modifier (and it appears that
the required Environment
values have not been made visible to the public. So that means that the default onSubmit
view modifier that SwiftUI provides will not be called.
Provide a block that gets called when the search text changes.
DSFSearchField.SwiftUI(...)
.onUpdateSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
Provide a block that gets called when the search text is submitted (the user presses the return key in the field)
DSFSearchField.SwiftUI(...)
.onSubmitSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
var body: some View {
DSFSearchField.SwiftUI(
text: $search1,
autosaveName: "Search1"
)
.onUpdateSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
.onSubmitSearchText { newValue in
Swift.print("Submit with new value -> \(newValue)")
}
}
MIT. Use it for anything you want, just attribute my work. Let me know if you do use it somewhere, I'd love to hear about it!
MIT License
Copyright (c) 2023 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.