macOS widgets !295
13 files changed, +131 −13
Layout: unified · split
Package.swift +5 −1
| @@ -21,7 +21,8 @@ let package = Package( | ||
| 21 | 21 | .executable(name: "OrgstarMobile", targets: ["OrgstarMobile"]), |
| 22 | 22 | .executable(name: "OrgstarQuickLook", targets: ["OrgstarQuickLook"]), |
| 23 | 23 | .executable(name: "OrgstarSpotlight", targets: ["OrgstarSpotlight"]), |
| 24 | .executable(name: "OrgstarShare", targets: ["OrgstarShare"]) | |
| 24 | .executable(name: "OrgstarShare", targets: ["OrgstarShare"]), | |
| 25 | .executable(name: "OrgstarWidgets", targets: ["OrgstarWidgets"]) | |
| 25 | 26 | ], |
| 26 | 27 | dependencies: [ |
| 27 | 28 | .package(url: "https://github.com/groue/GRDB.swift", from: "7.11.1"), |
| @@ -92,6 +93,9 @@ let package = Package( | ||
| 92 | 93 | .executableTarget(name: "OrgstarSpotlight", dependencies: ["OrgCore"], linkerSettings: extensionEntry), |
| 93 | 94 | // The iOS share extension, bundled into the iOS app by scripts/build-ios-app.sh. |
| 94 | 95 | .executableTarget(name: "OrgstarShare", dependencies: ["OrgApp"], linkerSettings: extensionEntry), |
| 96 | // The Mac's widget extension, bundled into the app by scripts/build-app.sh; the iOS one | |
| 97 | // is built from the same sources by ios/Orgstar.xcodeproj. | |
| 98 | .executableTarget(name: "OrgstarWidgets", dependencies: ["OrgApp"]), | |
| 95 | 99 | .testTarget(name: "OrgCoreTests", dependencies: ["OrgCore"], exclude: ["Conformance"]), |
| 96 | 100 | .testTarget(name: "OrgDocumentTests", dependencies: ["OrgDocument"]), |
| 97 | 101 | .testTarget(name: "OrgIndexTests", dependencies: ["OrgIndex"]), |
Sources/OrgApp/AgendaSnapshot.swift +15 −1
| @@ -198,12 +198,26 @@ public struct AgendaSnapshot: Codable, Equatable, Sendable { | ||
| 198 | 198 | |
| 199 | 199 | // MARK: - The file |
| 200 | 200 | |
| 201 | /// `Widgets/agenda.json` in the app group's container; nil without the group. | |
| 201 | /// `Widgets/agenda.json` in the app group's container; nil without the group. On the Mac, | |
| 202 | /// in `~/Library/Application Support/Orgstar/Widgets`, which the sandboxed widget extension | |
| 203 | /// reads by a sandbox exception, as an app group there needs a provisioning profile. | |
| 202 | 204 | public static func file(group: String = CaptureInbox.group) -> URL? { |
| 205 | #if os(macOS) | |
| 206 | return macWidgetsFolder.appendingPathComponent("agenda.json") | |
| 207 | #else | |
| 203 | 208 | FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group)? |
| 204 | 209 | .appendingPathComponent("Widgets").appendingPathComponent("agenda.json") |
| 210 | #endif | |
| 205 | 211 | } |
| 206 | 212 | |
| 213 | #if os(macOS) | |
| 214 | /// From the account's home folder, not a sandbox container's. | |
| 215 | static var macWidgetsFolder: URL { | |
| 216 | let home = getpwuid(getuid())?.pointee.pw_dir.map { String(cString: $0) } ?? NSHomeDirectory() | |
| 217 | return URL(fileURLWithPath: home).appendingPathComponent("Library/Application Support/Orgstar/Widgets") | |
| 218 | } | |
| 219 | #endif | |
| 220 | ||
| 207 | 221 | public static func read(from file: URL) -> AgendaSnapshot? { |
| 208 | 222 | guard let data = try? Data(contentsOf: file) else { return nil } |
| 209 | 223 | return try? decoder.decode(AgendaSnapshot.self, from: data) |
Sources/Orgstar/ContentView.swift +29
| @@ -7,6 +7,7 @@ import OrgWorkspace | ||
| 7 | 7 | import OrgIndex |
| 8 | 8 | import SwiftUI |
| 9 | 9 | import UniformTypeIdentifiers |
| 10 | import WidgetKit | |
| 10 | 11 | |
| 11 | 12 | struct ContentView: View { |
| 12 | 13 | let workspace: WorkspaceModel |
| @@ -19,6 +20,7 @@ struct ContentView: View { | ||
| 19 | 20 | @AppStorage("clockIdleTime") private var clockIdleTime = 0 |
| 20 | 21 | @AppStorage("agendaSubfolders") private var agendaSubfolders = false |
| 21 | 22 | @State private var reminderAgenda: AgendaModel? |
| 23 | @State private var widgetAgenda: AgendaModel? | |
| 22 | 24 | @State private var trust = TrustStore() |
| 23 | 25 | @State private var hotKey: GlobalHotKey? |
| 24 | 26 | @State private var statusItem: ClockStatusItem? |
| @@ -448,6 +450,7 @@ struct ContentView: View { | ||
| 448 | 450 | changed: workspace.changedFile?.version, edits: session.editVersion, hour: hour |
| 449 | 451 | )) { |
| 450 | 452 | await updateReminders() |
| 453 | await updateWidgets() | |
| 451 | 454 | } |
| 452 | 455 | .task { |
| 453 | 456 | while !Task.isCancelled { |
| @@ -917,6 +920,32 @@ struct ContentView: View { | ||
| 917 | 920 | await scheduler.reconcile(reminders) |
| 918 | 921 | } |
| 919 | 922 | |
| 923 | /// The desktop widgets' agenda and habits, written where the widget extension reads them, | |
| 924 | /// with the widgets reloaded when they changed. | |
| 925 | private func updateWidgets() async { | |
| 926 | guard !Task.isCancelled, !workspace.files.isEmpty, let file = AgendaSnapshot.file() else { return } | |
| 927 | let days = widgetAgenda ?? AgendaModel(workspace: workspace) | |
| 928 | widgetAgenda = days | |
| 929 | days.mode = .agenda(span: AgendaSnapshot.dayCount, startOffset: 0) | |
| 930 | days.startOnWeekday = nil | |
| 931 | days.includeSubfolders = agendaSubfolders | |
| 932 | days.agendaFiles = OrgPreferences.agendaFiles() | |
| 933 | days.options = OrgPreferences.agendaOptions(days.options) | |
| 934 | days.options.logMode = false | |
| 935 | days.calendarEvents = OrgPreferences.calendarEvents() && CalendarEvents.isAuthorized ? CalendarAccess.events : nil | |
| 936 | days.calendarNames = OrgPreferences.calendarEventCalendars() | |
| 937 | days.reminderLists = OrgPreferences.agendaReminders() && ReminderLists.isAuthorized ? CalendarAccess.reminders : nil | |
| 938 | days.reminderListNames = OrgPreferences.agendaReminderLists() | |
| 939 | let open = session.openTexts | |
| 940 | await days.refresh(open: open) | |
| 941 | guard !Task.isCancelled else { return } | |
| 942 | if (try? HabitSnapshot(habits: await days.habits(open: open), theme: theme).write()) == true { | |
| 943 | WidgetCenter.shared.reloadTimelines(ofKind: "sh.krz.orgstar.habits") | |
| 944 | } | |
| 945 | let snapshot = AgendaSnapshot(groups: days.groups(), today: days.today, theme: theme, generated: Date()) | |
| 946 | if (try? snapshot.write(to: file)) == true { WidgetCenter.shared.reloadAllTimelines() } | |
| 947 | } | |
| 948 | ||
| 920 | 949 | private func loadKeymap() { |
| 921 | 950 | let loaded = KeymapLoader.load(preset, commands: Set(EditorCommands.all.map(\.id))) |
| 922 | 951 | keymap = loaded.keymap |
Sources/Orgstar/OrgstarApp.swift +15 −3
| @@ -120,8 +120,20 @@ final class AppCommands { | ||
| 120 | 120 | captureRequest = (nil, [:], day, captureSerial) |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | /// `org-protocol://capture` and `store-link`. | |
| 123 | /// `org-protocol://capture` and `store-link`, and the widgets' `orgstar://` links. | |
| 124 | 124 | func handle(protocolURL url: String) { |
| 125 | if let link = URL(string: url).flatMap(AppLink.init(url:)) { | |
| 126 | switch link { | |
| 127 | case .agenda: run("app.agenda") | |
| 128 | case .clock: run("app.clock.goto") | |
| 129 | case .entry(let path, let offset): jump(path: path, offset: offset) | |
| 130 | case .capture(let template): | |
| 131 | captureSerial += 1 | |
| 132 | captureRequest = (template, [:], nil, captureSerial) | |
| 133 | run("app.capture") | |
| 134 | } | |
| 135 | return | |
| 136 | } | |
| 125 | 137 | switch OrgProtocol.parse(url) { |
| 126 | 138 | case .capture(let template, let properties): |
| 127 | 139 | captureSerial += 1 |
| @@ -197,7 +209,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | ||
| 197 | 209 | |
| 198 | 210 | func application(_ application: NSApplication, open urls: [URL]) { |
| 199 | 211 | let paths = urls.filter(\.isFileURL).map(\.path) |
| 200 | let links = urls.filter { $0.scheme == "org-protocol" }.map(\.absoluteString) | |
| 212 | let links = urls.filter { $0.scheme == "org-protocol" || $0.scheme == AppLink.scheme }.map(\.absoluteString) | |
| 201 | 213 | guard let commands, let session else { |
| 202 | 214 | Self.pendingOpens += paths |
| 203 | 215 | Self.pendingLinks += links |
| @@ -208,7 +220,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent | ||
| 208 | 220 | for link in links { commands.handle(protocolURL: link) } |
| 209 | 221 | } |
| 210 | 222 | |
| 211 | /// org-protocol links that arrive before the window is ready. | |
| 223 | /// org-protocol and orgstar:// links that arrive before the window is ready. | |
| 212 | 224 | static var pendingLinks: [String] = [] |
| 213 | 225 | |
| 214 | 226 | func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true } |
Sources/OrgstarWidgets/ClockActivityWidget.swift +2
| @@ -1,3 +1,4 @@ | ||
| 1 | #if os(iOS) | |
| 1 | 2 | import ActivityKit |
| 2 | 3 | import AppIntents |
| 3 | 4 | import OrgApp |
| @@ -82,3 +83,4 @@ struct ClockOutButton: View { | ||
| 82 | 83 | } contentStates: { |
| 83 | 84 | ClockActivityAttributes.ContentState(heading: "Review the draft", startedAt: .now.addingTimeInterval(-3725)) |
| 84 | 85 | } |
| 86 | #endif | |
Sources/OrgstarWidgets/Controls.swift +2
| @@ -1,3 +1,4 @@ | ||
| 1 | #if os(iOS) | |
| 1 | 2 | import AppIntents |
| 2 | 3 | import OrgApp |
| 3 | 4 | import SwiftUI |
| @@ -98,3 +99,4 @@ struct CaptureView: View { | ||
| 98 | 99 | .accessibilityLabel("Capture" + (entry.template.map { " with \($0.name)" } ?? "")) |
| 99 | 100 | } |
| 100 | 101 | } |
| 102 | #endif | |
Sources/OrgstarWidgets/HabitsWidget.swift +4
| @@ -60,7 +60,11 @@ struct HabitsView: View { | ||
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | .widgetURL(AppLink.agenda.url) |
| 63 | #if os(macOS) | |
| 64 | .containerBackground(for: .widget) { Color(nsColor: .windowBackgroundColor) } | |
| 65 | #else | |
| 63 | 66 | .containerBackground(for: .widget) { Color(uiColor: .systemBackground) } |
| 67 | #endif | |
| 64 | 68 | } |
| 65 | 69 | |
| 66 | 70 | private func content(_ snapshot: HabitSnapshot) -> some View { |
Sources/OrgstarWidgets/OrgstarWidgets.swift +6 −1
| @@ -4,21 +4,26 @@ import SwiftUI | ||
| 4 | 4 | import WidgetKit |
| 5 | 5 | |
| 6 | 6 | /// The agenda and habits on the Home Screen and the Lock Screen, capture and the clock in |
| 7 | /// Control Center, and the running clock's Live Activity. | |
| 7 | /// Control Center, and the running clock's Live Activity; on the Mac, the agenda and habits on | |
| 8 | /// the desktop and in Notification Center. | |
| 8 | 9 | @main |
| 9 | 10 | struct OrgstarWidgets: WidgetBundle { |
| 10 | 11 | var body: some Widget { |
| 11 | 12 | TodayWidget() |
| 12 | 13 | HabitsWidget() |
| 13 | 14 | UpNextWidget() |
| 15 | #if os(iOS) | |
| 14 | 16 | CaptureWidget() |
| 15 | 17 | ClockActivityWidget() |
| 16 | 18 | CaptureControl() |
| 17 | 19 | ClockControl() |
| 20 | #endif | |
| 18 | 21 | } |
| 19 | 22 | } |
| 20 | 23 | |
| 24 | #if os(iOS) | |
| 21 | 25 | /// The App Intents OrgApp declares: the Live Activity's Clock Out button. |
| 22 | 26 | struct OrgstarWidgetIntents: AppIntentsPackage { |
| 23 | 27 | static var includedPackages: [any AppIntentsPackage.Type] { [OrgAppIntents.self] } |
| 24 | 28 | } |
| 29 | #endif | |
Sources/OrgstarWidgets/TodayWidget.swift +4
| @@ -33,7 +33,11 @@ struct TodayView: View { | ||
| 33 | 33 | } |
| 34 | 34 | .widgetURL(AppLink.agenda.url) |
| 35 | 35 | .containerBackground(for: .widget) { |
| 36 | #if os(macOS) | |
| 37 | entry.snapshot?.color("agenda-background", scheme) ?? Color(nsColor: .windowBackgroundColor) | |
| 38 | #else | |
| 36 | 39 | entry.snapshot?.color("agenda-background", scheme) ?? Color(uiColor: .systemBackground) |
| 40 | #endif | |
| 37 | 41 | } |
| 38 | 42 | } |
| 39 | 43 | |
Sources/OrgstarWidgets/UpNextWidget.swift +11 −1
| @@ -3,7 +3,7 @@ import SwiftUI | ||
| 3 | 3 | import WidgetKit |
| 4 | 4 | |
| 5 | 5 | /// The agenda on the Lock Screen: the next entry (rectangular), the next entry or the count |
| 6 | /// (inline), today's open entries (circular). | |
| 6 | /// (inline), today's open entries (circular). On the Mac, the next entry in a small widget. | |
| 7 | 7 | struct UpNextWidget: Widget { |
| 8 | 8 | var body: some WidgetConfiguration { |
| 9 | 9 | StaticConfiguration(kind: "sh.krz.orgstar.up-next", provider: AgendaProvider()) { entry in |
| @@ -11,7 +11,11 @@ struct UpNextWidget: Widget { | ||
| 11 | 11 | } |
| 12 | 12 | .configurationDisplayName("Up Next") |
| 13 | 13 | .description("The next agenda entry, or how many are open today.") |
| 14 | #if os(macOS) | |
| 15 | .supportedFamilies([.systemSmall]) | |
| 16 | #else | |
| 14 | 17 | .supportedFamilies([.accessoryRectangular, .accessoryInline, .accessoryCircular]) |
| 18 | #endif | |
| 15 | 19 | } |
| 16 | 20 | } |
| 17 | 21 | |
| @@ -25,7 +29,11 @@ struct UpNextView: View { | ||
| 25 | 29 | var body: some View { |
| 26 | 30 | content |
| 27 | 31 | .widgetURL((next?.link ?? .agenda).url) |
| 32 | #if os(macOS) | |
| 33 | .containerBackground(for: .widget) { Color(nsColor: .windowBackgroundColor) } | |
| 34 | #else | |
| 28 | 35 | .containerBackground(for: .widget) { Color.clear } |
| 36 | #endif | |
| 29 | 37 | } |
| 30 | 38 | |
| 31 | 39 | @ViewBuilder |
| @@ -64,6 +72,7 @@ struct UpNextView: View { | ||
| 64 | 72 | } |
| 65 | 73 | } |
| 66 | 74 | |
| 75 | #if os(iOS) | |
| 67 | 76 | #Preview("Rectangular", as: .accessoryRectangular) { |
| 68 | 77 | UpNextWidget() |
| 69 | 78 | } timeline: { |
| @@ -81,3 +90,4 @@ struct UpNextView: View { | ||
| 81 | 90 | } timeline: { |
| 82 | 91 | AgendaEntry(date: .now, snapshot: .sample(.now)) |
| 83 | 92 | } |
| 93 | #endif | |
Tests/OrgAppTests/WidgetDataTests.swift +13
| @@ -90,3 +90,16 @@ struct PropertyInheritanceTests { | ||
| 90 | 90 | #expect(SemanticSettings.PropertyInheritance("style Team") == .only(["STYLE", "TEAM"])) |
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | ||
| 94 | #if os(macOS) | |
| 95 | struct MacWidgetFolderTests { | |
| 96 | /// The account's home, not a sandbox container's, so that the widget extension's sandbox | |
| 97 | /// exception names the same folder. | |
| 98 | @Test func widgetDataIsInApplicationSupport() throws { | |
| 99 | let agenda = try #require(AgendaSnapshot.file()) | |
| 100 | #expect(agenda.path.hasSuffix("/Library/Application Support/Orgstar/Widgets/agenda.json")) | |
| 101 | #expect(!agenda.path.contains("/Containers/")) | |
| 102 | #expect(HabitSnapshot.file()?.deletingLastPathComponent() == agenda.deletingLastPathComponent()) | |
| 103 | } | |
| 104 | } | |
| 105 | #endif | |
docs/manual/guide/07-agenda.org +13 −1
| @@ -224,7 +224,7 @@ On the Mac, double-click an event, or select it and press =RET=, to open it in C | ||
| 224 | 224 | |
| 225 | 225 | Both use the access granted for showing events. Read-only calendars, such as subscriptions and holidays, can't be chosen. |
| 226 | 226 | |
| 227 | * Reminders | |
| 227 | * Reminders from the Reminders app | |
| 228 | 228 | |
| 229 | 229 | The agenda can also show reminders from the Reminders app (EventKit), read-only. It is off by default. To turn it on: |
| 230 | 230 | |
| @@ -689,6 +689,18 @@ Choose Board from the Views menu in the Agenda tab. The board opens over the age | ||
| 689 | 689 | |
| 690 | 690 | The board is not available on iOS. |
| 691 | 691 | |
| 692 | * Desktop widgets | |
| 693 | ||
| 694 | On the Mac, orgstar's widgets go on the desktop and in Notification Center: right-click the desktop, choose Edit Widgets…, and pick orgstar in the gallery. They appear there once orgstar has run. | |
| 695 | ||
| 696 | | Widget | Sizes | Shows | | |
| 697 | |--------+-------------------------+-----------------------------------------------------------------------------------------| | |
| 698 | | Today | Small, medium and large | The date, the open count, and what's left of today, as on iOS. | | |
| 699 | | Up Next | Small | The next entry's time or status and title, or the agenda when nothing is left today. | | |
| 700 | | Habits | Small, medium and large | Each habit's consistency graph, as on iOS. | | |
| 701 | ||
| 702 | Click a row in Today, the Up Next widget or a habit to show that entry in orgstar; click elsewhere to open the agenda. The widgets read the next seven days of the agenda and the habits, which the app writes to =~/Library/Application Support/Orgstar/Widgets= when files, edits or settings change and every hour while it runs; see [[file:14-ios.org][iOS]] for what they show and leave out. | |
| 703 | ||
| 692 | 704 | * The agenda on iOS |
| 693 | 705 | |
| 694 | 706 | The Agenda tab shows the same views from the same files, using the settings in the synced =config.toml= and the views in =views.toml= (see [[file:14-ios.org][iOS]]). Differences from the Mac: |
scripts/build-app.sh +12 −5
| @@ -27,6 +27,8 @@ swift build -c release $arch_flags --product OrgstarQuickLook | ||
| 27 | 27 | # shellcheck disable=SC2086 |
| 28 | 28 | swift build -c release $arch_flags --product OrgstarSpotlight |
| 29 | 29 | # shellcheck disable=SC2086 |
| 30 | swift build -c release $arch_flags --product OrgstarWidgets | |
| 31 | # shellcheck disable=SC2086 | |
| 30 | 32 | binary="$(swift build -c release $arch_flags --show-bin-path)/Orgstar" |
| 31 | 33 | |
| 32 | 34 | rm -rf "$app" |
| @@ -119,7 +121,7 @@ cat > "$app/Contents/Info.plist" <<EOF | ||
| 119 | 121 | <key>CFBundleURLName</key> |
| 120 | 122 | <string>org-protocol</string> |
| 121 | 123 | <key>CFBundleURLSchemes</key> |
| 122 | <array><string>org-protocol</string></array> | |
| 124 | <array><string>org-protocol</string><string>orgstar</string></array> | |
| 123 | 125 | </dict> |
| 124 | 126 | </array> |
| 125 | 127 | <key>CFBundleShortVersionString</key> |
| @@ -156,8 +158,9 @@ echo "$PWD/$objects/Orgstar-primary.swiftconstvalues" > "$work/constvalues.txt" | ||
| 156 | 158 | --swift-const-vals-list "$work/constvalues.txt" --output "$app/Contents/Resources" --force > /dev/null 2>&1 |
| 157 | 159 | rm -rf "$work" |
| 158 | 160 | |
| 159 | # App extensions: Quick Look previews and the Spotlight importer. They run sandboxed. | |
| 160 | appex() { # name, extension point, principal class, attributes | |
| 161 | # App extensions: Quick Look previews, the Spotlight importer and the widgets. They run | |
| 162 | # sandboxed. | |
| 163 | appex() { # name, extension point, principal class (or ""), attributes, extra entitlements | |
| 161 | 164 | dir="$app/Contents/PlugIns/$1.appex/Contents" |
| 162 | 165 | mkdir -p "$dir/MacOS" |
| 163 | 166 | cp "$(dirname "$binary")/$1" "$dir/MacOS/$1" |
| @@ -184,8 +187,7 @@ appex() { # name, extension point, principal class, attributes | ||
| 184 | 187 | <dict> |
| 185 | 188 | <key>NSExtensionPointIdentifier</key> |
| 186 | 189 | <string>$2</string> |
| 187 | <key>NSExtensionPrincipalClass</key> | |
| 188 | <string>$3</string> | |
| 190 | $([ -n "$3" ] && printf ' <key>NSExtensionPrincipalClass</key>\n <string>%s</string>\n' "$3") | |
| 189 | 191 | <key>NSExtensionAttributes</key> |
| 190 | 192 | <dict>$4</dict> |
| 191 | 193 | </dict> |
| @@ -199,6 +201,7 @@ PLIST | ||
| 199 | 201 | <dict> |
| 200 | 202 | <key>com.apple.security.app-sandbox</key> |
| 201 | 203 | <true/> |
| 204 | ${5:-} | |
| 202 | 205 | </dict> |
| 203 | 206 | </plist> |
| 204 | 207 | PLIST |
| @@ -210,6 +213,10 @@ appex OrgstarQuickLook com.apple.quicklook.preview PreviewProvider \ | ||
| 210 | 213 | "<key>QLSupportedContentTypes</key><array><string>org.orgmode.org</string></array><key>QLIsDataBasedPreview</key><true/>" |
| 211 | 214 | appex OrgstarSpotlight com.apple.spotlight.import ImportExtension \ |
| 212 | 215 | "<key>CSSupportedContentTypes</key><array><string>org.orgmode.org</string></array>" |
| 216 | # The widgets read what the app writes in ~/Library/Application Support/Orgstar/Widgets. | |
| 217 | appex OrgstarWidgets com.apple.widgetkit-extension "" "" \ | |
| 218 | " <key>com.apple.security.temporary-exception.files.home-relative-path.read-only</key> | |
| 219 | <array><string>/Library/Application Support/Orgstar/Widgets/</string></array>" | |
| 213 | 220 | |
| 214 | 221 | # Sparkle's inner pieces are signed first, as its documentation has it, then the framework. |
| 215 | 222 | fw="$app/Contents/Frameworks/Sparkle.framework/Versions/B" |