Improves helder

This commit is contained in:
2026-10-05 09:24:25 +02:00
parent 8fafea4fc3
commit 336987d821
44 changed files with 3454 additions and 542 deletions
@@ -0,0 +1,128 @@
import SwiftUI
/// One button in a `ButtonGroup`. A toggle carries a state dot; an action has none.
public struct GroupItem {
public enum Kind: Equatable {
case action
case toggle(isOn: Bool)
}
var label: String
var keys: String?
var kind: Kind
var tip: Text
var action: () -> Void
public init(_ label: String, keys: String? = nil, kind: Kind, tip: Text, action: @escaping () -> Void) {
self.label = label
self.keys = keys
self.kind = kind
self.tip = tip
self.action = action
}
}
/// The title bar's shared button treatment: buttons edge to edge in one hairline box, a hairline between each.
public struct ButtonGroup: View {
var items: [GroupItem]
var tipEdge: HorizontalAlignment
public init(_ items: [GroupItem], tipEdge: HorizontalAlignment = .leading) {
self.items = items
self.tipEdge = tipEdge
}
public var body: some View {
HStack(spacing: 0) {
ForEach(Array(items.enumerated()), id: \.element.label) { index, item in
GroupButton(item: item, isFirst: index == 0, isLast: index == items.count - 1)
.tip(item.tip, edge: tipEdge)
}
}
.fixedSize()
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
}
}
/// The fill follows the group's rounded ends; the group is not clipped, so the tooltips can hang below it.
private struct GroupButton: View {
var item: GroupItem
var isFirst: Bool
var isLast: Bool
@State private var isHovered = false
var body: some View {
Button(action: item.action) {
HStack(spacing: Metrics.groupButtonGap) {
if case .toggle = item.kind {
Circle().fill(dotColor).frame(width: Metrics.groupButtonDot, height: Metrics.groupButtonDot)
}
Text(item.label).typeRole(Typography.button).foregroundStyle(labelColor)
if let keys = item.keys {
Text(keys).typeRole(Typography.keycapSmall).foregroundStyle(isOff ? Palette.text4 : Palette.amber)
}
}
.lineLimit(1)
.fixedSize()
.padding(Metrics.groupButtonPadding)
.background(fill, in: UnevenRoundedRectangle(cornerRadii: corners))
.overlay(alignment: .leading) {
if !isFirst { Rectangle().fill(Palette.raised).frame(width: Metrics.hairline) }
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.accessibilityAddTraits(isOn ? .isSelected : [])
.animation(Metrics.smallControlTransition, value: isHovered)
.animation(Metrics.smallControlTransition, value: item.kind)
}
private var isOn: Bool { item.kind == .toggle(isOn: true) }
private var isOff: Bool { item.kind == .toggle(isOn: false) }
private var labelColor: Color {
isOn ? Palette.text1 : isOff ? Palette.text4 : Palette.text3
}
private var dotColor: Color {
isOn ? Palette.amber : Palette.text5
}
private var fill: Color {
isOn ? Palette.amberTint : isHovered ? Palette.hover : .clear
}
private var corners: RectangleCornerRadii {
let r = Metrics.radiusSmall
return RectangleCornerRadii(topLeading: isFirst ? r : 0, bottomLeading: isFirst ? r : 0,
bottomTrailing: isLast ? r : 0, topTrailing: isLast ? r : 0)
}
}
/// The icon-only title-bar control (the `?`): a 26pt square with a hairline border.
public struct ToolbarIconButton: View {
var glyph: String
var action: () -> Void
@State private var isHovered = false
public init(_ glyph: String, action: @escaping () -> Void) {
self.glyph = glyph
self.action = action
}
public var body: some View {
Button(action: action) {
Text(glyph)
.typeRole(Typography.keycap)
.foregroundStyle(isHovered ? Palette.text1 : Palette.text4)
.frame(width: Metrics.toolbarIconButton, height: Metrics.toolbarIconButton)
.background(isHovered ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.animation(Metrics.smallControlTransition, value: isHovered)
}
}
+2 -12
View File
@@ -5,10 +5,6 @@ public struct Keycap: View {
public enum Style {
/// Amber glyph, raised border.
case normal
/// On an active toolbar toggle: the border turns amber.
case active
/// On a toggle that is off by meaning: glyph and border step back to text-4.
case muted
/// On an amber fill (primary button): ink glyph, ink border.
case onAmber
/// On an amber-deep fill (destructive button): light glyph, light border.
@@ -25,10 +21,6 @@ public struct Keycap: View {
role = small ? Typography.keycapSmall : Typography.keycap
}
public init(_ keys: String, highlighted: Bool) {
self.init(keys, style: highlighted ? .active : .normal)
}
public var body: some View {
Text(keys)
.typeRole(role)
@@ -41,8 +33,7 @@ public struct Keycap: View {
private var glyph: Color {
switch style {
case .normal, .active: Palette.amber
case .muted: Palette.text4
case .normal: Palette.amber
case .onAmber: Palette.ink
case .onDeep: Palette.onDeep
}
@@ -50,8 +41,7 @@ public struct Keycap: View {
private var border: Color {
switch style {
case .normal, .muted: Palette.raised
case .active: Palette.amberKeycapBorder
case .normal: Palette.raised
case .onAmber: Palette.inkKeycapBorder
case .onDeep: Palette.onDeep.opacity(0.4)
}
@@ -1,11 +1,11 @@
import SwiftUI
/// The frame every overlay sits in: a flat ink scrim (no blur) and a `panel` card with a hairline border,
/// radius 8 and the overlay shadow. `.inset` fills the window minus 34pt; `.sized` centres a fixed-width card
/// radius 8 and the overlay shadow. `.inset` fills the window minus 34pt, up to 1200pt wide; `.sized` centres a fixed-width card
/// at the top. A click on the scrim calls `onDismiss`.
public struct OverlayShell<Content: View>: View {
public enum Placement {
/// Search, Diff: the whole window minus the 34pt inset.
/// Search, Diff: the whole window minus the 34pt inset, at most `insetOverlayMaxWidth` wide.
case inset
/// A card of this width, top-aligned inside the inset.
case sized(width: CGFloat)
@@ -36,7 +36,7 @@ public struct OverlayShell<Content: View>: View {
switch placement {
case .inset:
OverlayCard { content }
.frame(maxWidth: .infinity, maxHeight: .infinity)
.frame(maxWidth: Metrics.insetOverlayMaxWidth, maxHeight: .infinity)
case let .sized(width):
OverlayCard { content }
.frame(width: width)
@@ -0,0 +1,50 @@
import AppKit
/// An NSTextView that only sets its I-beam while it is the view under the pointer.
/// On macOS 26 SwiftTerm makes the window send every mouse move to the first responder, and NSTextView
/// sets the I-beam on each one, which fights the arrow over the other columns and a splitter's resize cursor.
class PointerBoundTextView: NSTextView {
/// Grows the view to at least the clip view's height, so the empty area below the text still takes the I-beam and clicks.
var fillsClipView = false {
didSet { fitClipView() }
}
override func mouseMoved(with event: NSEvent) {
guard isUnderPointer else { return }
super.mouseMoved(with: event)
}
override func cursorUpdate(with event: NSEvent) {
guard isUnderPointer else { return }
super.cursorUpdate(with: event)
}
override func viewDidMoveToSuperview() {
super.viewDidMoveToSuperview()
NotificationCenter.default.removeObserver(self, name: NSView.frameDidChangeNotification, object: nil)
guard let clip = superview as? NSClipView else { return }
clip.postsFrameChangedNotifications = true
NotificationCenter.default.addObserver(self, selector: #selector(clipFrameChanged), name: NSView.frameDidChangeNotification,
object: clip)
fitClipView()
}
@objc private func clipFrameChanged() {
fitClipView()
}
private func fitClipView() {
guard fillsClipView, let clip = superview as? NSClipView else { return }
minSize.height = clip.bounds.height
sizeToFit()
}
/// A splitter's hit area lies on top of the text view's edge, so the visible rect alone is not enough.
private var isUnderPointer: Bool {
guard let window, let content = window.contentView else { return false }
let location = window.mouseLocationOutsideOfEventStream
guard visibleRect.contains(convert(location, from: nil)),
let hit = content.hitTest(content.superview?.convert(location, from: nil) ?? location) else { return false }
return hit.isDescendant(of: self)
}
}
+103 -31
View File
@@ -9,7 +9,6 @@ public struct Splitter: View {
var onEnd: () -> Void
@State private var isHovered = false
@State private var isDragging = false
@State private var last: CGFloat = 0
/// `.vertical` is a column divider dragged left and right; `.horizontal` a row divider dragged up and down.
public init(_ axis: Axis = .vertical, onDrag: @escaping (Double) -> Void, onEnd: @escaping () -> Void = {}) {
@@ -23,44 +22,117 @@ public struct Splitter: View {
.fill(isHovered || isDragging ? Palette.amber : Palette.raised)
.frame(width: axis == .vertical ? Metrics.hairline : nil, height: axis == .horizontal ? Metrics.hairline : nil)
.overlay {
Color.clear
SplitterHandle(axis: axis, onHover: { isHovered = $0 }, onDragging: { isDragging = $0 }, onDrag: onDrag, onEnd: onEnd)
.frame(width: axis == .vertical ? Metrics.splitterHitArea : nil,
height: axis == .horizontal ? Metrics.splitterHitArea : nil)
.contentShape(Rectangle())
.onHover(perform: hover)
.gesture(drag)
}
.zIndex(5)
.animation(Metrics.smallControlTransition, value: isHovered || isDragging)
}
}
private var drag: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .global)
.onChanged { value in
let position = axis == .vertical ? value.translation.width : value.translation.height
if !isDragging {
isDragging = true
last = 0
}
onDrag(Double(position - last))
last = position
}
.onEnded { _ in
isDragging = false
last = 0
if !isHovered { NSCursor.pop() }
onEnd()
}
/// The splitter's hit area as an AppKit view, so it owns its resize cursor instead of sharing a cursor stack.
private struct SplitterHandle: NSViewRepresentable {
var axis: Axis
var onHover: (Bool) -> Void
var onDragging: (Bool) -> Void
var onDrag: (Double) -> Void
var onEnd: () -> Void
func makeNSView(context: Context) -> SplitterHandleView {
let view = SplitterHandleView()
updateNSView(view, context: context)
return view
}
private func hover(_ inside: Bool) {
guard inside != isHovered else { return }
isHovered = inside
if isDragging { return }
if inside {
(axis == .vertical ? NSCursor.resizeLeftRight : NSCursor.resizeUpDown).push()
} else {
NSCursor.pop()
}
func updateNSView(_ view: SplitterHandleView, context: Context) {
view.axis = axis
view.onHover = onHover
view.onDragging = onDragging
view.onDrag = onDrag
view.onEnd = onEnd
}
}
final class SplitterHandleView: NSView {
var axis: Axis = .vertical {
didSet { if axis != oldValue { window?.invalidateCursorRects(for: self) } }
}
var onHover: (Bool) -> Void = { _ in }
var onDragging: (Bool) -> Void = { _ in }
var onDrag: (Double) -> Void = { _ in }
var onEnd: () -> Void = {}
private var dragStart: NSPoint?
private var last: CGFloat = 0
private var cursor: NSCursor {
axis == .vertical ? .resizeLeftRight : .resizeUpDown
}
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
override func resetCursorRects() {
addCursorRect(bounds, cursor: cursor)
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
trackingAreas.forEach(removeTrackingArea)
addTrackingArea(NSTrackingArea(rect: .zero, options: [.mouseEnteredAndExited, .cursorUpdate, .activeInActiveApp, .inVisibleRect],
owner: self))
}
override func cursorUpdate(with event: NSEvent) {
cursor.set()
}
override func mouseEntered(with event: NSEvent) {
onHover(true)
cursor.set()
}
override func mouseExited(with event: NSEvent) {
guard dragStart == nil else { return }
onHover(false)
}
/// Cursor rects stay off for the whole drag: the columns resize under the pointer and would rebuild their I-beam.
override func mouseDown(with event: NSEvent) {
dragStart = event.locationInWindow
last = 0
window?.disableCursorRects()
cursor.set()
onDragging(true)
}
override func mouseDragged(with event: NSEvent) {
guard let dragStart else { return }
cursor.set()
let point = event.locationInWindow
let position = axis == .vertical ? point.x - dragStart.x : dragStart.y - point.y
onDrag(Double(position - last))
last = position
}
override func mouseUp(with event: NSEvent) {
guard dragStart != nil else { return }
let inside = bounds.contains(convert(event.locationInWindow, from: nil))
(inside ? cursor : NSCursor.arrow).set()
endDrag()
onHover(inside)
onEnd()
}
override func viewWillMove(toWindow newWindow: NSWindow?) {
if dragStart != nil { endDrag() }
super.viewWillMove(toWindow: newWindow)
}
private func endDrag() {
dragStart = nil
last = 0
onDragging(false)
window?.enableCursorRects()
window?.resetCursorRects()
}
}
@@ -1,75 +0,0 @@
import SwiftUI
/// A title-bar toggle: Sans 12 label plus keycap. Off has no fill; on gets the amber tint and an amber keycap border;
/// `muted` is for a toggle that is off by meaning (hidden files), where label and keycap step back to text-4.
public struct ToolbarToggle: View {
var label: String
var keys: String
var isOn: Bool
var isMuted: Bool
var action: () -> Void
@State private var isHovered = false
public init(_ label: String, keys: String, isOn: Bool, muted: Bool = false, action: @escaping () -> Void) {
self.label = label
self.keys = keys
self.isOn = isOn
isMuted = muted
self.action = action
}
public var body: some View {
Button(action: action) {
HStack(spacing: 8) {
Text(label).typeRole(isOn ? Typography.toolbarLabelOn : Typography.toolbarLabel)
Keycap(keys, style: isOn ? .active : isMuted ? .muted : .normal)
}
.foregroundStyle(labelColor)
.padding(Metrics.toolbarPadding)
.background(fill, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.animation(Metrics.smallControlTransition, value: isHovered)
.animation(Metrics.smallControlTransition, value: isOn)
}
private var labelColor: Color {
if isOn { return Palette.text1 }
if isMuted { return isHovered ? Palette.text3 : Palette.text4 }
return isHovered ? Palette.text1 : Palette.text3
}
private var fill: Color {
if isOn { return Palette.amberTint }
return isHovered && !isMuted ? Palette.hover : .clear
}
}
/// The icon-only title-bar control (the `?`): a 26pt square with a hairline border.
public struct ToolbarIconButton: View {
var glyph: String
var action: () -> Void
@State private var isHovered = false
public init(_ glyph: String, action: @escaping () -> Void) {
self.glyph = glyph
self.action = action
}
public var body: some View {
Button(action: action) {
Text(glyph)
.typeRole(Typography.keycap)
.foregroundStyle(isHovered ? Palette.text1 : Palette.text4)
.frame(width: Metrics.toolbarIconButton, height: Metrics.toolbarIconButton)
.background(isHovered ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.animation(Metrics.smallControlTransition, value: isHovered)
}
}
+33 -13
View File
@@ -19,17 +19,32 @@ public struct Tooltip: View {
}
public var body: some View {
text
.typeRole(Typography.tooltip)
.foregroundStyle(Palette.text2)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, 10)
.padding(.vertical, 8)
.frame(maxWidth: Metrics.tooltipMaxWidth, alignment: .leading)
.fixedSize()
.background(Palette.raised, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.tooltipBorder, lineWidth: Metrics.hairline))
CappedWidth(maxWidth: Metrics.tooltipMaxWidth) {
text
.typeRole(Typography.tooltip)
.foregroundStyle(Palette.text2)
.multilineTextAlignment(.leading)
.padding(.horizontal, 10)
.padding(.vertical, 8)
}
.background(Palette.raised, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.tooltipBorder, lineWidth: Metrics.hairline))
}
}
/// Sizes its content to its ideal width up to `maxWidth`, wrapping and growing taller beyond it, whatever is proposed.
/// `frame(maxWidth:)` under `fixedSize()` measures a wrapped text at its one-line height, so the text overflows the box.
private struct CappedWidth: Layout {
var maxWidth: CGFloat
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
guard let content = subviews.first else { return .zero }
let ideal = content.sizeThatFits(.unspecified)
return ideal.width <= maxWidth ? ideal : content.sizeThatFits(ProposedViewSize(width: maxWidth, height: nil))
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
subviews.first?.place(at: bounds.origin, anchor: .topLeading, proposal: ProposedViewSize(bounds.size))
}
}
@@ -54,18 +69,23 @@ struct TooltipModifier: ViewModifier {
}
.simultaneousGesture(TapGesture().onEnded { pending?.cancel(); isShown = false })
.overlay(alignment: Alignment(horizontal: edge, vertical: .bottom)) {
// A zero-height frame hangs the tip from the host's bottom edge; an explicit `.bottom`
// alignment guide is ignored here and left the tip covering the host.
if isShown {
Tooltip(text)
.alignmentGuide(.bottom) { $0[.top] - 8 }
.frame(height: 0, alignment: .top)
.offset(y: 8)
.allowsHitTesting(false)
}
}
.zIndex(isShown ? 1 : 0)
}
}
extension View {
/// Shows a design tooltip 350ms after the pointer rests on the view, 8pt below it.
/// The host must sit above its neighbours (`zIndex`) so the tip is not drawn under them.
/// The host lifts itself above its siblings while the tip shows; its container must still sit above
/// the views below it (`zIndex`), as the title bar does over the columns.
public func tip(_ text: Text, edge: HorizontalAlignment = .leading) -> some View {
modifier(TooltipModifier(text: text, edge: edge))
}