Skip to content

Commit

Permalink
refactor: removes expanded editor (kevinhermawan#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhermawan authored Dec 7, 2023
1 parent 1a3e4b4 commit 176d6ca
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 163 deletions.
22 changes: 11 additions & 11 deletions Ollamac.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
0A3D25152AFFF82D0014351D /* PromptEditorExpandedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A3D25142AFFF82D0014351D /* PromptEditorExpandedView.swift */; };
0A3D25192B0078080014351D /* PromptEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A3D25182B0078080014351D /* PromptEditor.swift */; };
0A20FFA32B21465000112783 /* PromptField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A20FFA22B21465000112783 /* PromptField.swift */; };
0A20FFA52B2146D100112783 /* String+AppendNewLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A20FFA42B2146D100112783 /* String+AppendNewLine.swift */; };
0A4D35852AF541D900846890 /* OllamacApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A4D35842AF541D900846890 /* OllamacApp.swift */; };
0A4D358B2AF541DA00846890 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A4D358A2AF541DA00846890 /* Assets.xcassets */; };
0A4D358E2AF541DA00846890 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A4D358D2AF541DA00846890 /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -40,8 +40,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0A3D25142AFFF82D0014351D /* PromptEditorExpandedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromptEditorExpandedView.swift; sourceTree = "<group>"; };
0A3D25182B0078080014351D /* PromptEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromptEditor.swift; sourceTree = "<group>"; };
0A20FFA22B21465000112783 /* PromptField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromptField.swift; sourceTree = "<group>"; };
0A20FFA42B2146D100112783 /* String+AppendNewLine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+AppendNewLine.swift"; sourceTree = "<group>"; };
0A4D35812AF541D900846890 /* Ollamac.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Ollamac.app; sourceTree = BUILT_PRODUCTS_DIR; };
0A4D35842AF541D900846890 /* OllamacApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OllamacApp.swift; sourceTree = "<group>"; };
0A4D358A2AF541DA00846890 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -86,14 +86,13 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0A3D250F2AFFBD370014351D /* CommonViews */ = {
0A20FFA12B21463F00112783 /* Common */ = {
isa = PBXGroup;
children = (
0A3D25182B0078080014351D /* PromptEditor.swift */,
0A3D25142AFFF82D0014351D /* PromptEditorExpandedView.swift */,
0A20FFA22B21465000112783 /* PromptField.swift */,
0AB5F2972B0B9C340040B72E /* TextError.swift */,
);
path = CommonViews;
path = Common;
sourceTree = "<group>";
};
0A4D35782AF541D900846890 = {
Expand Down Expand Up @@ -148,7 +147,7 @@
children = (
0A4D35982AF5425900846890 /* AppView.swift */,
0ACA4D4F2AF799A5000DA76F /* ChatViews */,
0A3D250F2AFFBD370014351D /* CommonViews */,
0A20FFA12B21463F00112783 /* Common */,
0ACA4D522AF7A788000DA76F /* MessageViews */,
);
path = Views;
Expand Down Expand Up @@ -198,6 +197,7 @@
isa = PBXGroup;
children = (
0ACA4D502AF7A34D000DA76F /* ModelContext+SaveChanges.swift */,
0A20FFA42B2146D100112783 /* String+AppendNewLine.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -312,15 +312,14 @@
files = (
0A4D35BA2AF563BE00846890 /* Chat.swift in Sources */,
0A4D359F2AF5429500846890 /* OllamaModel.swift in Sources */,
0A3D25192B0078080014351D /* PromptEditor.swift in Sources */,
0A4D35B62AF562CB00846890 /* MessageViewModel.swift in Sources */,
0A4D35BE2AF56E7A00846890 /* OllamaViewModel.swift in Sources */,
0A4D35C42AF575E700846890 /* MessageView.swift in Sources */,
0ACA4D4A2AF7946A000DA76F /* ChatSidebarListView.swift in Sources */,
0A4D35992AF5425900846890 /* AppView.swift in Sources */,
0A20FFA52B2146D100112783 /* String+AppendNewLine.swift in Sources */,
0ACA4D4E2AF799A1000DA76F /* RenameChatView.swift in Sources */,
0ACA4D512AF7A34D000DA76F /* ModelContext+SaveChanges.swift in Sources */,
0A3D25152AFFF82D0014351D /* PromptEditorExpandedView.swift in Sources */,
0AB5F2982B0B9C340040B72E /* TextError.swift in Sources */,
0AE024862AFB8EB900347C18 /* UpdaterViewModel.swift in Sources */,
0A4D35B82AF5630600846890 /* Message.swift in Sources */,
Expand All @@ -330,6 +329,7 @@
0ACA4D4C2AF794B8000DA76F /* CommandViewModel.swift in Sources */,
0A4D35852AF541D900846890 /* OllamacApp.swift in Sources */,
0A4D35BC2AF565CB00846890 /* ChatViewModel.swift in Sources */,
0A20FFA32B21465000112783 /* PromptField.swift in Sources */,
0ACA4D842AF94A9C000DA76F /* ChatContextMenu.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 14 additions & 0 deletions Ollamac/Extensions/String+AppendNewLine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// String+AppendNewLine.swift
// Ollamac
//
// Created by Kevin Hermawan on 07/12/23.
//

import Foundation

extension String {
mutating func appendNewLine() {
self += "\n"
}
}
39 changes: 39 additions & 0 deletions Ollamac/Views/Common/PromptField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// PromptField.swift
// Ollamac
//
// Created by Kevin Hermawan on 07/12/23.
//

import SwiftUI
import SwiftUIIntrospect

struct PromptField: View {
@Binding var prompt: String
let onSubmit: () -> Void

var body: some View {
TextField("Type a message...", text: $prompt, axis: .vertical)
.padding(.vertical, 6)
.padding(.horizontal, 10)
.textFieldStyle(.plain)
.font(.system(size: 14))
.clipShape(
RoundedRectangle(cornerRadius: 20, style: .continuous)
)
.overlay(
RoundedRectangle(cornerRadius: 20, style: .continuous)
.stroke(Color(nsColor: .lightGray))
)
.introspect(.textField(axis: .vertical), on: .macOS(.v14)) { textField in
textField.lineBreakMode = .byWordWrapping
}
.onSubmit {
if NSApp.currentEvent?.modifierFlags.contains(.shift) == true {
prompt.appendNewLine()
} else {
onSubmit()
}
}
}
}
File renamed without changes.
45 changes: 0 additions & 45 deletions Ollamac/Views/CommonViews/PromptEditor.swift

This file was deleted.

62 changes: 0 additions & 62 deletions Ollamac/Views/CommonViews/PromptEditorExpandedView.swift

This file was deleted.

69 changes: 24 additions & 45 deletions Ollamac/Views/MessageViews/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ struct MessageView: View {
@Environment(MessageViewModel.self) private var messageViewModel
@Environment(OllamaViewModel.self) private var ollamaViewModel

@FocusState private var isEditorFocused: Bool
@State private var isEditorExpanded: Bool = false
@State private var viewState: ViewState? = nil

@FocusState private var promptFocused: Bool
@State private var prompt: String = ""

init(for chat: Chat) {
Expand Down Expand Up @@ -63,46 +62,31 @@ struct MessageView: View {
messageViewModel.stopGenerate()
}

VStack(spacing: 8) {
HStack(alignment: .bottom, spacing: 16) {
PromptEditor(prompt: $prompt, large: false)
.overlay(alignment: .topTrailing) {
Button {
isEditorExpanded = true
} label: {
Image(systemName: "arrow.up.left.and.arrow.down.right")
.font(.footnote)
.foregroundStyle(.secondary)
}
.padding(8)
.buttonStyle(.plain)
.keyboardShortcut("e", modifiers: .command)
.help("Expand editor (⌘ + E)")
}
.focused($isEditorFocused)
.onSubmit(sendAction)

Button(action: messageViewModel.stopGenerate) {
Image(systemName: "stop.circle.fill")
.padding(8)
.help("Stop generate")
}
.buttonStyle(.borderedProminent)
.visible(if: isGenerating, removeCompletely: true)

Button(action: sendAction) {
Image(systemName: "paperplane.fill")
.padding(8)
.help("Send message")
}
.buttonStyle(.borderedProminent)
.hide(if: isGenerating, removeCompletely: true)
HStack(alignment: .bottom, spacing: 16) {
PromptField(prompt: $prompt, onSubmit: sendAction)
.focused($promptFocused)

Button(action: messageViewModel.stopGenerate) {
Image(systemName: "stop.circle.fill")
.resizable()
.frame(width: 28, height: 28)
}
.buttonStyle(.plain)
.help("Stop generation")
.visible(if: isGenerating, removeCompletely: true)

Button(action: sendAction) {
Image(systemName: "arrow.up.circle.fill")
.resizable()
.frame(width: 28, height: 28)
}
.padding(.horizontal)
.buttonStyle(.plain)
.help("Send message")
.hide(if: isGenerating, removeCompletely: true)
}
.padding(.top, 8)
.padding(.bottom, 16)
.hide(if: isEditorExpanded)
.padding(.horizontal)
}
.navigationTitle(chat.name)
.navigationSubtitle(chat.model?.name ?? "")
Expand All @@ -112,26 +96,21 @@ struct MessageView: View {
.onChange(of: chat) {
initAction()
}
.sheet(isPresented: $isEditorExpanded, onDismiss: { isEditorFocused = true }) {
PromptEditorExpandedView(prompt: $prompt) {
sendAction()
}
}
}

// MARK: - Actions
private func initAction() {
try? messageViewModel.fetch(for: chat)

isEditorFocused = true
promptFocused = true
}

private func sendAction() {
guard messageViewModel.sendViewState.isNil else { return }
guard prompt.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 else { return }

let message = Message(prompt: prompt, response: nil)
message.context = messageViewModel.messages.last?.context ?? []
message.context = chat.messages.last?.context ?? []
message.chat = chat

Task {
Expand Down

0 comments on commit 176d6ca

Please sign in to comment.