Repository Public
1// ORIX Docs2// File: App/Sources/DesignSystem/InspectorControls.swift3// Description: Reusable inspector controls: collapsible groups, property rows with override markers, AppKit-backed commit fields4// (numbers, lengths, text), pop-up choices, icon segments, switches, colour swatches and small action buttons.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import AppKit10import DocModel1112// MARK: - Units1314/// Unit used to show page geometry. The model always stores points.15enum LengthUnit {16 case inches, centimetres1718 static var current: LengthUnit { Locale.current.measurementSystem == .us ? .inches : .centimetres }19 var pointsPerUnit: Double { self == .inches ? 72 : 72 / 2.54 }20 var suffix: String { self == .inches ? String(localized: "in") : String(localized: "cm") }21 var step: Double { self == .inches ? 0.05 : 0.1 }2223 /// "8.5 in" / "21.59 cm".24 func format(_ points: Double) -> String {25 let f = NumberFormatter(); f.minimumFractionDigits = 0; f.maximumFractionDigits = 2; f.usesGroupingSeparator = false26 return (f.string(from: NSNumber(value: points / pointsPerUnit)) ?? "") + " " + suffix27 }28}2930// MARK: - Scope of the formatting controls3132/// What the typography controls are editing: the selection (direct formatting) or a named style.33enum FormatScope {34 case selection, style35 var overrideHelp: LocalizedStringKey {36 switch self {37 case .selection: return "Direct formatting. Click to return to the style’s value."38 case .style: return "Set by this style. Click to inherit it from the parent style."39 }40 }41}4243private struct FormatScopeKey: EnvironmentKey { static let defaultValue = FormatScope.selection }44extension EnvironmentValues {45 var formatScope: FormatScope {46 get { self[FormatScopeKey.self] }47 set { self[FormatScopeKey.self] = newValue }48 }49}5051// MARK: - Groups and rows5253/// A titled, collapsible inspector group. Its open state is remembered on this Mac.54struct InspectorGroup<Content: View>: View {55 let title: LocalizedStringKey56 let content: Content57 @AppStorage private var expanded: Bool5859 init(_ title: LocalizedStringKey, id: String, expanded: Bool = true, @ViewBuilder content: () -> Content) {60 self.title = title61 self.content = content()62 _expanded = AppStorage(wrappedValue: expanded, "inspector.group.\(id)")63 }6465 var body: some View {66 VStack(alignment: .leading, spacing: ORIX.Space.s) {67 Button { withAnimation(ORIX.Motion.quick) { expanded.toggle() } } label: {68 HStack(spacing: ORIX.Space.xs) {69 Text(title).font(ORIX.Font.sectionTitle).textCase(.uppercase).kerning(0.6).foregroundStyle(ORIX.Palette.textTertiary)70 Spacer(minLength: 0)71 Image(systemName: "chevron.right").font(.system(size: 8, weight: .bold)).foregroundStyle(ORIX.Palette.textTertiary)72 .rotationEffect(.degrees(expanded ? 90 : 0))73 }74 .contentShape(Rectangle())75 }76 .buttonStyle(.plain)77 .accessibilityAddTraits(.isHeader)78 .accessibilityValue(expanded ? Text("Expanded") : Text("Collapsed"))79 if expanded { content }80 }81 .padding(.horizontal, ORIX.Space.l)82 .padding(.vertical, ORIX.Space.m)83 .frame(maxWidth: .infinity, alignment: .leading)84 .overlay(alignment: .bottom) { Rectangle().fill(ORIX.Palette.hairline.opacity(0.7)).frame(height: 1) }85 }86}8788/// A small dot shown next to a property that is set here rather than inherited. Clicking it removes the override.89struct OverrideMarker: View {90 @Environment(\.formatScope) private var scope91 let reset: () -> Void92 var body: some View {93 Button(action: reset) {94 Circle().fill(ORIX.Palette.accent).frame(width: 5, height: 5).frame(width: 11, height: 14).contentShape(Rectangle())95 }96 .buttonStyle(.plain)97 .help(scope.overrideHelp)98 .accessibilityLabel(Text(scope.overrideHelp))99 }100}101102/// Label on the left (with an optional override marker), control on the right.103struct PropertyRow<Content: View>: View {104 let label: LocalizedStringKey105 var onReset: (() -> Void)?106 let content: Content107108 init(_ label: LocalizedStringKey, onReset: (() -> Void)? = nil, @ViewBuilder content: () -> Content) {109 self.label = label; self.onReset = onReset; self.content = content()110 }111112 var body: some View {113 HStack(spacing: ORIX.Space.s) {114 HStack(spacing: 1) {115 Text(label).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1).fixedSize()116 if let onReset { OverrideMarker(reset: onReset) }117 }118 Spacer(minLength: 0)119 content120 }121 .frame(minHeight: 22)122 }123}124125/// A caption under a group of controls.126struct InspectorNote: View {127 let text: LocalizedStringKey128 var symbol: String?129 var color: Color = ORIX.Palette.textTertiary130 init(_ text: LocalizedStringKey, symbol: String? = nil, color: Color = ORIX.Palette.textTertiary) { self.text = text; self.symbol = symbol; self.color = color }131 var body: some View {132 HStack(alignment: .firstTextBaseline, spacing: ORIX.Space.xs) {133 if let symbol { Image(systemName: symbol).font(.system(size: 10, weight: .medium)).foregroundStyle(color).accessibilityHidden(true) }134 Text(text).font(.system(size: 10.5)).foregroundStyle(color).fixedSize(horizontal: false, vertical: true)135 }136 .frame(maxWidth: .infinity, alignment: .leading)137 .accessibilityElement(children: .combine)138 }139}140141// MARK: - Commit fields (AppKit)142143/// A text field that commits when editing ends (Return, Tab or a click elsewhere). It is backed by NSTextField because AppKit ends editing144/// synchronously, BEFORE a click in the document moves the selection: the edit is applied to what was selected while typing.145struct CommitField: NSViewRepresentable {146 var text: String147 var placeholder = ""148 var alignment: NSTextAlignment = .left149 var font: NSFont = .systemFont(ofSize: 11.5)150 var multiline = false151 var label: String = ""152 var identifier: String?153 @Binding var focused: Bool154 let onCommit: (String) -> Void155 /// Called after a commit by Return (single-line fields), to hand the keyboard back to the document.156 var onReturn: (() -> Void)?157158 final class Field: NSTextField {159 var focusChanged: ((Bool) -> Void)?160 override func becomeFirstResponder() -> Bool {161 let ok = super.becomeFirstResponder()162 if ok { focusChanged?(true) }163 return ok164 }165 }166167 final class Coordinator: NSObject, NSTextFieldDelegate {168 var parent: CommitField169 var lastText: String170 init(_ parent: CommitField) { self.parent = parent; self.lastText = parent.text }171172 func controlTextDidEndEditing(_ obj: Notification) {173 guard let field = obj.object as? NSTextField else { return }174 let value = field.stringValue175 parent.focused = false176 if value != parent.text { parent.onCommit(value) }177 // Show the model's value again (the commit may have clamped or refused the entry).178 DispatchQueue.main.async { [weak self, weak field] in179 guard let self, let field, field.currentEditor() == nil else { return }180 field.stringValue = self.parent.text181 }182 }183184 func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool {185 if selector == #selector(NSResponder.cancelOperation(_:)) {186 control.stringValue = parent.text187 control.window?.makeFirstResponder(nil)188 parent.onReturn?()189 return true190 }191 if selector == #selector(NSResponder.insertNewline(_:)) {192 if parent.multiline, NSApp.currentEvent?.modifierFlags.contains(.option) == true {193 textView.insertNewlineIgnoringFieldEditor(nil)194 return true195 }196 control.window?.makeFirstResponder(nil)197 parent.onReturn?()198 return true199 }200 return false201 }202 }203204 func makeCoordinator() -> Coordinator { Coordinator(self) }205206 func makeNSView(context: Context) -> Field {207 let f = Field()208 f.isBordered = false209 f.drawsBackground = false210 f.focusRingType = .none211 f.delegate = context.coordinator212 f.stringValue = text213 f.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)214 f.setContentHuggingPriority(.defaultLow, for: .horizontal)215 if multiline {216 f.usesSingleLineMode = false217 f.lineBreakMode = .byWordWrapping218 f.maximumNumberOfLines = 6219 f.cell?.wraps = true220 f.cell?.isScrollable = false221 } else {222 f.usesSingleLineMode = true223 f.lineBreakMode = .byTruncatingTail224 f.cell?.isScrollable = true225 }226 let coordinator = context.coordinator227 f.focusChanged = { [weak coordinator] on in coordinator?.parent.focused = on }228 return f229 }230231 func updateNSView(_ f: Field, context: Context) {232 context.coordinator.parent = self233 f.font = font234 f.alignment = alignment235 f.placeholderString = placeholder236 f.textColor = NSColor(ORIX.Palette.text)237 if !label.isEmpty { f.setAccessibilityLabel(label) }238 if let identifier { f.setAccessibilityIdentifier(identifier) }239 // While the user types, the field is left alone unless the model changed under it (stepper, undo, another selection).240 if f.currentEditor() == nil || context.coordinator.lastText != text {241 if f.stringValue != text { f.stringValue = text }242 }243 context.coordinator.lastText = text244 }245246 func sizeThatFits(_ proposal: ProposedViewSize, nsView f: Field, context: Context) -> CGSize? {247 guard multiline, let width = proposal.width, width > 0, width.isFinite, let cell = f.cell else { return nil }248 let probe = text.isEmpty ? " " : f.stringValue249 let saved = f.stringValue250 if saved.isEmpty { f.stringValue = probe }251 let h = cell.cellSize(forBounds: NSRect(x: 0, y: 0, width: width, height: 2000)).height252 if saved.isEmpty { f.stringValue = saved }253 return CGSize(width: width, height: max(16, ceil(h)))254 }255}256257/// Hands the keyboard back to the document after an inspector control acted, unless the user is typing in a field.258extension DocumentSession {259 func refocusCanvas() {260 guard let window = canvas?.window, !(window.firstResponder is NSText) else { return }261 window.makeFirstResponder(canvas)262 }263 func focusCanvas() { canvas?.window?.makeFirstResponder(canvas) }264}265266private struct FieldChrome: ViewModifier {267 let focused: Bool268 func body(content: Content) -> some View {269 content270 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(ORIX.Palette.surface))271 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.s).strokeBorder(focused ? ORIX.Palette.accent : ORIX.Palette.hairline))272 }273}274275/// A compact numeric field with a stepper. Commits on Return, Tab or when focus leaves; each stepper click commits at once.276struct ValueField: View {277 let value: Double278 var range: ClosedRange<Double> = 0...10_000279 var step: Double = 1280 var decimals = 1281 var suffix = ""282 var width: CGFloat = 64283 var label: LocalizedStringKey = ""284 var identifier: String?285 var isEnabled = true286 let onCommit: (Double) -> Void287 var onReturn: (() -> Void)?288 @State private var focused = false289290 private func format(_ v: Double) -> String {291 let f = NumberFormatter(); f.minimumFractionDigits = 0; f.maximumFractionDigits = decimals; f.usesGroupingSeparator = false292 return f.string(from: NSNumber(value: v)) ?? String(v)293 }294 private func clamp(_ v: Double) -> Double { min(range.upperBound, max(range.lowerBound, v)) }295 private func parse(_ s: String) {296 let cleaned = s.replacingOccurrences(of: ",", with: ".").replacingOccurrences(of: "−", with: "-").filter { $0.isNumber || $0 == "." || $0 == "-" }297 if let v = Double(cleaned) { onCommit(clamp(v)) }298 }299 /// Steps land on multiples of the step so repeated clicks give round values.300 private func stepped(_ direction: Double) -> Double {301 let scaled = value / step302 let snapped = direction > 0 ? (scaled + 1e-6).rounded(.down) + 1 : (scaled - 1e-6).rounded(.up) - 1303 return clamp(snapped * step)304 }305306 var body: some View {307 HStack(spacing: 2) {308 CommitField(text: format(value), alignment: .right, font: .monospacedDigitSystemFont(ofSize: 11.5, weight: .regular),309 label: "", identifier: identifier, focused: $focused, onCommit: parse, onReturn: onReturn)310 .frame(height: 16)311 .accessibilityLabel(Text(label))312 if !suffix.isEmpty { Text(suffix).font(.system(size: 10)).foregroundStyle(ORIX.Palette.textTertiary).fixedSize() }313 Stepper("", onIncrement: { onCommit(stepped(1)) }, onDecrement: { onCommit(stepped(-1)) })314 .labelsHidden().controlSize(.mini)315 .accessibilityLabel(Text(label))316 }317 .padding(.leading, 5).padding(.trailing, 1).padding(.vertical, 2)318 .frame(width: width)319 .modifier(FieldChrome(focused: focused))320 .opacity(isEnabled ? 1 : 0.45)321 .disabled(!isEnabled)322 .help(label)323 }324}325326/// A length shown in inches or centimetres (page geometry). The model value is in points.327struct LengthField: View {328 let points: Double329 var range: ClosedRange<Double> = 0...1584330 var width: CGFloat = 78331 var label: LocalizedStringKey = ""332 var identifier: String?333 var isEnabled = true334 let onCommit: (Double) -> Void335 var onReturn: (() -> Void)?336337 var body: some View {338 let unit = LengthUnit.current339 let k = unit.pointsPerUnit340 ValueField(value: points / k, range: (range.lowerBound / k)...(range.upperBound / k), step: unit.step, decimals: 2, suffix: unit.suffix,341 width: width, label: label, identifier: identifier, isEnabled: isEnabled, onCommit: { onCommit(($0 * k * 100).rounded() / 100) }, onReturn: onReturn)342 }343}344345/// A text field in the inspector's field chrome. Commits when editing ends.346struct InspectorTextField: View {347 let text: String348 var placeholder = ""349 var multiline = false350 var monospaced = false351 var label: LocalizedStringKey = ""352 var identifier: String?353 let onCommit: (String) -> Void354 var onReturn: (() -> Void)?355 @State private var focused = false356357 var body: some View {358 CommitField(text: text, placeholder: placeholder, font: monospaced ? .monospacedSystemFont(ofSize: 11, weight: .regular) : .systemFont(ofSize: 11.5),359 multiline: multiline, identifier: identifier, focused: $focused, onCommit: onCommit, onReturn: onReturn)360 .frame(minHeight: 16)361 .padding(.horizontal, 6).padding(.vertical, 3)362 .frame(maxWidth: .infinity)363 .modifier(FieldChrome(focused: focused))364 .accessibilityLabel(Text(label))365 }366}367368// MARK: - Pop-up choices369370/// A pop-up button in the inspector's field chrome, with free menu content.371struct InspectorPopup<MenuContent: View>: View {372 let title: String373 var width: CGFloat?374 var label: LocalizedStringKey = ""375 var identifier: String?376 var isEnabled = true377 let content: MenuContent378379 init(_ title: String, width: CGFloat? = nil, label: LocalizedStringKey = "", identifier: String? = nil, isEnabled: Bool = true, @ViewBuilder content: () -> MenuContent) {380 self.title = title; self.width = width; self.label = label; self.identifier = identifier; self.isEnabled = isEnabled; self.content = content()381 }382383 var body: some View {384 Menu { content } label: {385 HStack(spacing: 4) {386 Text(title).font(.system(size: 11.5)).foregroundStyle(ORIX.Palette.text).lineLimit(1).truncationMode(.tail)387 Spacer(minLength: 0)388 Image(systemName: "chevron.up.chevron.down").font(.system(size: 8, weight: .semibold)).foregroundStyle(ORIX.Palette.textTertiary)389 }390 .padding(.horizontal, 7)391 .frame(width: width, height: 22)392 .frame(maxWidth: width == nil ? .infinity : nil)393 .modifier(FieldChrome(focused: false))394 .contentShape(Rectangle())395 }396 .menuStyle(.button).buttonStyle(.plain).menuIndicator(.hidden)397 .frame(width: width)398 .opacity(isEnabled ? 1 : 0.45)399 .disabled(!isEnabled)400 .help(label)401 .accessibilityLabel(Text(label))402 .accessibilityValue(Text(title))403 .accessibilityIdentifier(identifier ?? "")404 }405}406407/// A pop-up over a fixed list of values.408struct ChoicePopup<T: Hashable>: View {409 let selection: T?410 let options: [(value: T, title: String)]411 var width: CGFloat?412 var label: LocalizedStringKey = ""413 var identifier: String?414 var isEnabled = true415 let onPick: (T) -> Void416417 var body: some View {418 InspectorPopup(options.first { $0.value == selection }?.title ?? "—", width: width, label: label, identifier: identifier, isEnabled: isEnabled) {419 ForEach(options, id: \.value) { option in420 Button { onPick(option.value) } label: {421 if option.value == selection { Label(option.title, systemImage: "checkmark") } else { Text(option.title) }422 }423 }424 }425 }426}427428// MARK: - Buttons, segments and switches429430/// A text glyph in a toolbar-sized toggle button (for states that have no SF Symbol).431struct GlyphButton: View {432 let glyph: String433 let help: LocalizedStringKey434 var isOn = false435 var isEnabled = true436 let action: () -> Void437 @State private var hover = false438439 var body: some View {440 Button(action: action) {441 Text(glyph).font(.system(size: 11.5, weight: .semibold))442 .frame(width: 28, height: 26)443 .foregroundStyle(isOn ? ORIX.Palette.accent : ORIX.Palette.text.opacity(isEnabled ? 0.85 : 0.3))444 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(isOn ? ORIX.Palette.accent.opacity(0.16) : (hover && isEnabled ? ORIX.Palette.text.opacity(0.07) : .clear)))445 .contentShape(Rectangle())446 }447 .buttonStyle(.plain).disabled(!isEnabled)448 .onHover { hover = $0 }449 .help(help).accessibilityLabel(Text(help)).accessibilityAddTraits(isOn ? .isSelected : [])450 }451}452453/// A row of mutually exclusive icon choices.454struct IconSegments<T: Hashable>: View {455 struct Item { let value: T; let symbol: String; let title: String }456 let items: [Item]457 let selection: T?458 var identifier: String?459 let onPick: (T) -> Void460461 var body: some View {462 HStack(spacing: 1) {463 ForEach(items, id: \.value) { item in464 BarButton(symbol: item.symbol, help: LocalizedStringKey(item.title), isOn: item.value == selection) { onPick(item.value) }465 .accessibilityIdentifier(identifier.map { "\($0)-\(item.value)" } ?? "")466 }467 }468 .padding(1)469 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s + 1).fill(ORIX.Palette.text.opacity(0.045)))470 }471}472473/// A row of mutually exclusive text choices filling the available width.474struct TextSegments<T: Hashable>: View {475 let items: [(value: T, title: String)]476 let selection: T?477 var identifier: String?478 let onPick: (T) -> Void479480 var body: some View {481 HStack(spacing: 1) {482 ForEach(items, id: \.value) { item in483 let on = item.value == selection484 Button { onPick(item.value) } label: {485 Text(item.title).font(.system(size: 11, weight: on ? .semibold : .regular)).lineLimit(1)486 .frame(maxWidth: .infinity).frame(height: 20)487 .foregroundStyle(on ? ORIX.Palette.accent : ORIX.Palette.textSecondary)488 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(on ? ORIX.Palette.surfaceRaised : .clear)489 .shadow(color: .black.opacity(on ? 0.12 : 0), radius: 1, y: 0.5))490 .contentShape(Rectangle())491 }492 .buttonStyle(.plain)493 .accessibilityAddTraits(on ? .isSelected : [])494 .accessibilityIdentifier(identifier.map { "\($0)-\(item.value)" } ?? "")495 }496 }497 .padding(2)498 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s + 2).fill(ORIX.Palette.text.opacity(0.06)))499 }500}501502/// A labelled switch.503struct SwitchRow: View {504 let label: LocalizedStringKey505 let isOn: Bool506 var onReset: (() -> Void)?507 var isEnabled = true508 var identifier: String?509 let onChange: (Bool) -> Void510511 init(_ label: LocalizedStringKey, isOn: Bool, onReset: (() -> Void)? = nil, isEnabled: Bool = true, identifier: String? = nil, onChange: @escaping (Bool) -> Void) {512 self.label = label; self.isOn = isOn; self.onReset = onReset; self.isEnabled = isEnabled; self.identifier = identifier; self.onChange = onChange513 }514515 var body: some View {516 PropertyRow(label, onReset: onReset) {517 Toggle(isOn: Binding(get: { isOn }, set: { onChange($0) })) { Text(label) }518 .labelsHidden().toggleStyle(.switch).controlSize(.mini)519 .disabled(!isEnabled)520 .accessibilityIdentifier(identifier ?? "")521 }522 .opacity(isEnabled ? 1 : 0.5)523 }524}525526/// A quiet, full-width or compact action button for inspector panels.527struct InspectorButton: View {528 let title: LocalizedStringKey529 var symbol: String?530 var isEnabled = true531 var prominent = false532 var fill = true533 var identifier: String?534 let action: () -> Void535 @State private var hover = false536537 init(_ title: LocalizedStringKey, symbol: String? = nil, isEnabled: Bool = true, prominent: Bool = false, fill: Bool = true, identifier: String? = nil, action: @escaping () -> Void) {538 self.title = title; self.symbol = symbol; self.isEnabled = isEnabled; self.prominent = prominent; self.fill = fill; self.identifier = identifier; self.action = action539 }540541 var body: some View {542 Button(action: action) {543 HStack(spacing: 5) {544 if let symbol { Image(systemName: symbol).font(.system(size: 10.5, weight: .medium)) }545 Text(title).font(.system(size: 11.5, weight: .medium)).lineLimit(1).minimumScaleFactor(0.85)546 }547 .padding(.horizontal, 8)548 .frame(maxWidth: fill ? .infinity : nil).frame(height: 24)549 .foregroundStyle(prominent ? ORIX.Palette.accent : ORIX.Palette.text.opacity(isEnabled ? 0.9 : 0.35))550 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s)551 .fill(prominent ? ORIX.Palette.accent.opacity(hover ? 0.2 : 0.12) : ORIX.Palette.text.opacity(hover && isEnabled ? 0.11 : 0.065)))552 .contentShape(Rectangle())553 }554 .buttonStyle(.plain).disabled(!isEnabled)555 .onHover { hover = $0 }556 .accessibilityIdentifier(identifier ?? "")557 }558}559560// MARK: - Colours561562/// A colour swatch that opens a palette menu. "None" is drawn as a struck-through swatch, never by colour alone.563struct SwatchMenu: View {564 let help: LocalizedStringKey565 let current: RGBA?566 var allowsNone = false567 var palette: [(String, UInt32)] = ColorMenu.inks568 var identifier: String?569 let onPick: (RGBA?) -> Void570571 private var shown: RGBA? { current.flatMap { $0.a <= 0 ? nil : $0 } }572573 var body: some View {574 Menu {575 if allowsNone { Button("None") { onPick(nil) } }576 ForEach(palette, id: \.1) { name, hex in577 Button { onPick(RGBA(hex: hex)) } label: { Label { Text(name) } icon: { Image(nsImage: ColorMenu.swatch(hex)) } }578 }579 Divider()580 Button("More Colours…") { ColorPanelBridge.shared.pick(onPick) }581 } label: {582 Image(nsImage: SwatchMenu.image(shown))583 }584 .menuStyle(.button).buttonStyle(.plain).menuIndicator(.hidden).fixedSize()585 .help(help)586 .accessibilityLabel(Text(help))587 .accessibilityValue(shown.map { Text(verbatim: "#" + $0.hexString) } ?? Text("None"))588 .accessibilityIdentifier(identifier ?? "")589 }590591 static func image(_ color: RGBA?) -> NSImage {592 NSImage(size: NSSize(width: 34, height: 20), flipped: false) { rect in593 let r = rect.insetBy(dx: 1, dy: 1)594 let path = NSBezierPath(roundedRect: r, xRadius: 5, yRadius: 5)595 if let color {596 NSColor(srgbRed: color.r, green: color.g, blue: color.b, alpha: 1).setFill()597 path.fill()598 } else {599 NSColor.white.setFill()600 path.fill()601 NSGraphicsContext.saveGraphicsState()602 path.addClip()603 NSColor(hex: 0xD92D4B).setStroke()604 let slash = NSBezierPath()605 slash.move(to: NSPoint(x: r.minX, y: r.minY)); slash.line(to: NSPoint(x: r.maxX, y: r.maxY))606 slash.lineWidth = 1.5607 slash.stroke()608 NSGraphicsContext.restoreGraphicsState()609 }610 NSColor.gray.withAlphaComponent(0.55).setStroke()611 let border = NSBezierPath(roundedRect: r.insetBy(dx: 0.25, dy: 0.25), xRadius: 5, yRadius: 5)612 border.lineWidth = 0.5613 border.stroke()614 return true615 }616 }617}618619// MARK: - Fonts620621extension Font {622 /// A SwiftUI font for a document family name, including the system designs the engine addresses by name.623 static func documentFamily(_ family: String, size: CGFloat, weight: Font.Weight = .regular) -> Font {624 switch family {625 case "New York": return .system(size: size, weight: weight, design: .serif)626 case "SF Pro", "SF Pro Display", "SF Pro Text", "System": return .system(size: size, weight: weight, design: .default)627 case "SF Pro Rounded": return .system(size: size, weight: weight, design: .rounded)628 case "SF Mono": return .system(size: size, weight: weight, design: .monospaced)629 default: return .custom(family, fixedSize: size).weight(weight)630 }631 }632}633634extension Font.Weight {635 /// The SwiftUI weight nearest to a CSS-like 100…900 weight.636 init(css w: Int) {637 switch w {638 case ..<150: self = .ultraLight639 case ..<250: self = .thin640 case ..<350: self = .light641 case ..<450: self = .regular642 case ..<550: self = .medium643 case ..<650: self = .semibold644 case ..<750: self = .bold645 case ..<850: self = .heavy646 default: self = .black647 }648 }649}650