Added a lot of functionality. Bugfixes and changes
This commit is contained in:
@@ -12,6 +12,8 @@ struct HistoryView: View {
|
||||
@Environment(\.dismiss) var dismiss
|
||||
@State private var searchText = ""
|
||||
@State private var historyEntries: [HistoryEntry] = []
|
||||
@State private var selectedIndex: Int = 0
|
||||
@FocusState private var isListFocused: Bool
|
||||
var onSelect: ((String) -> Void)?
|
||||
|
||||
private var filteredHistory: [HistoryEntry] {
|
||||
@@ -80,23 +82,61 @@ struct HistoryView: View {
|
||||
}
|
||||
Spacer()
|
||||
} else {
|
||||
List {
|
||||
ForEach(filteredHistory) { entry in
|
||||
HistoryRow(entry: entry)
|
||||
ScrollViewReader { proxy in
|
||||
List {
|
||||
ForEach(Array(filteredHistory.enumerated()), id: \.element.id) { index, entry in
|
||||
HistoryRow(
|
||||
entry: entry,
|
||||
isSelected: index == selectedIndex
|
||||
)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
selectedIndex = index
|
||||
onSelect?(entry.input)
|
||||
dismiss()
|
||||
}
|
||||
.id(entry.id)
|
||||
}
|
||||
}
|
||||
.listStyle(.plain)
|
||||
.focused($isListFocused)
|
||||
.onChange(of: selectedIndex) {
|
||||
withAnimation {
|
||||
proxy.scrollTo(filteredHistory[selectedIndex].id, anchor: .center)
|
||||
}
|
||||
}
|
||||
.onChange(of: searchText) {
|
||||
selectedIndex = 0
|
||||
}
|
||||
#if os(macOS)
|
||||
.onKeyPress(.upArrow) {
|
||||
if selectedIndex > 0 {
|
||||
selectedIndex -= 1
|
||||
}
|
||||
return .handled
|
||||
}
|
||||
.onKeyPress(.downArrow) {
|
||||
if selectedIndex < filteredHistory.count - 1 {
|
||||
selectedIndex += 1
|
||||
}
|
||||
return .handled
|
||||
}
|
||||
.onKeyPress(.return) {
|
||||
if !filteredHistory.isEmpty && selectedIndex < filteredHistory.count {
|
||||
onSelect?(filteredHistory[selectedIndex].input)
|
||||
dismiss()
|
||||
}
|
||||
return .handled
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.listStyle(.plain)
|
||||
}
|
||||
}
|
||||
.frame(minWidth: 600, minHeight: 400)
|
||||
.background(Color.oaiBackground)
|
||||
.task {
|
||||
loadHistory()
|
||||
isListFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +152,7 @@ struct HistoryView: View {
|
||||
|
||||
struct HistoryRow: View {
|
||||
let entry: HistoryEntry
|
||||
let isSelected: Bool
|
||||
private let settings = SettingsService.shared
|
||||
|
||||
var body: some View {
|
||||
@@ -134,6 +175,7 @@ struct HistoryRow: View {
|
||||
}
|
||||
.padding(.vertical, 8)
|
||||
.padding(.horizontal, 4)
|
||||
.listRowBackground(isSelected ? Color.oaiAccent.opacity(0.2) : Color.clear)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,3 +184,13 @@ struct HistoryRow: View {
|
||||
print("Selected: \(input)")
|
||||
}
|
||||
}
|
||||
|
||||
#Preview("History Row") {
|
||||
HistoryRow(
|
||||
entry: HistoryEntry(
|
||||
input: "Tell me about Swift concurrency",
|
||||
timestamp: Date()
|
||||
),
|
||||
isSelected: true
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user