#if os(macOS) import AppKit import RastrilloApple import SwiftUI public struct CalendarMenuView: View { public let client: NativeHTTP @ObservedObject var model: CalendarModel @Environment(\.openWindow) private var openWindow @Environment(\.openSettings) private var openSettings public init(client: NativeHTTP, model: CalendarModel) { self.client = client; self.model = model } private var quickExpanded: Bool { !model.quickText.isEmpty || !model.quickSavedNotice.isEmpty } private var panelHeight: CGFloat { min(quickExpanded ? 760 : 640, (NSScreen.main?.visibleFrame.height ?? 800) - 16) } private var quickHeight: CGFloat { quickExpanded ? min(220, max(84, panelHeight - 530)) : 84 } public var body: some View { VStack(spacing: 0) { HStack { Text(model.calendars.first(where: { $0.id == model.selectedCalendar })?.name ?? "All calendars").font(.headline) Spacer() Button("Open Calendar", systemImage: "arrow.up.forward.app") { openWindow(id: "calendar"); NSApplication.shared.activate(ignoringOtherApps: true) }.labelStyle(.iconOnly) Button("Settings", systemImage: "gear") { openSettings(); NSApplication.shared.activate(ignoringOtherApps: true) }.labelStyle(.iconOnly) }.padding() CalendarMonthView(model: model).controlSize(.small).padding(.horizontal) Divider() HStack { Text(model.display(model.date, template: "EEEE d MMMM")).font(.headline) Spacer() if !model.loading && (model.error.isEmpty || model.hasLoaded) { Text("\(model.dayEvents(model.date).count)").foregroundStyle(.secondary) .accessibilityLabel("\(model.dayEvents(model.date).count) events") } }.padding(.horizontal).padding(.vertical, 10) ScrollViewReader { scroll in ScrollView { VStack(alignment: .leading, spacing: 10) { Color.clear.frame(height: 0).id("agendaTop") if model.loading { ProgressView("Loading events…") } if !model.error.isEmpty { if model.hasLoaded { Label("Couldn’t refresh. Showing what was last loaded.", systemImage: "wifi.exclamationmark").font(.callout) } Text(model.error).foregroundStyle(.secondary) Button("Try again") { Task { await model.load(client) } } } if (model.error.isEmpty || model.hasLoaded) && model.dayEvents(model.date).isEmpty && !model.loading { Text("No events on this day").foregroundStyle(.secondary) } ForEach(model.dayEvents(model.date), id: \.identity) { event in HStack(alignment: .top, spacing: 10) { RoundedRectangle(cornerRadius: 2).fill(Color.accentColor).frame(width: 3) VStack(alignment: .leading, spacing: 4) { Text(event.title).font(.headline).strikethrough(event.status == "cancelled") Text(event.all_day ? "All day" : model.display(Date(timeIntervalSince1970: Double(event.start)), template: "jm")) Text(event.calendar_name).font(.caption).foregroundStyle(.secondary) if event.status == "cancelled" { Label("Cancelled", systemImage: "xmark.circle") } if event.status == "tentative" { Label("Tentative", systemImage: "questionmark.circle") } } Spacer(minLength: 0) }.fixedSize(horizontal: false, vertical: true) .accessibilityElement(children: .combine) Divider() } }.frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal) }.accessibilityIdentifier("menuDayAgenda") .onChange(of: model.date) { _, _ in scroll.scrollTo("agendaTop", anchor: .top) } }.frame(minHeight: 100) Divider().padding(.vertical, 8) QuickAddView(client: client, model: model).frame(height: quickHeight).padding(.horizontal) HStack { Text(model.zone).font(.caption).foregroundStyle(.secondary) Spacer() Button("Quit Calendar") { NSApplication.shared.terminate(nil) } }.padding() }.frame(width: 380, height: panelHeight) .onAppear { Task { await model.load(client) } } .onChange(of: model.monthStart) { _, _ in Task { await model.load(client) } } .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in if model.zone != CalendarModel.preferredZone { Task { await model.load(client) } } } .onReceive(NotificationCenter.default.publisher(for: .NSSystemTimeZoneDidChange)) { _ in Task { await model.load(client) } } } } #endif