Graphley / app · graphley.ai / Apps / Mac / ContentView.swift
| 1 | import SwiftUI |
| 2 | import UniformTypeIdentifiers |
| 3 | import AnalyticsKit |
| 4 | import AnalyticsUI |
| 5 | |
| 6 | enum SidebarItem: Hashable { case overview, apps, dashboard(String), agents, sources, settings, site(String) } |
| 7 | |
| 8 | /// The main window: our own sidebar rows inside the native sidebar, and one screen on the right. |
| 9 | /// Layout and copy follow design/DESIGN.md. |
| 10 | struct ContentView: View { |
| 11 | @Environment(AppModel.self) private var model |
| 12 | |
| 13 | var body: some View { |
| 14 | NavigationSplitView { |
| 15 | Sidebar() |
| 16 | .navigationSplitViewColumnWidth(min: 200, ideal: 220) |
| 17 | } detail: { |
| 18 | ScrollView { |
| 19 | screen |
| 20 | .padding(24) |
| 21 | .frame(maxWidth: narrow ? 728 : .infinity, alignment: .leading) |
| 22 | .frame(maxWidth: .infinity, alignment: .leading) |
| 23 | .background(OverlayScrollers()) |
| 24 | } |
| 25 | .background(Tide.surface) |
| 26 | .navigationTitle(title) |
| 27 | .modifier(TopBar(showsRange: showsRange)) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | @ViewBuilder var screen: some View { |
| 32 | switch model.selection { |
| 33 | case .overview: OverviewView() |
| 34 | case .apps: AppStoreView() |
| 35 | case .dashboard(let id): DashboardView(dashboardID: id) |
| 36 | case .agents: AgentsView() |
| 37 | case .sources: SourcesView() |
| 38 | case .settings: SettingsView() |
| 39 | case .site(let id): SiteView(siteID: id) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Forms read best at a fixed width; data screens use the whole window. |
| 44 | var narrow: Bool { [.sources, .settings].contains(model.selection) } |
| 45 | |
| 46 | /// The range only changes Overview, App Store and site screens (dashboard widgets have their own days). |
| 47 | var showsRange: Bool { |
| 48 | switch model.selection { |
| 49 | case .overview, .apps, .site: true |
| 50 | default: false |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | var title: String { |
| 55 | switch model.selection { |
| 56 | case .overview: "Overview" |
| 57 | case .apps: "Apps" |
| 58 | case .dashboard(let id): model.workspace.dashboards.first { $0.id == id }?.name ?? "Dashboard" |
| 59 | case .agents: "Agents" |
| 60 | case .sources: "Sources & keys" |
| 61 | case .settings: "Settings" |
| 62 | case .site(let id): model.sites.first { $0.id == id }?.name ?? "Site" |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // MARK: Top bar |
| 68 | |
| 69 | /// One calm top bar (design/DESIGN.md 2.3): the range pill and the refresh pill, both 30 pt, |
| 70 | /// 12 pt apart, ending in line with the cards below. Our own controls, with the system glass off. |
| 71 | /// Without the title, toolbar items flow from the leading edge, so a flexible spacer pushes ours right. |
| 72 | struct TopBar: ViewModifier { |
| 73 | let showsRange: Bool |
| 74 | func body(content: Content) -> some View { |
| 75 | if #available(macOS 26, *) { |
| 76 | content.toolbar(removing: .title).toolbar { |
| 77 | ToolbarSpacer(.flexible) |
| 78 | ToolbarItem { TopBarCluster(showsRange: showsRange) } |
| 79 | .sharedBackgroundVisibility(.hidden) |
| 80 | } |
| 81 | } else { |
| 82 | content.toolbar { ToolbarItem { TopBarCluster(showsRange: showsRange) } } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | struct TopBarCluster: View { |
| 88 | let showsRange: Bool |
| 89 | @Environment(AppModel.self) private var model |
| 90 | /// Extra space after the refresh pill so it ends 24 pt from the window edge, like the cards. |
| 91 | static let trailingNudge: CGFloat = 20 |
| 92 | |
| 93 | var body: some View { |
| 94 | @Bindable var model = model |
| 95 | HStack(spacing: 12) { |
| 96 | if showsRange { RangeSelector(selection: $model.range) } |
| 97 | RefreshPill(status: status, refresh: { Task { await model.refresh() } }, openSources: { model.selection = .sources }) |
| 98 | } |
| 99 | .padding(.trailing, Self.trailingNudge) |
| 100 | .transaction { $0.animation = nil } |
| 101 | } |
| 102 | |
| 103 | var status: RefreshPillStatus { |
| 104 | if model.refreshing { return .refreshing } |
| 105 | if !model.failures.isEmpty { return .failed(model.failures.count) } |
| 106 | guard let at = model.workspace.settings.lastRefreshAt else { |
| 107 | // Numbers can arrive without an in-app refresh (an agent, an older version), so say how fresh they are. |
| 108 | guard let day = model.points.map(\.day).max(), let date = ISO8601DateFormatter.dayOnly.date(from: day) else { return .never } |
| 109 | return .dataThrough(date.formatted(.dateTime.month(.abbreviated).day())) |
| 110 | } |
| 111 | let when = RefreshTime.describe(at) |
| 112 | return Date.now.timeIntervalSince(at) > 26 * 3600 ? .stale(when) : .fresh(when) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Keeps the detail page's scroll bar in overlay style even when a mouse makes macOS show it always, |
| 117 | /// so the content column (and the top bar lined up with it) is the same width on every screen. |
| 118 | struct OverlayScrollers: NSViewRepresentable { |
| 119 | func makeNSView(context: Context) -> Probe { Probe() } |
| 120 | func updateNSView(_ view: Probe, context: Context) { view.apply() } |
| 121 | |
| 122 | final class Probe: NSView { |
| 123 | private var observing = false |
| 124 | override func viewDidMoveToWindow() { |
| 125 | super.viewDidMoveToWindow() |
| 126 | apply() |
| 127 | guard !observing else { return } |
| 128 | observing = true |
| 129 | NotificationCenter.default.addObserver(self, selector: #selector(styleChanged), |
| 130 | name: NSScroller.preferredScrollerStyleDidChangeNotification, object: nil) |
| 131 | } |
| 132 | @objc private func styleChanged() { apply() } |
| 133 | func apply() { |
| 134 | // Set it after the system's own update from the same notification. |
| 135 | DispatchQueue.main.async { [weak self] in self?.enclosingScrollView?.scrollerStyle = .overlay } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | enum RefreshTime { |
| 141 | static func describe(_ date: Date) -> String { |
| 142 | let cal = Calendar.current |
| 143 | if cal.isDateInToday(date) { return date.formatted(date: .omitted, time: .shortened) } |
| 144 | if cal.isDateInYesterday(date) { return "yesterday" } |
| 145 | let days = cal.dateComponents([.day], from: date, to: .now).day ?? 0 |
| 146 | return days < 7 ? "\(days) days ago" : date.formatted(.dateTime.month(.abbreviated).day()) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // MARK: Sidebar |
| 151 | |
| 152 | struct Sidebar: View { |
| 153 | @Environment(AppModel.self) private var model |
| 154 | /// The site being dragged to a new place in the list. |
| 155 | @State private var dragging: String? |
| 156 | |
| 157 | var body: some View { |
| 158 | VStack(spacing: 0) { |
| 159 | ScrollView { |
| 160 | VStack(alignment: .leading, spacing: 2) { |
| 161 | SectionLabel("Portfolio") |
| 162 | SidebarRow("Overview", icon: "chart.xyaxis.line", item: .overview) |
| 163 | SidebarRow("Apps", icon: "arrow.down.app", item: .apps) |
| 164 | ForEach(model.workspace.dashboards) { d in SidebarRow(d.name, icon: "rectangle.3.group", item: .dashboard(d.id)) } |
| 165 | SidebarRow("Agents", icon: "terminal", item: .agents) |
| 166 | SidebarRow("Sources & keys", icon: "key.horizontal", item: .sources) |
| 167 | |
| 168 | SectionLabel("Sites") |
| 169 | if model.sites.isEmpty { |
| 170 | VStack(alignment: .leading, spacing: 6) { |
| 171 | Text("No sites yet").font(Tide.secondary).foregroundStyle(Tide.muted) |
| 172 | Button("Add Sites…") { model.selection = .sources; model.wantsSitePicker = model.googleConnected } |
| 173 | .buttonStyle(.tide(.quiet, size: .small)) |
| 174 | } |
| 175 | .padding(.horizontal, 10) |
| 176 | } |
| 177 | ForEach(model.sites) { site in |
| 178 | SidebarRow(site.name, dot: Tide.color(for: site), count: model.total(.users, site: site.id, days: model.range), item: .site(site.id)) |
| 179 | .opacity(dragging == site.id ? 0.35 : 1) |
| 180 | .onDrag { |
| 181 | dragging = site.id |
| 182 | return NSItemProvider(object: site.id as NSString) |
| 183 | } preview: { |
| 184 | SiteDragPreview(site: site) |
| 185 | } |
| 186 | .help("Drag to change the order. Your iPhone and iPad use the same order.") |
| 187 | .onDrop(of: [.plainText], delegate: SiteDropDelegate(target: site.id, dragging: $dragging, model: model)) |
| 188 | .contextMenu { |
| 189 | Button("Move Up") { withAnimation(.snappy(duration: 0.2)) { model.moveSite(site.id, by: -1) } } |
| 190 | .disabled(model.sites.first?.id == site.id) |
| 191 | Button("Move Down") { withAnimation(.snappy(duration: 0.2)) { model.moveSite(site.id, by: 1) } } |
| 192 | .disabled(model.sites.last?.id == site.id) |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | .padding(.horizontal, 10).padding(.bottom, 10) |
| 197 | // A drop in the gaps between rows still ends the drag cleanly. |
| 198 | .onDrop(of: [.plainText], isTargeted: nil) { _ in |
| 199 | let wasDragging = dragging != nil |
| 200 | dragging = nil |
| 201 | return wasDragging |
| 202 | } |
| 203 | } |
| 204 | .onChange(of: model.selection) { dragging = nil } |
| 205 | VStack(alignment: .leading, spacing: 6) { |
| 206 | SidebarRow("Settings", icon: "gearshape", item: .settings) |
| 207 | Label(iPhoneStatus, systemImage: "iphone").font(Tide.caption).foregroundStyle(Tide.muted) |
| 208 | .labelStyle(SmallIconLabel()).padding(.horizontal, 10) |
| 209 | } |
| 210 | .padding(.horizontal, 10).padding(.vertical, 10) |
| 211 | } |
| 212 | .focusable() |
| 213 | .focusEffectDisabled() |
| 214 | .onMoveCommand { direction in move(direction) } |
| 215 | } |
| 216 | |
| 217 | var iPhoneStatus: String { |
| 218 | let s = model.workspace.settings |
| 219 | guard s.shareWithIPhone else { return "iPhone sharing is off" } |
| 220 | guard let at = s.lastSharedAt else { return "iPhone: not shared yet" } |
| 221 | return "iPhone: updated \(RefreshTime.describe(at))" |
| 222 | } |
| 223 | |
| 224 | var order: [SidebarItem] { |
| 225 | [.overview, .apps] + model.workspace.dashboards.map { .dashboard($0.id) } + [.agents, .sources] + model.sites.map { .site($0.id) } + [.settings] |
| 226 | } |
| 227 | |
| 228 | func move(_ direction: MoveCommandDirection) { |
| 229 | guard let i = order.firstIndex(of: model.selection) else { return } |
| 230 | switch direction { |
| 231 | case .up where i > 0: model.selection = order[i - 1] |
| 232 | case .down where i < order.count - 1: model.selection = order[i + 1] |
| 233 | default: break |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /// Live reordering: as the dragged site passes over another row, it takes that row's place, so the |
| 239 | /// list shows the new order before the drop. The order is saved as it changes. |
| 240 | struct SiteDropDelegate: DropDelegate { |
| 241 | let target: String |
| 242 | @Binding var dragging: String? |
| 243 | let model: AppModel |
| 244 | |
| 245 | func validateDrop(info: DropInfo) -> Bool { dragging != nil } |
| 246 | |
| 247 | func dropEntered(info: DropInfo) { |
| 248 | guard let dragging, dragging != target else { return } |
| 249 | withAnimation(.snappy(duration: 0.2)) { model.moveSite(dragging, to: target) } |
| 250 | } |
| 251 | |
| 252 | func dropUpdated(info: DropInfo) -> DropProposal? { DropProposal(operation: .move) } |
| 253 | |
| 254 | func performDrop(info: DropInfo) -> Bool { |
| 255 | dragging = nil |
| 256 | return true |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | struct SiteDragPreview: View { |
| 261 | let site: Site |
| 262 | var body: some View { |
| 263 | HStack(spacing: 8) { |
| 264 | Circle().fill(Tide.color(for: site)).frame(width: 8, height: 8) |
| 265 | Text(site.name).font(Tide.sans(13, .medium)).foregroundStyle(Tide.ink) |
| 266 | } |
| 267 | .padding(.horizontal, 12).frame(height: 28) |
| 268 | .background(Tide.surface, in: RoundedRectangle(cornerRadius: 8)) |
| 269 | .overlay(RoundedRectangle(cornerRadius: 8).strokeBorder(Tide.line2)) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | struct SmallIconLabel: LabelStyle { |
| 274 | func makeBody(configuration: Configuration) -> some View { |
| 275 | HStack(spacing: 6) { configuration.icon.font(.system(size: 11)); configuration.title } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | struct SectionLabel: View { |
| 280 | let text: String |
| 281 | init(_ text: String) { self.text = text } |
| 282 | var body: some View { |
| 283 | Text(text.uppercased()).font(Tide.sectionLabel).tracking(0.8).foregroundStyle(Tide.muted) |
| 284 | .padding(.leading, 10).padding(.top, 14).padding(.bottom, 6) |
| 285 | .accessibilityAddTraits(.isHeader) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | struct SidebarRow: View { |
| 290 | let title: String, icon: String?, dot: Color?, count: Double?, item: SidebarItem |
| 291 | @Environment(AppModel.self) private var model |
| 292 | @Environment(\.colorScheme) private var scheme |
| 293 | @State private var hover = false |
| 294 | |
| 295 | init(_ title: String, icon: String? = nil, dot: Color? = nil, count: Double? = nil, item: SidebarItem) { |
| 296 | self.title = title; self.icon = icon; self.dot = dot; self.count = count; self.item = item |
| 297 | } |
| 298 | |
| 299 | var body: some View { |
| 300 | let selected = model.selection == item |
| 301 | Button { model.selection = item } label: { |
| 302 | HStack(spacing: 8) { |
| 303 | if let icon { |
| 304 | Image(systemName: icon).font(.system(size: 14, weight: .medium)).frame(width: 18) |
| 305 | .foregroundStyle(selected ? Tide.accentInk : Tide.muted) |
| 306 | } else if let dot { |
| 307 | Circle().fill(dot).frame(width: 8, height: 8).frame(width: 18) |
| 308 | } |
| 309 | Text(title).font(Tide.sans(13, .medium)).lineLimit(1).truncationMode(.tail) |
| 310 | .foregroundStyle(selected ? Tide.accentInk : Tide.ink) |
| 311 | Spacer(minLength: 4) |
| 312 | if let count { |
| 313 | Text(Tide.number(count)).font(Tide.figure(11.5)) |
| 314 | .foregroundStyle(selected ? Tide.accentInk.opacity(0.75) : Tide.muted) |
| 315 | } |
| 316 | } |
| 317 | .padding(.horizontal, 10) |
| 318 | .frame(height: 28) |
| 319 | .background(selected ? Tide.fill : hover ? (scheme == .dark ? Tide.surface2 : Tide.sunk) : .clear, in: RoundedRectangle(cornerRadius: 8)) |
| 320 | .contentShape(RoundedRectangle(cornerRadius: 8)) |
| 321 | } |
| 322 | .buttonStyle(.plain) |
| 323 | .onHover { hover = $0 } |
| 324 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 325 | } |
| 326 | } |