forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskPromptEditorWindow.swift
More file actions
157 lines (132 loc) · 4.39 KB
/
Copy pathTaskPromptEditorWindow.swift
File metadata and controls
157 lines (132 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Cocoa
import OmiTheme
import SwiftUI
/// SwiftUI view for editing the task extraction prompt
struct TaskPromptEditorView: View {
@State private var prompt: String
@Environment(\.dismiss) private var dismiss
var onClose: (() -> Void)?
init(onClose: (() -> Void)? = nil) {
self.onClose = onClose
_prompt = State(initialValue: TaskAssistantSettings.shared.analysisPrompt)
}
var body: some View {
VStack(spacing: OmiSpacing.lg) {
// Header
HStack {
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text("Task Extraction Prompt")
.scaledFont(size: OmiType.subheading, weight: .semibold)
.foregroundColor(.primary)
Text("Customize the AI instructions for task extraction")
.scaledFont(size: OmiType.caption)
.foregroundColor(.secondary)
}
Spacer()
// Reset button
Button(action: resetToDefault) {
HStack(spacing: OmiSpacing.xxs) {
Image(systemName: "arrow.counterclockwise")
.scaledFont(size: OmiType.caption)
Text("Reset to Default")
.scaledFont(size: OmiType.caption)
}
}
.buttonStyle(.bordered)
.controlSize(.small)
}
// Text editor
TextEditor(text: $prompt)
.scaledFont(size: OmiType.body, design: .monospaced)
.padding(OmiSpacing.md)
.background(
RoundedRectangle(cornerRadius: OmiChrome.elementRadius)
.fill(Color(nsColor: .textBackgroundColor))
)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.elementRadius)
.strokeBorder(Color.primary.opacity(0.2), lineWidth: 1)
)
.onChange(of: prompt) { _, newValue in
TaskAssistantSettings.shared.analysisPrompt = newValue
}
// Footer with character count
HStack {
Text("\(prompt.count) characters")
.scaledFont(size: OmiType.caption)
.foregroundColor(.secondary)
Spacer()
Button("Done") {
onClose?()
}
.keyboardShortcut(.return, modifiers: .command)
.buttonStyle(.borderedProminent)
.controlSize(.regular)
}
}
.padding(OmiSpacing.xl)
.frame(width: 600, height: 500)
.inkGlassPanel(cornerRadius: 0, shadow: nil)
}
private func resetToDefault() {
TaskAssistantSettings.shared.resetPromptToDefault()
prompt = TaskAssistantSettings.shared.analysisPrompt
}
}
/// NSWindow subclass that hosts the Task Prompt Editor SwiftUI view
class TaskPromptEditorWindow: NSWindow {
private static var sharedWindow: TaskPromptEditorWindow?
/// Shows the task prompt editor window, creating it if necessary
static func show() {
if let existingWindow = sharedWindow {
existingWindow.makeKeyAndOrderFront(nil)
NSApp.activate()
return
}
let window = TaskPromptEditorWindow()
sharedWindow = window
window.makeKeyAndOrderFront(nil)
NSApp.activate()
}
/// Closes the task prompt editor window
static func close() {
sharedWindow?.close()
sharedWindow = nil
}
private init() {
let contentRect = NSRect(x: 0, y: 0, width: 600, height: 500)
super.init(
contentRect: contentRect,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
self.title = "Edit Task Extraction Prompt"
self.isReleasedWhenClosed = false
// The inside of a titled window is glass: transparent, light-pinned, and shadowed by its own
// frame (`WindowGlass.Kind.titled`). Without the pin the title bar and the traffic lights stay in
// the machine's appearance — a dark title bar on a white sheet on a Dark Mac.
WindowGlass.wear(self, as: .titled)
self.delegate = self
self.minSize = NSSize(width: 500, height: 400)
// Center on screen
self.center()
// Create SwiftUI view
let editorView = TaskPromptEditorView(onClose: { [weak self] in
self?.close()
})
let hostingView = NSHostingView(rootView: editorView.withFontScaling())
self.contentView = hostingView
}
}
// MARK: - NSWindowDelegate
extension TaskPromptEditorWindow: NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
TaskPromptEditorWindow.sharedWindow = nil
}
}
#if canImport(PreviewsMacros)
#Preview {
TaskPromptEditorView()
}
#endif