Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3325,12 +3325,12 @@
}
}
},
"Chips overlap to save room. A covered chip shows a smaller icon in the part still in view.": {
"Chips overlap to save room, each one over the next.": {
"localizations": {
"ko": {
"stringUnit": {
"state": "translated",
"value": "칩을 겹쳐 자리를 아낍니다. 가려진 칩은 보이는 부분에 아이콘을 작게 표시합니다."
"value": "자리를 아끼도록 칩을 앞에서부터 차례로 겹쳐 놓습니다."
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
> Unreleased. Requires macOS 14+ on Apple Silicon.

## Changes

- Chips in the menu bar are stacked again by default, each one over the next, as before 0.7.43. Side by side is still there under Settings ▸ Menu Bar ▸ Chips ▸ Spacing; if you chose a spacing there, it is kept.
- A stacked chip's icon stays in the middle of its chip at full size. In 0.7.43 it was made smaller and moved into the part of the chip left in view, so the icons no longer lined up with their chips.
72 changes: 10 additions & 62 deletions Sources/MenubucketApp/MenuBarClusterRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ extension MenuBarController {
/// A glyph may sit this far from its drawing's bounding-box middle,
/// towards where its weight is (`GlyphCache`).
static let opticalLimit: CGFloat = 1
/// Stacked, the glyph of a covered chip keeps this much clear of
/// the edges of the part in view.
static let coverMargin: CGFloat = 1.5
/// How quickly a chip the update wave lifts over its neighbour
/// takes the neighbour's place on top: the glyphs glide between
/// "covered" and "whole" over a quarter of the lift, not in a jump.
static let coverBlend: CGFloat = 4
/// The arc's track, barely there.
static let trackAlpha: CGFloat = 0.12
static let staleAlpha = CGFloat(Fill.staleOpacity)
Expand Down Expand Up @@ -297,9 +290,7 @@ extension MenuBarController {
) -> NSImage {
let geometry = ClusterGeometry(count: chips.count, spacing: look.spacing)
let dark = isDark(appearance)
// A glyph moved aside (below) still starts on a pixel of the
// coarsest display.
let pixel = 1 / max((GlyphCache.shared.scalesForTesting ?? screenScales()).min() ?? 1, 1)

let size = NSSize(width: ceil(lead + geometry.itemWidth), height: height)
let image = NSImage(size: size, flipped: true) { _ in
appearance.performAsCurrentDrawingAppearance {
Expand Down Expand Up @@ -328,44 +319,10 @@ extension MenuBarController {
func pillLeft(_ index: Int) -> CGFloat {
(positions?[cluster: index] ?? CGFloat(geometry.collapsedX(index))) + CGFloat(ClusterGeometry.slotInset)
}
// The drawn shape's edges: a disc, or the focused pill.
func leading(_ index: Int) -> CGFloat {
index == focused && focusedIsPill ? pillLeft(index) : center(index).x - m.face * scale(index) / 2
}
func trailing(_ index: Int) -> CGFloat {
index == focused && focusedIsPill
? pillLeft(index) + max(pillSpan, m.face * m.focusScale)
: center(index).x + m.face * scale(index) / 2
}
// How far `over` is on top of its neighbour `under`, 0...1.
// At rest the earlier chip is on top; the focused chip is on
// top of both neighbours, and so is a chip the wave lifts
// higher than them (`coverBlend`).
func cover(_ over: Int, _ under: Int) -> CGFloat {
if under == focused { return 0 }
if over == focused { return 1 }
let ahead = min(max((lift(over) - lift(under)) * m.coverBlend, -1), 1)
return over < under ? 1 + min(ahead, 0) : max(ahead, 0)
}
// Where a chip's glyph goes: the middle of the part of the
// chip in view, smaller if that part is narrower than the
// glyph. Stacked, a glyph in the middle of its disc was cut
// in half by the chip over it. Taken from where the chips
// are drawn now, so it glides as they fold and fan.
func glyphPlace(_ index: Int) -> GlyphPlace {
let middle = center(index).x, size = scale(index)
var low = middle - m.face * size / 2, high = middle + m.face * size / 2
if index > 0 {
low += cover(index - 1, index) * max(0, trailing(index - 1) + m.cutout - low)
}
if index < chips.count - 1 {
high -= cover(index + 1, index) * max(0, high - (leading(index + 1) - m.cutout))
}
let free = max(high - low, 0)
guard free < m.face * size - 0.01 else { return GlyphPlace(x: middle, factor: 1) }
let x = ((low + high) / 2 / pixel).rounded() * pixel
return GlyphPlace(x: x, factor: min(1, max(0, free - m.coverMargin) / (look.glyphCircle * size)))
}
// A chip's glyph is always at the middle of its disc, at
// full size. Stacked, the chip on top covers part of the one
// beneath, glyph included, as a stack of avatars does:
// moving the glyph into the part in view put it off centre.
// Folded, each chip lies over the one to its right, as a
// stack of avatars does, so a chip's top-trailing corner —
// where its alert dot goes — is over the chip beneath and in
Expand All @@ -376,7 +333,7 @@ extension MenuBarController {
for index in order {
drawChip(
chips[index], at: center(index), color: color(chips[index]), context: context,
scale: scale(index), glow: lift(index), look: look, dark: dark, glyph: glyphPlace(index)
scale: scale(index), glow: lift(index), look: look, dark: dark
)
// With its chip, so a chip drawn later covers it as it
// covers the chip, and it grows with the chip.
Expand Down Expand Up @@ -453,17 +410,9 @@ extension MenuBarController {
tinted.draw(in: fitted, from: .zero, operation: .sourceOver, fraction: alpha, respectFlipped: true, hints: nil)
}

/// Where a chip's glyph is drawn when not at its disc's centre at full
/// size: `x` is its middle, `factor` how much smaller it is.
struct GlyphPlace: Equatable {
var x: CGFloat
var factor: CGFloat
}

static func drawChip(
_ chip: ClusterChip, at center: NSPoint, color: NSColor, context: NSGraphicsContext,
scale: CGFloat = 1, glow: CGFloat = 0, look: ChipAppearance = ChipAppearance(), dark: Bool = true,
glyph place: GlyphPlace? = nil
scale: CGFloat = 1, glow: CGFloat = 0, look: ChipAppearance = ChipAppearance(), dark: Bool = true
) {
let m = ChipMetrics.self
let face = m.face * scale
Expand All @@ -485,10 +434,10 @@ extension MenuBarController {
let disc = circle(face)
Self.paint(disc, rim: circle(face - m.rimWidth), in: disc.bounds, with: paint)

let size = scale * (place?.factor ?? 1)
let size = scale
guard size > 0 else { return }
drawGlyph(
chip, at: NSPoint(x: place?.x ?? center.x, y: center.y), alpha: alpha, scale: size,
chip, at: center, alpha: alpha, scale: size,
look: look, ink: paint, context: context
)
}
Expand Down Expand Up @@ -698,8 +647,7 @@ final class GlyphCache: @unchecked Sendable {
}

/// Main actor: the key includes the attached displays' densities.
/// `scale` is the chip's (a chip in focus or in the wave is larger, a
/// covered one's glyph smaller).
/// `scale` is the chip's (a chip in focus or in the wave is larger).
@MainActor
func glyph(_ name: String, look: ChipAppearance = ChipAppearance(), scale: CGFloat = 1) -> NSImage? {
guard let source = source(name, set: look.glyphSet) else { return nil }
Expand Down
4 changes: 2 additions & 2 deletions Sources/MenubucketApp/Settings/MenuBarPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ struct MenuBarPage: View {
"Spacing",
caption: look.spacing == .apart
? "Chips sit side by side, each one whole."
: "Chips overlap to save room. A covered chip shows a smaller icon in the part still in view.",
: "Chips overlap to save room, each one over the next.",
selection: chipBinding(\.spacing)
) {
Text("Apart", comment: "Chip spacing: side by side").tag(ChipAppearance.Spacing.apart)
Text("Stacked", comment: "Chip spacing: overlapping").tag(ChipAppearance.Spacing.stacked)
Text("Apart", comment: "Chip spacing: side by side").tag(ChipAppearance.Spacing.apart)
}
} header: {
Text("Chips")
Expand Down
2 changes: 1 addition & 1 deletion Sources/MenubucketCore/ChipAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct ChipAppearance: Codable, Equatable, Sendable {

public init(
style: Style = .solid, tone: Tone = .balanced, glyphSet: GlyphSet = .simple,
glyphSize: GlyphSize = .medium, spacing: Spacing = .apart
glyphSize: GlyphSize = .medium, spacing: Spacing = .stacked
) {
self.style = style
self.tone = tone
Expand Down
2 changes: 1 addition & 1 deletion Sources/MenubucketCore/MenuBarCluster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public struct ClusterGeometry: Equatable, Sendable {
/// The BarShelf icon's square.
public static let markWidth: Double = 22

public init(count: Int, spacing: ChipAppearance.Spacing = .apart) {
public init(count: Int, spacing: ChipAppearance.Spacing = ChipAppearance().spacing) {
self.count = max(count, 0)
self.collapsedStep = Self.foldedStep(for: spacing)
}
Expand Down
83 changes: 36 additions & 47 deletions Tests/MenubucketAppTests/ChipAppearanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,76 +140,65 @@ final class ChipAppearanceTests: XCTestCase {
print(String(format: "CHIP-CENTRING apart: worst optical offset x %.3f pt, y %.3f pt; worst box offset x %.3f pt", worstX, worstY, worstBox))
}

/// Stacked, a chip lies over the one to its right. The covered chip's
/// glyph is whole, inside the part of the chip in view and in the
/// middle of it; the chip on top is as it is alone.
func testStackedACoveredGlyphIsWholeInTheMiddleOfWhatShows() throws {
/// Stacked, a chip lies over the one to its right, glyph and all. The
/// covered chip's glyph stays where it is on a chip alone, in the
/// middle of its disc at full size: what shows of it is exactly that
/// part of the glyph, not a smaller glyph moved into the part in view.
func testStackedACoveredGlyphStaysInTheMiddleOfItsDisc() throws {
let m = MenuBarController.ChipMetrics.self
var worst = 0.0
let face = Double(m.face)
for set in ChipAppearance.GlyphSet.allCases {
let look = ChipAppearance(glyphSet: set, spacing: .stacked)
let geometry = ClusterGeometry(count: 3, spacing: .stacked)
XCTAssertLessThan(geometry.collapsedStep, ClusterGeometry.disc, "the discs overlap")
for trio in Self.trios {
let rep = try render(chips(trio), look: look)
let face = Double(m.face)
// The chip on top.
// The chip on top is as it is alone.
let top = ink(rep, from: center(0, geometry) - face / 2, to: center(0, geometry) + face / 2)
let alone = ink(try render(chips([trio[0]]), look: look), from: 0, to: ClusterGeometry.slot)
XCTAssertEqual(top.width, alone.width, accuracy: 0.3, "\(trio[0]) on top changed")
XCTAssertEqual(top.height, alone.height, accuracy: 0.3, "\(trio[0]) on top changed")
let first = ink(try render(chips([trio[0]]), look: look), from: 0, to: ClusterGeometry.slot)
XCTAssertEqual(top.width, first.width, accuracy: 0.3, "\(trio[0]) on top changed")
XCTAssertEqual(top.midX, center(0, geometry), accuracy: 0.3, "\(trio[0]) on top moved")
for index in 1..<trio.count {
let symbol = trio[index]
let middle = center(index, geometry)
let neighbourEdge = center(index - 1, geometry) + face / 2
let visibleLeft = max(middle - face / 2, neighbourEdge + Double(m.cutout))
let visibleRight = middle + face / 2
XCTAssertEqual(visibleRight - visibleLeft, 11.5, accuracy: 0.01)
// Nothing of a glyph at the neighbour's edge or in the
// cutout: the neighbour's own glyph ends well before.
let hidden = ink(rep, from: neighbourEdge - 1, to: visibleLeft)
XCTAssertEqual(hidden.count, 0, "\(symbol) (\(set)) shows under the chip over it")
let shown = ink(rep, from: visibleLeft, to: visibleRight)
XCTAssertGreaterThan(shown.count, 0, "\(symbol) drew nothing")
XCTAssertGreaterThan(shown.left, visibleLeft + 0.2, "\(symbol) (\(set)) touches the cutout")
XCTAssertLessThan(shown.right, visibleRight - 0.2, "\(symbol) (\(set)) touches the rim")
XCTAssertEqual(shown.midX, (visibleLeft + visibleRight) / 2, accuracy: 0.4, "\(symbol) (\(set)) is not in the middle of what shows")
XCTAssertEqual(shown.midY, Double(Self.height) / 2, accuracy: 0.4, symbol)
worst = max(worst, abs(shown.midX - (visibleLeft + visibleRight) / 2))
// Whole: the shape it has alone, only smaller.
let whole = ink(try render(chips([symbol]), look: look), from: 0, to: ClusterGeometry.slot)
XCTAssertEqual(shown.width / shown.height, whole.width / whole.height, accuracy: 0.12, "\(symbol) (\(set)) is cut")
XCTAssertLessThan(shown.width, whole.width, "\(symbol) is drawn smaller to fit")
XCTAssertGreaterThan(max(shown.width, shown.height), 5, "\(symbol) is still a glyph")
// In view: from the chip on top and its cutout to this
// chip's own edge.
let visibleLeft = center(index - 1, geometry) + face / 2 + Double(m.cutout)
let aloneRep = try render(chips([symbol]), look: look)
let aloneMiddle = ClusterGeometry.slot / 2
// The same slice of the glyph, on the chip alone. Half a
// point in, so the cutout's soft edge is not compared.
let from = visibleLeft - middle + 0.5
let shown = ink(rep, from: middle + from, to: middle + face / 2)
let slice = ink(aloneRep, from: aloneMiddle + from, to: aloneMiddle + face / 2)
XCTAssertGreaterThan(slice.count, 0, "\(symbol) has nothing in view to compare")
XCTAssertEqual(shown.right - middle, slice.right - aloneMiddle, accuracy: 0.15, "\(symbol) (\(set)) moved sideways")
XCTAssertEqual(shown.left - middle, slice.left - aloneMiddle, accuracy: 0.15, "\(symbol) (\(set)) moved sideways")
XCTAssertEqual(shown.height, slice.height, accuracy: 0.15, "\(symbol) (\(set)) changed size")
XCTAssertEqual(Double(shown.minY), Double(slice.minY), accuracy: 1, "\(symbol) (\(set)) moved up or down")
XCTAssertEqual(Double(shown.count), Double(slice.count), accuracy: Double(slice.count) * 0.03 + 8, "\(symbol) (\(set)) is not the same drawing")
}
}
}
print(String(format: "CHIP-CENTRING stacked: worst offset in the visible part %.3f pt", worst))
}

/// A covered chip's glyph follows the chips as they fan out: halfway
/// uncovered it is larger than folded and smaller than whole, and whole
/// once nothing covers it.
func testACoveredGlyphGrowsAsItsChipIsUncovered() throws {
/// Fanning out only uncovers a glyph: it does not move or grow.
func testACoveredGlyphDoesNotMoveAsItsChipIsUncovered() throws {
let look = ChipAppearance(spacing: .stacked)
let pair = chips(["cpu", "cpu"])
func second(gap: CGFloat) throws -> Ink {
let alone = ink(try render(chips(["cpu"]), look: look), from: 0, to: ClusterGeometry.slot)
for gap in [CGFloat(13), 15, 17, 19, 21] {
let rep = try bitmap(MenuBarController.clusterImage(
pair, positions: [0, gap], mark: NSImage(size: NSSize(width: 1, height: 1)), badged: false,
color: { _ in Self.disc }, look: look, appearance: try XCTUnwrap(NSAppearance(named: .darkAqua)),
height: CGFloat(Self.height)
))
// From where chip 0 (on top, ending at 22) and its cutout end.
return ink(rep, from: max(Double(gap) + 4 - 0.5, 23.5), to: Double(gap) + 22.5)
let middle = Double(gap) + ClusterGeometry.slot / 2
// Chip 0 (on top) ends at 22; its cutout at 23.5.
let second = ink(rep, from: max(middle - 9, 24), to: middle + 9)
XCTAssertEqual(second.right - middle, alone.right - ClusterGeometry.slot / 2, accuracy: 0.15, "gap \(gap)")
XCTAssertEqual(second.height, alone.height, accuracy: 0.15, "gap \(gap)")
}
let widths = try [13, 15, 17, 19, 21].map { try second(gap: $0).width }
XCTAssertEqual(widths, widths.sorted(), "the glyph only grows: \(widths)")
XCTAssertLessThan(widths[0], widths[2])
XCTAssertLessThan(widths[2], widths[4])
let alone = ink(try render(chips(["cpu"]), look: look), from: 0, to: ClusterGeometry.slot)
XCTAssertEqual(widths[4], alone.width, accuracy: 0.3, "uncovered, it is whole")
XCTAssertEqual(try second(gap: 21).midX, 21 + 13, accuracy: 0.3, "and back in the middle of its disc")
}

/// The chip in focus is on top of both neighbours: its glyph is whole.
Expand Down Expand Up @@ -570,7 +559,7 @@ final class ChipAppearanceTests: XCTestCase {
XCTAssertNotEqual(pixels(ChipAppearance()), pixels(ChipAppearance(style: .soft)))
XCTAssertNotEqual(pixels(ChipAppearance()), pixels(ChipAppearance(glyphSet: .symbols)))
XCTAssertNotEqual(pixels(ChipAppearance()), pixels(ChipAppearance(glyphSize: .large)))
XCTAssertNotEqual(pixels(ChipAppearance()), pixels(ChipAppearance(spacing: .stacked)))
XCTAssertNotEqual(pixels(ChipAppearance()), pixels(ChipAppearance(spacing: .apart)))
}

/// Where the chips rest follows the spacing, folded and with one in
Expand All @@ -579,7 +568,7 @@ final class ChipAppearanceTests: XCTestCase {
let chips = Self.styleChips
XCTAssertEqual(MenuBarController.clusterTargets(chips, focus: nil, look: ChipAppearance(spacing: .apart)), [0, 20, 40])
XCTAssertEqual(MenuBarController.clusterTargets(chips, focus: nil, look: ChipAppearance(spacing: .stacked)), [0, 13, 26])
XCTAssertEqual(MenuBarController.clusterTargets(chips, focus: nil), [0, 20, 40], "apart unless told")
XCTAssertEqual(MenuBarController.clusterTargets(chips, focus: nil), [0, 13, 26], "stacked unless told")
for spacing in ChipAppearance.Spacing.allCases {
let look = ChipAppearance(spacing: spacing)
let geometry = ClusterGeometry(count: chips.count, spacing: spacing)
Expand Down
Loading
Loading