2023-05-26 21:18:55 +00:00
|
|
|
import Repeat
|
|
|
|
import SwiftUI
|
2023-09-26 15:31:44 +00:00
|
|
|
import SwiftUIIntrospect
|
2023-05-26 21:18:55 +00:00
|
|
|
|
|
|
|
struct FocusableSearchTextField: View {
|
|
|
|
@ObservedObject private var state = SearchModel.shared
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
SearchTextField()
|
2023-09-26 15:31:44 +00:00
|
|
|
#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
|
2023-05-26 21:18:55 +00:00
|
|
|
}
|
|
|
|
.onChange(of: state.focused) { newValue in
|
2023-09-26 15:31:44 +00:00
|
|
|
if newValue, let textField = state.textField, !textField.isFirstResponder {
|
2023-05-26 21:18:55 +00:00
|
|
|
textField.becomeFirstResponder()
|
|
|
|
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|