]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/SearchBar.swift
7e921b08123764d66370c90ef260dd2c010807bf
[rbdr/map] / Map / Presentation / Base Components / SearchBar.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17 import SwiftUI
18
19 struct SearchBar: View {
20
21 @Binding var term: String
22 @FocusState var isSearchFocused: Bool
23 var onNext: () -> Void = {}
24 var onPrevious: () -> Void = {}
25 var onSubmit: () -> Void = {}
26 var onDismiss: () -> Void = {}
27
28 var body: some View {
29 HStack(spacing: 2.0) {
30 ZStack {
31 TextField("Search", text: $term)
32 .textFieldStyle(.roundedBorder)
33 .padding(.trailing, 16.0)
34 .focused($isSearchFocused)
35 .onAppear {
36 isSearchFocused = true
37 }
38 .onKeyPress { press in
39 if press.key == .return {
40 if press.modifiers.contains(.shift) {
41 onPrevious()
42 return .handled
43 }
44 onNext()
45 return .handled
46 }
47 return .ignored
48 }
49 }
50 Spacer()
51 Button(action: onPrevious) {
52 Image(systemName: "chevron.left")
53 .font(.theme.smallControl)
54 }.keyboardShortcut(
55 "g", modifiers: EventModifiers([.command, .shift])
56 ).help("Find Previous (⇧⌘G)")
57 Button(action: onNext) {
58 Image(systemName: "chevron.right")
59 .font(.theme.smallControl)
60 }.keyboardShortcut(
61 "g", modifiers: EventModifiers([.command])
62 ).help("Find Next (⌘G)")
63 Button(action: onDismiss) {
64 Text("Done")
65 .font(.theme.smallControl)
66 }.keyboardShortcut(.escape, modifiers: EventModifiers())
67 .help("Done (⎋)")
68 }.padding(4.0)
69 }
70 }
71
72 #Preview {
73 SearchBar(term: .constant("Hello"))
74 }