DSFSearchField

main

A simple NSSearchField with a localizable, managed recent searches menu.
dagronf/DSFSearchField

DSFSearchField

A simple NSSearchField with a localizable, managed recent searches menu and SwiftUI support.

Why

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.

Installation

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)

Swift Package Manager

Add https://github.com/dagronf/DSFSearchField to your project.

Direct

Copy DSFSearchField.swift (and DSFSearchField+SwiftUI.swift) if you want SwiftUI support) to your project.

Usage

Via Interface Builder

  • Add a new NSSearchField using Interface Builder, then change the class type to DSFSearchField.
  • 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.

Programatically

let searchField = DSFSearchField(frame: rect, recentsAutosaveName: "primary-search")

Conveniences

Search text change callback

This is called whenever the text changes within the search field.

   searchField.searchTermChangeCallback = { [weak self] newSearchTerm in
      // Do something with 'newSearchTerm'
      ...
   }

Submit callback

This is called when the user presses 'return' in the search field.

   searchField.searchSubmitCallback = { [weak self] newSearchTerm in
      // Do something with 'newSearchTerm'
      ...
   }

Simple searchTerm interface

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)

SwiftUI

DSFSearchField provides a basic SwiftUI interface to the search control.

DSFSearchField.SwiftUI(
   text: $searchText, 
   placeholderText: "Search for colors…",
   autosaveName: "SystemColorsSearchField"
)

Available view modifiers

.bezelStyle

The style of bezel for the search bar.

.controlSize

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.

onUpdateSearchText

Provide a block that gets called when the search text changes.

DSFSearchField.SwiftUI(...)
   .onUpdateSearchText { newValue in
      Swift.print("Update with new value -> \(newValue)")
   }

onSubmitSearchText

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)")
   }

Example

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)")
   }
}

License

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.

Description

  • Swift Tools 5.4.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 23:56:08 GMT-0900 (Hawaii-Aleutian Daylight Time)