2023-05-27 02:48:55 +05:30
|
|
|
import Repeat
|
|
|
|
import SwiftUI
|
2023-09-26 21:01:44 +05:30
|
|
|
import SwiftUIIntrospect
|
2023-05-27 02:48:55 +05:30
|
|
|
|
2023-10-15 17:05:23 +05:30
|
|
|
@available(iOS 15.0, macOS 12, *)
|
2023-05-27 02:48:55 +05:30
|
|
|
struct FocusableSearchTextField: View {
|
|
|
|
@ObservedObject private var state = SearchModel.shared
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
SearchTextField()
|
2023-09-26 21:01:44 +05:30
|
|
|
#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-27 02:48:55 +05:30
|
|
|
}
|
|
|
|
.onChange(of: state.focused) { newValue in
|
2023-09-26 21:01:44 +05:30
|
|
|
if newValue, let textField = state.textField, !textField.isFirstResponder {
|
2023-05-27 02:48:55 +05:30
|
|
|
textField.becomeFirstResponder()
|
|
|
|
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|