1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Search/SearchTextField.swift

83 lines
2.6 KiB
Swift
Raw Normal View History

import Repeat
2021-11-28 20:07:55 +05:30
import SwiftUI
struct SearchTextField: View {
private var navigation = NavigationModel.shared
@ObservedObject private var state = SearchModel.shared
2021-11-28 20:07:55 +05:30
var body: some View {
ZStack {
#if os(macOS)
fieldBorder
#endif
HStack(spacing: 0) {
#if os(macOS)
Image(systemName: "magnifyingglass")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 12, height: 12)
.padding(.horizontal, 8)
.opacity(0.8)
#endif
2022-08-17 21:04:25 +05:30
TextField("Search...", text: $state.queryText) {
2022-03-26 19:52:29 +05:30
state.changeQuery { query in
query.query = state.queryText
navigation.hideKeyboard()
}
RecentsModel.shared.addQuery(state.queryText)
2021-11-28 20:07:55 +05:30
}
.disableAutocorrection(true)
2021-11-28 20:07:55 +05:30
#if os(macOS)
2022-08-17 21:04:25 +05:30
.frame(maxWidth: 190)
.textFieldStyle(.plain)
2021-11-28 20:07:55 +05:30
#else
2022-12-16 04:43:36 +05:30
.frame(minWidth: 200)
2022-08-17 21:04:25 +05:30
.textFieldStyle(.roundedBorder)
2022-12-11 18:58:16 +05:30
.padding(.horizontal, 5)
.padding(.trailing, state.queryText.isEmpty ? 0 : 10)
2021-11-28 20:07:55 +05:30
#endif
2021-12-05 01:05:41 +05:30
2022-08-17 21:04:25 +05:30
if !state.queryText.isEmpty {
2021-11-28 20:07:55 +05:30
clearButton
2021-12-06 23:43:20 +05:30
} else {
#if os(macOS)
clearButton
.opacity(0)
#endif
2021-11-28 20:07:55 +05:30
}
}
}
2022-12-16 04:43:36 +05:30
.transaction { t in t.animation = nil }
2021-11-28 20:07:55 +05:30
}
private var fieldBorder: some View {
RoundedRectangle(cornerRadius: 5, style: .continuous)
.fill(Color.background)
.frame(width: 250, height: 32)
.overlay(
RoundedRectangle(cornerRadius: 5, style: .continuous)
.stroke(Color.gray.opacity(0.4), lineWidth: 1)
2021-11-28 20:07:55 +05:30
.frame(width: 250, height: 31)
)
}
private var clearButton: some View {
Button(action: {
self.state.queryText = ""
}) {
Image(systemName: "xmark.circle.fill")
#if os(macOS)
2022-12-10 06:49:36 +05:30
.imageScale(.small)
2021-11-28 20:07:55 +05:30
#else
2022-12-10 06:49:36 +05:30
.imageScale(.medium)
2021-11-28 20:07:55 +05:30
#endif
}
.buttonStyle(PlainButtonStyle())
2022-12-10 06:49:36 +05:30
#if os(macOS)
.padding(.trailing, 10)
#endif
.opacity(0.7)
2021-11-28 20:07:55 +05:30
}
}