2021-11-28 20:07:55 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SearchTextField: View {
|
2022-11-25 02:06:05 +05:30
|
|
|
private var navigation = NavigationModel.shared
|
|
|
|
@ObservedObject private var state = SearchModel.shared
|
2021-11-28 20:07:55 +05:30
|
|
|
|
2024-09-09 19:35:24 +05:30
|
|
|
#if os(macOS)
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
2021-11-28 20:07:55 +05:30
|
|
|
fieldBorder
|
|
|
|
|
2024-09-09 19:35:24 +05:30
|
|
|
HStack(spacing: 0) {
|
2021-11-28 20:07:55 +05:30
|
|
|
Image(systemName: "magnifyingglass")
|
|
|
|
.resizable()
|
2024-08-18 18:16:51 +05:30
|
|
|
.scaledToFill()
|
2021-11-28 20:07:55 +05:30
|
|
|
.frame(width: 12, height: 12)
|
2024-09-09 19:35:24 +05:30
|
|
|
.padding(.horizontal, 6)
|
2021-11-28 20:07:55 +05:30
|
|
|
.opacity(0.8)
|
2024-09-09 19:35:24 +05:30
|
|
|
|
|
|
|
GeometryReader { geometry in
|
|
|
|
TextField("Search...", text: $state.queryText) {
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.query = state.queryText
|
|
|
|
navigation.hideKeyboard()
|
|
|
|
}
|
|
|
|
RecentsModel.shared.addQuery(state.queryText)
|
|
|
|
}
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.frame(maxWidth: geometry.size.width - 5)
|
|
|
|
.textFieldStyle(.plain)
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
.frame(height: 27, alignment: .center)
|
2022-03-26 19:52:29 +05:30
|
|
|
}
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2024-09-09 19:35:24 +05:30
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
clearButton
|
|
|
|
} else {
|
2021-12-06 23:43:20 +05:30
|
|
|
clearButton
|
|
|
|
.opacity(0)
|
2024-09-09 19:35:24 +05:30
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
}
|
2024-09-09 19:35:24 +05:30
|
|
|
.transaction { t in t.animation = nil }
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
2024-09-09 19:35:24 +05:30
|
|
|
#else
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
|
|
|
HStack {
|
|
|
|
HStack(spacing: 0) {
|
|
|
|
Image(systemName: "magnifyingglass")
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
.padding(.leading, 5)
|
|
|
|
.padding(.trailing, 5)
|
|
|
|
.imageScale(.medium)
|
|
|
|
|
|
|
|
TextField("Search...", text: $state.queryText) {
|
|
|
|
state.changeQuery { query in
|
|
|
|
query.query = state.queryText
|
|
|
|
navigation.hideKeyboard()
|
|
|
|
}
|
|
|
|
RecentsModel.shared.addQuery(state.queryText)
|
|
|
|
}
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.textFieldStyle(.plain)
|
|
|
|
.padding(.vertical, 7)
|
|
|
|
|
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
clearButton
|
|
|
|
.padding(.leading, 5)
|
|
|
|
.padding(.trailing, 5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.background(
|
|
|
|
RoundedRectangle(cornerRadius: 8)
|
|
|
|
.fill(Color("SearchTextFieldBackground"))
|
|
|
|
)
|
2024-09-10 22:31:41 +05:30
|
|
|
.overlay(
|
|
|
|
RoundedRectangle(cornerRadius: 8)
|
2024-09-11 12:25:18 +05:30
|
|
|
.stroke(Color(UIColor.secondarySystemBackground), lineWidth: 1)
|
2024-09-10 22:31:41 +05:30
|
|
|
)
|
2024-09-09 19:35:24 +05:30
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
.padding(.horizontal, 0)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = nil }
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-28 20:07:55 +05:30
|
|
|
|
|
|
|
private var fieldBorder: some View {
|
|
|
|
RoundedRectangle(cornerRadius: 5, style: .continuous)
|
|
|
|
.fill(Color.background)
|
2024-09-09 19:35:24 +05:30
|
|
|
.frame(width: 250, height: 27)
|
2021-11-28 20:07:55 +05:30
|
|
|
.overlay(
|
|
|
|
RoundedRectangle(cornerRadius: 5, style: .continuous)
|
2022-08-05 04:00:09 +05:30
|
|
|
.stroke(Color.gray.opacity(0.4), lineWidth: 1)
|
2024-09-09 19:35:24 +05:30
|
|
|
.frame(width: 250, height: 27)
|
2021-11-28 20:07:55 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var clearButton: some View {
|
|
|
|
Button(action: {
|
|
|
|
self.state.queryText = ""
|
|
|
|
}) {
|
|
|
|
Image(systemName: "xmark.circle.fill")
|
2022-12-10 06:49:36 +05:30
|
|
|
.imageScale(.medium)
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
.buttonStyle(PlainButtonStyle())
|
2022-12-10 06:49:36 +05:30
|
|
|
#if os(macOS)
|
2024-09-09 19:35:24 +05:30
|
|
|
.padding(.trailing, 5)
|
|
|
|
#elseif os(iOS)
|
|
|
|
.padding(.trailing, 5)
|
|
|
|
.foregroundColor(.gray)
|
2022-12-10 06:49:36 +05:30
|
|
|
#endif
|
|
|
|
.opacity(0.7)
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
}
|