-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainView.swift
More file actions
467 lines (427 loc) · 21.3 KB
/
Copy pathMainView.swift
File metadata and controls
467 lines (427 loc) · 21.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import SwiftUI
import Cocoa
/// Single visible window of halfFull. Pure settings + about — no in-app converter.
/// The product itself is the global hotkey; this window is purely how you configure
/// and identify it.
struct MainView: View {
@ObservedObject var prefs = PreferencesStore.shared
@State private var trustState = AccessibilityHelper.shared.refreshTrustState()
@State private var axPollTimer: Timer?
@State private var launchAtLoginToggle: Bool = LaunchAtLoginManager.isEnabled
@State private var selectedTarget: TargetMode = .focusedText
private let twitterURL = URL(string: "https://x.com/taresky")!
// Placeholder — replace once the public source URL is decided.
private let sourceURL = URL(string: "https://github.com/taresky/halffull")!
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 24) {
hero
Divider()
targetPicker
if selectedTarget == .focusedText {
permissionRow
}
hotkeySection
Divider()
targetSettingsSection
Divider()
sharedSettingsSection
Divider()
aboutSection
}
.padding(24)
}
.frame(width: 460, height: 620)
.onAppear {
launchAtLoginToggle = LaunchAtLoginManager.isEnabled
startAXPolling()
}
.onDisappear { stopAXPolling() }
}
// MARK: - Hero (icon + name + tagline)
private var hero: some View {
HStack(spacing: 16) {
ZStack {
// Off-white "paper" with the aA wordmark — matches the .icns
// visual identity so the window mirrors what the user sees in
// the Dock and on the landing page.
LinearGradient(colors: [Color(red: 1.00, green: 1.00, blue: 1.00),
Color(red: 0.92, green: 0.92, blue: 0.94)],
startPoint: .top, endPoint: .bottom)
.clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
HStack(spacing: 0) {
Text("a").font(.system(size: 30, weight: .heavy))
Text("A").font(.system(size: 44, weight: .heavy))
}
.foregroundStyle(Color(red: 0.05, green: 0.05, blue: 0.08))
}
.frame(width: 64, height: 64)
VStack(alignment: .leading, spacing: 4) {
Text(Bundle.main.appName)
.font(.title2).fontWeight(.semibold)
Text(NSLocalizedString("hero.tagline",
value: "Transform focused text or clean the clipboard.",
comment: ""))
.font(.callout).foregroundColor(.secondary)
}
Spacer()
}
}
// MARK: - Permission status
@ViewBuilder private var permissionRow: some View {
let state = trustState
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 10) {
Image(systemName: bannerIcon(state))
.foregroundStyle(bannerIconColor(state))
VStack(alignment: .leading, spacing: 1) {
Text(bannerTitle(state))
.font(.callout).fontWeight(.medium)
Text(bannerSubtitle(state))
.font(.caption).foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer()
}
switch state {
case .granted:
EmptyView()
case .staleGrant:
// ONE button — the recovery flow. We run tccutil reset, relaunch
// the app, and the fresh process auto-re-prompts. User just toggles
// the entry on once in System Settings and is done.
HStack(spacing: 8) {
Button(NSLocalizedString("permission.recover",
value: "Recover Permission",
comment: "")) {
AccessibilityHelper.shared.resetAndRelaunch()
}
.keyboardShortcut(.defaultAction)
Button(NSLocalizedString("permission.recheck",
value: "Re-check",
comment: "")) {
trustState = AccessibilityHelper.shared.refreshTrustState()
}
Spacer()
}
.padding(.leading, 30)
case .freshNeed:
HStack(spacing: 8) {
Button(NSLocalizedString("permission.openSettings",
value: "Open Settings…",
comment: "")) {
AccessibilityHelper.shared.ensureTrustedPrompt()
AccessibilityHelper.shared.openAccessibilitySettings()
}
.keyboardShortcut(.defaultAction)
Button(NSLocalizedString("permission.recheck",
value: "Re-check",
comment: "")) {
trustState = AccessibilityHelper.shared.refreshTrustState()
}
Spacer()
}
.padding(.leading, 30)
}
}
.padding(.vertical, 10).padding(.horizontal, 14)
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.fill(bannerBackground(state))
)
}
private func bannerIcon(_ s: TrustState) -> String {
switch s {
case .granted: return "checkmark.seal.fill"
case .staleGrant: return "arrow.triangle.2.circlepath.circle.fill"
case .freshNeed: return "exclamationmark.triangle.fill"
}
}
private func bannerIconColor(_ s: TrustState) -> Color {
switch s {
case .granted: return .green
case .staleGrant: return .orange
case .freshNeed: return .orange
}
}
private func bannerTitle(_ s: TrustState) -> String {
switch s {
case .granted:
return NSLocalizedString("permission.granted",
value: "Accessibility granted", comment: "")
case .staleGrant:
return NSLocalizedString("permission.stale.title",
value: "Permission was reset by an update",
comment: "")
case .freshNeed:
return NSLocalizedString("permission.required",
value: "Accessibility permission required",
comment: "")
}
}
private func bannerSubtitle(_ s: TrustState) -> String {
switch s {
case .granted:
return String(format: NSLocalizedString("permission.readyHint",
value: "Press %@ inside any text field.",
comment: ""), hotkeyLabel)
case .staleGrant:
return NSLocalizedString("permission.stale.hint",
value: "macOS dropped the previous grant when halfFull was updated. Click Recover Permission — we'll reset and re-prompt automatically.",
comment: "")
case .freshNeed:
return NSLocalizedString("permission.requiredHint",
value: "halfFull needs Accessibility to detect the focused text field.",
comment: "")
}
}
private func bannerBackground(_ s: TrustState) -> Color {
switch s {
case .granted: return Color.green.opacity(0.08)
case .staleGrant: return Color.orange.opacity(0.12)
case .freshNeed: return Color.orange.opacity(0.12)
}
}
private var hotkeyLabel: String {
prefs.hotKey(for: .focusedText).symbolicDescription
}
// MARK: - Target
private var targetPicker: some View {
VStack(alignment: .leading, spacing: 10) {
sectionHeader(NSLocalizedString("section.target", value: "Target", comment: ""))
Picker("", selection: $selectedTarget) {
Text(NSLocalizedString("target.focusedText",
value: "Focused Text", comment: ""))
.tag(TargetMode.focusedText)
Text(NSLocalizedString("target.clipboard",
value: "Clipboard", comment: ""))
.tag(TargetMode.clipboard)
}
.pickerStyle(.segmented)
.labelsHidden()
}
}
// MARK: - Hotkey
private var hotkeySection: some View {
VStack(alignment: .leading, spacing: 10) {
sectionHeader(NSLocalizedString("section.hotkey", value: "Hotkey", comment: ""))
Text(NSLocalizedString("prefs.hotkey.intro",
value: "Press the keys you want to bind. Must include ⌘, ⌃, or ⌥.",
comment: ""))
.font(.caption).foregroundColor(.secondary)
HotKeyRecorderView(prefs: prefs, mode: selectedTarget)
}
}
// MARK: - Settings
@ViewBuilder private var targetSettingsSection: some View {
switch selectedTarget {
case .focusedText:
focusedTextSettingsSection
case .clipboard:
plainClipSettingsSection
}
}
private var focusedTextSettingsSection: some View {
VStack(alignment: .leading, spacing: 12) {
sectionHeader(NSLocalizedString("section.focusedTextSettings",
value: "Focused Text Settings", comment: ""))
row(NSLocalizedString("prefs.direction", value: "Default direction", comment: "")) {
Picker("", selection: Binding(get: { prefs.conversionDirection },
set: { prefs.conversionDirection = $0 })) {
ForEach(ConversionDirection.allCases) { Text($0.localizedName).tag($0) }
}
.labelsHidden().frame(width: 180)
}
row(NSLocalizedString("prefs.scope", value: "Scope", comment: "")) {
Picker("", selection: Binding(get: { prefs.conversionScope },
set: { prefs.conversionScope = $0 })) {
ForEach(ConversionScope.allCases) { Text($0.localizedName).tag($0) }
}
.labelsHidden().frame(width: 180)
}
Toggle(NSLocalizedString("prefs.convertSpace",
value: "Convert space to ideographic (U+3000)",
comment: ""),
isOn: Binding(get: { prefs.convertSpaceToIdeographic },
set: { prefs.convertSpaceToIdeographic = $0 }))
Toggle(NSLocalizedString("prefs.restoreClipboard",
value: "Restore clipboard after fallback paste",
comment: ""),
isOn: Binding(get: { prefs.restoreClipboard },
set: { prefs.restoreClipboard = $0 }))
}
}
private var plainClipSettingsSection: some View {
VStack(alignment: .leading, spacing: 12) {
sectionHeader(NSLocalizedString("section.clipboardSettings",
value: "Clipboard Settings", comment: ""))
Text(NSLocalizedString("plainClip.intro",
value: "Always removes rich formatting. Optional cleanup runs in the order shown.",
comment: ""))
.font(.caption)
.foregroundColor(.secondary)
Toggle(NSLocalizedString("plainClip.trimTrailingLineWhitespace",
value: "Remove trailing spaces and tabs from lines", comment: ""),
isOn: Binding(get: { prefs.plainClipTrimTrailingLineWhitespace },
set: { prefs.plainClipTrimTrailingLineWhitespace = $0 }))
Toggle(NSLocalizedString("plainClip.trimLeadingLineWhitespace",
value: "Remove leading spaces and tabs from lines", comment: ""),
isOn: Binding(get: { prefs.plainClipTrimLeadingLineWhitespace },
set: { prefs.plainClipTrimLeadingLineWhitespace = $0 }))
Toggle(NSLocalizedString("plainClip.removeInvisibleCharacters",
value: "Remove invisible control characters", comment: ""),
isOn: Binding(get: { prefs.plainClipRemoveInvisibleCharacters },
set: { prefs.plainClipRemoveInvisibleCharacters = $0 }))
Toggle(NSLocalizedString("plainClip.removeLineBreaks",
value: "Remove line breaks", comment: ""),
isOn: Binding(get: { prefs.plainClipRemoveLineBreaks },
set: { prefs.plainClipRemoveLineBreaks = $0 }))
Toggle(NSLocalizedString("plainClip.normalizeUnicode",
value: "Normalize Unicode (NFC)", comment: ""),
isOn: Binding(get: { prefs.plainClipNormalizeUnicode },
set: { prefs.plainClipNormalizeUnicode = $0 }))
Toggle(NSLocalizedString("plainClip.replaceTabs",
value: "Replace tabs with spaces", comment: ""),
isOn: Binding(get: { prefs.plainClipReplaceTabs },
set: { prefs.plainClipReplaceTabs = $0 }))
Toggle(NSLocalizedString("plainClip.collapseSpaces",
value: "Collapse consecutive spaces", comment: ""),
isOn: Binding(get: { prefs.plainClipCollapseSpaces },
set: { prefs.plainClipCollapseSpaces = $0 }))
Toggle(NSLocalizedString("plainClip.removeBlankLines",
value: "Remove blank lines", comment: ""),
isOn: Binding(get: { prefs.plainClipRemoveBlankLines },
set: { prefs.plainClipRemoveBlankLines = $0 }))
Toggle(NSLocalizedString("plainClip.straightenQuotes",
value: "Replace smart quotes with straight quotes", comment: ""),
isOn: Binding(get: { prefs.plainClipStraightenQuotes },
set: { prefs.plainClipStraightenQuotes = $0 }))
Toggle(NSLocalizedString("plainClip.convertToASCII",
value: "Convert supported characters to ASCII (preserve others)",
comment: ""),
isOn: Binding(get: { prefs.plainClipConvertToASCII },
set: { prefs.plainClipConvertToASCII = $0 }))
Toggle(NSLocalizedString("plainClip.trimWholeString",
value: "Trim surrounding whitespace", comment: ""),
isOn: Binding(get: { prefs.plainClipTrimWholeString },
set: { prefs.plainClipTrimWholeString = $0 }))
Divider()
Toggle(NSLocalizedString("plainClip.pasteAfterCleaning",
value: "Paste automatically after cleaning", comment: ""),
isOn: Binding(get: { prefs.plainClipPasteAfterCleaning },
set: { prefs.plainClipPasteAfterCleaning = $0 }))
Text(NSLocalizedString("plainClip.pastePermissionHint",
value: "macOS may request permission the first time automatic paste is used.",
comment: ""))
.font(.caption)
.foregroundColor(.secondary)
}
}
private var sharedSettingsSection: some View {
VStack(alignment: .leading, spacing: 12) {
sectionHeader(NSLocalizedString("section.appSettings",
value: "App Settings", comment: ""))
Toggle(NSLocalizedString("prefs.playSound",
value: "Play sound on success",
comment: ""),
isOn: Binding(get: { prefs.playSoundOnSuccess },
set: { prefs.playSoundOnSuccess = $0 }))
Toggle(NSLocalizedString("prefs.notifications",
value: "Show notifications",
comment: ""),
isOn: Binding(get: { prefs.showNotifications },
set: { newValue in
prefs.showNotifications = newValue
// Lazy permission request: only prompt when the user
// actively opts in. If they already granted (or denied)
// before, this call is a no-op.
if newValue {
NotificationPresenter.shared.requestAuthorization()
}
}))
Toggle(NSLocalizedString("prefs.launchAtLogin",
value: "Launch at login",
comment: ""),
isOn: Binding(get: { launchAtLoginToggle },
set: { newValue in
_ = LaunchAtLoginManager.setEnabled(newValue)
launchAtLoginToggle = LaunchAtLoginManager.isEnabled
prefs.launchAtLogin = launchAtLoginToggle
}))
Toggle(NSLocalizedString("prefs.showMenuBarIcon",
value: "Show in menu bar",
comment: ""),
isOn: Binding(get: { prefs.showMenuBarIcon },
set: { prefs.showMenuBarIcon = $0 }))
}
}
// MARK: - About
private var aboutSection: some View {
VStack(alignment: .leading, spacing: 10) {
sectionHeader(NSLocalizedString("section.about", value: "About", comment: ""))
HStack {
Text(String(format: NSLocalizedString("about.version",
value: "Version %@ (%@)",
comment: ""),
Bundle.main.shortVersion, Bundle.main.buildNumber))
.font(.callout).foregroundColor(.secondary)
Spacer()
}
HStack(spacing: 12) {
Link(destination: twitterURL) {
Label("Twitter @taresky", systemImage: "bird")
}
Link(destination: sourceURL) {
Label("Source", systemImage: "chevron.left.forwardslash.chevron.right")
}
}
.font(.callout)
Text(NSLocalizedString("about.copyright",
value: "© 2026 Taresky. PolyForm Noncommercial 1.0.0.",
comment: ""))
.font(.footnote).foregroundColor(.secondary)
}
}
// MARK: - Helpers
private func sectionHeader(_ text: String) -> some View {
Text(text)
.font(.subheadline).fontWeight(.semibold)
.foregroundStyle(.secondary)
.textCase(.uppercase)
}
/// Two-column row: label on the left, content on the right.
private func row<Content: View>(_ label: String,
@ViewBuilder content: () -> Content) -> some View {
HStack {
Text(label)
Spacer()
content()
}
}
// MARK: - AX polling
private func startAXPolling() {
axPollTimer?.invalidate()
trustState = AccessibilityHelper.shared.refreshTrustState()
axPollTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
let state = AccessibilityHelper.shared.refreshTrustState()
if state != trustState { trustState = state }
}
}
private func stopAXPolling() {
axPollTimer?.invalidate()
axPollTimer = nil
}
}
extension Bundle {
var appName: String {
(object(forInfoDictionaryKey: "CFBundleDisplayName") as? String)
?? (object(forInfoDictionaryKey: "CFBundleName") as? String)
?? "halfFull"
}
var shortVersion: String {
(object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String) ?? "0.0"
}
var buildNumber: String {
(object(forInfoDictionaryKey: "CFBundleVersion") as? String) ?? "0"
}
}