Glimmer / app · glimmertoday.ai / native / Glimmer / Messages.swift
| 1 | import SwiftUI |
| 2 | #if os(iOS) |
| 3 | import UIKit |
| 4 | #else |
| 5 | import AppKit |
| 6 | #endif |
| 7 | |
| 8 | /// Read the room: how a message might come across, before you reply or send. |
| 9 | struct ReadTheRoomView: View { |
| 10 | @SceneStorage("glimmer.toolkit.room") private var text = "" |
| 11 | @State private var received = true |
| 12 | @State private var reading: ToolkitResult<ToneReading>? |
| 13 | @State private var working: Task<Void, Never>? |
| 14 | @State private var readText = "" |
| 15 | |
| 16 | var body: some View { |
| 17 | Page(title: "Read the room", subtitle: "Paste a message to see how it might come across. Nothing leaves your device unless you’ve connected a writing agent.", width: 620) { |
| 18 | GlassSegments(label: "Whose message is it?", |
| 19 | options: [GlassSegment(value: true, title: "I received it"), |
| 20 | GlassSegment(value: false, title: "I’m sending it")], |
| 21 | selection: $received) |
| 22 | .accessibilityIdentifier("room.direction") |
| 23 | Panel { |
| 24 | ToolkitTextEditor(text: $text, prompt: received ? "Paste the message you got…" : "Paste what you plan to send…") |
| 25 | .accessibilityIdentifier("room.text") |
| 26 | } |
| 27 | if working != nil { Panel { ToolkitThinking(text: "Reading it through…") } } |
| 28 | if let reading { result(reading) } |
| 29 | } |
| 30 | .actionBar(width: 620) { |
| 31 | if reading == nil { |
| 32 | PrimaryAction(title: "Read the room", systemImage: "eye", action: read) |
| 33 | .disabled(working != nil || text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 34 | .accessibilityIdentifier("room.read") |
| 35 | } |
| 36 | } |
| 37 | .onChange(of: received) { reading = nil } |
| 38 | .onChange(of: text) { if reading != nil && text != readText { reading = nil } } |
| 39 | .onDisappear { working?.cancel() } |
| 40 | #if DEBUG |
| 41 | .onAppear { |
| 42 | guard text.isEmpty, let sample = ToolkitSample.text else { return } |
| 43 | text = sample |
| 44 | ToolkitSample.run { read() } |
| 45 | } |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | @ViewBuilder private func result(_ reading: ToolkitResult<ToneReading>) -> some View { |
| 50 | Panel(title: "How it may come across") { |
| 51 | VStack(alignment: .leading, spacing: 12) { |
| 52 | ForEach(reading.value.tones) { note in |
| 53 | VStack(alignment: .leading, spacing: 2) { |
| 54 | Text(note.tone).font(.headline) |
| 55 | Text(note.why.prefix(1).uppercased() + note.why.dropFirst()).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 56 | } |
| 57 | .accessibilityElement(children: .combine) |
| 58 | } |
| 59 | } |
| 60 | .accessibilityIdentifier("room.tones") |
| 61 | Divider() |
| 62 | Text(reading.value.takeaway).fixedSize(horizontal: false, vertical: true) |
| 63 | ToolkitSourceLabel(source: reading.source, note: reading.note) |
| 64 | if !received { |
| 65 | NavigationLink { RewordView(initial: text) } label: { |
| 66 | Label("Reword it", systemImage: "wand.and.sparkles").fontWeight(.medium).frame(minHeight: 24) |
| 67 | } |
| 68 | .glimmerButton().buttonBorderShape(.capsule).controlSize(.large) |
| 69 | .accessibilityIdentifier("room.reword") |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private func read() { |
| 75 | let message = text |
| 76 | let direction = received |
| 77 | readText = message |
| 78 | working = Task { |
| 79 | let result = await ToolkitAI.readTheRoom(message, received: direction) |
| 80 | if !Task.isCancelled { reading = result } |
| 81 | working = nil |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Reword: the same message in a different tone. |
| 87 | struct RewordView: View { |
| 88 | @AppStorage(StepSize.preference) private var stepSize = StepSize.balanced.rawValue |
| 89 | @Environment(\.readingTheme) private var theme |
| 90 | @State private var text: String |
| 91 | @State private var tone: RewordTone |
| 92 | @State private var rewrite: ToolkitResult<String>? |
| 93 | @State private var previous: String? |
| 94 | @State private var working: Task<Void, Never>? |
| 95 | @State private var copied = false |
| 96 | private let hasModel = ToolkitAI.canReadTone |
| 97 | /// Without a model, the tones that still work come first so they're in view. |
| 98 | private var tones: [RewordTone] { |
| 99 | hasModel ? RewordTone.allCases : RewordTone.allCases.filter(\.worksOffline) + RewordTone.allCases.filter { !$0.worksOffline } |
| 100 | } |
| 101 | |
| 102 | init(initial: String = "") { |
| 103 | _text = State(initialValue: initial) |
| 104 | _tone = State(initialValue: ToolkitAI.canReadTone ? .kinder : .shorter) |
| 105 | } |
| 106 | |
| 107 | var body: some View { |
| 108 | Page(title: "Reword", subtitle: "Say the same thing in a different tone. Your meaning, names and facts stay put.", width: 620) { |
| 109 | Panel { |
| 110 | ToolkitTextEditor(text: $text, prompt: "Paste or type your message…", minHeight: rewrite == nil ? 160 : 72) |
| 111 | .accessibilityIdentifier("reword.text") |
| 112 | if previous != nil { |
| 113 | Button("Undo", systemImage: "arrow.uturn.backward") { |
| 114 | if let previous { text = previous } |
| 115 | previous = nil |
| 116 | } |
| 117 | .font(.callout).buttonStyle(.borderless) |
| 118 | } |
| 119 | } |
| 120 | if working != nil { Panel { ToolkitThinking(text: "Rewording…") } } |
| 121 | if let rewrite { result(rewrite) } |
| 122 | Panel(title: "Tone") { |
| 123 | ScrollView(.horizontal, showsIndicators: false) { |
| 124 | HStack(spacing: 8) { |
| 125 | ForEach(tones) { option in |
| 126 | Chip(label: option.rawValue, selected: tone == option, tint: theme.selectionInk) { tone = option } |
| 127 | .disabled(!hasModel && !option.worksOffline) |
| 128 | .opacity(!hasModel && !option.worksOffline ? 0.4 : 1) |
| 129 | } |
| 130 | } |
| 131 | .padding(.vertical, 2) |
| 132 | } |
| 133 | .scrollClipDisabled() |
| 134 | .accessibilityIdentifier("reword.tone") |
| 135 | Text(hasModel |
| 136 | ? "Step size sets how much it changes: \(StepSize(rawValue: stepSize)?.title.lowercased() ?? "balanced")." |
| 137 | : "Without Apple Intelligence or a connected writing agent, Glimmer can make it shorter or turn it into bullet points.") |
| 138 | .font(.footnote).foregroundStyle(.secondary) |
| 139 | .fixedSize(horizontal: false, vertical: true) |
| 140 | } |
| 141 | } |
| 142 | .actionBar(width: 620) { |
| 143 | if let rewrite { |
| 144 | Button("Try again", systemImage: "arrow.clockwise", action: run) |
| 145 | .glimmerButton().controlSize(.large) |
| 146 | .disabled(working != nil) |
| 147 | .accessibilityIdentifier("reword.run") |
| 148 | PrimaryAction(title: copied ? "Copied" : "Copy", systemImage: copied ? "checkmark" : "doc.on.doc") { copy(rewrite.value) } |
| 149 | .accessibilityIdentifier("reword.copy") |
| 150 | } else { |
| 151 | PrimaryAction(title: "Reword", systemImage: "wand.and.sparkles", action: run) |
| 152 | .disabled(working != nil || text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 153 | || (!hasModel && !tone.worksOffline)) |
| 154 | .accessibilityIdentifier("reword.run") |
| 155 | } |
| 156 | } |
| 157 | .hidesTabBar() |
| 158 | .onChange(of: tone) { rewrite = nil } |
| 159 | |
| 160 | .onDisappear { working?.cancel() } |
| 161 | #if DEBUG |
| 162 | .onAppear { |
| 163 | guard text.isEmpty, let sample = ToolkitSample.text else { return } |
| 164 | text = sample |
| 165 | ToolkitSample.run { run() } |
| 166 | } |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | @ViewBuilder private func result(_ rewrite: ToolkitResult<String>) -> some View { |
| 171 | Panel(title: tone.rawValue) { |
| 172 | Text(rewrite.value) |
| 173 | .textSelection(.enabled) |
| 174 | .fixedSize(horizontal: false, vertical: true) |
| 175 | .accessibilityIdentifier("reword.result") |
| 176 | ToolkitSourceLabel(source: rewrite.source, note: rewrite.note) |
| 177 | FlowLayout(spacing: 8) { |
| 178 | Button("Use this", systemImage: "arrow.up.doc") { |
| 179 | previous = text |
| 180 | text = rewrite.value |
| 181 | self.rewrite = nil |
| 182 | } |
| 183 | .accessibilityHint("Replaces your message so you can reword it again") |
| 184 | ShareLink(item: rewrite.value) |
| 185 | } |
| 186 | .glimmerButton() |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private func run() { |
| 191 | let message = text |
| 192 | let chosen = tone |
| 193 | let size = StepSize(rawValue: stepSize) ?? .balanced |
| 194 | copied = false |
| 195 | working = Task { |
| 196 | let result = await ToolkitAI.reword(message, tone: chosen, size: size) |
| 197 | if !Task.isCancelled { rewrite = result } |
| 198 | working = nil |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private func copy(_ value: String) { |
| 203 | #if os(iOS) |
| 204 | UIPasteboard.general.string = value |
| 205 | #else |
| 206 | NSPasteboard.general.clearContents() |
| 207 | NSPasteboard.general.setString(value, forType: .string) |
| 208 | #endif |
| 209 | copied = true |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #if DEBUG |
| 214 | /// Review launches: GLIMMER_TOOLKIT_TEXT fills a Toolkit field ("|" is a new line). |
| 215 | enum ToolkitSample { |
| 216 | static var text: String? { |
| 217 | ProcessInfo.processInfo.environment["GLIMMER_TOOLKIT_TEXT"]?.replacingOccurrences(of: "|", with: "\n") |
| 218 | } |
| 219 | /// Runs the tool once the page has settled, when GLIMMER_TOOLKIT_AUTORUN=1. |
| 220 | @MainActor static func run(_ action: @escaping @MainActor () -> Void) { |
| 221 | guard ProcessInfo.processInfo.environment["GLIMMER_TOOLKIT_AUTORUN"] == "1" else { return } |
| 222 | Task { @MainActor in |
| 223 | try? await Task.sleep(for: .milliseconds(800)) |
| 224 | action() |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | #endif |