mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
@@ -1,24 +1,27 @@
|
||||
import Introspect
|
||||
import Repeat
|
||||
import SwiftUI
|
||||
import SwiftUIIntrospect
|
||||
|
||||
struct FocusableSearchTextField: View {
|
||||
@ObservedObject private var state = SearchModel.shared
|
||||
|
||||
#if os(iOS)
|
||||
@State private var textField: UITextField?
|
||||
#elseif os(macOS)
|
||||
@State private var textField: NSTextField?
|
||||
#endif
|
||||
|
||||
var body: some View {
|
||||
SearchTextField()
|
||||
#if os(iOS)
|
||||
.introspectTextField { field in
|
||||
textField = field
|
||||
#if os(macOS)
|
||||
.introspect(.textField, on: .macOS(.v12, .v13, .v14)) { textField in
|
||||
state.textField = textField
|
||||
}
|
||||
.onAppear {
|
||||
DispatchQueue.main.async {
|
||||
state.textField?.becomeFirstResponder()
|
||||
}
|
||||
}
|
||||
#elseif os(iOS)
|
||||
.introspect(.textField, on: .iOS(.v15, .v16, .v17)) { textField in
|
||||
state.textField = textField
|
||||
}
|
||||
.onChange(of: state.focused) { newValue in
|
||||
if newValue, let textField, !textField.isFirstResponder {
|
||||
if newValue, let textField = state.textField, !textField.isFirstResponder {
|
||||
textField.becomeFirstResponder()
|
||||
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
|
||||
}
|
||||
|
Reference in New Issue
Block a user