Skip to content

Commit

Permalink
feat: keyboard shortcuts UI (gluonfield#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gluonfield authored Feb 24, 2024
1 parent f3b4917 commit a5c07d1
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 42 deletions.
8 changes: 8 additions & 0 deletions Enchanted.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
FF2F03442B79631800349855 /* Button+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2F03432B79631800349855 /* Button+Extension.swift */; };
FF2F03492B796A6500349855 /* HotkeyService.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2F03482B796A6500349855 /* HotkeyService.swift */; };
FF2F03722B79743400349855 /* Magnet in Frameworks */ = {isa = PBXBuildFile; platformFilters = (macos, ); productRef = FF2F03712B79743400349855 /* Magnet */; };
FF33066C2B83BB31007B33E5 /* SidebarButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF33066B2B83BB31007B33E5 /* SidebarButton.swift */; };
FF33066E2B83CFEC007B33E5 /* KeyboardShortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF33066D2B83CFEC007B33E5 /* KeyboardShortcuts.swift */; };
FF38F84F2B7A7B6700546B56 /* MenuBarControlView_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF38F84E2B7A7B6700546B56 /* MenuBarControlView_macOS.swift */; };
FF38F8512B7A919C00546B56 /* MenuBarControl_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF38F8502B7A919C00546B56 /* MenuBarControl_macOS.swift */; };
FF38F8532B7AA9C400546B56 /* ApplicationEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF38F8522B7AA9C400546B56 /* ApplicationEntry.swift */; };
Expand Down Expand Up @@ -104,6 +106,8 @@
FF2F03412B795E0B00349855 /* Clipboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clipboard.swift; sourceTree = "<group>"; };
FF2F03432B79631800349855 /* Button+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Button+Extension.swift"; sourceTree = "<group>"; };
FF2F03482B796A6500349855 /* HotkeyService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HotkeyService.swift; sourceTree = "<group>"; };
FF33066B2B83BB31007B33E5 /* SidebarButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarButton.swift; sourceTree = "<group>"; };
FF33066D2B83CFEC007B33E5 /* KeyboardShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardShortcuts.swift; sourceTree = "<group>"; };
FF38F84E2B7A7B6700546B56 /* MenuBarControlView_macOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarControlView_macOS.swift; sourceTree = "<group>"; };
FF38F8502B7A919C00546B56 /* MenuBarControl_macOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarControl_macOS.swift; sourceTree = "<group>"; };
FF38F8522B7AA9C400546B56 /* ApplicationEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationEntry.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -215,6 +219,8 @@
children = (
FF10023F2B24F7320011A4DC /* SideBarMenuView.swift */,
FF1002592B2624C40011A4DC /* ConversationHistoryListView.swift */,
FF33066B2B83BB31007B33E5 /* SidebarButton.swift */,
FF33066D2B83CFEC007B33E5 /* KeyboardShortcuts.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -535,6 +541,7 @@
FF1002302B2482BA0011A4DC /* ChatMessageView.swift in Sources */,
FF1002322B2483A20011A4DC /* Colours+Extension.swift in Sources */,
FFB21A872B7BD0BA00D148A4 /* KeyboardReadable+Extension.swift in Sources */,
FF33066E2B83CFEC007B33E5 /* KeyboardShortcuts.swift in Sources */,
FF66A51D2B76949A00FAAC1E /* Helpers.swift in Sources */,
FF10026A2B2731C60011A4DC /* ModelSelectorView.swift in Sources */,
FF38F84F2B7A7B6700546B56 /* MenuBarControlView_macOS.swift in Sources */,
Expand Down Expand Up @@ -573,6 +580,7 @@
FF38F8582B7AB1AD00546B56 /* PanelManager.swift in Sources */,
FF10025A2B2624C40011A4DC /* ConversationHistoryListView.swift in Sources */,
FF10026D2B2751760011A4DC /* SettingsView.swift in Sources */,
FF33066C2B83BB31007B33E5 /* SidebarButton.swift in Sources */,
FF1002542B261D460011A4DC /* MessageListVIew.swift in Sources */,
FF10024E2B25C2A70011A4DC /* ConversationSD.swift in Sources */,
FFEC32912B24779A003E5C04 /* EnchantedApp.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions Enchanted/Application/EnchantedApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ struct EnchantedApp: App {
#endif
}
}
#if os(macOS)
Window("Keyboard Shortcuts", id: "keyboard-shortcuts") {
KeyboardShortcuts()
}
#endif

#if os(macOS)
#if false
Expand Down
6 changes: 5 additions & 1 deletion Enchanted/Models/SamplePrompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ extension SamplePrompts {
.init(prompt: "How to center div in HTML?", type: .question),
.init(prompt: "What's unique about Go programming language?", type: .question),
.init(prompt: "Give 10 gift ideas for best friend", type: .action),
.init(prompt: "Write a text message asking a friend to be my plus-one at a wedding", type: .action),
.init(prompt: "Explain supercomputers like I'm five years old", type: .action),
.init(prompt: "How to do personal taxes in USA?", type: .question),
.init(prompt: "What are the largest cities in USA in population? Give a table", type: .question)
.init(prompt: "What are the largest cities in USA in population? Give a table", type: .question),
.init(prompt: "Give me ideas about New Years resolutions", type: .action),
.init(prompt: "What is bubble sort? Write example in python", type: .question)
]

static var shuffled: [SamplePrompts] {
Expand Down
1 change: 0 additions & 1 deletion Enchanted/Stores/LanguageModelStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ final class LanguageModelStore {
print("completed saveModels()")

models = try await swiftDataService.fetchModels()
sleepTest("loadModels")
}

func deleteAllModels() async throws {
Expand Down
16 changes: 12 additions & 4 deletions Enchanted/UI/Shared/Chat/Components/EmptyConversaitonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct EmptyConversaitonView: View, KeyboardReadable {
#else
var columns = [GridItem(.flexible()), GridItem(.flexible())]
#endif
@State var visibleItems = Set<Int>()

var body: some View {
VStack {
Expand All @@ -42,16 +43,16 @@ struct EmptyConversaitonView: View, KeyboardReadable {
.foregroundStyle(Color(.systemGray))

LazyVGrid(columns: columns, alignment: .leading, spacing: 15) {
ForEach(prompts.prefix(4), id: \.self) { prompt in
Button(action: {sendPrompt(prompt.prompt)}) {
ForEach(0..<prompts.prefix(4).count, id: \.self) { index in
Button(action: {sendPrompt(prompts[index].prompt)}) {
VStack(alignment: .leading) {
Text(prompt.prompt)
Text(prompts[index].prompt)
.font(.system(size: 15))
Spacer()

HStack {
Spacer()
Image(systemName: prompt.type.icon)
Image(systemName: prompts[index].type.icon)
}
}
.frame(maxWidth: .infinity)
Expand All @@ -60,11 +61,18 @@ struct EmptyConversaitonView: View, KeyboardReadable {
.clipShape(RoundedRectangle(cornerRadius: 10))

}
.opacity(visibleItems.contains(index) ? 1 : 0)
.animation(.easeIn(duration: 0.3).delay(0.2 * Double(index)), value: visibleItems)
.transition(.slide)
.showIf(showPromptsAnimation)
.buttonStyle(.plain)
}
}
.onAppear {
for index in 0..<4 {
visibleItems.insert(index)
}
}
.frame(maxWidth: 700)
.padding()
.transition(AnyTransition(.opacity).combined(with: .slide))
Expand Down
6 changes: 3 additions & 3 deletions Enchanted/UI/Shared/Chat/Components/UnreachableAPIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ struct UnreachableAPIView: View {
.background(Color(.pink).opacity(0.2))
.clipShape(RoundedRectangle(cornerRadius: 10))
.padding()
.sheet(isPresented: $showSettings) {
Settings()
}
.sheet(isPresented: $showSettings) {
Settings()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Enchanted/UI/Shared/Settings/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import SwiftUI

struct Settings: View {
private var languageModelStore = LanguageModelStore.shared
private var conversationStore = ConversationStore.shared
var languageModelStore = LanguageModelStore.shared
var conversationStore = ConversationStore.shared

@AppStorage("ollamaUri") private var ollamaUri: String = ""
@AppStorage("systemPrompt") private var systemPrompt: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct ConversationHistoryList: View {
Text(dailyConversation.name)
.lineLimit(1)
.font(.system(size: 16))
.fontWeight(.medium)
.fontWeight(.regular)
.foregroundColor(Color(.label))
Spacer()
}
Expand Down
40 changes: 40 additions & 0 deletions Enchanted/UI/Shared/Sidebar/Components/KeyboardShortcuts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// KeyboardShortcuts.swift
// Enchanted
//
// Created by Augustinas Malinauskas on 19/02/2024.
//

import SwiftUI

struct KeyboardShortcut: Identifiable {
let id: Int
var keys: [String]
var description: String
}

struct KeyboardShortcuts: View {

var shortcuts = [
KeyboardShortcut(id: 1, keys: ["", "", "K"], description: "Open Panel Window"),
KeyboardShortcut(id: 2, keys: ["", "N"], description: "New Conversation"),
KeyboardShortcut(id: 3, keys: ["", "", "S"], description: "Hide/Show sidebar"),
KeyboardShortcut(id: 4, keys: ["", "V"], description: "Paste text or image from clipboard into message box ")
]

var body: some View {
Table(shortcuts) {
TableColumn("Shortcut") { shortcut in
Text(shortcut.keys.joined(separator: " + "))
}
.width(min: 100, max: 150)
TableColumn("Description") { shortcut in
Text(String(shortcut.description))
}
}
}
}

#Preview {
KeyboardShortcuts()
}
39 changes: 39 additions & 0 deletions Enchanted/UI/Shared/Sidebar/Components/SidebarButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// SidebarButton.swift
// Enchanted
//
// Created by Augustinas Malinauskas on 19/02/2024.
//

import SwiftUI

struct SidebarButton: View {
var title: String
var image: String
var onClick: () -> ()

var body: some View {
Button(action: onClick) {
HStack {
Image(systemName: image)
.resizable()
.scaledToFit()
.frame(width: 16)

Text(title)
.lineLimit(1)
.font(.system(size: 14))
.fontWeight(.regular)

Spacer()
}
.padding(8)
.foregroundColor(Color(.label))
}
.buttonStyle(.plain)
}
}

#Preview {
SidebarButton(title: "Settings", image: "gearshape.fill", onClick: {})
}
38 changes: 18 additions & 20 deletions Enchanted/UI/Shared/Sidebar/SidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
import SwiftUI

struct SidebarView: View {
@Environment(\.openWindow) var openWindow
var conversations: [ConversationSD]
var onConversationTap: (_ conversation: ConversationSD) -> ()
var onConversationDelete: (_ conversation: ConversationSD) -> ()
var onDeleteDailyConversations: (_ date: Date) -> ()
@State var showSettings = false

private func onSettingsTap() {
showSettings.toggle()
Haptics.shared.mediumTap()
}

private func showKeyboardShortcuts() {
openWindow(id: "keyboard-shortcuts")
}

var body: some View {
VStack {
ScrollView() {
Expand All @@ -26,26 +36,14 @@ struct SidebarView: View {
}
.scrollIndicators(.never)

Button(action: {
showSettings.toggle()
Haptics.shared.mediumTap()
}) {
HStack {
Image(systemName: "gearshape.fill")
.resizable()
.scaledToFit()
.frame(width: 18)

Text("Settings")
.font(.system(size: 16))
.fontWeight(.medium)

Spacer()
}
.foregroundColor(Color(.label))
.padding()
.clipShape(RoundedRectangle(cornerRadius: 10))
}
Divider()

#if os(macOS)
SidebarButton(title: "Shortcuts", image: "keyboard.fill", onClick: showKeyboardShortcuts)
#endif

SidebarButton(title: "Settings", image: "gearshape.fill", onClick: onSettingsTap)

}
.padding()
.sheet(isPresented: $showSettings) {
Expand Down
20 changes: 10 additions & 10 deletions Enchanted/UI/macOS/PromptPanel/PromptPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ struct PromptPanelView: View {

var body: some View {
VStack(spacing: 0) {
// ZStack(alignment: .top) {
// VortexView(.splash.makeUniqueCopy()) {
// Circle()
// .fill(.white)
// .frame(width: 20, height: 20)
// .tag("circle")
// }
// }
// .frame(height: 50)
// .background(Color.clear)
ZStack(alignment: .top) {
VortexView(.splash.makeUniqueCopy()) {
Circle()
.fill(.white)
.frame(width: 20, height: 20)
.tag("circle")
}
}
.frame(height: 50)
.background(Color.clear)

VStack(alignment: .leading) {
inputField
Expand Down

0 comments on commit a5c07d1

Please sign in to comment.