Repository Public MIT
1// ORIX Calc2// File: App/Sources/ModeNavigation.swift3// Description: Environment switchers — the compact mode strip (iPhone) and the sidebar (iPad), with favourites and recents.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import ORIXUI910/// One-tap switching between the eight environments. The active one expands to show its name.11struct ModeStrip: View {12 @Environment(AppModel.self) private var app13 @Binding var showHistory: Bool14 @Binding var showSettings: Bool15 @Binding var showCamera: Bool16 @Namespace private var selection1718 var body: some View {19 HStack(spacing: 6) {20 ScrollViewReader { proxy in21 ScrollView(.horizontal, showsIndicators: false) {22 HStack(spacing: 2) {23 ForEach(app.settings.orderedModes) { mode in24 ModeChip(mode: mode, selected: mode == app.mode, namespace: selection) {25 Haptics.selection()26 withAnimation(ORIXMotion.mode) { app.mode = mode }27 }28 .id(mode)29 .contextMenu {30 Button {31 app.settings.toggleFavorite(mode)32 } label: {33 Label(app.settings.data.favorites.contains(mode) ? "Remove from Favorites" : "Add to Favorites",34 systemImage: app.settings.data.favorites.contains(mode) ? "star.slash" : "star")35 }36 Button { app.settings.data.defaultMode = mode } label: { Label("Open at Launch", systemImage: "arrow.down.to.line") }37 }38 }39 }40 .padding(4)41 }42 .onChange(of: app.mode) { _, new in withAnimation { proxy.scrollTo(new, anchor: .center) } }43 .onAppear { proxy.scrollTo(app.mode, anchor: .center) }44 }45 .background(ORIXColor.surface, in: Capsule())46 .overlay(Capsule().strokeBorder(ORIXColor.hairline.opacity(0.6), lineWidth: 0.5))4748 Button { showHistory = true } label: { Image(systemName: "clock.arrow.circlepath").frame(width: 34, height: 40) }49 .accessibilityLabel("History")50 .accessibilityIdentifier("shell.history")51 Menu {52 Button { showCamera = true } label: { Label("Math Camera (Beta)", systemImage: "camera.viewfinder") }53 Button { showSettings = true } label: { Label("Settings", systemImage: "gearshape") }54 } label: { Image(systemName: "ellipsis.circle").frame(width: 34, height: 40) }55 .accessibilityLabel("More")56 .accessibilityIdentifier("shell.more")57 }58 .font(.title3)59 .foregroundStyle(ORIXColor.textSecondary)60 .padding(.horizontal, ORIXSpacing.m)61 .padding(.vertical, 6)62 }63}6465private struct ModeChip: View {66 let mode: CalcMode67 let selected: Bool68 let namespace: Namespace.ID69 let action: () -> Void7071 var body: some View {72 Button(action: action) {73 HStack(spacing: 6) {74 Image(systemName: mode.symbol).font(.system(size: 15, weight: .semibold)).frame(width: 20)75 if selected { Text(mode.title).font(.subheadline.weight(.semibold)).lineLimit(1).fixedSize() }76 }77 .padding(.horizontal, selected ? 12 : 7)78 .frame(height: 36)79 .foregroundStyle(selected ? ORIXColor.onTint : ORIXColor.textSecondary)80 .background {81 if selected { Capsule().fill(mode.tint.gradient).matchedGeometryEffect(id: "selection", in: namespace) }82 }83 .contentShape(Capsule())84 }85 .buttonStyle(.plain)86 .accessibilityLabel(mode.title)87 .accessibilityHint(mode.tagline)88 .accessibilityAddTraits(selected ? .isSelected : [])89 .accessibilityIdentifier("mode." + mode.rawValue)90 }91}9293struct ModeSidebar: View {94 @Environment(AppModel.self) private var app95 @Binding var expanded: Bool96 @Binding var showHistory: Bool97 @Binding var showSettings: Bool98 @Binding var showCamera: Bool99 @Binding var companion: Bool100101 var body: some View {102 VStack(alignment: .leading, spacing: 0) {103 header104 ScrollView {105 VStack(alignment: .leading, spacing: 4) {106 let favorites = app.settings.data.favorites107 if !favorites.isEmpty {108 sectionTitle("Favorites")109 ForEach(favorites) { row($0) }110 sectionTitle("Environments")111 }112 ForEach(CalcMode.allCases.filter { !favorites.contains($0) }) { row($0) }113 }114 .padding(.horizontal, 10)115 }116 Divider().padding(.horizontal, 10)117 VStack(alignment: .leading, spacing: 2) {118 utility("Companion Calculator", symbol: companion ? "rectangle.split.2x1.fill" : "rectangle.split.2x1", id: "shell.companion", active: companion) { companion.toggle() }119 utility("History", symbol: "clock.arrow.circlepath", id: "shell.history") { showHistory = true }120 utility("Math Camera (Beta)", symbol: "camera.viewfinder", id: "shell.camera") { showCamera = true }121 utility("Settings", symbol: "gearshape", id: "shell.settings") { showSettings = true }122 }123 .padding(10)124 }125 .background(ORIXColor.surface.opacity(0.6))126 }127128 private var header: some View {129 HStack(spacing: ORIXSpacing.m) {130 Button {131 withAnimation(ORIXMotion.mode) { expanded.toggle() }132 } label: {133 Image("Glyph").resizable().scaledToFit().frame(width: 34, height: 34)134 }135 .buttonStyle(.plain)136 .accessibilityLabel(expanded ? "Collapse sidebar" : "Expand sidebar")137 .accessibilityIdentifier("shell.sidebarToggle")138 if expanded {139 VStack(alignment: .leading, spacing: 0) {140 Text("ORIX Calc").font(.title3.weight(.bold)).foregroundStyle(ORIXColor.textPrimary)141 Text("One calculator. Infinite possibilities.").font(.caption2).foregroundStyle(ORIXColor.textSecondary).lineLimit(1).minimumScaleFactor(0.7)142 }143 Spacer(minLength: 0)144 Button { withAnimation(ORIXMotion.mode) { expanded = false } } label: { Image(systemName: "sidebar.left") }145 .foregroundStyle(ORIXColor.textSecondary)146 .accessibilityLabel("Collapse sidebar")147 }148 }149 .padding(.horizontal, 17)150 .padding(.vertical, 14)151 }152153 @ViewBuilder private func sectionTitle(_ title: String) -> some View {154 if expanded {155 Text(title.uppercased()).font(ORIXFont.section).kerning(0.6).foregroundStyle(ORIXColor.textTertiary).padding(.horizontal, 8).padding(.top, 10)156 } else {157 Divider().padding(.vertical, 6)158 }159 }160161 private func utility(_ title: String, symbol: String, id: String, active: Bool = false, action: @escaping () -> Void) -> some View {162 Button(action: action) {163 HStack(spacing: ORIXSpacing.m) {164 Image(systemName: symbol).font(.system(size: 16, weight: .medium)).frame(width: 32, height: 32)165 .foregroundStyle(active ? app.mode.tint : ORIXColor.textSecondary)166 if expanded { Text(title).font(.callout).foregroundStyle(ORIXColor.textPrimary).lineLimit(1); Spacer(minLength: 0) }167 }168 .padding(.horizontal, 8).padding(.vertical, 3)169 .contentShape(Rectangle())170 }171 .buttonStyle(.plain)172 .accessibilityLabel(title)173 .accessibilityAddTraits(active ? .isSelected : [])174 .accessibilityIdentifier(id)175 }176177 private func row(_ mode: CalcMode) -> some View {178 let selected = mode == app.mode179 return Button {180 Haptics.selection()181 withAnimation(ORIXMotion.mode) { app.mode = mode }182 } label: {183 HStack(spacing: ORIXSpacing.m) {184 Image(systemName: mode.symbol)185 .font(.system(size: 15, weight: .semibold))186 .frame(width: 32, height: 32)187 .foregroundStyle(selected ? ORIXColor.onTint : mode.tint)188 .background(selected ? AnyShapeStyle(mode.tint.gradient) : AnyShapeStyle(mode.tint.opacity(0.14)), in: RoundedRectangle(cornerRadius: 9, style: .continuous))189 if expanded {190 VStack(alignment: .leading, spacing: 1) {191 Text(mode.title).font(.body.weight(selected ? .semibold : .regular)).foregroundStyle(ORIXColor.textPrimary)192 Text(mode.tagline).font(.caption).foregroundStyle(ORIXColor.textSecondary).lineLimit(1)193 }194 Spacer(minLength: 0)195 Text("⌘\(mode.shortcutNumber)").font(.caption2.monospaced()).foregroundStyle(ORIXColor.textTertiary).accessibilityHidden(true)196 }197 }198 .padding(.horizontal, 8).padding(.vertical, 6)199 .background(selected && expanded ? mode.tint.opacity(0.12) : Color.clear, in: RoundedRectangle(cornerRadius: 12, style: .continuous))200 .contentShape(Rectangle())201 }202 .buttonStyle(.plain)203 .accessibilityLabel(mode.title)204 .accessibilityHint(mode.tagline)205 .accessibilityAddTraits(selected ? .isSelected : [])206 .accessibilityIdentifier("mode." + mode.rawValue)207 .contextMenu {208 Button { app.settings.toggleFavorite(mode) } label: {209 Label(app.settings.data.favorites.contains(mode) ? "Remove from Favorites" : "Add to Favorites", systemImage: "star")210 }211 Button { app.settings.data.defaultMode = mode } label: { Label("Open at Launch", systemImage: "arrow.down.to.line") }212 }213 }214}215