Add keyboard shortcut and toggle

This commit is contained in:
Balazs Perlaki-Horvath 2024-07-07 01:53:33 +02:00 committed by Kelson
parent 35c8b8c956
commit 98a3573640

View File

@ -20,24 +20,61 @@ struct ContentSearchBar: View {
@Binding private var searchText: String
@State private var isActivated: Bool = false
@FocusState private var focusedState: Bool
init(text: Binding<String>) {
_searchText = text
}
var body: some View {
searchBar
if isActivated {
field
.focused($focusedState)
} else {
button
.keyboardShortcut("f")
}
}
private var searchBar: some View {
private var button: some View {
Button {
isActivated = true
focusedState = true
} label: {
Image(systemName: "magnifyingglass")
.font(.system(size: 18))
}
.buttonStyle(PlainButtonStyle())
.padding(32)
}
private var field: some View {
HStack {
Image(systemName: "magnifyingglass").foregroundColor(.primary)
searchImage
TextField("common.search".localized, text: $searchText)
.textFieldStyle(.roundedBorder)
closeButton
}
.padding(8)
.background(Color.background)
.cornerRadius(16)
.padding(16)
.cornerRadius(16)
.frame(maxWidth: 320)
}
private var searchImage: some View {
Image(systemName: "magnifyingglass")
.foregroundColor(.primary)
}
private var closeButton: some View {
Button {
searchText = ""
isActivated = false
} label: {
Image(systemName: "xmark.circle.fill").foregroundColor(.primary)
}
}
}