| 1 | #if os(macOS) |
| 2 | import RastrilloApple |
| 3 | import SwiftUI |
| 4 | |
| 5 | /// The Mac's window (B, mockup `mac-main-b`): the sky, a floating glass |
| 6 | /// sidebar with the apps, and Ask in the middle as a sheet. The sidebar |
| 7 | /// toggle sits in Ask's bar. |
| 8 | public struct MacMainView: View { |
| 9 | let me: Me? |
| 10 | let home: HomeData? |
| 11 | let ask: AskModel? |
| 12 | let transport: NativeTransport? |
| 13 | let actions: BaseActions |
| 14 | @Binding var sidebarShown: Bool |
| 15 | @Environment(\.colorScheme) private var scheme |
| 16 | |
| 17 | public init(me: Me?, home: HomeData?, ask: AskModel?, transport: NativeTransport?, actions: BaseActions, sidebarShown: Binding<Bool>) { |
| 18 | self.me = me; self.home = home; self.ask = ask; self.transport = transport; self.actions = actions; _sidebarShown = sidebarShown |
| 19 | } |
| 20 | |
| 21 | public var body: some View { |
| 22 | ZStack { |
| 23 | Sky() |
| 24 | HStack(spacing: 10) { |
| 25 | if sidebarShown { |
| 26 | MacSidebar(me: me, home: home, waiting: ask?.waitingJobs ?? 0, transport: transport, actions: actions) |
| 27 | .frame(width: 236) |
| 28 | .transition(.move(edge: .leading).combined(with: .opacity)) |
| 29 | } |
| 30 | pane |
| 31 | } |
| 32 | .padding(10) |
| 33 | } |
| 34 | .animation(.easeOut(duration: 0.2), value: sidebarShown) |
| 35 | } |
| 36 | |
| 37 | private var pane: some View { |
| 38 | let p = Palette(scheme) |
| 39 | return VStack(spacing: 0) { |
| 40 | HStack(spacing: 10) { |
| 41 | if !sidebarShown { Color.clear.frame(width: 64, height: 1) } // the window's buttons |
| 42 | HStack(spacing: 9) { |
| 43 | BaseMark(size: 16) |
| 44 | Text(BaseText.text("native.sidebar.ask")).font(BaseFont.heading(17, .semibold, relativeTo: .headline)) |
| 45 | } |
| 46 | .accessibilityElement(children: .combine) |
| 47 | .accessibilityAddTraits(.isHeader) |
| 48 | Spacer() |
| 49 | Button { sidebarShown.toggle() } label: { |
| 50 | Image(systemName: "sidebar.left").font(.system(size: 14, weight: .medium)) |
| 51 | .frame(width: 32, height: 32) |
| 52 | .background(p[.chip], in: Circle()) |
| 53 | .overlay { Circle().strokeBorder(p[.chipEdge], lineWidth: 1) } |
| 54 | .contentShape(Circle()) |
| 55 | } |
| 56 | .buttonStyle(.plain) |
| 57 | .help(Text(BaseText.text(sidebarShown ? "native.sidebar.toggle_a11y" : "native.sidebar.toggle_a11y.show"))) |
| 58 | .accessibilityLabel(Text(BaseText.text(sidebarShown ? "native.sidebar.toggle_a11y" : "native.sidebar.toggle_a11y.show"))) |
| 59 | .keyboardShortcut("s", modifiers: [.command, .control]) |
| 60 | } |
| 61 | .padding(.leading, 20).padding(.trailing, 12) |
| 62 | .frame(height: 52) |
| 63 | .overlay(alignment: .bottom) { Rectangle().fill(p[.rule]).frame(height: 1).padding(.horizontal, 12) } |
| 64 | if let ask { |
| 65 | AskView(model: ask, layout: .window, timeZone: me?.base.timeZone, actions: AskActions(setUp: actions.setUpAsk, setUpOnWeb: actions.setUpAskOnWeb)) |
| 66 | .frame(maxWidth: 760) |
| 67 | .frame(maxWidth: .infinity) |
| 68 | } else { |
| 69 | Spacer() |
| 70 | } |
| 71 | } |
| 72 | .foregroundStyle(p[.frost]) |
| 73 | .glassSheet(cornerRadius: 18) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// The floating sidebar: Ask Base (with its badge), Getting started while |
| 78 | /// a step is open, the apps, the team's Store and People, and the person |
| 79 | /// and team at its foot with the account's menu. |
| 80 | struct MacSidebar: View { |
| 81 | let me: Me? |
| 82 | let home: HomeData? |
| 83 | let waiting: Int |
| 84 | let transport: NativeTransport? |
| 85 | let actions: BaseActions |
| 86 | @State private var switching = false |
| 87 | @State private var confirmingSignOut = false |
| 88 | @Environment(\.colorScheme) private var scheme |
| 89 | |
| 90 | var body: some View { |
| 91 | let p = Palette(scheme) |
| 92 | VStack(alignment: .leading, spacing: 0) { |
| 93 | Color.clear.frame(height: 34) // the window's buttons sit here |
| 94 | ScrollView { |
| 95 | VStack(alignment: .leading, spacing: 2) { |
| 96 | askRow(p) |
| 97 | if let home, home.stepsOpen { steps(home, p) } |
| 98 | if let home, !home.launchable.isEmpty { |
| 99 | heading(BaseText.text("native.sidebar.apps"), p) |
| 100 | ForEach(home.launchable) { app in appRow(app, p) } |
| 101 | } |
| 102 | if let home, !home.own.isEmpty { |
| 103 | heading(BaseText.format("native.sidebar.team", ["team": me?.base.name ?? ""]), p) |
| 104 | ForEach(home.own.filter { $0.id != "settings" }) { app in appRow(app, p) } |
| 105 | } |
| 106 | } |
| 107 | .padding(.horizontal, 10) |
| 108 | } |
| 109 | .scrollIndicators(.never) |
| 110 | foot(p) |
| 111 | } |
| 112 | .foregroundStyle(p[.frost]) |
| 113 | .glass(cornerRadius: 18) |
| 114 | .accountChoices(actions: actions, switching: $switching, confirmingSignOut: $confirmingSignOut) |
| 115 | } |
| 116 | |
| 117 | private func askRow(_ p: Palette) -> some View { |
| 118 | HStack(spacing: 10) { |
| 119 | BaseMark(size: 16).frame(width: 22, height: 22) |
| 120 | Text(BaseText.text("native.sidebar.ask")).font(BaseFont.body(14, .semibold)) |
| 121 | Spacer(minLength: 4) |
| 122 | if waiting > 0 { |
| 123 | Text(verbatim: "\(waiting)") |
| 124 | .font(BaseFont.body(11.5, .bold, relativeTo: .caption)) |
| 125 | .foregroundStyle(p[.onSignal]) |
| 126 | .padding(.horizontal, 6).frame(minWidth: 20, minHeight: 20) |
| 127 | .background(p[.stAsk], in: Capsule()) |
| 128 | .accessibilityLabel(Text(BaseText.plural("native.sidebar.ask.badge_a11y", waiting))) |
| 129 | } |
| 130 | } |
| 131 | .padding(.horizontal, 10).frame(minHeight: 32) |
| 132 | .background(p[.hover], in: RoundedRectangle(cornerRadius: 9, style: .continuous)) |
| 133 | .overlay { RoundedRectangle(cornerRadius: 9, style: .continuous).strokeBorder(p[.accentEdge], lineWidth: 1) } |
| 134 | .accessibilityElement(children: .combine) |
| 135 | .accessibilityAddTraits(.isSelected) |
| 136 | } |
| 137 | |
| 138 | private func steps(_ home: HomeData, _ p: Palette) -> some View { |
| 139 | VStack(alignment: .leading, spacing: 5) { |
| 140 | Text(BaseText.text("native.sidebar.steps")).font(BaseFont.body(13, .semibold)).padding(.bottom, 1) |
| 141 | .accessibilityAddTraits(.isHeader) |
| 142 | ForEach(home.steps, id: \.self) { step in |
| 143 | HStack(spacing: 8) { |
| 144 | StepDot(done: step.done) |
| 145 | Text(verbatim: step.label).font(BaseFont.body(13)).foregroundStyle(step.done ? p[.haze] : p[.frost]) |
| 146 | } |
| 147 | .accessibilityElement(children: .combine) |
| 148 | .accessibilityAddTraits(step.done ? .isSelected : []) |
| 149 | } |
| 150 | } |
| 151 | .padding(.vertical, 10).padding(.horizontal, 12) |
| 152 | .frame(maxWidth: .infinity, alignment: .leading) |
| 153 | .background(p[.chip], in: RoundedRectangle(cornerRadius: 12, style: .continuous)) |
| 154 | .overlay { RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(p[.chipEdge], lineWidth: 1) } |
| 155 | .padding(.top, 6).padding(.horizontal, 2) |
| 156 | } |
| 157 | |
| 158 | private func heading(_ text: String, _ p: Palette) -> some View { |
| 159 | Text(verbatim: text).font(BaseFont.body(11.5, .semibold, relativeTo: .caption)).foregroundStyle(p[.haze]) |
| 160 | .padding(.top, 14).padding(.bottom, 4).padding(.horizontal, 10) |
| 161 | .accessibilityAddTraits(.isHeader) |
| 162 | } |
| 163 | |
| 164 | private func appRow(_ app: HomeData.App, _ p: Palette) -> some View { |
| 165 | Button { actions.open(app) } label: { |
| 166 | HStack(spacing: 10) { |
| 167 | AppIcon(app, size: 22, transport: transport) |
| 168 | Text(verbatim: app.name).font(BaseFont.body(14)) |
| 169 | Spacer(minLength: 0) |
| 170 | } |
| 171 | .padding(.horizontal, 10).frame(minHeight: 32) |
| 172 | .contentShape(Rectangle()) |
| 173 | } |
| 174 | .buttonStyle(SidebarRowStyle()) |
| 175 | } |
| 176 | |
| 177 | private func foot(_ p: Palette) -> some View { |
| 178 | Menu { |
| 179 | AccountMenuItems(actions: actions, switching: $switching, confirmingSignOut: $confirmingSignOut) |
| 180 | } label: { |
| 181 | HStack(spacing: 10) { |
| 182 | Avatar(me?.member.initials ?? "", size: 30) |
| 183 | VStack(alignment: .leading, spacing: 0) { |
| 184 | Text(verbatim: me?.member.name ?? "").font(BaseFont.body(13.5, .medium)) |
| 185 | Text(verbatim: me?.base.name ?? "").font(BaseFont.body(12, relativeTo: .caption)).foregroundStyle(p[.haze]) |
| 186 | } |
| 187 | Spacer(minLength: 0) |
| 188 | } |
| 189 | .contentShape(Rectangle()) |
| 190 | } |
| 191 | .menuStyle(.button) |
| 192 | .buttonStyle(.plain) |
| 193 | .menuIndicator(.hidden) |
| 194 | .padding(.top, 10).padding(.leading, 8).padding(.horizontal, 10).padding(.bottom, 10) |
| 195 | .overlay(alignment: .top) { Rectangle().fill(p[.rule]).frame(height: 1).padding(.horizontal, 10) } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// A sidebar row: the hover's tint while pressed or pointed at. |
| 200 | struct SidebarRowStyle: ButtonStyle { |
| 201 | @State private var hovering = false |
| 202 | @Environment(\.colorScheme) private var scheme |
| 203 | func makeBody(configuration: Configuration) -> some View { |
| 204 | configuration.label |
| 205 | .foregroundStyle(Palette(scheme)[.frost]) |
| 206 | .background((configuration.isPressed || hovering) ? Palette(scheme)[.hover] : .clear, in: RoundedRectangle(cornerRadius: 9, style: .continuous)) |
| 207 | .onHover { hovering = $0 } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | #endif |
| 212 | |