|
| 1 | +// |
| 2 | +// StatusBar.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Lukas Pistrol on 19.03.22. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +@available(macOS 12, *) |
| 11 | +public struct StatusBarView: View { |
| 12 | + |
| 13 | + @ObservedObject private var model: StatusBarModel |
| 14 | + |
| 15 | + public init() { |
| 16 | + self.model = .init() |
| 17 | + } |
| 18 | + |
| 19 | + public var body: some View { |
| 20 | + VStack(spacing: 0) { |
| 21 | + bar |
| 22 | + if model.isExpanded { |
| 23 | + terminal |
| 24 | + } |
| 25 | + } |
| 26 | + // removes weird light gray bar above when in light mode |
| 27 | + .padding(.top, -8) // (comment out to make it look normal in preview) |
| 28 | + } |
| 29 | + |
| 30 | + private var dragGesture: some Gesture { |
| 31 | + DragGesture() |
| 32 | + .onChanged { value in |
| 33 | + let newHeight = max(0, min(height - value.translation.height, 500)) |
| 34 | + if newHeight-1 > height || newHeight+1 < height { |
| 35 | + height = newHeight |
| 36 | + } |
| 37 | + model.isExpanded = height < 1 ? false : true |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private var bar: some View { |
| 42 | + ZStack { |
| 43 | + Rectangle() |
| 44 | + .foregroundStyle(.bar) |
| 45 | + HStack(spacing: 14) { |
| 46 | + HStack(spacing: 8) { |
| 47 | + labelButton(model.errorCount.formatted(), image: "xmark.octagon") |
| 48 | + labelButton(model.warningCount.formatted(), image: "exclamationmark.triangle") |
| 49 | + } |
| 50 | + branchPicker |
| 51 | + reloadButton |
| 52 | + Spacer() |
| 53 | + cursorLocationLabel |
| 54 | + indentSelector |
| 55 | + encodingSelector |
| 56 | + lineEndSelector |
| 57 | + expandButton |
| 58 | + } |
| 59 | + .padding(.horizontal, 10) |
| 60 | + } |
| 61 | + .overlay(alignment: .top) { |
| 62 | + Divider() |
| 63 | + } |
| 64 | + .frame(height: 32) |
| 65 | + .gesture(dragGesture) |
| 66 | + .onHover { hovering in |
| 67 | + if hovering { |
| 68 | + NSCursor.resizeUpDown.push() |
| 69 | + } else { |
| 70 | + NSCursor.pop() |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @State private var height: Double = 300 |
| 76 | + |
| 77 | + private var terminal: some View { |
| 78 | + Rectangle() |
| 79 | + .foregroundColor(Color(red: 0.163, green: 0.163, blue: 0.188, opacity: 1.000)) |
| 80 | + .frame(minHeight: 0, idealHeight: height, maxHeight: height) |
| 81 | + } |
| 82 | + |
| 83 | + private func labelButton(_ text: String, image: String) -> some View { |
| 84 | + Button { |
| 85 | + // show errors/warnings |
| 86 | + } label: { |
| 87 | + HStack(spacing: 4) { |
| 88 | + Image(systemName: image) |
| 89 | + .font(.headline) |
| 90 | + Text(text) |
| 91 | + } |
| 92 | + } |
| 93 | + .buttonStyle(.borderless) |
| 94 | + .foregroundStyle(.primary) |
| 95 | + .onHover { hovering in |
| 96 | + if hovering { |
| 97 | + NSCursor.pointingHand.push() |
| 98 | + } else { |
| 99 | + NSCursor.pop() |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private var branchPicker: some View { |
| 105 | + Menu(model.branches[model.selectedBranch]) { |
| 106 | + ForEach(model.branches.indices, id: \.self) { branch in |
| 107 | + Button { model.selectedBranch = branch } label: { |
| 108 | + Text(model.branches[branch]) |
| 109 | + // checkout branch |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + .menuStyle(.borderlessButton) |
| 114 | + .fixedSize() |
| 115 | + .onHover { hovering in |
| 116 | + if hovering { |
| 117 | + NSCursor.pointingHand.push() |
| 118 | + } else { |
| 119 | + NSCursor.pop() |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private var reloadButton: some View { |
| 125 | + // Temporary |
| 126 | + Button { |
| 127 | + model.isReloading = true |
| 128 | + // Just for looks for now. In future we'll call a function like |
| 129 | + // `reloadFileStatus()` here which will set/unset `reloading` |
| 130 | + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { |
| 131 | + self.model.isReloading = false |
| 132 | + } |
| 133 | + } label: { |
| 134 | + Image(systemName: "arrow.triangle.2.circlepath") |
| 135 | + .imageScale(.large) |
| 136 | + .rotationEffect(.degrees(model.isReloading ? 360 : 0)) |
| 137 | + .animation(animation, value: model.isReloading) |
| 138 | + .opacity(model.isReloading ? 1 : 0) |
| 139 | + // A bit of a hacky solution to prevent spinning counterclockwise once `reloading` changes to `false` |
| 140 | + .overlay { |
| 141 | + Image(systemName: "arrow.triangle.2.circlepath") |
| 142 | + .imageScale(.large) |
| 143 | + .opacity(model.isReloading ? 0 : 1) |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + .buttonStyle(.borderless) |
| 148 | + .foregroundStyle(.primary) |
| 149 | + .onHover { hovering in |
| 150 | + if hovering { |
| 151 | + NSCursor.pointingHand.push() |
| 152 | + } else { |
| 153 | + NSCursor.pop() |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Temporary |
| 159 | + private var animation: Animation { |
| 160 | + // 10x speed when not reloading to make invisible ccw spin go fast in case button is pressed multiple times. |
| 161 | + .linear.speed(model.isReloading ? 0.5 : 10) |
| 162 | + } |
| 163 | + |
| 164 | + private var cursorLocationLabel: some View { |
| 165 | + Text("Ln \(model.currentLine), Col \(model.currentCol)") |
| 166 | + .foregroundStyle(.primary) |
| 167 | + .onHover { hovering in |
| 168 | + if hovering { |
| 169 | + NSCursor.pointingHand.push() |
| 170 | + } else { |
| 171 | + NSCursor.pop() |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private var indentSelector: some View { |
| 177 | + Menu("2 Spaces") { |
| 178 | + // 2 spaces, 4 spaces, ... |
| 179 | + } |
| 180 | + .menuStyle(.borderlessButton) |
| 181 | + .fixedSize() |
| 182 | + .onHover { hovering in |
| 183 | + if hovering { |
| 184 | + NSCursor.pointingHand.push() |
| 185 | + } else { |
| 186 | + NSCursor.pop() |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private var encodingSelector: some View { |
| 192 | + Menu("UTF 8") { |
| 193 | + // UTF 8, ASCII, ... |
| 194 | + } |
| 195 | + .menuStyle(.borderlessButton) |
| 196 | + .fixedSize() |
| 197 | + .onHover { hovering in |
| 198 | + if hovering { |
| 199 | + NSCursor.pointingHand.push() |
| 200 | + } else { |
| 201 | + NSCursor.pop() |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private var lineEndSelector: some View { |
| 207 | + Menu("LF") { |
| 208 | + // LF, CRLF |
| 209 | + } |
| 210 | + .menuStyle(.borderlessButton) |
| 211 | + .fixedSize() |
| 212 | + .onHover { hovering in |
| 213 | + if hovering { |
| 214 | + NSCursor.pointingHand.push() |
| 215 | + } else { |
| 216 | + NSCursor.pop() |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private var expandButton: some View { |
| 222 | + Button { |
| 223 | + model.isExpanded.toggle() |
| 224 | + if model.isExpanded && height < 1 { |
| 225 | + height = 300 |
| 226 | + } |
| 227 | + // Show/hide terminal window |
| 228 | + } label: { |
| 229 | + Image(systemName: "rectangle.bottomthird.inset.filled") |
| 230 | + .imageScale(.large) |
| 231 | + } |
| 232 | + .tint(model.isExpanded ? .accentColor : .primary) |
| 233 | + .buttonStyle(.borderless) |
| 234 | + .onHover { hovering in |
| 235 | + if hovering { |
| 236 | + NSCursor.pointingHand.push() |
| 237 | + } else { |
| 238 | + NSCursor.pop() |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +@available(macOS 12, *) |
| 245 | +struct SwiftUIView_Previews: PreviewProvider { |
| 246 | + static var previews: some View { |
| 247 | + ZStack(alignment: .bottom) { |
| 248 | + Color.white |
| 249 | + StatusBarView() |
| 250 | + .previewLayout(.fixed(width: 1.336, height: 500.0)) |
| 251 | + .preferredColorScheme(.light) |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments