| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import RastrilloApple |
| 4 | import SwiftUI |
| 5 | |
| 6 | public struct CalendarMenuView: View { |
| 7 | public let client: NativeHTTP |
| 8 | @ObservedObject var model: CalendarModel |
| 9 | @Environment(\.openWindow) private var openWindow |
| 10 | @Environment(\.openSettings) private var openSettings |
| 11 | public init(client: NativeHTTP, model: CalendarModel) { self.client = client; self.model = model } |
| 12 | private var quickExpanded: Bool { !model.quickText.isEmpty || !model.quickSavedNotice.isEmpty } |
| 13 | private var panelHeight: CGFloat { min(quickExpanded ? 760 : 640, (NSScreen.main?.visibleFrame.height ?? 800) - 16) } |
| 14 | private var quickHeight: CGFloat { quickExpanded ? min(220, max(84, panelHeight - 530)) : 84 } |
| 15 | public var body: some View { |
| 16 | VStack(spacing: 0) { |
| 17 | HStack { |
| 18 | Text(model.calendars.first(where: { $0.id == model.selectedCalendar })?.name ?? "All calendars").font(.headline) |
| 19 | Spacer() |
| 20 | Button("Open Calendar", systemImage: "arrow.up.forward.app") { openWindow(id: "calendar"); NSApplication.shared.activate(ignoringOtherApps: true) }.labelStyle(.iconOnly) |
| 21 | Button("Settings", systemImage: "gear") { openSettings(); NSApplication.shared.activate(ignoringOtherApps: true) }.labelStyle(.iconOnly) |
| 22 | }.padding() |
| 23 | CalendarMonthView(model: model).controlSize(.small).padding(.horizontal) |
| 24 | Divider() |
| 25 | HStack { |
| 26 | Text(model.display(model.date, template: "EEEE d MMMM")).font(.headline) |
| 27 | Spacer() |
| 28 | if !model.loading && (model.error.isEmpty || model.hasLoaded) { |
| 29 | Text("\(model.dayEvents(model.date).count)").foregroundStyle(.secondary) |
| 30 | .accessibilityLabel("\(model.dayEvents(model.date).count) events") |
| 31 | } |
| 32 | }.padding(.horizontal).padding(.vertical, 10) |
| 33 | ScrollViewReader { scroll in |
| 34 | ScrollView { |
| 35 | VStack(alignment: .leading, spacing: 10) { |
| 36 | Color.clear.frame(height: 0).id("agendaTop") |
| 37 | if model.loading { ProgressView("Loading events…") } |
| 38 | if !model.error.isEmpty { |
| 39 | if model.hasLoaded { Label("Couldn’t refresh. Showing what was last loaded.", systemImage: "wifi.exclamationmark").font(.callout) } |
| 40 | Text(model.error).foregroundStyle(.secondary) |
| 41 | Button("Try again") { Task { await model.load(client) } } |
| 42 | } |
| 43 | if (model.error.isEmpty || model.hasLoaded) && model.dayEvents(model.date).isEmpty && !model.loading { |
| 44 | Text("No events on this day").foregroundStyle(.secondary) |
| 45 | } |
| 46 | ForEach(model.dayEvents(model.date), id: \.identity) { event in |
| 47 | HStack(alignment: .top, spacing: 10) { |
| 48 | RoundedRectangle(cornerRadius: 2).fill(Color.accentColor).frame(width: 3) |
| 49 | VStack(alignment: .leading, spacing: 4) { |
| 50 | Text(event.title).font(.headline).strikethrough(event.status == "cancelled") |
| 51 | Text(event.all_day ? "All day" : model.display(Date(timeIntervalSince1970: Double(event.start)), template: "jm")) |
| 52 | Text(event.calendar_name).font(.caption).foregroundStyle(.secondary) |
| 53 | if event.status == "cancelled" { Label("Cancelled", systemImage: "xmark.circle") } |
| 54 | if event.status == "tentative" { Label("Tentative", systemImage: "questionmark.circle") } |
| 55 | } |
| 56 | Spacer(minLength: 0) |
| 57 | }.fixedSize(horizontal: false, vertical: true) |
| 58 | .accessibilityElement(children: .combine) |
| 59 | Divider() |
| 60 | } |
| 61 | }.frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal) |
| 62 | }.accessibilityIdentifier("menuDayAgenda") |
| 63 | .onChange(of: model.date) { _, _ in scroll.scrollTo("agendaTop", anchor: .top) } |
| 64 | }.frame(minHeight: 100) |
| 65 | Divider().padding(.vertical, 8) |
| 66 | QuickAddView(client: client, model: model).frame(height: quickHeight).padding(.horizontal) |
| 67 | HStack { |
| 68 | Text(model.zone).font(.caption).foregroundStyle(.secondary) |
| 69 | Spacer() |
| 70 | Button("Quit Calendar") { NSApplication.shared.terminate(nil) } |
| 71 | }.padding() |
| 72 | }.frame(width: 380, height: panelHeight) |
| 73 | .onAppear { Task { await model.load(client) } } |
| 74 | .onChange(of: model.monthStart) { _, _ in Task { await model.load(client) } } |
| 75 | .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in |
| 76 | if model.zone != CalendarModel.preferredZone { Task { await model.load(client) } } |
| 77 | } |
| 78 | .onReceive(NotificationCenter.default.publisher(for: .NSSystemTimeZoneDidChange)) { _ in Task { await model.load(client) } } |
| 79 | } |
| 80 | } |
| 81 | #endif |
| 82 | |