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

83 lines
2.4 KiB
Swift
Raw Normal View History

2021-11-28 20:07:55 +05:30
import SwiftUI
struct SearchSuggestions: View {
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state
var body: some View {
List {
Button {
state.changeQuery { query in
query.query = state.queryText
state.fieldIsFocused = false
}
recents.addQuery(state.queryText)
} label: {
2021-12-06 23:42:33 +05:30
HStack {
Image(systemName: "magnifyingglass")
Text(state.queryText)
2021-11-28 20:07:55 +05:30
.lineLimit(1)
}
}
#if os(macOS)
.onHover(perform: onHover(_:))
#endif
ForEach(visibleSuggestions, id: \.self) { suggestion in
Button {
state.queryText = suggestion
} label: {
2021-12-06 23:42:33 +05:30
HStack {
Image(systemName: "arrow.up.left.circle")
2021-11-28 20:07:55 +05:30
.foregroundColor(.secondary)
2021-12-06 23:42:33 +05:30
HStack(spacing: 0) {
Text(state.suggestionsText)
.lineLimit(1)
.layoutPriority(2)
.foregroundColor(.secondary)
2021-11-28 20:07:55 +05:30
2021-12-06 23:42:33 +05:30
Text(querySuffix(suggestion))
.lineLimit(1)
.layoutPriority(1)
}
2021-11-28 20:07:55 +05:30
}
}
#if os(macOS)
.onHover(perform: onHover(_:))
#endif
}
}
#if os(macOS)
.buttonStyle(.link)
#endif
}
private var visibleSuggestions: [String] {
state.querySuggestions.collection.filter {
$0.compare(state.queryText, options: .caseInsensitive) != .orderedSame
}
}
private func querySuffix(_ suggestion: String) -> String {
2021-12-06 23:42:33 +05:30
suggestion.replacingFirstOccurrence(of: state.suggestionsText.lowercased(), with: "")
2021-11-28 20:07:55 +05:30
}
#if os(macOS)
private func onHover(_ inside: Bool) {
if inside {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
#endif
}
struct SearchSuggestions_Previews: PreviewProvider {
static var previews: some View {
SearchSuggestions()
.injectFixtureEnvironmentObjects()
}
}