sprag-cli 3.135.1 → 3.136.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/mac/SpragBar.swift +754 -433
  2. package/package.json +1 -1
@@ -2,7 +2,8 @@
2
2
  //
3
3
  // A runner in the menu bar moves faster the more of the machine the agent sessions use. A click opens a panel with every
4
4
  // Claude Code and Codex session, the machine's CPU, memory and GPU split by who is using them, account limits, settings and
5
- // rules. The dashboard window shows the same at full size.
5
+ // rules. The dashboard window has three sizes: wide shows a band of machine figures, every session's tile, what is running now
6
+ // and the trends; middle the band and the tiles; narrow only the session list. The window's width picks the size.
6
7
  //
7
8
  // Session data comes from `sprag bar --json` (one child at a time, never overlapping). Machine totals, each process's CPU
8
9
  // time and memory footprint, and GPU time per process come from the kernel and the I/O registry. A setting is changed by
@@ -948,264 +949,271 @@ func oval(_ c: NSPoint, _ rx: CGFloat, _ ry: CGFloat) { NSBezierPath(ovalIn: NSR
948
949
 
949
950
  func runner(_ sp: Sp, frame: Int, idle: Bool) -> NSImage {
950
951
  let size = NSSize(width: 31 * RUNNER_SCALE, height: 21.4 * RUNNER_SCALE)
951
- let img = NSImage(size: size, flipped: false) { _ in
952
- let t = NSAffineTransform(); t.scale(by: RUNNER_SCALE); t.concat()
953
- NSColor.black.setFill(); NSColor.black.setStroke()
954
- if sp.waddle { penguin(frame: frame, idle: idle); return true }
955
- if sp.fly > 0 { flyer(sp, frame: frame, idle: idle); return true }
956
- if sp.kind == 4 { frog(frame: frame, idle: idle); return true }
957
- if sp.kind == 5 || sp.kind == 6 { biped(chick: sp.kind == 6, frame: frame, idle: idle); return true }
958
- if sp.kind == 7 { ghost(frame: frame, idle: idle); return true }
959
- let k = idle ? STAND2 : GAL[frame % GAL.count]
960
- let n = GAL.count, ph = 2 * CGFloat.pi * CGFloat(frame % n) / CGFloat(n)
961
- let flex = CGFloat(k.flex * sp.flexAmp), lift = CGFloat(k.lift * sp.liftAmp) * 1.2
962
- let pitch = CGFloat(k.pitch * sp.pitchAmp) * .pi / 180
963
- let ground: CGFloat = 1.3
964
- let airborne = sp.x.contains("wings") && !idle // runs on air: the hooves never touch the ground
965
- let y0 = ground + sp.legH * LEGK * 0.86 + 0.9 + lift + (idle ? 0.12 * sin(ph) : 0) + (airborne ? 1.2 : 0)
966
- let c = NSPoint(x: 14.6, y: y0)
967
- let half = CGFloat(sp.len) * BODYK / 2 * (1 + 0.17 * flex)
968
- let arch = flex < 0 ? -flex * 1.5 : -flex * 0.3 // gathered: the back humps up; stretched: nearly flat
969
- let H = rot(NSPoint(x: c.x - half, y: y0), c, pitch), S = rot(NSPoint(x: c.x + half, y: y0), c, pitch)
970
- let M = rot(NSPoint(x: c.x, y: y0 + arch * 2), c, pitch)
971
- func leg(_ joint: NSPoint, _ a: (Double, Double), _ len: CGFloat, _ near: Bool) {
972
- let a1 = CGFloat(a.0) * .pi / 180, a2 = CGFloat(a.1) * .pi / 180
973
- let u = len * 0.5, l = len * 0.52
974
- let knee = NSPoint(x: joint.x + sin(a1) * u, y: joint.y - cos(a1) * u)
975
- var foot = NSPoint(x: knee.x + sin(a2) * l, y: knee.y - cos(a2) * l)
976
- foot.y = max(foot.y, airborne ? 1.1 : ground)
977
- let w = CGFloat(sp.legW) * (near ? 1 : 0.86)
978
- taper(joint, knee, 3.6 * w, 2.0 * w); taper(knee, foot, 2.0 * w, 1.35 * w)
979
- oval(NSPoint(x: foot.x + 0.35, y: foot.y + 0.05), 1.0 * w, 0.72 * w)
980
- }
981
- let sh = NSPoint(x: S.x - 0.4, y: S.y - CGFloat(sp.chest) * 0.35), hp = NSPoint(x: H.x + 0.5, y: H.y - CGFloat(sp.hip) * 0.3)
982
- // far legs
983
- leg(sh, k.ff, CGFloat(sp.legF) * LEGK, false); leg(hp, k.hf, CGFloat(sp.legH) * LEGK, false)
984
- // tail, trailing the body by a quarter stride
985
- let lag = sin(ph - .pi / 2), tb = rot(NSPoint(x: c.x - half - 0.6, y: y0 + 0.8), c, pitch)
986
- let tail = NSBezierPath(); tail.lineCapStyle = .round; tail.lineJoinStyle = .round
987
- switch sp.tail {
988
- case .curl:
989
- tail.lineWidth = 3.1
990
- tail.move(to: tb)
991
- tail.curve(to: NSPoint(x: tb.x + 1.0 + flex * 0.4, y: tb.y + 5.2 - flex * 0.6), controlPoint1: NSPoint(x: tb.x - 5.0, y: tb.y + 0.4), controlPoint2: NSPoint(x: tb.x - 5.0 + lag, y: tb.y + 6.2))
992
- tail.stroke()
993
- case .long, .tuft, .thin:
994
- tail.lineWidth = sp.tail == .tuft ? 1.4 : 1.6
995
- let end = NSPoint(x: tb.x - 7.0 + abs(flex) * 0.6, y: tb.y + 1.2 - flex * 2.4 + lag * 1.6)
996
- tail.move(to: tb)
997
- tail.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.6, y: tb.y - 1.4 - lag), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y - 2.2 + lag * 1.4))
998
- tail.stroke()
999
- if sp.tail == .tuft { oval(end, 1.3, 1.1) }
1000
- case .bushy:
1001
- let end = NSPoint(x: tb.x - 7.2, y: tb.y - 0.4 - flex * 1.6 + lag * 1.2)
1002
- let b = NSBezierPath()
1003
- b.move(to: NSPoint(x: tb.x + 0.4, y: tb.y + 1.2))
1004
- b.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.6, y: tb.y + 2.4 + lag * 0.6), controlPoint2: NSPoint(x: end.x + 2.4, y: end.y + 2.2))
1005
- b.curve(to: NSPoint(x: tb.x + 0.4, y: tb.y - 1.2), controlPoint1: NSPoint(x: end.x + 1.6, y: end.y - 1.8), controlPoint2: NSPoint(x: tb.x - 2.8, y: tb.y - 2.0 + lag * 0.6))
1006
- b.close(); b.fill()
1007
- case .stub:
1008
- oval(NSPoint(x: tb.x + 0.6, y: tb.y + 0.6), 1.5, 1.4)
1009
- case .none: break
1010
- case .curly: // a pig's corkscrew
1011
- let cc = NSPoint(x: tb.x - 1.2, y: tb.y + 1.0 + lag * 0.3), t = NSBezierPath(); t.lineWidth = 0.85; t.lineCapStyle = .round
1012
- t.move(to: NSPoint(x: tb.x + 0.2, y: tb.y + 0.2))
1013
- t.appendArc(withCenter: cc, radius: 0.95, startAngle: -20, endAngle: 300, clockwise: false); t.stroke()
1014
- case .ringed, .hang:
1015
- let end = sp.tail == .hang ? NSPoint(x: tb.x - 5.6, y: tb.y - 3.6 + lag * 0.9 - flex * 0.6) : NSPoint(x: tb.x - 7.0, y: tb.y + 0.4 - flex * 1.4 + lag * 1.2)
1016
- let b = NSBezierPath()
1017
- b.move(to: NSPoint(x: tb.x + 0.4, y: tb.y + 1.2))
1018
- b.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.4, y: tb.y + 1.6 + lag * 0.5), controlPoint2: NSPoint(x: end.x + 2.0, y: end.y + 2.0))
1019
- b.curve(to: NSPoint(x: tb.x + 0.4, y: tb.y - 1.2), controlPoint1: NSPoint(x: end.x + 1.4, y: end.y - 1.6), controlPoint2: NSPoint(x: tb.x - 2.6, y: tb.y - 1.8 + lag * 0.5))
1020
- b.close(); b.fill()
1021
- if sp.tail == .ringed { // dark and pale rings: two gaps cut across the tail
1022
- NSGraphicsContext.current?.compositingOperation = .clear
1023
- for t in [0.42, 0.7] as [CGFloat] {
1024
- let m = NSPoint(x: tb.x + (end.x - tb.x) * t, y: tb.y + (end.y - tb.y) * t + 0.3)
1025
- line2(NSPoint(x: m.x + 0.5, y: m.y + 2.2), NSPoint(x: m.x - 0.5, y: m.y - 2.2), 0.6)
1026
- }
1027
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1028
- }
1029
- case .squirrel: // a big plume carried up over the back, its tip curling forward
1030
- let t = NSBezierPath(); t.lineWidth = 3.6; t.lineCapStyle = .round
1031
- let tip = NSPoint(x: tb.x + 0.8 + lag * 0.4, y: tb.y + 8.6 - flex * 0.6)
1032
- t.move(to: tb); t.curve(to: tip, controlPoint1: NSPoint(x: tb.x - 5.2, y: tb.y + 1.4), controlPoint2: NSPoint(x: tb.x - 4.4 + lag * 0.5, y: tb.y + 9.4)); t.stroke()
1033
- oval(tip, 1.8, 1.6)
1034
- case .thinLong, .monkey:
1035
- tail.lineWidth = sp.tail == .monkey ? 1.1 : 0.75
1036
- let end = NSPoint(x: tb.x - 7.4, y: tb.y + (sp.tail == .monkey ? 4.2 : 0.2) - flex * 1.2 + lag * 1.4)
1037
- tail.move(to: tb)
1038
- tail.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.8, y: tb.y - 1.6 - lag), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y - 2.4 + lag))
1039
- if sp.tail == .monkey { tail.appendArc(withCenter: NSPoint(x: end.x + 0.9, y: end.y), radius: 0.9, startAngle: 180, endAngle: 450, clockwise: false) }
1040
- tail.stroke()
1041
- case .flow: // a horse's tail: a few strands streaming back
1042
- for (dy, w) in [(0.0, 1.5), (-0.9, 1.1), (0.8, 1.0)] as [(CGFloat, CGFloat)] {
1043
- let t = NSBezierPath(); t.lineWidth = w; t.lineCapStyle = .round
1044
- let end = NSPoint(x: tb.x - 6.4, y: tb.y - 2.2 + dy - flex * 1.2 + lag * 1.4)
1045
- t.move(to: NSPoint(x: tb.x, y: tb.y + 0.6)); t.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.4, y: tb.y + 1.4 + dy), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y + 1.2 - lag)); t.stroke()
1046
- }
1047
- case .wag: // a puppy's short tail, held up and wagging
1048
- line2(NSPoint(x: tb.x + 0.3, y: tb.y + 0.4), NSPoint(x: tb.x - 1.8 + lag * 0.9, y: tb.y + 3.0), 1.4)
1049
- case .short:
1050
- oval(NSPoint(x: tb.x - 0.4, y: tb.y + 1.2), 0.9, 1.3)
1051
- case .rope: // an elephant's thin tail with a tuft
1052
- let e = NSPoint(x: tb.x - 1.6 + lag * 0.5, y: tb.y - 2.8)
1053
- line2(tb, e, 0.7); oval(e, 0.6, 0.8)
1054
- }
1055
- // wings beat once a stride, the downstroke as the body rises; the far wing goes behind the body
1056
- let wingFlap: CGFloat = idle ? -0.15 : sin(ph + 0.6)
1057
- let wingRoot = rot(NSPoint(x: c.x + half - 1.8, y: y0 + CGFloat(sp.chest) * 0.75), c, pitch)
1058
- if sp.x.contains("wings") { featherWing(NSPoint(x: wingRoot.x + 0.6, y: wingRoot.y + 0.3), flap: idle ? -0.15 : sin(ph + 0.25), span: idle ? 5.5 : 7.6, near: false) }
1059
- // torso: hip and chest masses joined along a spine that bends
1060
- let sp2 = NSBezierPath(); sp2.lineWidth = CGFloat(min(sp.chest, sp.hip)) * 2.15; sp2.lineCapStyle = .round
1061
- sp2.move(to: H); sp2.curve(to: S, controlPoint1: M, controlPoint2: M); sp2.stroke()
1062
- let sq = 1 - 0.09 * max(0, -lift) // squashed a little on landing
1063
- oval(NSPoint(x: H.x + 0.3, y: H.y), CGFloat(sp.hip) * (2 - sq), CGFloat(sp.hip) * 0.95 * sq)
1064
- oval(NSPoint(x: S.x - 0.2, y: S.y - 0.25), CGFloat(sp.chest) * (2 - sq), CGFloat(sp.chest) * sq)
1065
- if sp.hump { oval(rot(NSPoint(x: S.x - 1.4, y: S.y + CGFloat(sp.chest) * 0.62), S, pitch), CGFloat(sp.chest) * 0.78, CGFloat(sp.chest) * 0.62) }
1066
- let br = CGFloat(min(sp.chest, sp.hip)) * 1.07
1067
- func along(_ t: CGFloat) -> NSPoint { // a point on the spine, t = 0 at the hip and 1 at the shoulder
1068
- let u = 1 - t; return NSPoint(x: u * u * u * H.x + 3 * u * u * t * M.x + 3 * u * t * t * M.x + t * t * t * S.x, y: u * u * u * H.y + 3 * u * u * t * M.y + 3 * u * t * t * M.y + t * t * t * S.y)
1069
- }
1070
- if sp.x.contains("wool") { // a white fleece with a scalloped outline: the puffs drawn dark, then hollowed out
1071
- leg(sh, k.fn, CGFloat(sp.legF) * LEGK, true); leg(hp, k.hn, CGFloat(sp.legH) * LEGK, true) // under the fleece
1072
- var puffs: [(NSPoint, CGFloat)] = []
1073
- for i in 0...5 { let p = along(CGFloat(i) / 5); puffs.append((NSPoint(x: p.x, y: p.y + br * 0.8), br * 0.62)); puffs.append((NSPoint(x: p.x, y: p.y - br * 0.55), br * 0.58)) }
1074
- puffs.append((NSPoint(x: H.x - br * 0.8, y: H.y + 0.2), br * 0.62)); puffs.append((NSPoint(x: S.x + br * 0.55, y: S.y + br * 0.35), br * 0.6))
1075
- for (c3, r) in puffs { oval(c3, r, r) }
1076
- oval(along(0.5), br * 1.6, br * 1.1)
952
+ // The drawing is its own function. Swift 6.0 type-checks a multi-statement closure together with the call that
953
+ // takes it, and this body inside NSImage(size:flipped:) gave up with "unable to type-check this expression in
954
+ // reasonable time" (#22). A function body is checked statement by statement.
955
+ let img = NSImage(size: size, flipped: false) { _ in drawRunner(sp, frame: frame, idle: idle); return true }
956
+ img.isTemplate = true
957
+ return img
958
+ }
959
+
960
+ func drawRunner(_ sp: Sp, frame: Int, idle: Bool) {
961
+ let t = NSAffineTransform(); t.scale(by: RUNNER_SCALE); t.concat()
962
+ NSColor.black.setFill(); NSColor.black.setStroke()
963
+ if sp.waddle { penguin(frame: frame, idle: idle); return }
964
+ if sp.fly > 0 { flyer(sp, frame: frame, idle: idle); return }
965
+ if sp.kind == 4 { frog(frame: frame, idle: idle); return }
966
+ if sp.kind == 5 || sp.kind == 6 { biped(chick: sp.kind == 6, frame: frame, idle: idle); return }
967
+ if sp.kind == 7 { ghost(frame: frame, idle: idle); return }
968
+ let k = idle ? STAND2 : GAL[frame % GAL.count]
969
+ let n = GAL.count, ph = 2 * CGFloat.pi * CGFloat(frame % n) / CGFloat(n)
970
+ let flex = CGFloat(k.flex * sp.flexAmp), lift = CGFloat(k.lift * sp.liftAmp) * 1.2
971
+ let pitch = CGFloat(k.pitch * sp.pitchAmp) * .pi / 180
972
+ let ground: CGFloat = 1.3
973
+ let airborne = sp.x.contains("wings") && !idle // runs on air: the hooves never touch the ground
974
+ let standY: CGFloat = ground + CGFloat(sp.legH) * LEGK * 0.86 + 0.9
975
+ let bob: CGFloat = idle ? 0.12 * sin(ph) : 0
976
+ let hover: CGFloat = airborne ? 1.2 : 0
977
+ let y0: CGFloat = standY + lift + bob + hover
978
+ let c = NSPoint(x: 14.6, y: y0)
979
+ let half = CGFloat(sp.len) * BODYK / 2 * (1 + 0.17 * flex)
980
+ let arch = flex < 0 ? -flex * 1.5 : -flex * 0.3 // gathered: the back humps up; stretched: nearly flat
981
+ let H = rot(NSPoint(x: c.x - half, y: y0), c, pitch), S = rot(NSPoint(x: c.x + half, y: y0), c, pitch)
982
+ let M = rot(NSPoint(x: c.x, y: y0 + arch * 2), c, pitch)
983
+ func leg(_ joint: NSPoint, _ a: (Double, Double), _ len: CGFloat, _ near: Bool) {
984
+ let a1 = CGFloat(a.0) * .pi / 180, a2 = CGFloat(a.1) * .pi / 180
985
+ let u = len * 0.5, l = len * 0.52
986
+ let knee = NSPoint(x: joint.x + sin(a1) * u, y: joint.y - cos(a1) * u)
987
+ var foot = NSPoint(x: knee.x + sin(a2) * l, y: knee.y - cos(a2) * l)
988
+ foot.y = max(foot.y, airborne ? 1.1 : ground)
989
+ let w = CGFloat(sp.legW) * (near ? 1 : 0.86)
990
+ taper(joint, knee, 3.6 * w, 2.0 * w); taper(knee, foot, 2.0 * w, 1.35 * w)
991
+ oval(NSPoint(x: foot.x + 0.35, y: foot.y + 0.05), 1.0 * w, 0.72 * w)
992
+ }
993
+ let sh = NSPoint(x: S.x - 0.4, y: S.y - CGFloat(sp.chest) * 0.35), hp = NSPoint(x: H.x + 0.5, y: H.y - CGFloat(sp.hip) * 0.3)
994
+ // far legs
995
+ leg(sh, k.ff, CGFloat(sp.legF) * LEGK, false); leg(hp, k.hf, CGFloat(sp.legH) * LEGK, false)
996
+ // tail, trailing the body by a quarter stride
997
+ let lag = sin(ph - .pi / 2), tb = rot(NSPoint(x: c.x - half - 0.6, y: y0 + 0.8), c, pitch)
998
+ let tail = NSBezierPath(); tail.lineCapStyle = .round; tail.lineJoinStyle = .round
999
+ switch sp.tail {
1000
+ case .curl:
1001
+ tail.lineWidth = 3.1
1002
+ tail.move(to: tb)
1003
+ tail.curve(to: NSPoint(x: tb.x + 1.0 + flex * 0.4, y: tb.y + 5.2 - flex * 0.6), controlPoint1: NSPoint(x: tb.x - 5.0, y: tb.y + 0.4), controlPoint2: NSPoint(x: tb.x - 5.0 + lag, y: tb.y + 6.2))
1004
+ tail.stroke()
1005
+ case .long, .tuft, .thin:
1006
+ tail.lineWidth = sp.tail == .tuft ? 1.4 : 1.6
1007
+ let end = NSPoint(x: tb.x - 7.0 + abs(flex) * 0.6, y: tb.y + 1.2 - flex * 2.4 + lag * 1.6)
1008
+ tail.move(to: tb)
1009
+ tail.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.6, y: tb.y - 1.4 - lag), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y - 2.2 + lag * 1.4))
1010
+ tail.stroke()
1011
+ if sp.tail == .tuft { oval(end, 1.3, 1.1) }
1012
+ case .bushy:
1013
+ let end = NSPoint(x: tb.x - 7.2, y: tb.y - 0.4 - flex * 1.6 + lag * 1.2)
1014
+ let b = NSBezierPath()
1015
+ b.move(to: NSPoint(x: tb.x + 0.4, y: tb.y + 1.2))
1016
+ b.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.6, y: tb.y + 2.4 + lag * 0.6), controlPoint2: NSPoint(x: end.x + 2.4, y: end.y + 2.2))
1017
+ b.curve(to: NSPoint(x: tb.x + 0.4, y: tb.y - 1.2), controlPoint1: NSPoint(x: end.x + 1.6, y: end.y - 1.8), controlPoint2: NSPoint(x: tb.x - 2.8, y: tb.y - 2.0 + lag * 0.6))
1018
+ b.close(); b.fill()
1019
+ case .stub:
1020
+ oval(NSPoint(x: tb.x + 0.6, y: tb.y + 0.6), 1.5, 1.4)
1021
+ case .none: break
1022
+ case .curly: // a pig's corkscrew
1023
+ let cc = NSPoint(x: tb.x - 1.2, y: tb.y + 1.0 + lag * 0.3), t = NSBezierPath(); t.lineWidth = 0.85; t.lineCapStyle = .round
1024
+ t.move(to: NSPoint(x: tb.x + 0.2, y: tb.y + 0.2))
1025
+ t.appendArc(withCenter: cc, radius: 0.95, startAngle: -20, endAngle: 300, clockwise: false); t.stroke()
1026
+ case .ringed, .hang:
1027
+ let end = sp.tail == .hang ? NSPoint(x: tb.x - 5.6, y: tb.y - 3.6 + lag * 0.9 - flex * 0.6) : NSPoint(x: tb.x - 7.0, y: tb.y + 0.4 - flex * 1.4 + lag * 1.2)
1028
+ let b = NSBezierPath()
1029
+ b.move(to: NSPoint(x: tb.x + 0.4, y: tb.y + 1.2))
1030
+ b.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.4, y: tb.y + 1.6 + lag * 0.5), controlPoint2: NSPoint(x: end.x + 2.0, y: end.y + 2.0))
1031
+ b.curve(to: NSPoint(x: tb.x + 0.4, y: tb.y - 1.2), controlPoint1: NSPoint(x: end.x + 1.4, y: end.y - 1.6), controlPoint2: NSPoint(x: tb.x - 2.6, y: tb.y - 1.8 + lag * 0.5))
1032
+ b.close(); b.fill()
1033
+ if sp.tail == .ringed { // dark and pale rings: two gaps cut across the tail
1077
1034
  NSGraphicsContext.current?.compositingOperation = .clear
1078
- for (c3, r) in puffs { oval(c3, r - 0.75, r - 0.75) }
1079
- oval(along(0.5), br * 1.6 - 0.75, br * 1.1 - 0.75)
1035
+ for t in [0.42, 0.7] as [CGFloat] {
1036
+ let m = NSPoint(x: tb.x + (end.x - tb.x) * t, y: tb.y + (end.y - tb.y) * t + 0.3)
1037
+ line2(NSPoint(x: m.x + 0.5, y: m.y + 2.2), NSPoint(x: m.x - 0.5, y: m.y - 2.2), 0.6)
1038
+ }
1080
1039
  NSGraphicsContext.current?.compositingOperation = .sourceOver
1081
1040
  }
1082
- if sp.x.contains("spikes") { // a hedgehog's coat: spines over the back, pointing back
1083
- for i in 0..<10 {
1084
- let t = 0.05 + CGFloat(i) / 10, p = along(t), up = br * (0.6 + 0.4 * sin(t * .pi))
1085
- let b0 = NSPoint(x: p.x - 0.8, y: p.y + up * 0.7), b1 = NSPoint(x: p.x + 0.8, y: p.y + up * 0.75)
1086
- let tip = NSPoint(x: p.x - 2.6 - lag * 0.2, y: p.y + up + 2.8)
1087
- let sp3 = NSBezierPath(); sp3.move(to: b0); sp3.line(to: tip); sp3.line(to: b1); sp3.close(); sp3.fill()
1088
- }
1089
- for i in 0..<3 { let p = along(0.02); let sp3 = NSBezierPath(); sp3.move(to: NSPoint(x: p.x - 0.4, y: p.y + CGFloat(i) - 0.6)); sp3.line(to: NSPoint(x: p.x - br - 1.6, y: p.y + CGFloat(i) * 1.2 - 0.6)); sp3.line(to: NSPoint(x: p.x - 0.4, y: p.y + CGFloat(i) + 0.4)); sp3.close(); sp3.fill() }
1041
+ case .squirrel: // a big plume carried up over the back, its tip curling forward
1042
+ let t = NSBezierPath(); t.lineWidth = 3.6; t.lineCapStyle = .round
1043
+ let tip = NSPoint(x: tb.x + 0.8 + lag * 0.4, y: tb.y + 8.6 - flex * 0.6)
1044
+ t.move(to: tb); t.curve(to: tip, controlPoint1: NSPoint(x: tb.x - 5.2, y: tb.y + 1.4), controlPoint2: NSPoint(x: tb.x - 4.4 + lag * 0.5, y: tb.y + 9.4)); t.stroke()
1045
+ oval(tip, 1.8, 1.6)
1046
+ case .thinLong, .monkey:
1047
+ tail.lineWidth = sp.tail == .monkey ? 1.1 : 0.75
1048
+ let end = NSPoint(x: tb.x - 7.4, y: tb.y + (sp.tail == .monkey ? 4.2 : 0.2) - flex * 1.2 + lag * 1.4)
1049
+ tail.move(to: tb)
1050
+ tail.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.8, y: tb.y - 1.6 - lag), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y - 2.4 + lag))
1051
+ if sp.tail == .monkey { tail.appendArc(withCenter: NSPoint(x: end.x + 0.9, y: end.y), radius: 0.9, startAngle: 180, endAngle: 450, clockwise: false) }
1052
+ tail.stroke()
1053
+ case .flow: // a horse's tail: a few strands streaming back
1054
+ for (dy, w) in [(0.0, 1.5), (-0.9, 1.1), (0.8, 1.0)] as [(CGFloat, CGFloat)] {
1055
+ let t = NSBezierPath(); t.lineWidth = w; t.lineCapStyle = .round
1056
+ let end = NSPoint(x: tb.x - 6.4, y: tb.y - 2.2 + dy - flex * 1.2 + lag * 1.4)
1057
+ t.move(to: NSPoint(x: tb.x, y: tb.y + 0.6)); t.curve(to: end, controlPoint1: NSPoint(x: tb.x - 2.4, y: tb.y + 1.4 + dy), controlPoint2: NSPoint(x: end.x + 2.6, y: end.y + 1.2 - lag)); t.stroke()
1090
1058
  }
1091
- if sp.x.contains("stripes") {
1092
- NSGraphicsContext.current?.compositingOperation = .clear
1093
- // a tiger's stripes, cut across the back
1094
- for t in [0.12, 0.32, 0.52, 0.72] as [CGFloat] { let p = along(t); line2(NSPoint(x: p.x - 0.4, y: p.y + br * 1.05), NSPoint(x: p.x + 0.4, y: p.y + br * 0.15), 0.55) }
1095
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1059
+ case .wag: // a puppy's short tail, held up and wagging
1060
+ line2(NSPoint(x: tb.x + 0.3, y: tb.y + 0.4), NSPoint(x: tb.x - 1.8 + lag * 0.9, y: tb.y + 3.0), 1.4)
1061
+ case .short:
1062
+ oval(NSPoint(x: tb.x - 0.4, y: tb.y + 1.2), 0.9, 1.3)
1063
+ case .rope: // an elephant's thin tail with a tuft
1064
+ let e = NSPoint(x: tb.x - 1.6 + lag * 0.5, y: tb.y - 2.8)
1065
+ line2(tb, e, 0.7); oval(e, 0.6, 0.8)
1066
+ }
1067
+ // wings beat once a stride, the downstroke as the body rises; the far wing goes behind the body
1068
+ let wingFlap: CGFloat = idle ? -0.15 : sin(ph + 0.6)
1069
+ let wingRoot = rot(NSPoint(x: c.x + half - 1.8, y: y0 + CGFloat(sp.chest) * 0.75), c, pitch)
1070
+ if sp.x.contains("wings") { featherWing(NSPoint(x: wingRoot.x + 0.6, y: wingRoot.y + 0.3), flap: idle ? -0.15 : sin(ph + 0.25), span: idle ? 5.5 : 7.6, near: false) }
1071
+ // torso: hip and chest masses joined along a spine that bends
1072
+ let sp2 = NSBezierPath(); sp2.lineWidth = CGFloat(min(sp.chest, sp.hip)) * 2.15; sp2.lineCapStyle = .round
1073
+ sp2.move(to: H); sp2.curve(to: S, controlPoint1: M, controlPoint2: M); sp2.stroke()
1074
+ let sq = 1 - 0.09 * max(0, -lift) // squashed a little on landing
1075
+ oval(NSPoint(x: H.x + 0.3, y: H.y), CGFloat(sp.hip) * (2 - sq), CGFloat(sp.hip) * 0.95 * sq)
1076
+ oval(NSPoint(x: S.x - 0.2, y: S.y - 0.25), CGFloat(sp.chest) * (2 - sq), CGFloat(sp.chest) * sq)
1077
+ if sp.hump { oval(rot(NSPoint(x: S.x - 1.4, y: S.y + CGFloat(sp.chest) * 0.62), S, pitch), CGFloat(sp.chest) * 0.78, CGFloat(sp.chest) * 0.62) }
1078
+ let br = CGFloat(min(sp.chest, sp.hip)) * 1.07
1079
+ func along(_ t: CGFloat) -> NSPoint { // a point on the spine, t = 0 at the hip and 1 at the shoulder
1080
+ let u = 1 - t; return NSPoint(x: u * u * u * H.x + 3 * u * u * t * M.x + 3 * u * t * t * M.x + t * t * t * S.x, y: u * u * u * H.y + 3 * u * u * t * M.y + 3 * u * t * t * M.y + t * t * t * S.y)
1081
+ }
1082
+ if sp.x.contains("wool") { // a white fleece with a scalloped outline: the puffs drawn dark, then hollowed out
1083
+ leg(sh, k.fn, CGFloat(sp.legF) * LEGK, true); leg(hp, k.hn, CGFloat(sp.legH) * LEGK, true) // under the fleece
1084
+ var puffs: [(NSPoint, CGFloat)] = []
1085
+ for i in 0...5 { let p = along(CGFloat(i) / 5); puffs.append((NSPoint(x: p.x, y: p.y + br * 0.8), br * 0.62)); puffs.append((NSPoint(x: p.x, y: p.y - br * 0.55), br * 0.58)) }
1086
+ puffs.append((NSPoint(x: H.x - br * 0.8, y: H.y + 0.2), br * 0.62)); puffs.append((NSPoint(x: S.x + br * 0.55, y: S.y + br * 0.35), br * 0.6))
1087
+ for (c3, r) in puffs { oval(c3, r, r) }
1088
+ oval(along(0.5), br * 1.6, br * 1.1)
1089
+ NSGraphicsContext.current?.compositingOperation = .clear
1090
+ for (c3, r) in puffs { oval(c3, r - 0.75, r - 0.75) }
1091
+ oval(along(0.5), br * 1.6 - 0.75, br * 1.1 - 0.75)
1092
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1093
+ }
1094
+ if sp.x.contains("spikes") { // a hedgehog's coat: spines over the back, pointing back
1095
+ for i in 0..<10 {
1096
+ let t = 0.05 + CGFloat(i) / 10, p = along(t), up = br * (0.6 + 0.4 * sin(t * .pi))
1097
+ let b0 = NSPoint(x: p.x - 0.8, y: p.y + up * 0.7), b1 = NSPoint(x: p.x + 0.8, y: p.y + up * 0.75)
1098
+ let tip = NSPoint(x: p.x - 2.6 - lag * 0.2, y: p.y + up + 2.8)
1099
+ let sp3 = NSBezierPath(); sp3.move(to: b0); sp3.line(to: tip); sp3.line(to: b1); sp3.close(); sp3.fill()
1096
1100
  }
1097
- if sp.x.contains("wings") { featherWing(wingRoot, flap: wingFlap, span: idle ? 6.0 : 9.0, near: true) }
1098
- // neck and head, turned three-quarters toward the viewer so both eyes show; the head stays steadier than the body
1099
- let hpitch = pitch * 0.35, hr = CGFloat(sp.head) * HEADK
1100
- let hc = NSPoint(x: S.x + CGFloat(sp.neckX) * 0.62 * cos(pitch) - CGFloat(sp.neckY) * 0.4 * sin(pitch),
1101
- y: S.y + CGFloat(sp.neckY) * 0.62 - lift * 0.3 + CGFloat(sp.neckX) * 0.8 * sin(pitch))
1102
- line2(NSPoint(x: S.x + 0.4, y: S.y + 0.4), hc, hr * 1.3)
1103
- if sp.mane {
1104
- lionMane(hc, hr, sweep: idle ? 0.05 : 0.32 + 0.12 * lag)
1105
- }
1106
- oval(hc, hr * 1.05, hr)
1107
- let sn = CGFloat(sp.snout), sh2 = CGFloat(sp.snoutH)
1108
- if sn > 1.6 { // a dog's or fox's muzzle points forward and down from the turned face
1109
- let mc = rot(NSPoint(x: hc.x + hr * 0.62 + sn * 0.2, y: hc.y - hr * 0.45), hc, hpitch)
1110
- oval(mc, hr * 0.42 + sn * 0.24, sh2 * 0.7 + 0.45)
1111
- if sp.hump { oval(rot(NSPoint(x: mc.x + hr * 0.42 + sn * 0.2, y: mc.y + sh2 * 0.5), mc, 0), 0.85, 0.7) } // a bear's nose at the tip
1112
- }
1113
- let back = (lift > 0.8 ? 0.35 : 0.0) + (idle ? 0 : 0.12)
1114
- func ear(_ base0: CGFloat, _ base1: CGFloat, _ tipX: CGFloat, _ tipY: CGFloat) {
1115
- let a = rot(NSPoint(x: hc.x + base0, y: hc.y + hr * 0.55), hc, hpitch), b = rot(NSPoint(x: hc.x + base1, y: hc.y + hr * 0.7), hc, hpitch)
1116
- let tip = rot(NSPoint(x: hc.x + tipX, y: hc.y + tipY), hc, hpitch + CGFloat(back))
1117
- let e = NSBezierPath(); e.move(to: a); e.line(to: tip); e.line(to: b); e.close(); e.fill()
1118
- }
1119
- switch sp.ear {
1120
- case .pricked: ear(-hr * 0.9, -hr * 0.15, -hr * 0.7, hr * 1.75); ear(hr * 0.05, hr * 0.85, hr * 0.55, hr * 1.75)
1121
- case .cat: ear(-hr * 0.95, -hr * 0.2, -hr * 0.72, hr * 1.55); ear(hr * 0.0, hr * 0.85, hr * 0.5, hr * 1.55)
1122
- case .round:
1123
- for dx in [-hr * 0.6, hr * 0.45] { let p = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.82), hc, hpitch); oval(p, hr * 0.38, hr * 0.38) }
1124
- case .long:
1125
- for (dx, sw) in [(-hr * 0.45, 0.1), (hr * 0.2, 0.3)] as [(CGFloat, CGFloat)] {
1126
- let base = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.6), hc, hpitch)
1127
- let ang = hpitch + CGFloat(back) * 1.6 + 0.3 + sw - lag * 0.15
1128
- let tip = NSPoint(x: base.x - sin(ang) * hr * 1.55, y: base.y + cos(ang) * hr * 1.55)
1129
- taper(base, tip, hr * 0.42, hr * 0.36)
1130
- }
1131
- case .tiny, .none: break
1132
- case .floppy: // a puppy's ears hang and swing
1133
- // the far ear peeks over the crown; the near one hangs by the cheek, set off from the head by a thin gap
1134
- let fb = rot(NSPoint(x: hc.x + hr * 0.55, y: hc.y + hr * 0.75), hc, hpitch)
1135
- taper(fb, NSPoint(x: fb.x + hr * 0.35, y: fb.y - hr * 0.35), hr * 0.5, hr * 0.45)
1136
- let base = rot(NSPoint(x: hc.x - hr * 0.88, y: hc.y + hr * 0.55), hc, hpitch)
1137
- let tip = NSPoint(x: base.x - hr * 0.55 - lag * hr * 0.3, y: base.y - hr * 1.4 + (lift > 0.8 ? hr * 0.55 : 0))
1138
- NSGraphicsContext.current?.compositingOperation = .clear
1139
- taper(base, tip, hr * 0.5 + 1.0, hr * 0.66 + 1.0)
1140
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1141
- taper(base, tip, hr * 0.5, hr * 0.66)
1142
- case .side: // round ears on the sides: big and fluffy on a koala
1143
- let big = sp.x.contains("koalanose")
1144
- for (dx, dy, r) in [(-hr * (big ? 0.95 : 1.0), hr * (big ? 0.55 : 0.15), hr * (big ? 0.62 : 0.36)), (hr * (big ? 0.8 : 0.95), hr * (big ? 0.6 : 0.2), hr * (big ? 0.5 : 0.3))] as [(CGFloat, CGFloat, CGFloat)] {
1145
- let ec = rot(NSPoint(x: hc.x + dx, y: hc.y + dy), hc, hpitch)
1146
- oval(ec, r, r)
1147
- if big { NSGraphicsContext.current?.compositingOperation = .clear; oval(NSPoint(x: ec.x + (dx < 0 ? -r * 0.12 : r * 0.12), y: ec.y + r * 0.05), r * 0.55, r * 0.55); NSGraphicsContext.current?.compositingOperation = .sourceOver }
1148
- }
1149
- case .mouse:
1150
- for (dx, r) in [(-hr * 0.55, hr * 0.62), (hr * 0.45, hr * 0.55)] as [(CGFloat, CGFloat)] { oval(rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 1.0), hc, hpitch), r, r) }
1151
- case .horse: ear(-hr * 0.6, -hr * 0.15, -hr * 0.55, hr * 1.6); ear(hr * 0.0, hr * 0.4, hr * 0.15, hr * 1.55)
1152
- case .sheep:
1153
- for (dx, f) in [(-hr * 0.7, 1.0), (hr * 0.4, 0.8)] as [(CGFloat, CGFloat)] { let b = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.45), hc, hpitch); taper(b, NSPoint(x: b.x - hr * 0.9 * f, y: b.y - hr * 0.1 + lag * 0.2), hr * 0.42, hr * 0.32) }
1154
- case .pig: ear(-hr * 0.85, -hr * 0.15, hr * 0.1, hr * 1.35); ear(hr * 0.0, hr * 0.7, hr * 0.95, hr * 1.2)
1155
- }
1156
- // head features
1157
- if sp.x.contains("horn") {
1158
- let a = rot(NSPoint(x: hc.x + hr * 0.15, y: hc.y + hr * 0.8), hc, hpitch), b = rot(NSPoint(x: hc.x + hr * 0.65, y: hc.y + hr * 0.85), hc, hpitch), t = rot(NSPoint(x: hc.x + hr * 1.15, y: hc.y + hr * 2.25), hc, hpitch)
1159
- let h = NSBezierPath(); h.move(to: a); h.line(to: t); h.line(to: b); h.close(); h.fill()
1160
- }
1161
- if sp.x.contains("neckmane") { // a mane down the back of the neck, streaming in the run
1162
- for i in 0..<5 {
1163
- let t = CGFloat(i) / 4, base = NSPoint(x: hc.x - hr * 0.75 + (S.x - 0.6 - hc.x + hr * 0.75) * t, y: hc.y + hr * 0.55 + (S.y + CGFloat(sp.chest) * 0.7 - hc.y - hr * 0.55) * t)
1164
- let tip = NSPoint(x: base.x - 2.2 - lag * 0.5, y: base.y + 0.4 - t * 0.6)
1165
- let m = NSBezierPath(); m.move(to: NSPoint(x: base.x + 0.7, y: base.y + 0.4)); m.line(to: tip); m.line(to: NSPoint(x: base.x + 0.3, y: base.y - 0.9)); m.close(); m.fill()
1166
- }
1167
- }
1168
- if sp.x.contains("antlers") {
1169
- for (dx, f) in [(-hr * 0.35, 1.0), (hr * 0.2, 0.85)] as [(CGFloat, CGFloat)] {
1170
- let b = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.8), hc, hpitch)
1171
- let top = NSPoint(x: b.x - hr * 0.7 * f, y: b.y + hr * 1.7 * f), m1 = NSPoint(x: b.x - hr * 0.25 * f, y: b.y + hr * 0.75 * f), m2 = NSPoint(x: b.x - hr * 0.5 * f, y: b.y + hr * 1.25 * f)
1172
- line2(b, top, 0.75); line2(m1, NSPoint(x: m1.x + hr * 0.55 * f, y: m1.y + hr * 0.45 * f), 0.65); line2(m2, NSPoint(x: m2.x + hr * 0.5 * f, y: m2.y + hr * 0.55 * f), 0.6)
1173
- }
1174
- }
1175
- if sp.x.contains("trunk") { // an elephant: a big ear set apart from the head, and a swinging trunk
1176
- let ec = rot(NSPoint(x: hc.x - hr * 0.95, y: hc.y - hr * 0.05), hc, hpitch)
1177
- NSGraphicsContext.current?.compositingOperation = .clear
1178
- NSBezierPath(ovalIn: NSRect(x: ec.x - hr * 0.78, y: ec.y - hr * 0.98, width: hr * 1.56, height: hr * 1.96)).fill()
1179
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1180
- oval(ec, hr * 0.68 + lag * 0.15, hr * 0.88)
1181
- let r0 = rot(NSPoint(x: hc.x + hr * 0.8, y: hc.y - hr * 0.25), hc, hpitch), mid = NSPoint(x: r0.x + hr * 0.55, y: r0.y - hr * 0.8), tip = NSPoint(x: r0.x + hr * 0.6 + lag * hr * 0.35, y: r0.y - hr * 1.7)
1182
- taper(r0, mid, hr * 0.7, hr * 0.5); taper(mid, tip, hr * 0.5, hr * 0.38)
1101
+ for i in 0..<3 { let p = along(0.02); let sp3 = NSBezierPath(); sp3.move(to: NSPoint(x: p.x - 0.4, y: p.y + CGFloat(i) - 0.6)); sp3.line(to: NSPoint(x: p.x - br - 1.6, y: p.y + CGFloat(i) * 1.2 - 0.6)); sp3.line(to: NSPoint(x: p.x - 0.4, y: p.y + CGFloat(i) + 0.4)); sp3.close(); sp3.fill() }
1102
+ }
1103
+ if sp.x.contains("stripes") {
1104
+ NSGraphicsContext.current?.compositingOperation = .clear
1105
+ // a tiger's stripes, cut across the back
1106
+ for t in [0.12, 0.32, 0.52, 0.72] as [CGFloat] { let p = along(t); line2(NSPoint(x: p.x - 0.4, y: p.y + br * 1.05), NSPoint(x: p.x + 0.4, y: p.y + br * 0.15), 0.55) }
1107
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1108
+ }
1109
+ if sp.x.contains("wings") { featherWing(wingRoot, flap: wingFlap, span: idle ? 6.0 : 9.0, near: true) }
1110
+ // neck and head, turned three-quarters toward the viewer so both eyes show; the head stays steadier than the body
1111
+ let hpitch = pitch * 0.35, hr = CGFloat(sp.head) * HEADK
1112
+ let hc = NSPoint(x: S.x + CGFloat(sp.neckX) * 0.62 * cos(pitch) - CGFloat(sp.neckY) * 0.4 * sin(pitch),
1113
+ y: S.y + CGFloat(sp.neckY) * 0.62 - lift * 0.3 + CGFloat(sp.neckX) * 0.8 * sin(pitch))
1114
+ line2(NSPoint(x: S.x + 0.4, y: S.y + 0.4), hc, hr * 1.3)
1115
+ if sp.mane {
1116
+ lionMane(hc, hr, sweep: idle ? 0.05 : 0.32 + 0.12 * lag)
1117
+ }
1118
+ oval(hc, hr * 1.05, hr)
1119
+ let sn = CGFloat(sp.snout), sh2 = CGFloat(sp.snoutH)
1120
+ if sn > 1.6 { // a dog's or fox's muzzle points forward and down from the turned face
1121
+ let mc = rot(NSPoint(x: hc.x + hr * 0.62 + sn * 0.2, y: hc.y - hr * 0.45), hc, hpitch)
1122
+ oval(mc, hr * 0.42 + sn * 0.24, sh2 * 0.7 + 0.45)
1123
+ if sp.hump { oval(rot(NSPoint(x: mc.x + hr * 0.42 + sn * 0.2, y: mc.y + sh2 * 0.5), mc, 0), 0.85, 0.7) } // a bear's nose at the tip
1124
+ }
1125
+ let back = (lift > 0.8 ? 0.35 : 0.0) + (idle ? 0 : 0.12)
1126
+ func ear(_ base0: CGFloat, _ base1: CGFloat, _ tipX: CGFloat, _ tipY: CGFloat) {
1127
+ let a = rot(NSPoint(x: hc.x + base0, y: hc.y + hr * 0.55), hc, hpitch), b = rot(NSPoint(x: hc.x + base1, y: hc.y + hr * 0.7), hc, hpitch)
1128
+ let tip = rot(NSPoint(x: hc.x + tipX, y: hc.y + tipY), hc, hpitch + CGFloat(back))
1129
+ let e = NSBezierPath(); e.move(to: a); e.line(to: tip); e.line(to: b); e.close(); e.fill()
1130
+ }
1131
+ switch sp.ear {
1132
+ case .pricked: ear(-hr * 0.9, -hr * 0.15, -hr * 0.7, hr * 1.75); ear(hr * 0.05, hr * 0.85, hr * 0.55, hr * 1.75)
1133
+ case .cat: ear(-hr * 0.95, -hr * 0.2, -hr * 0.72, hr * 1.55); ear(hr * 0.0, hr * 0.85, hr * 0.5, hr * 1.55)
1134
+ case .round:
1135
+ for dx in [-hr * 0.6, hr * 0.45] { let p = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.82), hc, hpitch); oval(p, hr * 0.38, hr * 0.38) }
1136
+ case .long:
1137
+ for (dx, sw) in [(-hr * 0.45, 0.1), (hr * 0.2, 0.3)] as [(CGFloat, CGFloat)] {
1138
+ let base = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.6), hc, hpitch)
1139
+ let ang = hpitch + CGFloat(back) * 1.6 + 0.3 + sw - lag * 0.15
1140
+ let tip = NSPoint(x: base.x - sin(ang) * hr * 1.55, y: base.y + cos(ang) * hr * 1.55)
1141
+ taper(base, tip, hr * 0.42, hr * 0.36)
1183
1142
  }
1184
- if sp.x.contains("pigsnout") {
1185
- let c2 = rot(NSPoint(x: hc.x + hr * 1.0, y: hc.y - hr * 0.3), hc, hpitch)
1186
- oval(c2, hr * 0.38, hr * 0.48)
1187
- NSGraphicsContext.current?.compositingOperation = .clear
1188
- for dy in [hr * 0.15, -hr * 0.17] { oval(NSPoint(x: c2.x + hr * 0.12, y: c2.y + dy), hr * 0.09, hr * 0.12) }
1189
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1143
+ case .tiny, .none: break
1144
+ case .floppy: // a puppy's ears hang and swing
1145
+ // the far ear peeks over the crown; the near one hangs by the cheek, set off from the head by a thin gap
1146
+ let fb = rot(NSPoint(x: hc.x + hr * 0.55, y: hc.y + hr * 0.75), hc, hpitch)
1147
+ taper(fb, NSPoint(x: fb.x + hr * 0.35, y: fb.y - hr * 0.35), hr * 0.5, hr * 0.45)
1148
+ let base = rot(NSPoint(x: hc.x - hr * 0.88, y: hc.y + hr * 0.55), hc, hpitch)
1149
+ let tip = NSPoint(x: base.x - hr * 0.55 - lag * hr * 0.3, y: base.y - hr * 1.4 + (lift > 0.8 ? hr * 0.55 : 0))
1150
+ NSGraphicsContext.current?.compositingOperation = .clear
1151
+ taper(base, tip, hr * 0.5 + 1.0, hr * 0.66 + 1.0)
1152
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1153
+ taper(base, tip, hr * 0.5, hr * 0.66)
1154
+ case .side: // round ears on the sides: big and fluffy on a koala
1155
+ let big = sp.x.contains("koalanose")
1156
+ for (dx, dy, r) in [(-hr * (big ? 0.95 : 1.0), hr * (big ? 0.55 : 0.15), hr * (big ? 0.62 : 0.36)), (hr * (big ? 0.8 : 0.95), hr * (big ? 0.6 : 0.2), hr * (big ? 0.5 : 0.3))] as [(CGFloat, CGFloat, CGFloat)] {
1157
+ let ec = rot(NSPoint(x: hc.x + dx, y: hc.y + dy), hc, hpitch)
1158
+ oval(ec, r, r)
1159
+ if big { NSGraphicsContext.current?.compositingOperation = .clear; oval(NSPoint(x: ec.x + (dx < 0 ? -r * 0.12 : r * 0.12), y: ec.y + r * 0.05), r * 0.55, r * 0.55); NSGraphicsContext.current?.compositingOperation = .sourceOver }
1190
1160
  }
1191
- if sp.x.contains("koalanose") { oval(rot(NSPoint(x: hc.x + hr * 1.0, y: hc.y - hr * 0.2), hc, hpitch), hr * 0.38, hr * 0.5) }
1192
- if sp.x.contains("panda") { // pale face, dark eye patches
1193
- NSGraphicsContext.current?.compositingOperation = .clear
1194
- oval(hc, hr * 0.84, hr * 0.8)
1195
- NSGraphicsContext.current?.compositingOperation = .sourceOver
1196
- for (x, y, r) in [(0.42, 0.06, 0.36), (-0.2, 0.1, 0.3)] as [(CGFloat, CGFloat, CGFloat)] {
1197
- let p = NSBezierPath(ovalIn: NSRect(x: -r * hr, y: -r * hr * 0.8, width: 2 * r * hr, height: 1.6 * r * hr))
1198
- let tr = NSAffineTransform(); tr.translateX(by: hc.x + hr * x, yBy: hc.y + hr * y); tr.rotate(byRadians: -0.5); p.transform(using: tr as AffineTransform); p.fill()
1199
- }
1200
- oval(NSPoint(x: hc.x + hr * 0.78, y: hc.y - hr * 0.32), hr * 0.17, hr * 0.13)
1161
+ case .mouse:
1162
+ for (dx, r) in [(-hr * 0.55, hr * 0.62), (hr * 0.45, hr * 0.55)] as [(CGFloat, CGFloat)] { oval(rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 1.0), hc, hpitch), r, r) }
1163
+ case .horse: ear(-hr * 0.6, -hr * 0.15, -hr * 0.55, hr * 1.6); ear(hr * 0.0, hr * 0.4, hr * 0.15, hr * 1.55)
1164
+ case .sheep:
1165
+ for (dx, f) in [(-hr * 0.7, 1.0), (hr * 0.4, 0.8)] as [(CGFloat, CGFloat)] { let b = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.45), hc, hpitch); taper(b, NSPoint(x: b.x - hr * 0.9 * f, y: b.y - hr * 0.1 + lag * 0.2), hr * 0.42, hr * 0.32) }
1166
+ case .pig: ear(-hr * 0.85, -hr * 0.15, hr * 0.1, hr * 1.35); ear(hr * 0.0, hr * 0.7, hr * 0.95, hr * 1.2)
1167
+ }
1168
+ // head features
1169
+ if sp.x.contains("horn") {
1170
+ let a = rot(NSPoint(x: hc.x + hr * 0.15, y: hc.y + hr * 0.8), hc, hpitch), b = rot(NSPoint(x: hc.x + hr * 0.65, y: hc.y + hr * 0.85), hc, hpitch), t = rot(NSPoint(x: hc.x + hr * 1.15, y: hc.y + hr * 2.25), hc, hpitch)
1171
+ let h = NSBezierPath(); h.move(to: a); h.line(to: t); h.line(to: b); h.close(); h.fill()
1172
+ }
1173
+ if sp.x.contains("neckmane") { // a mane down the back of the neck, streaming in the run
1174
+ for i in 0..<5 {
1175
+ let t = CGFloat(i) / 4, base = NSPoint(x: hc.x - hr * 0.75 + (S.x - 0.6 - hc.x + hr * 0.75) * t, y: hc.y + hr * 0.55 + (S.y + CGFloat(sp.chest) * 0.7 - hc.y - hr * 0.55) * t)
1176
+ let tip = NSPoint(x: base.x - 2.2 - lag * 0.5, y: base.y + 0.4 - t * 0.6)
1177
+ let m = NSBezierPath(); m.move(to: NSPoint(x: base.x + 0.7, y: base.y + 0.4)); m.line(to: tip); m.line(to: NSPoint(x: base.x + 0.3, y: base.y - 0.9)); m.close(); m.fill()
1201
1178
  }
1202
- // near legs
1203
- if !sp.x.contains("wool") { leg(sh, k.fn, CGFloat(sp.legF) * LEGK, true); leg(hp, k.hn, CGFloat(sp.legH) * LEGK, true) }
1204
- eyes(NSPoint(x: hc.x + hr * CGFloat(sp.eyeDx), y: hc.y), hr, hpitch, CGFloat(sp.eyeK))
1205
- return true
1206
1179
  }
1207
- img.isTemplate = true
1208
- return img
1180
+ if sp.x.contains("antlers") {
1181
+ for (dx, f) in [(-hr * 0.35, 1.0), (hr * 0.2, 0.85)] as [(CGFloat, CGFloat)] {
1182
+ let b = rot(NSPoint(x: hc.x + dx, y: hc.y + hr * 0.8), hc, hpitch)
1183
+ let top = NSPoint(x: b.x - hr * 0.7 * f, y: b.y + hr * 1.7 * f), m1 = NSPoint(x: b.x - hr * 0.25 * f, y: b.y + hr * 0.75 * f), m2 = NSPoint(x: b.x - hr * 0.5 * f, y: b.y + hr * 1.25 * f)
1184
+ line2(b, top, 0.75); line2(m1, NSPoint(x: m1.x + hr * 0.55 * f, y: m1.y + hr * 0.45 * f), 0.65); line2(m2, NSPoint(x: m2.x + hr * 0.5 * f, y: m2.y + hr * 0.55 * f), 0.6)
1185
+ }
1186
+ }
1187
+ if sp.x.contains("trunk") { // an elephant: a big ear set apart from the head, and a swinging trunk
1188
+ let ec = rot(NSPoint(x: hc.x - hr * 0.95, y: hc.y - hr * 0.05), hc, hpitch)
1189
+ NSGraphicsContext.current?.compositingOperation = .clear
1190
+ NSBezierPath(ovalIn: NSRect(x: ec.x - hr * 0.78, y: ec.y - hr * 0.98, width: hr * 1.56, height: hr * 1.96)).fill()
1191
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1192
+ oval(ec, hr * 0.68 + lag * 0.15, hr * 0.88)
1193
+ let r0 = rot(NSPoint(x: hc.x + hr * 0.8, y: hc.y - hr * 0.25), hc, hpitch), mid = NSPoint(x: r0.x + hr * 0.55, y: r0.y - hr * 0.8), tip = NSPoint(x: r0.x + hr * 0.6 + lag * hr * 0.35, y: r0.y - hr * 1.7)
1194
+ taper(r0, mid, hr * 0.7, hr * 0.5); taper(mid, tip, hr * 0.5, hr * 0.38)
1195
+ }
1196
+ if sp.x.contains("pigsnout") {
1197
+ let c2 = rot(NSPoint(x: hc.x + hr * 1.0, y: hc.y - hr * 0.3), hc, hpitch)
1198
+ oval(c2, hr * 0.38, hr * 0.48)
1199
+ NSGraphicsContext.current?.compositingOperation = .clear
1200
+ for dy in [hr * 0.15, -hr * 0.17] { oval(NSPoint(x: c2.x + hr * 0.12, y: c2.y + dy), hr * 0.09, hr * 0.12) }
1201
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1202
+ }
1203
+ if sp.x.contains("koalanose") { oval(rot(NSPoint(x: hc.x + hr * 1.0, y: hc.y - hr * 0.2), hc, hpitch), hr * 0.38, hr * 0.5) }
1204
+ if sp.x.contains("panda") { // pale face, dark eye patches
1205
+ NSGraphicsContext.current?.compositingOperation = .clear
1206
+ oval(hc, hr * 0.84, hr * 0.8)
1207
+ NSGraphicsContext.current?.compositingOperation = .sourceOver
1208
+ for (x, y, r) in [(0.42, 0.06, 0.36), (-0.2, 0.1, 0.3)] as [(CGFloat, CGFloat, CGFloat)] {
1209
+ let p = NSBezierPath(ovalIn: NSRect(x: -r * hr, y: -r * hr * 0.8, width: 2 * r * hr, height: 1.6 * r * hr))
1210
+ let tr = NSAffineTransform(); tr.translateX(by: hc.x + hr * x, yBy: hc.y + hr * y); tr.rotate(byRadians: -0.5); p.transform(using: tr as AffineTransform); p.fill()
1211
+ }
1212
+ oval(NSPoint(x: hc.x + hr * 0.78, y: hc.y - hr * 0.32), hr * 0.17, hr * 0.13)
1213
+ }
1214
+ // near legs
1215
+ if !sp.x.contains("wool") { leg(sh, k.fn, CGFloat(sp.legF) * LEGK, true); leg(hp, k.hn, CGFloat(sp.legH) * LEGK, true) }
1216
+ eyes(NSPoint(x: hc.x + hr * CGFloat(sp.eyeDx), y: hc.y), hr, hpitch, CGFloat(sp.eyeK))
1209
1217
  }
1210
1218
 
1211
1219
  /** A penguin does not gallop: it waddles, rocking side to side with the flippers out. */
@@ -2576,12 +2584,51 @@ struct SessionTile: View {
2576
2584
  .contentShape(Rectangle())
2577
2585
  }
2578
2586
  }
2579
- /** Every session on one screen, in a window of its own: numbers, meters, trend lines and icons. A session's text shows when its tile is clicked. */
2587
+ /** The dashboard's three layouts. The window's width alone picks one, so dragging the edge and pressing a preset agree. */
2588
+ enum DashSize: String, CaseIterable { case mini, medium, large
2589
+ static func of(_ width: CGFloat) -> DashSize { width < 620 ? .mini : width < 1100 ? .medium : .large }
2590
+ var label: String { self == .mini ? "작게" : self == .medium ? "중간" : "크게" }
2591
+ var tip: String { self == .mini ? "세션 목록만 봅니다 (⌘1)" : self == .medium ? "세션 타일을 봅니다 (⌘2)" : "세션 타일과 지금 도는 것, 추이를 봅니다 (⌘3)" }
2592
+ }
2593
+ /**
2594
+ * The height of the dashboard's transparent title bar. The window's view ignores the safe area SwiftUI keeps for that bar
2595
+ * and every layout leaves this much itself, so a colour can run up behind the bar while the first row starts right under
2596
+ * it. Keeping the safe area as well doubled the gap, which the narrow window cannot afford. The right column and the
2597
+ * picked session's overlay start 10 points lower, level with the band: a scroll view that begins right at the bar is
2598
+ * composited by the system in a way the self-test's capture cannot read, and the whole column came out blank there.
2599
+ */
2600
+ let TITLE_BAR: CGFloat = 28
2601
+ /** What the title bar controls show: the layout the window is in, and whether it floats over other windows. */
2602
+ final class DashUI: ObservableObject { @Published var size: DashSize = .large; @Published var pinned = false }
2603
+ /** The three size buttons and the pin, in the title bar next to the traffic lights. Drawn like `Segments`, small enough for the standard title bar. */
2604
+ struct DashControls: View {
2605
+ @ObservedObject var ui: DashUI
2606
+ let pick: (DashSize) -> Void; let pin: () -> Void
2607
+ var body: some View {
2608
+ HStack(spacing: 6) {
2609
+ HStack(spacing: 1) {
2610
+ ForEach(DashSize.allCases, id: \.self) { s in
2611
+ let on = ui.size == s
2612
+ Button(action: { if !on { pick(s) } }) {
2613
+ Text(s.label).font(.system(size: 11, weight: on ? .semibold : .regular)).lineLimit(1).fixedSize().padding(.horizontal, 7).frame(height: 18)
2614
+ .background(RoundedRectangle(cornerRadius: 4).fill(on ? Color.primary.opacity(0.24) : Color.clear)).contentShape(Rectangle())
2615
+ }.buttonStyle(HoverStyle(radius: 4, off: on)).help(s.tip)
2616
+ }
2617
+ }.padding(2).background(RoundedRectangle(cornerRadius: 6).fill(Color.primary.opacity(0.09)))
2618
+ Button(action: pin) {
2619
+ Image(systemName: ui.pinned ? "pin.fill" : "pin").font(.system(size: 11)).foregroundColor(ui.pinned ? Color.accentColor : Color.secondary).frame(width: 22, height: 22).contentShape(Rectangle())
2620
+ }.buttonStyle(HoverStyle(radius: 5)).help(ui.pinned ? "다른 창 위에 고정했습니다. 누르면 풉니다" : "다른 창 위에 고정합니다")
2621
+ }.padding(.horizontal, 8).frame(height: 24)
2622
+ }
2623
+ }
2624
+ /** Every session on one screen, in a window of its own, in three sizes: wide shows tiles, what is running and the trends, middle the tiles, narrow only the session list. */
2580
2625
  struct DashboardView: View {
2581
2626
  @ObservedObject var model: Model
2582
2627
  var still = false
2583
2628
  var stillWidth: CGFloat = 1480
2584
2629
  var lit: Int? = nil // draw this session's tile as if the pointer were on it, for a capture
2630
+ var forcedDetail: Bool? = nil // a capture says whether the machine detail is open; the window remembers it
2631
+ @AppStorage("dashMachineDetail") private var detail = false
2585
2632
  @State private var selected: Int?
2586
2633
  @State private var hoverPid: Int? // the session whose row in a largest-users list is under the pointer
2587
2634
  @State private var busyLeft: Set<String> = [] // leftover lines with an open connection: left unticked, something may be using them
@@ -2589,17 +2636,35 @@ struct DashboardView: View {
2589
2636
  @State private var picked: Set<String> = []
2590
2637
  @State private var cleanNote = ""
2591
2638
  @Environment(\.colorScheme) private var scheme
2592
- init(model: Model, still: Bool = false, preselect: Int? = nil, width: CGFloat = 1480, lit: Int? = nil, cleaning: Bool = false) { self.model = model; self.still = still; self.stillWidth = width; self.lit = lit; if cleaning { let ids = model.leftoverItems.map { $0.id }.filter { !model.leftoverShared.contains($0) }; let busy = model.leftoverBusy(ids); _cleaning = State(initialValue: true); _busyLeft = State(initialValue: busy); _picked = State(initialValue: Set(ids.filter { !busy.contains($0) })) }; _selected = State(initialValue: preselect) }
2639
+ private var showDetail: Bool { still ? (forcedDetail ?? false) : detail }
2640
+ init(model: Model, still: Bool = false, preselect: Int? = nil, width: CGFloat = 1480, lit: Int? = nil, cleaning: Bool = false, detail: Bool? = nil) { self.model = model; self.still = still; self.stillWidth = width; self.lit = lit; self.forcedDetail = detail; if cleaning { let ids = model.leftoverItems.map { $0.id }.filter { !model.leftoverShared.contains($0) }; let busy = model.leftoverBusy(ids); _cleaning = State(initialValue: true); _busyLeft = State(initialValue: busy); _picked = State(initialValue: Set(ids.filter { !busy.contains($0) })) }; _selected = State(initialValue: preselect) }
2593
2641
  func key(_ c: Color, _ t: String, _ pal: Pal) -> some View { HStack(spacing: 3) { Circle().fill(c).frame(width: 6, height: 6); Text(t).font(.system(size: 10)).foregroundColor(pal.text2).lineLimit(1) } }
2594
2642
  func chip(_ symbol: String, _ color: Color, _ text: String, _ pal: Pal) -> some View {
2595
2643
  HStack(spacing: 4) { Image(systemName: symbol).font(.system(size: 10)).foregroundColor(color).frame(width: 13); Text(text).font(.system(size: 11, weight: .medium)).foregroundColor(pal.text) }.lineLimit(1).fixedSize()
2596
2644
  }
2597
- func smallTile<V: View>(_ label: String, _ pal: Pal, @ViewBuilder _ content: () -> V) -> some View {
2598
- VStack(alignment: .leading, spacing: 5) { Text(label).font(.system(size: 11)).foregroundColor(pal.text2).lineLimit(1); content() }
2599
- .padding(12).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading).background(RoundedRectangle(cornerRadius: 10).fill(pal.tile))
2600
- }
2601
- /** The GPU split the same way as CPU and memory: who is on it, by name, and what is left over. */
2602
- func gpuCard(_ snap: Snapshot, _ pal: Pal) -> some View {
2645
+ /** Who uses the machine's CPU: the whole figure, and its parts by owner. The band and the detail tiles both read this. */
2646
+ func cpuSplit(_ snap: Snapshot, _ pal: Pal) -> (total: Double, parts: [Part]) {
2647
+ let cl = snap.sessions.filter { $0.agent != "codex" }, cx = snap.sessions.filter { $0.agent == "codex" }
2648
+ let cpuCl = cl.reduce(0) { $0 + model.cpuOf($1) }, cpuCx = cx.reduce(0) { $0 + model.cpuOf($1) }, cpuLeft = model.leftover.cpu * 100
2649
+ let all = max((model.sysCpu ?? 0) * 100, cpuCl + cpuCx + cpuLeft)
2650
+ var parts = [Part(id: "Claude 세션", value: cpuCl, color: pal.claude)]
2651
+ if !cx.isEmpty { parts.append(Part(id: "Codex 세션", value: cpuCx, color: pal.codex)) }
2652
+ parts.append(Part(id: "끝난 세션이 남긴 것", value: cpuLeft, color: pal.left))
2653
+ parts.append(Part(id: "그 밖", value: max(0, all - cpuCl - cpuCx - cpuLeft), color: pal.neutral))
2654
+ return (all, parts)
2655
+ }
2656
+ /** The same for memory, in MB. */
2657
+ func memSplit(_ snap: Snapshot, _ pal: Pal) -> (total: Double, parts: [Part]) {
2658
+ let cl = snap.sessions.filter { $0.agent != "codex" }, cx = snap.sessions.filter { $0.agent == "codex" }
2659
+ let memCl = cl.reduce(0) { $0 + model.memOf($1) }, memCx = cx.reduce(0) { $0 + model.memOf($1) }, memLeft = model.leftover.mem
2660
+ var parts = [Part(id: "Claude 세션", value: memCl, color: pal.claude)]
2661
+ if !cx.isEmpty { parts.append(Part(id: "Codex 세션", value: memCx, color: pal.codex)) }
2662
+ parts.append(Part(id: "끝난 세션이 남긴 것", value: memLeft, color: pal.left))
2663
+ parts.append(Part(id: "다른 프로그램", value: model.others.reduce(0) { $0 + $1.mem }, color: pal.neutral))
2664
+ return (model.sysMemMb ?? 0, parts)
2665
+ }
2666
+ /** The GPU split the same way as CPU and memory: who is on it, and what is left over. */
2667
+ func gpuSplit(_ snap: Snapshot, _ pal: Pal) -> (total: Double, parts: [Part]) {
2603
2668
  let g = (model.gpu ?? 0) * 100
2604
2669
  let hasCodex = snap.sessions.contains { $0.agent == "codex" }
2605
2670
  let cl = min(100, snap.sessions.filter { $0.agent != "codex" }.reduce(0) { $0 + (model.gpuBySession[$1.pid] ?? 0) } * 100)
@@ -2609,6 +2674,21 @@ struct DashboardView: View {
2609
2674
  if hasCodex { parts.append(Part(id: "Codex 세션", value: cx, color: pal.codex)) }
2610
2675
  parts.append(Part(id: "끝난 세션이 남긴 것", value: left, color: pal.left))
2611
2676
  parts.append(Part(id: "그 밖", value: max(0, g - cl - cx - left), color: pal.neutral))
2677
+ return (g, parts)
2678
+ }
2679
+ /** The thin bar split by owner, the rest in the well colour: the detail tiles draw it 8 points high, the band 6. */
2680
+ func splitBar(_ parts: [Part], scale: Double, height: CGFloat, _ pal: Pal) -> some View {
2681
+ let full = max(scale, parts.reduce(0) { $0 + $1.value }, 0.000001)
2682
+ return GeometryReader { g in
2683
+ HStack(spacing: 2) {
2684
+ ForEach(parts) { p in if p.value > 0 { Rectangle().fill(p.color).frame(width: max(3, (g.size.width - 2 * CGFloat(parts.count)) * CGFloat(p.value / full))) } }
2685
+ Rectangle().fill(pal.well).frame(maxWidth: .infinity)
2686
+ }.clipShape(Capsule())
2687
+ }.frame(height: height)
2688
+ }
2689
+ /** The GPU detail tile, shown under the band when the detail is open. */
2690
+ func gpuCard(_ snap: Snapshot, _ pal: Pal) -> some View {
2691
+ let sp = gpuSplit(snap, pal)
2612
2692
  let top: [Top] = model.gpuLines(snap.sessions, max: 5).map { l in
2613
2693
  let s = l.pid.flatMap { p in snap.sessions.first { $0.pid == p } }
2614
2694
  return Top(id: l.id, name: l.label, value: l.share * 100,
@@ -2616,78 +2696,121 @@ struct DashboardView: View {
2616
2696
  pid: s?.pid, program: s == nil || l.note.isEmpty ? nil : l.note, doing: s.flatMap { doingNow($0) },
2617
2697
  origin: s != nil ? nil : l.agent == "left" ? "끝난 세션이 남긴 것" : "세션과 무관한 프로그램", originSymbol: l.agent == "left" ? "moon.zzz.fill" : "macwindow")
2618
2698
  }
2619
- return machineTile(title: "GPU", total: pct1(g), sub: "기기 전체", parts: parts, scale: 100, fmt: { pct1($0) }, top: top, series: [],
2699
+ return machineTile(title: "GPU", total: pct1(sp.total), sub: "기기 전체", parts: sp.parts, scale: 100, fmt: { pct1($0) }, top: top, series: [],
2620
2700
  tip: "전체 값은 GPU 장치의 사용률이고 이름별 값은 각 프로그램이 GPU를 쓴 시간의 비율입니다. 재는 방식이 달라 합이 조금 어긋날 수 있습니다", pal: pal)
2621
2701
  }
2622
- func kpis(_ snap: Snapshot, _ pal: Pal, width: CGFloat) -> some View {
2623
- let sm = model.samples, span = spanText(sm.first?.at, sm.last?.at)
2624
- let costs = sm.map { $0.cost }, costUp = (costs.last ?? 0) - (costs.first ?? 0)
2702
+ struct Counts { var active = 0, mains = 0, user = 0, idle = 0, subs = 0 }
2703
+ /** How many main sessions work, wait for the user or rest, and how many sessions they started. Every place that says it reads this one count. */
2704
+ func counts(_ snap: Snapshot) -> Counts {
2625
2705
  let mainOnes = snap.sessions.filter { model.parentSession[$0.pid] == nil }
2626
2706
  let phases = mainOnes.map { model.phase($0) }
2627
- let nUser = phases.filter { $0 == .needsUser }.count, nIdle = phases.filter { $0 == .idle }.count, nSubs = snap.sessions.count - mainOnes.count
2628
- let wins = snap.usage?.claude?.windows ?? []
2629
- let working = smallTile("작업 중인 세션", pal) {
2630
- HStack(alignment: .firstTextBaseline, spacing: 5) {
2631
- Text("\(phases.filter { $0 == .active }.count)").font(.system(size: 40, weight: .semibold)).foregroundColor(pal.text)
2632
- Text("/ \(mainOnes.count)").font(.system(size: 16, weight: .medium)).foregroundColor(pal.text2)
2633
- }
2634
- VStack(alignment: .leading, spacing: 2) {
2635
- if nUser > 0 { chip("hand.raised.fill", pal.warning, "확인 대기 \(nUser)", pal) }
2636
- if nIdle > 0 { chip("pause.circle", pal.text2, "입력 대기 \(nIdle)", pal) }
2637
- if nSubs > 0 { chip("arrow.turn.down.right", pal.text2, "띄운 세션 \(nSubs)", pal) }
2638
- }
2639
- }
2640
- let cost = smallTile("세션 비용, 정가 환산", pal) {
2641
- Text(snap.totals.costUsd.map { String(format: "$%.2f", $0) } ?? "-").font(.system(size: 22, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).lineLimit(1).fixedSize()
2642
- Text("실행 중인 세션 \(snap.sessions.count)개 합계").font(.system(size: 10.5)).foregroundColor(pal.text2).lineLimit(1)
2643
- if costUp > 0.005 { Text(String(format: "%@ 동안 +$%.2f", span, costUp)).font(.system(size: 10.5)).monospacedDigit().foregroundColor(pal.text2).lineLimit(1) }
2644
- }.help("구독 청구액이 아니라 쓴 토큰을 정가로 환산한 값입니다")
2645
- let limits = smallTile("Claude 한도", pal) {
2646
- if wins.isEmpty { Text("받은 값이 없습니다").font(.system(size: 11)).foregroundColor(pal.text2) }
2647
- ForEach(wins, id: \.key) { w in
2648
- let sv = severity(w.usedPct, pal.claude, pal)
2649
- VStack(alignment: .leading, spacing: 3) {
2650
- HStack(spacing: 5) {
2651
- Text(windowName(w)).font(.system(size: 11.5, weight: .medium)).foregroundColor(pal.text)
2652
- if let sym = sv.symbol { Image(systemName: sym).font(.system(size: 10)).foregroundColor(sv.color) }
2653
- Spacer(minLength: 4)
2654
- Text("\(Int(w.usedPct.rounded()))%").font(.system(size: 15, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).fixedSize()
2655
- }
2656
- Meter(value: w.usedPct / 100, fill: sv.color)
2657
- Text(resetText(w.resetsAt, now: snap.at)).font(.system(size: 10.5)).foregroundColor(pal.text2).lineLimit(1).minimumScaleFactor(0.85)
2658
- }
2707
+ return Counts(active: phases.filter { $0 == .active }.count, mains: mainOnes.count, user: phases.filter { $0 == .needsUser }.count, idle: phases.filter { $0 == .idle }.count, subs: snap.sessions.count - mainOnes.count)
2708
+ }
2709
+ /** One tile of the band: 58 pt high on the same plate as every tile. `grow` lets it take the width the others leave. */
2710
+ func bandTile<V: View>(_ pal: Pal, width: CGFloat? = nil, grow: Bool = false, @ViewBuilder _ content: () -> V) -> some View {
2711
+ VStack(alignment: .leading, spacing: 3) { content() }
2712
+ .padding(.horizontal, 10).padding(.vertical, 8)
2713
+ .frame(width: width, height: 58, alignment: .leading)
2714
+ .frame(maxWidth: grow ? CGFloat.infinity : nil, alignment: .leading)
2715
+ .background(RoundedRectangle(cornerRadius: 10).fill(pal.tile))
2716
+ }
2717
+ func bandLabel(_ text: String, _ pal: Pal) -> some View { Text(text).font(.system(size: 10.5)).foregroundColor(pal.text2).fixedSize() }
2718
+ func sessionsTile(_ snap: Snapshot, _ pal: Pal) -> some View {
2719
+ let n = counts(snap)
2720
+ return bandTile(pal, grow: true) {
2721
+ bandLabel("작업 중인 세션", pal)
2722
+ HStack(spacing: 7) {
2723
+ if n.active > 0 { LiveDot(color: pal.good) }
2724
+ Text("\(n.active)").font(.system(size: 18, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).fixedSize()
2725
+ Text("/ \(n.mains)").font(.system(size: 12)).foregroundColor(pal.text2).fixedSize()
2726
+ if n.user > 0 { chip("hand.raised.fill", pal.warning, "확인 대기 \(n.user)", pal) }
2727
+ if n.idle > 0 { chip("pause.circle", pal.text2, "입력 대기 \(n.idle)", pal) }
2728
+ if n.subs > 0 { chip("arrow.turn.down.right", pal.text2, "띄운 세션 \(n.subs)", pal) }
2659
2729
  }
2660
2730
  }
2661
- let rank = smallTile("이달의 랭킹", pal) {
2662
- if let st = snap.standing {
2663
- HStack(spacing: 8) {
2664
- Text(st.emoji).font(.system(size: 28)).fixedSize()
2665
- VStack(alignment: .leading, spacing: 0) {
2666
- HStack(spacing: 4) { Text(st.name).font(.system(size: 14, weight: .semibold)).foregroundColor(pal.text).lineLimit(1); if st.crown == true { Image(systemName: "crown.fill").font(.system(size: 9.5)).foregroundColor(pal.warning) } }
2667
- Text(st.rank != nil && st.ranked != nil ? "\(Int(st.ranked!))명 중 \(Int(st.rank!))위" : "집계 전").font(.system(size: 11.5)).foregroundColor(pal.text2).lineLimit(1)
2668
- }
2669
- }
2670
- if let p = st.scorePct {
2671
- Meter(value: p / 100, fill: pal.neutral, tick: st.next?.floorPct.map { $0 / 100 }, tickColor: pal.text)
2672
- Text("점수 백분위 \(Int(p))%" + (st.next.map { ", 다음 \($0.emoji) \($0.floorPct.map { "\(Int($0))%" } ?? "")" } ?? "")).font(.system(size: 10.5)).foregroundColor(pal.text2).lineLimit(1)
2673
- }
2674
- } else { Text("팀에 참여하면 나옵니다").font(.system(size: 11)).foregroundColor(pal.text2) }
2731
+ }
2732
+ /** CPU, memory or GPU as a figure and the split bar; the line each part says is in the tooltip. */
2733
+ func resourceTile(_ title: String, _ value: String, _ parts: [Part], scale: Double, fmt: (Double) -> String, fixed: Bool, _ pal: Pal) -> some View {
2734
+ let tip = (["누르면 나눠 쓰는 몫과 많이 쓰는 곳을 펼칩니다"] + parts.map { "\($0.id) \(fmt($0.value))" }).joined(separator: "\n")
2735
+ return bandTile(pal, width: fixed ? 118 : nil, grow: !fixed) {
2736
+ HStack(spacing: 4) {
2737
+ Text(title).font(.system(size: 10.5)).foregroundColor(pal.text2).fixedSize()
2738
+ Spacer(minLength: 4)
2739
+ Text(value).font(.system(size: 13, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).fixedSize()
2740
+ }
2741
+ splitBar(parts, scale: scale, height: 6, pal).padding(.top, 3)
2742
+ }.help(tip)
2743
+ }
2744
+ /** The three machine tiles are one button: pressing any of them opens or closes the detail above the sessions. */
2745
+ func resources(_ snap: Snapshot, _ pal: Pal, fixed: Bool) -> some View {
2746
+ let cpu = cpuSplit(snap, pal), mem = memSplit(snap, pal), gpu = gpuSplit(snap, pal)
2747
+ return Pressable(radius: 10, action: { detail.toggle() }) {
2748
+ HStack(spacing: 6) {
2749
+ resourceTile("CPU", pct1(cpu.total), cpu.parts, scale: 100, fmt: { pct1($0) }, fixed: fixed, pal)
2750
+ resourceTile("메모리", memText(mem.total), mem.parts, scale: RAM_MB, fmt: { memText($0) }, fixed: fixed, pal)
2751
+ if model.gpu != nil { resourceTile("GPU", pct1(gpu.total), gpu.parts, scale: 100, fmt: { pct1($0) }, fixed: fixed, pal) }
2752
+ Image(systemName: showDetail ? "chevron.up" : "chevron.down").font(.system(size: 9)).foregroundColor(pal.text2).frame(width: 12)
2753
+ }
2675
2754
  }
2676
- return Group {
2677
- if width >= 980 {
2678
- HStack(alignment: .top, spacing: 10) {
2679
- if model.gpu != nil { gpuCard(snap, pal) }
2680
- working.frame(width: 132); cost.frame(width: 156); limits.frame(width: 170); rank.frame(width: 170)
2681
- }.fixedSize(horizontal: false, vertical: true)
2682
- } else {
2683
- VStack(spacing: 10) {
2684
- if model.gpu != nil { gpuCard(snap, pal) }
2685
- HStack(alignment: .top, spacing: 10) { working; cost }.fixedSize(horizontal: false, vertical: true)
2686
- HStack(alignment: .top, spacing: 10) { limits; rank }.fixedSize(horizontal: false, vertical: true)
2755
+ }
2756
+ func limitsTile(_ snap: Snapshot, _ pal: Pal) -> some View {
2757
+ let wins = snap.usage?.claude?.windows ?? []
2758
+ return bandTile(pal) {
2759
+ bandLabel("Claude 한도", pal)
2760
+ HStack(alignment: .top, spacing: 12) {
2761
+ ForEach(wins, id: \.key) { w in
2762
+ let sv = severity(w.usedPct, pal.claude, pal)
2763
+ VStack(alignment: .leading, spacing: 3) {
2764
+ HStack(spacing: 4) {
2765
+ Text(windowName(w)).font(.system(size: 11.5, weight: .medium)).foregroundColor(pal.text).fixedSize()
2766
+ if let sym = sv.symbol { Image(systemName: sym).font(.system(size: 10)).foregroundColor(sv.color) }
2767
+ Spacer(minLength: 4)
2768
+ Text("\(Int(w.usedPct.rounded()))%").font(.system(size: 13, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).fixedSize()
2769
+ }
2770
+ Meter(value: w.usedPct / 100, fill: sv.color, height: 5)
2771
+ }.frame(minWidth: 84, alignment: .leading).help(resetText(w.resetsAt, now: snap.at))
2687
2772
  }
2688
2773
  }
2689
2774
  }
2690
2775
  }
2776
+ func costTile(_ snap: Snapshot, _ pal: Pal) -> some View {
2777
+ let sm = model.samples, span = spanText(sm.first?.at, sm.last?.at)
2778
+ let costs = sm.map { $0.cost }, costUp = (costs.last ?? 0) - (costs.first ?? 0)
2779
+ var tip = "실행 중인 세션 \(snap.sessions.count)개 합계입니다. 구독 청구액이 아니라 쓴 토큰을 정가로 환산한 값입니다"
2780
+ if costUp > 0.005 { tip += "\n" + String(format: "%@ 동안 +$%.2f", span, costUp) }
2781
+ return bandTile(pal) {
2782
+ bandLabel("세션 비용, 정가 환산", pal)
2783
+ Text(snap.totals.costUsd.map { String(format: "$%.2f", $0) } ?? "-").font(.system(size: 15, weight: .semibold)).monospacedDigit().foregroundColor(pal.text).fixedSize()
2784
+ }.help(tip)
2785
+ }
2786
+ func rankTile(_ st: Standing, _ pal: Pal) -> some View {
2787
+ var tip = ""
2788
+ if let p = st.scorePct { tip = "점수 백분위 \(Int(p))%" + (st.next.map { ", 다음 \($0.emoji) \($0.floorPct.map { "\(Int($0))%" } ?? "")" } ?? "") }
2789
+ return bandTile(pal) {
2790
+ bandLabel("이달의 랭킹", pal)
2791
+ HStack(spacing: 6) {
2792
+ Text(st.emoji).font(.system(size: 15)).fixedSize()
2793
+ Text(st.name).font(.system(size: 12.5, weight: .semibold)).foregroundColor(pal.text).fixedSize()
2794
+ if st.crown == true { Image(systemName: "crown.fill").font(.system(size: 9.5)).foregroundColor(pal.warning) }
2795
+ Text(st.rank != nil && st.ranked != nil ? "\(Int(st.ranked!))명 중 \(Int(st.rank!))위" : "집계 전").font(.system(size: 11.5)).foregroundColor(pal.text2).fixedSize()
2796
+ }
2797
+ }.help(tip)
2798
+ }
2799
+ /**
2800
+ * One thin band instead of the big machine and summary tiles: what the sessions are doing, the machine as three small
2801
+ * bars, the account limits, the cost and the ranking. The first row that fits the width wins, and the last one wraps.
2802
+ */
2803
+ func band(_ snap: Snapshot, _ pal: Pal) -> some View {
2804
+ let sessions = sessionsTile(snap, pal), cost = costTile(snap, pal)
2805
+ let hasLimits = !(snap.usage?.claude?.windows ?? []).isEmpty
2806
+ return ViewThatFits(in: .horizontal) {
2807
+ HStack(spacing: 8) { sessions; resources(snap, pal, fixed: true); if hasLimits { limitsTile(snap, pal) }; cost; if let st = snap.standing { rankTile(st, pal) } }
2808
+ HStack(spacing: 8) { sessions; resources(snap, pal, fixed: true); if hasLimits { limitsTile(snap, pal) }; cost }
2809
+ HStack(spacing: 8) { sessions; resources(snap, pal, fixed: true); if hasLimits { limitsTile(snap, pal) } }
2810
+ HStack(spacing: 8) { sessions; resources(snap, pal, fixed: true) }
2811
+ VStack(spacing: 8) { sessions; resources(snap, pal, fixed: false) }
2812
+ }
2813
+ }
2691
2814
  /** Trends at a size and with the labels that let them be read: the machine against the sessions, for CPU, memory and GPU. */
2692
2815
  func trends(_ pal: Pal) -> some View {
2693
2816
  let sm = model.samples, span = spanText(sm.first?.at, sm.last?.at)
@@ -2779,7 +2902,6 @@ struct DashboardView: View {
2779
2902
  }
2780
2903
  /** One machine resource: the total, a thin bar split by who is using it with its legend, a trend, and the largest users by name. */
2781
2904
  func machineTile(title: String, total: String, sub: String, parts: [Part], scale: Double, fmt: @escaping (Double) -> String, top: [Top], series: [(v: [Double], c: Color)], tip: String, pal: Pal) -> some View {
2782
- let full = max(scale, parts.reduce(0) { $0 + $1.value }, 0.000001)
2783
2905
  let topMax = max(top.map { $0.value }.max() ?? 0, 0.000001)
2784
2906
  // The figure, bar and legend take a fixed narrow column, so every remaining point of width goes to the names of the largest users.
2785
2907
  return HStack(alignment: .top, spacing: 14) {
@@ -2789,12 +2911,7 @@ struct DashboardView: View {
2789
2911
  Text(total).font(.system(size: 26, weight: .semibold)).foregroundColor(pal.text).lineLimit(1).fixedSize()
2790
2912
  Text(sub).font(.system(size: 11)).foregroundColor(pal.text2).lineLimit(1)
2791
2913
  }
2792
- GeometryReader { g in
2793
- HStack(spacing: 2) {
2794
- ForEach(parts) { p in if p.value > 0 { Rectangle().fill(p.color).frame(width: max(3, (g.size.width - 2 * CGFloat(parts.count)) * CGFloat(p.value / full))) } }
2795
- Rectangle().fill(pal.well).frame(maxWidth: .infinity)
2796
- }.clipShape(Capsule())
2797
- }.frame(height: 8)
2914
+ splitBar(parts, scale: scale, height: 8, pal)
2798
2915
  VStack(alignment: .leading, spacing: 3) {
2799
2916
  ForEach(parts) { p in
2800
2917
  HStack(spacing: 5) {
@@ -2835,25 +2952,17 @@ struct DashboardView: View {
2835
2952
  }
2836
2953
  func machine(_ snap: Snapshot, _ pal: Pal, width: CGFloat) -> some View {
2837
2954
  let sm = model.samples, ses = snap.sessions
2838
- let cl = ses.filter { $0.agent != "codex" }, cx = ses.filter { $0.agent == "codex" }
2839
- let cpuCl = cl.reduce(0) { $0 + model.cpuOf($1) }, cpuCx = cx.reduce(0) { $0 + model.cpuOf($1) }, cpuLeft = model.leftover.cpu * 100
2840
- let cpuAll = max((model.sysCpu ?? 0) * 100, cpuCl + cpuCx + cpuLeft)
2841
- let memCl = cl.reduce(0) { $0 + model.memOf($1) }, memCx = cx.reduce(0) { $0 + model.memOf($1) }, memLeft = model.leftover.mem
2842
- let memAll = model.sysMemMb ?? 0
2843
- var cpuParts = [Part(id: "Claude 세션", value: cpuCl, color: pal.claude)], memParts = [Part(id: "Claude 세션", value: memCl, color: pal.claude)]
2844
- if !cx.isEmpty { cpuParts.append(Part(id: "Codex 세션", value: cpuCx, color: pal.codex)); memParts.append(Part(id: "Codex 세션", value: memCx, color: pal.codex)) }
2845
- cpuParts.append(Part(id: "끝난 세션이 남긴 것", value: cpuLeft, color: pal.left)); memParts.append(Part(id: "끝난 세션이 남긴 것", value: memLeft, color: pal.left))
2846
- cpuParts.append(Part(id: "그 밖", value: max(0, cpuAll - cpuCl - cpuCx - cpuLeft), color: pal.neutral)); memParts.append(Part(id: "다른 프로그램", value: model.others.reduce(0) { $0 + $1.mem }, color: pal.neutral))
2955
+ let cpu = cpuSplit(snap, pal), mem = memSplit(snap, pal)
2847
2956
  func tops(_ pick: (AppLoad) -> Double, _ of: (Session) -> Double, _ part: (Work) -> Double, _ show: (Double) -> String) -> [Top] {
2848
2957
  var t = ses.map { s in Top(id: "s\(s.pid)", name: s.name ?? s.agent, value: of(s), color: pal.agent(s.agent), pid: s.pid, program: heaviest(s, total: of(s), part, show), doing: doingNow(s)) }
2849
2958
  t += model.leftoverItems.map { Top(id: "l\($0.id)", name: $0.name, value: pick($0), color: pal.left, origin: leftOrigin($0.id)) }
2850
2959
  t += model.others.map { Top(id: "o\($0.id)", name: $0.name, value: pick($0), color: pal.neutral, origin: "세션과 무관한 프로그램", originSymbol: "macwindow") }
2851
2960
  return Array(t.sorted { $0.value > $1.value }.prefix(5))
2852
2961
  }
2853
- let a = machineTile(title: "CPU", total: pct1(cpuAll), sub: "기기 전체", parts: cpuParts, scale: 100, fmt: { pct1($0) }, top: tops({ $0.cpu * 100 }, { model.cpuOf($0) }, { $0.cpu * 100 }, { pct1($0) }),
2962
+ let a = machineTile(title: "CPU", total: pct1(cpu.total), sub: "기기 전체", parts: cpu.parts, scale: 100, fmt: { pct1($0) }, top: tops({ $0.cpu * 100 }, { model.cpuOf($0) }, { $0.cpu * 100 }, { pct1($0) }),
2854
2963
  series: [(sm.map { $0.sysCpu * 100 }, pal.neutral), (sm.map { ($0.claudeCpu + $0.codexCpu) * 100 }, pal.claude)],
2855
2964
  tip: "세션 값에는 세션이 따로 떼어 띄운 프로세스도 들어갑니다. 회색 선은 기기 전체, 색 선은 실행 중인 세션입니다", pal: pal)
2856
- let b = machineTile(title: "메모리", total: memText(memAll), sub: "/ \(Int((RAM_MB / 1024).rounded())) GB 사용", parts: memParts, scale: RAM_MB, fmt: { memText($0) }, top: tops({ $0.mem }, { model.memOf($0) }, { $0.mem }, { memText($0) }),
2965
+ let b = machineTile(title: "메모리", total: memText(mem.total), sub: "/ \(Int((RAM_MB / 1024).rounded())) GB 사용", parts: mem.parts, scale: RAM_MB, fmt: { memText($0) }, top: tops({ $0.mem }, { model.memOf($0) }, { $0.mem }, { memText($0) }),
2857
2966
  series: [(sm.map { $0.sysMem * 100 }, pal.neutral), (sm.map { ($0.claudeMem + $0.codexMem) * 100 }, pal.claude)],
2858
2967
  tip: "프로그램별 값은 압축된 메모리와 GPU 메모리까지 센 점유량입니다. 그래서 합이 실제 사용량보다 클 수 있습니다", pal: pal)
2859
2968
  return Group { if width >= 900 { HStack(alignment: .top, spacing: 10) { a; b } } else { VStack(spacing: 10) { a; b } } }
@@ -3013,115 +3122,246 @@ struct DashboardView: View {
3013
3122
  if w.mem >= 512 { IconStat(symbol: "memorychip", value: memText(w.mem), tip: "메모리", pal: pal) }
3014
3123
  }
3015
3124
  }
3016
- /** The right column while no session is picked: what is running now, whoever needs the user first. Picking a session shows its detail in this same column. */
3017
- func nowColumn(_ snap: Snapshot, _ pal: Pal) -> some View {
3125
+ func warnText(_ w: Warn) -> String { w.chip.replacingOccurrences(of: "⚠ ", with: "").replacingOccurrences(of: "🚨 ", with: "") }
3126
+ func warnIcon(_ w: Warn, _ pal: Pal) -> some View {
3127
+ Image(systemName: w.level == "critical" ? "exclamationmark.octagon.fill" : "exclamationmark.triangle.fill").font(.system(size: 11)).foregroundColor(w.level == "critical" ? pal.critical : pal.warning)
3128
+ }
3129
+ /** The statusline's warnings as capsules in one row, under the band. */
3130
+ func warnChips(_ ws: [Warn], _ pal: Pal) -> some View {
3131
+ HStack(spacing: 8) {
3132
+ ForEach(ws) { w in
3133
+ HStack(spacing: 6) { warnIcon(w, pal); Text(warnText(w)).font(.system(size: 12, weight: .medium)).foregroundColor(pal.text) }
3134
+ .padding(.horizontal, 9).padding(.vertical, 5).background(Capsule().fill(pal.tile)).help(w.advice?.first?.tip ?? w.advice?.first?.title ?? "")
3135
+ }
3136
+ Spacer(minLength: 0)
3137
+ }
3138
+ }
3139
+ /** One session that is working or waiting for the user. `dense` says in one line what it is doing, where the wide layout lists its programs. */
3140
+ func liveRow(_ s: Session, _ snap: Snapshot, _ pal: Pal, subs: [Session], dense: Bool, pick: @escaping (Session) -> Void) -> some View {
3141
+ let ph = model.phase(s), tint = pal.state(ph), mark = stateMark(s)
3142
+ let byLoad = ph == .active && s.state != "busy" && s.state != "shell" && s.subagents == 0
3143
+ let pending = (s.detail?.recent ?? []).last { $0.pending == true }
3144
+ let name = Text(s.name ?? s.agent).font(.system(size: 12.5, weight: .semibold)).foregroundColor(pal.text)
3145
+ let row = VStack(alignment: .leading, spacing: 6) {
3146
+ HStack(alignment: dense ? .top : .center, spacing: 6) {
3147
+ if ph == .active { LiveDot(color: tint) } else { Image(systemName: mark.symbol).font(.system(size: 11)).foregroundColor(tint).frame(width: 12) }
3148
+ // The narrow layout never cuts a name: it takes a second line first.
3149
+ if dense { name.lineLimit(2).multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true) } else { name.lineLimit(1).truncationMode(.middle) }
3150
+ Spacer(minLength: 4)
3151
+ Text(byLoad ? "프로그램 실행 중" : "\(mark.label) \(sinceText(s.stateSince, now: snap.at))").font(.system(size: 11)).foregroundColor(pal.text2).lineLimit(1).fixedSize()
3152
+ }
3153
+ if dense { doingLine(s, pending, pal) } else {
3154
+ if let p = pending {
3155
+ HStack(alignment: .top, spacing: 6) {
3156
+ Image(systemName: toolSymbol(p.tool)).font(.system(size: 10)).foregroundColor(pal.text2).frame(width: 12, height: 14)
3157
+ Text((p.target ?? "").isEmpty ? toolName(p.tool) : (p.target ?? "")).font(.system(size: 11.5)).foregroundColor(pal.text).lineLimit(2).fixedSize(horizontal: false, vertical: true)
3158
+ }
3159
+ }
3160
+ ForEach(Array(model.working(s, max: 3).enumerated()), id: \.offset) { _, it in
3161
+ VStack(alignment: .leading, spacing: 2) {
3162
+ HStack(spacing: 6) {
3163
+ Image(systemName: "gearshape.2.fill").font(.system(size: 9.5)).foregroundColor(pal.text2).frame(width: 12)
3164
+ Text(it.name).font(.system(size: 11.5, weight: .medium)).foregroundColor(pal.text).lineLimit(1).truncationMode(.middle).help(it.name)
3165
+ }
3166
+ stats(it.w, pal).padding(.leading, 18)
3167
+ }
3168
+ }
3169
+ }
3170
+ if !subs.isEmpty {
3171
+ HStack(spacing: 6) {
3172
+ Image(systemName: "arrow.turn.down.right").font(.system(size: 9.5, weight: .semibold)).foregroundColor(pal.text2).frame(width: 12)
3173
+ Text("띄운 세션 \(subs.count)개").font(.system(size: 11.5)).foregroundColor(pal.text)
3174
+ Spacer(minLength: 4)
3175
+ Text("작업 중 \(subs.filter { model.phase($0) == .active }.count)").font(.system(size: 11, weight: .medium)).monospacedDigit().foregroundColor(pal.text)
3176
+ }
3177
+ }
3178
+ }.padding(10).frame(maxWidth: .infinity, alignment: .leading).background(RoundedRectangle(cornerRadius: 8).fill(pal.well.opacity(0.55)))
3179
+ return Pressable(radius: 8, action: { pick(s) }) { row }.help(dense ? "누르면 이 세션이 열려 있는 터미널로 갑니다" : "")
3180
+ }
3181
+ /** What a working session does right now, in one line: the tool call in flight, else its heaviest program, else that it is writing. Its context fill sits at the end. */
3182
+ @ViewBuilder func doingLine(_ s: Session, _ pending: ToolCall?, _ pal: Pal) -> some View {
3183
+ let top = model.working(s, max: 1).first
3184
+ let what: (symbol: String, text: String)? = pending.map { p in (toolSymbol(p.tool), (p.target ?? "").isEmpty ? toolName(p.tool) : (p.target ?? "")) }
3185
+ ?? top.map { ("gearshape.2.fill", $0.name) } ?? (s.state == "busy" ? ("ellipsis.bubble.fill", "응답을 쓰는 중") : nil)
3186
+ let ctx = s.context?.pct
3187
+ if what != nil || ctx != nil {
3188
+ HStack(alignment: .top, spacing: 6) {
3189
+ if let w = what {
3190
+ Image(systemName: w.symbol).font(.system(size: 10)).foregroundColor(pal.text2).frame(width: 12, height: 14)
3191
+ Text(w.text).font(.system(size: 11.5)).foregroundColor(pal.text).lineLimit(2).multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true)
3192
+ }
3193
+ Spacer(minLength: 4)
3194
+ if let p = ctx { Text("컨텍스트 \(Int(p))%").font(.system(size: 10.5)).monospacedDigit().foregroundColor(severity(p, pal.text2, pal, warnAt: 80, critAt: 95).color).lineLimit(1).fixedSize() }
3195
+ }
3196
+ }
3197
+ }
3198
+ /** A session resting at its prompt: one quiet line. */
3199
+ func idleRow(_ s: Session, _ snap: Snapshot, _ pal: Pal, dense: Bool, pick: @escaping (Session) -> Void) -> some View {
3200
+ let name = Text(s.name ?? s.agent).font(.system(size: 12)).foregroundColor(pal.text2)
3201
+ return Pressable(radius: 6, action: { pick(s) }) {
3202
+ HStack(alignment: dense ? .top : .center, spacing: 6) {
3203
+ Image(systemName: "pause.circle").font(.system(size: 11)).foregroundColor(pal.text2).frame(width: 12)
3204
+ if dense { name.lineLimit(2).multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true) } else { name.lineLimit(1).truncationMode(.middle) }
3205
+ Spacer(minLength: 4)
3206
+ Text(sinceText(s.stateSince, now: snap.at)).font(.system(size: 10.5)).foregroundColor(pal.text2).fixedSize()
3207
+ }.padding(.vertical, 5).padding(.horizontal, 10).frame(maxWidth: .infinity, alignment: .leading)
3208
+ }.help(dense ? "누르면 이 세션이 열려 있는 터미널로 갑니다" : "")
3209
+ }
3210
+ /**
3211
+ * Who is working or waiting for the user, then who rests at the prompt, as rows that can be pressed. It fills the width
3212
+ * it is given: the right column gives it 372 points, the narrow layout the whole window.
3213
+ */
3214
+ func nowList(_ snap: Snapshot, _ pal: Pal, dense: Bool, pick: @escaping (Session) -> Void) -> some View {
3018
3215
  let everyone = model.ordered(snap.sessions)
3019
3216
  let all = everyone.filter { model.parentSession[$0.pid] == nil }
3020
3217
  let subsOf: (Session) -> [Session] = { up in everyone.filter { model.parentSession[$0.pid] == up.pid } }
3021
3218
  let busy: (Session) -> Bool = { s in model.phase(s) != .idle || subsOf(s).contains { model.phase($0) != .idle } }
3022
3219
  let live = all.filter { busy($0) }, idle = all.filter { !busy($0) }
3023
- let body = VStack(alignment: .leading, spacing: 10) {
3024
- Text("지금 도는 것").font(.system(size: 15, weight: .semibold)).foregroundColor(pal.text)
3220
+ return VStack(alignment: .leading, spacing: 10) {
3025
3221
  if live.isEmpty { Text("작업 중인 세션이 없습니다").font(.system(size: 12)).foregroundColor(pal.text2) }
3026
- ForEach(live) { s in
3027
- let ph = model.phase(s), tint = pal.state(ph), mark = stateMark(s)
3028
- let byLoad = ph == .active && s.state != "busy" && s.state != "shell" && s.subagents == 0
3029
- let pending = (s.detail?.recent ?? []).last { $0.pending == true }
3030
- Pressable(radius: 8, action: { selected = s.pid }) {
3031
- VStack(alignment: .leading, spacing: 6) {
3032
- HStack(spacing: 6) {
3033
- if ph == .active { LiveDot(color: tint) } else { Image(systemName: mark.symbol).font(.system(size: 11)).foregroundColor(tint).frame(width: 12) }
3034
- Text(s.name ?? s.agent).font(.system(size: 12.5, weight: .semibold)).foregroundColor(pal.text).lineLimit(1).truncationMode(.middle)
3035
- Spacer(minLength: 4)
3036
- Text(byLoad ? "프로그램 실행 중" : "\(mark.label) \(sinceText(s.stateSince, now: snap.at))").font(.system(size: 11)).foregroundColor(pal.text2).lineLimit(1).fixedSize()
3037
- }
3038
- if let p = pending {
3039
- HStack(alignment: .top, spacing: 6) {
3040
- Image(systemName: toolSymbol(p.tool)).font(.system(size: 10)).foregroundColor(pal.text2).frame(width: 12, height: 14)
3041
- Text((p.target ?? "").isEmpty ? toolName(p.tool) : (p.target ?? "")).font(.system(size: 11.5)).foregroundColor(pal.text).lineLimit(2).fixedSize(horizontal: false, vertical: true)
3042
- }
3043
- }
3044
- ForEach(Array(model.working(s, max: 3).enumerated()), id: \.offset) { _, it in
3045
- VStack(alignment: .leading, spacing: 2) {
3046
- HStack(spacing: 6) {
3047
- Image(systemName: "gearshape.2.fill").font(.system(size: 9.5)).foregroundColor(pal.text2).frame(width: 12)
3048
- Text(it.name).font(.system(size: 11.5, weight: .medium)).foregroundColor(pal.text).lineLimit(1).truncationMode(.middle).help(it.name)
3049
- }
3050
- stats(it.w, pal).padding(.leading, 18)
3051
- }
3052
- }
3053
- let subs = subsOf(s)
3054
- if !subs.isEmpty {
3055
- HStack(spacing: 6) {
3056
- Image(systemName: "arrow.turn.down.right").font(.system(size: 9.5, weight: .semibold)).foregroundColor(pal.text2).frame(width: 12)
3057
- Text("띄운 세션 \(subs.count)개").font(.system(size: 11.5)).foregroundColor(pal.text)
3058
- Spacer(minLength: 4)
3059
- Text("작업 중 \(subs.filter { model.phase($0) == .active }.count)").font(.system(size: 11, weight: .medium)).monospacedDigit().foregroundColor(pal.text)
3060
- }
3061
- }
3062
- }.padding(10).frame(maxWidth: .infinity, alignment: .leading).background(RoundedRectangle(cornerRadius: 8).fill(pal.well.opacity(0.55)))
3063
- }
3064
- }
3222
+ ForEach(live) { s in liveRow(s, snap, pal, subs: subsOf(s), dense: dense, pick: pick) }
3065
3223
  if !idle.isEmpty {
3066
3224
  Text("입력 대기").font(.system(size: 11)).foregroundColor(pal.text2).padding(.top, 4)
3067
- ForEach(idle) { s in
3068
- Pressable(radius: 6, action: { selected = s.pid }) {
3069
- HStack(spacing: 6) {
3070
- Image(systemName: "pause.circle").font(.system(size: 11)).foregroundColor(pal.text2).frame(width: 12)
3071
- Text(s.name ?? s.agent).font(.system(size: 12)).foregroundColor(pal.text2).lineLimit(1).truncationMode(.middle)
3072
- Spacer(minLength: 4)
3073
- Text(sinceText(s.stateSince, now: snap.at)).font(.system(size: 10.5)).foregroundColor(pal.text2)
3074
- }.padding(.vertical, 5).padding(.horizontal, 10).frame(maxWidth: .infinity, alignment: .leading)
3075
- }
3076
- }
3225
+ ForEach(idle) { s in idleRow(s, snap, pal, dense: dense, pick: pick) }
3077
3226
  }
3078
3227
  if model.leftover.n > 0 {
3079
3228
  HStack(spacing: 6) {
3080
3229
  Image(systemName: "moon.zzz.fill").font(.system(size: 11)).foregroundColor(pal.left).frame(width: 12)
3081
3230
  Text("끝난 세션이 남긴 프로세스 \(model.leftover.n)개").font(.system(size: 11.5)).foregroundColor(pal.text)
3082
3231
  Spacer(minLength: 4)
3083
- Text("\(pct1(model.leftover.cpu * 100)), \(memText(model.leftover.mem))").font(.system(size: 11, weight: .medium)).monospacedDigit().foregroundColor(pal.text)
3232
+ Text("\(pct1(model.leftover.cpu * 100)), \(memText(model.leftover.mem))").font(.system(size: 11, weight: .medium)).monospacedDigit().foregroundColor(pal.text).fixedSize()
3084
3233
  }.padding(.top, 6).padding(.horizontal, 10)
3085
3234
  }
3235
+ }.frame(maxWidth: .infinity, alignment: .topLeading)
3236
+ }
3237
+ /** The right column while no session is picked: what is running now, whoever needs the user first. Picking a session shows its detail in this same column. */
3238
+ func nowColumn(_ snap: Snapshot, _ pal: Pal) -> some View {
3239
+ let body = VStack(alignment: .leading, spacing: 10) {
3240
+ Text("지금 도는 것").font(.system(size: 15, weight: .semibold)).foregroundColor(pal.text)
3241
+ nowList(snap, pal, dense: false, pick: { selected = $0.pid })
3086
3242
  Divider().padding(.vertical, 4)
3087
3243
  trends(pal)
3088
3244
  }.padding(14).frame(width: 372, alignment: .topLeading)
3089
3245
  return Group { if still { body } else { ScrollView { body } } }
3090
3246
  }
3091
- func content(_ width: CGFloat) -> some View {
3092
- let pal = Pal(dark: scheme == .dark)
3247
+ /** What scrolls under the band: the machine detail when it is open, then the project groups. */
3248
+ func scrollRegion(_ snap: Snapshot, _ pal: Pal, inner: CGFloat) -> some View {
3249
+ let body = VStack(alignment: .leading, spacing: 12) {
3250
+ if showDetail {
3251
+ machine(snap, pal, width: inner)
3252
+ if model.gpu != nil { gpuCard(snap, pal) }
3253
+ }
3254
+ if snap.sessions.isEmpty { Text("실행 중인 세션이 없습니다").font(.system(size: 13)).foregroundColor(pal.text2).padding(.top, 20).frame(maxWidth: .infinity) } else { groupsView(snap, pal, width: inner) }
3255
+ }
3256
+ return Group { if still { body } else { ScrollView { body } } }
3257
+ }
3258
+ /** The band, the warnings and the scroll region. The band and the warnings stay put while the rest scrolls. */
3259
+ func leftColumn(_ pal: Pal, inner: CGFloat) -> some View {
3260
+ VStack(alignment: .leading, spacing: 12) {
3261
+ if let snap = model.snap {
3262
+ band(snap, pal)
3263
+ if let ws = snap.warnings, !ws.isEmpty { warnChips(ws, pal) }
3264
+ if snap.sessions.isEmpty && !showDetail { Text("실행 중인 세션이 없습니다").font(.system(size: 13)).foregroundColor(pal.text2).frame(maxWidth: .infinity, maxHeight: .infinity) }
3265
+ else { scrollRegion(snap, pal, inner: inner) }
3266
+ } else {
3267
+ Text(model.failure ?? "불러오는 중입니다").font(.system(size: 13)).foregroundColor(pal.text2).frame(maxWidth: .infinity, maxHeight: .infinity)
3268
+ }
3269
+ }.padding(.horizontal, 18).padding(.bottom, 18).padding(.top, still ? 12 : TITLE_BAR + 10).frame(maxWidth: .infinity, maxHeight: still ? nil : .infinity, alignment: .topLeading)
3270
+ }
3271
+ /** Wide: the left column and a 372 point column on the right that holds what is running now, or the picked session. */
3272
+ func largeLayout(_ pal: Pal, width: CGFloat) -> some View {
3093
3273
  let open = selected.flatMap { pid in model.snap?.sessions.first { $0.pid == pid } }
3094
- let inner = max(318, width - 372 - 36)
3095
3274
  return HStack(alignment: .top, spacing: 0) {
3096
- VStack(alignment: .leading, spacing: 12) {
3097
- if let snap = model.snap {
3098
- machine(snap, pal, width: inner)
3099
- kpis(snap, pal, width: inner)
3100
- if let ws = snap.warnings, !ws.isEmpty {
3101
- HStack(spacing: 8) {
3102
- ForEach(ws) { w in
3103
- HStack(spacing: 6) {
3104
- Image(systemName: w.level == "critical" ? "exclamationmark.octagon.fill" : "exclamationmark.triangle.fill").font(.system(size: 11)).foregroundColor(w.level == "critical" ? pal.critical : pal.warning)
3105
- Text(w.chip.replacingOccurrences(of: "⚠ ", with: "").replacingOccurrences(of: "🚨 ", with: "")).font(.system(size: 12, weight: .medium)).foregroundColor(pal.text)
3106
- }.padding(.horizontal, 9).padding(.vertical, 5).background(Capsule().fill(pal.tile)).help(w.advice?.first?.tip ?? w.advice?.first?.title ?? "")
3107
- }
3108
- Spacer(minLength: 0)
3275
+ leftColumn(pal, inner: max(318, width - 372 - 36))
3276
+ if let snap = model.snap {
3277
+ Group { if let s = open { inspector(s, snap, pal) } else { nowColumn(snap, pal) } }.padding(.top, still ? 0 : TITLE_BAR + 10).frame(width: 372, alignment: .top)
3278
+ }
3279
+ }.background(alignment: .topTrailing) { Rectangle().fill(pal.tile).frame(width: 372) }
3280
+ }
3281
+ /** Middle: no right column. A picked session's detail lies over the trailing edge, so nothing under it moves when it opens or closes. */
3282
+ func mediumLayout(_ pal: Pal, width: CGFloat) -> some View {
3283
+ let open = selected.flatMap { pid in model.snap?.sessions.first { $0.pid == pid } }
3284
+ return ZStack(alignment: .topTrailing) {
3285
+ leftColumn(pal, inner: width - 36)
3286
+ if let s = open, let snap = model.snap {
3287
+ inspector(s, snap, pal).padding(.top, still ? 0 : TITLE_BAR + 10).frame(width: 372).frame(maxHeight: still ? nil : .infinity, alignment: .top)
3288
+ .background(pal.tile).shadow(color: Color.black.opacity(pal.dark ? 0.5 : 0.2), radius: 10, x: -4, y: 0)
3289
+ }
3290
+ }
3291
+ }
3292
+ func metric(_ label: String, _ value: String, _ pal: Pal, color: Color? = nil) -> some View {
3293
+ HStack(spacing: 3) {
3294
+ Text(label).font(.system(size: 10.5)).foregroundColor(pal.text2)
3295
+ Text(value).font(.system(size: 11, weight: .medium)).monospacedDigit().foregroundColor(color ?? pal.text)
3296
+ }.lineLimit(1).fixedSize()
3297
+ }
3298
+ /** The machine figures and the account limits on one line, the limits dropped one by one when the width runs out. */
3299
+ func metricsLine(_ snap: Snapshot, _ pal: Pal, limits: Int) -> some View {
3300
+ let wins = Array((snap.usage?.claude?.windows ?? []).prefix(limits))
3301
+ return HStack(spacing: 10) {
3302
+ metric("CPU", pct1(cpuSplit(snap, pal).total), pal)
3303
+ metric("메모리", memText(memSplit(snap, pal).total), pal)
3304
+ if model.gpu != nil { metric("GPU", pct1(gpuSplit(snap, pal).total), pal) }
3305
+ ForEach(wins, id: \.key) { w in metric("\(windowName(w)) 한도", "\(Int(w.usedPct.rounded()))%", pal, color: severity(w.usedPct, pal.text, pal).color) }
3306
+ Spacer(minLength: 0)
3307
+ }
3308
+ }
3309
+ func miniHeader(_ snap: Snapshot, _ pal: Pal) -> some View {
3310
+ let n = counts(snap)
3311
+ return VStack(alignment: .leading, spacing: 5) {
3312
+ HStack(spacing: 6) {
3313
+ if n.active > 0 { LiveDot(color: pal.good) }
3314
+ Text("작업 중 \(n.active)").font(.system(size: 15, weight: .semibold)).foregroundColor(pal.text).fixedSize()
3315
+ Text("/ \(n.mains)").font(.system(size: 12)).foregroundColor(pal.text2).fixedSize()
3316
+ Spacer(minLength: 6)
3317
+ if n.user > 0 { chip("hand.raised.fill", pal.warning, "확인 대기 \(n.user)", pal) }
3318
+ if n.idle > 0 { chip("pause.circle", pal.text2, "입력 대기 \(n.idle)", pal) }
3319
+ }
3320
+ ViewThatFits(in: .horizontal) {
3321
+ metricsLine(snap, pal, limits: 4)
3322
+ metricsLine(snap, pal, limits: 1)
3323
+ metricsLine(snap, pal, limits: 0)
3324
+ }
3325
+ }.padding(.horizontal, 12).padding(.top, still ? 10 : TITLE_BAR + 6).padding(.bottom, 8)
3326
+ }
3327
+ /** Narrow: one column of sessions on the tile colour, a header with the counts and the machine, and every session as a row. */
3328
+ func miniLayout(_ pal: Pal) -> some View {
3329
+ VStack(alignment: .leading, spacing: 0) {
3330
+ if let snap = model.snap {
3331
+ miniHeader(snap, pal)
3332
+ if let ws = snap.warnings, !ws.isEmpty {
3333
+ VStack(alignment: .leading, spacing: 4) {
3334
+ ForEach(ws) { w in
3335
+ HStack(alignment: .top, spacing: 6) {
3336
+ warnIcon(w, pal).frame(height: 15)
3337
+ Text(warnText(w)).font(.system(size: 12, weight: .medium)).foregroundColor(pal.text).multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true)
3338
+ Spacer(minLength: 0)
3339
+ }.help(w.advice?.first?.tip ?? w.advice?.first?.title ?? "")
3109
3340
  }
3110
- }
3111
- if snap.sessions.isEmpty { Text("실행 중인 세션이 없습니다").font(.system(size: 13)).foregroundColor(pal.text2).frame(maxWidth: .infinity, maxHeight: .infinity) }
3112
- else if still { groupsView(snap, pal, width: inner) } else { ScrollView { groupsView(snap, pal, width: inner) } }
3113
- } else {
3114
- Text(model.failure ?? "불러오는 중입니다").font(.system(size: 13)).foregroundColor(pal.text2).frame(maxWidth: .infinity, maxHeight: .infinity)
3341
+ }.padding(.horizontal, 12).padding(.bottom, 8)
3115
3342
  }
3116
- }.padding(.horizontal, 18).padding(.bottom, 18).padding(.top, 38).frame(maxWidth: .infinity, maxHeight: still ? nil : .infinity, alignment: .topLeading)
3117
- if let snap = model.snap {
3118
- Group { if let s = open { inspector(s, snap, pal) } else { nowColumn(snap, pal) } }.padding(.top, still ? 0 : 28).frame(width: 372, alignment: .top)
3343
+ Divider()
3344
+ let list = nowList(snap, pal, dense: true, pick: { jump(to: $0) }).padding(10)
3345
+ if still { list } else { ScrollView { list } }
3346
+ } else {
3347
+ Text(model.failure ?? "불러오는 중입니다").font(.system(size: 13)).foregroundColor(pal.text2).padding(.top, still ? 10 : TITLE_BAR + 6).padding(.horizontal, 12).frame(maxWidth: .infinity, maxHeight: .infinity)
3119
3348
  }
3120
- }.frame(minWidth: 1060, maxWidth: .infinity, minHeight: 420, maxHeight: still ? nil : .infinity, alignment: .topLeading)
3121
- .background(alignment: .topTrailing) { Rectangle().fill(pal.tile).frame(width: 372) }.background(pal.surface)
3349
+ }.frame(maxWidth: .infinity, maxHeight: still ? nil : .infinity, alignment: .topLeading).background(pal.tile)
3350
+ }
3351
+ func content(_ width: CGFloat) -> some View {
3352
+ let pal = Pal(dark: scheme == .dark)
3353
+ let mode = DashSize.of(width)
3354
+ return Group {
3355
+ switch mode {
3356
+ case .large: largeLayout(pal, width: width)
3357
+ case .medium: mediumLayout(pal, width: width)
3358
+ case .mini: miniLayout(pal)
3359
+ }
3360
+ }.frame(minWidth: mode == .large ? 1100 : mode == .medium ? 620 : 300, maxWidth: .infinity, minHeight: 200, maxHeight: still ? nil : .infinity, alignment: .topLeading)
3361
+ .background(pal.surface)
3122
3362
  }
3123
3363
  var body: some View {
3124
- if still { content(stillWidth) } else { GeometryReader { g in content(g.size.width) } }
3364
+ if still { content(stillWidth) } else { GeometryReader { g in content(g.size.width) }.ignoresSafeArea() }
3125
3365
  }
3126
3366
  }
3127
3367
  /** The animal is a rank, so the rank is said beside it: which species, where in the company, and what comes next. */
@@ -3376,7 +3616,7 @@ func dockIcon(_ grade: String?) -> NSImage {
3376
3616
  }
3377
3617
  }
3378
3618
  /** The few menu commands a window needs once the app is a regular one: close, minimise, quit. */
3379
- func dashboardMenu() -> NSMenu {
3619
+ func dashboardMenu(_ app: App) -> NSMenu {
3380
3620
  let bar = NSMenu()
3381
3621
  let appItem = NSMenuItem(), appMenu = NSMenu()
3382
3622
  appMenu.addItem(NSMenuItem(title: "Sprag 끝내기", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
@@ -3384,9 +3624,20 @@ func dashboardMenu() -> NSMenu {
3384
3624
  let winItem = NSMenuItem(), winMenu = NSMenu(title: "윈도우")
3385
3625
  winMenu.addItem(NSMenuItem(title: "닫기", action: #selector(NSWindow.performClose(_:)), keyEquivalent: "w"))
3386
3626
  winMenu.addItem(NSMenuItem(title: "최소화", action: #selector(NSWindow.performMiniaturize(_:)), keyEquivalent: "m"))
3627
+ winMenu.addItem(.separator())
3628
+ for (title, key, action) in [("작게", "1", #selector(App.dashMini)), ("중간", "2", #selector(App.dashMedium)), ("크게", "3", #selector(App.dashLarge)), ("항상 위에 두기", "", #selector(App.dashPinMenu))] {
3629
+ let it = NSMenuItem(title: title, action: action, keyEquivalent: key); it.target = app; winMenu.addItem(it)
3630
+ }
3387
3631
  winItem.submenu = winMenu; bar.addItem(winItem)
3388
3632
  return bar
3389
3633
  }
3634
+ extension App: NSMenuItemValidation {
3635
+ /** The checkmark of "항상 위에 두기" follows the pin, whichever of the two controls changed it. */
3636
+ func validateMenuItem(_ item: NSMenuItem) -> Bool {
3637
+ if item.action == #selector(App.dashPinMenu) { item.state = dashUI.pinned ? .on : .off }
3638
+ return true
3639
+ }
3640
+ }
3390
3641
  extension App: NSApplicationDelegate {
3391
3642
  /** A click on the Dock icon: show the dashboard again, wherever it went. */
3392
3643
  func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { showDashboard(); return false }
@@ -3403,6 +3654,7 @@ final class App: NSObject {
3403
3654
  var frame = 0
3404
3655
  var outside: Any?, keys: Any?
3405
3656
  var dash: NSWindow?
3657
+ let dashUI = DashUI()
3406
3658
  var isOpen: Bool { panel.isVisible }
3407
3659
  var dashOpen: Bool { dash?.isVisible == true }
3408
3660
  /** Each session's work is asked for only while something that shows it is on screen. */
@@ -3417,22 +3669,60 @@ final class App: NSObject {
3417
3669
  if dash == nil {
3418
3670
  let room = NSScreen.main?.visibleFrame.size ?? NSSize(width: 1480, height: 900)
3419
3671
  let w = NSWindow(contentRect: NSRect(x: 0, y: 0, width: min(1480, room.width - 40), height: min(900, room.height - 60)), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
3420
- w.title = "Sprag 대시보드"; w.isReleasedWhenClosed = false; w.titlebarAppearsTransparent = true; w.minSize = NSSize(width: 1060, height: 560)
3672
+ w.title = "Sprag 대시보드"; w.isReleasedWhenClosed = false; w.titlebarAppearsTransparent = true; w.minSize = NSSize(width: 300, height: 200)
3421
3673
  // A plain surface, not a see-through material: the chart colours were checked against this one.
3422
3674
  w.contentView = NSHostingView(rootView: DashboardView(model: model)); w.center()
3675
+ // The last size and place come back next time. The self-test keeps the default frame and leaves the user's saved one alone.
3676
+ if selftest == nil { w.setFrameUsingName("SpragDashboard"); w.setFrameAutosaveName("SpragDashboard"); dashUI.pinned = UserDefaults.standard.bool(forKey: "dashPinned") }
3677
+ w.level = dashUI.pinned ? .floating : .normal
3678
+ let controls = NSHostingView(rootView: DashControls(ui: dashUI, pick: { [weak self] in self?.setDashSize($0) }, pin: { [weak self] in self?.toggleDashPin() }))
3679
+ controls.frame.size = NSSize(width: controls.fittingSize.width, height: 28)
3680
+ let accessory = NSTitlebarAccessoryViewController(); accessory.view = controls; accessory.layoutAttribute = .right
3681
+ w.addTitlebarAccessoryViewController(accessory)
3682
+ NotificationCenter.default.addObserver(forName: NSWindow.didResizeNotification, object: w, queue: .main) { [weak self] _ in self?.syncDashSize() }
3423
3683
  NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification, object: w, queue: .main) { [weak self] _ in DispatchQueue.main.async { self?.syncDetail(); NSApp.setActivationPolicy(.accessory) } }
3424
3684
  dash = w
3685
+ syncDashSize()
3425
3686
  }
3426
3687
  // A menu bar app has no Dock icon, so a dashboard that slipped behind other windows could only be found through the
3427
3688
  // menu bar again. While the window is open the app is a regular one: Dock icon, app switcher, and a click on either brings it back.
3428
3689
  NSApp.setActivationPolicy(.regular)
3429
3690
  dockGrade = model.snap?.animal?.grade; NSApp.applicationIconImage = dockIcon(dockGrade)
3430
- if NSApp.mainMenu == nil { NSApp.mainMenu = dashboardMenu() }
3691
+ if NSApp.mainMenu == nil { NSApp.mainMenu = dashboardMenu(self) }
3431
3692
  NSApp.activate(ignoringOtherApps: true)
3432
3693
  if dash?.isMiniaturized == true { dash?.deminiaturize(nil) }
3433
3694
  dash?.makeKeyAndOrderFront(nil)
3434
3695
  syncDetail()
3435
3696
  }
3697
+ /** The control and the title follow the window's real width, so a dragged edge and a pressed preset end up the same. */
3698
+ func syncDashSize() {
3699
+ guard let w = dash, let v = w.contentView else { return }
3700
+ let s = DashSize.of(v.bounds.width)
3701
+ if dashUI.size != s { dashUI.size = s }
3702
+ let title: NSWindow.TitleVisibility = s == .mini ? .hidden : .visible // the narrow title bar belongs to the controls
3703
+ if w.titleVisibility != title { w.titleVisibility = title }
3704
+ }
3705
+ /** Resize the window to one of the three layouts. The top edge stays, the side nearer the screen edge stays, and the result is kept on screen. */
3706
+ func setDashSize(_ s: DashSize, animate: Bool = true) {
3707
+ guard let w = dash else { return }
3708
+ let vis = (w.screen ?? NSScreen.main)?.visibleFrame ?? NSRect(x: 0, y: 0, width: 1480, height: 900)
3709
+ let size: NSSize = s == .mini ? NSSize(width: 340, height: 480) : s == .medium ? NSSize(width: 780, height: 680) : NSSize(width: min(1480, vis.width - 40), height: min(900, vis.height - 60))
3710
+ let now = w.frame
3711
+ var f = NSRect(x: now.midX > vis.midX ? now.maxX - size.width : now.minX, y: now.maxY - size.height, width: size.width, height: size.height)
3712
+ f.size.width = min(f.width, vis.width); f.size.height = min(f.height, vis.height)
3713
+ f.origin.x = max(vis.minX, min(f.origin.x, vis.maxX - f.width)); f.origin.y = max(vis.minY, min(f.origin.y, vis.maxY - f.height))
3714
+ w.setFrame(f, display: true, animate: animate)
3715
+ }
3716
+ /** Keep the dashboard above other windows, and remember that for the next start. */
3717
+ @objc func toggleDashPin() {
3718
+ dashUI.pinned.toggle()
3719
+ dash?.level = dashUI.pinned ? .floating : .normal
3720
+ if selftest == nil { UserDefaults.standard.set(dashUI.pinned, forKey: "dashPinned") }
3721
+ }
3722
+ @objc func dashMini() { setDashSize(.mini) }
3723
+ @objc func dashMedium() { setDashSize(.medium) }
3724
+ @objc func dashLarge() { setDashSize(.large) }
3725
+ @objc func dashPinMenu() { toggleDashPin() }
3436
3726
  func start() {
3437
3727
  panel = FloatPanel(contentRect: NSRect(x: 0, y: 0, width: 408, height: 300), styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: true)
3438
3728
  panel.isOpaque = false; panel.backgroundColor = .clear; panel.hasShadow = true; panel.level = .popUpMenu
@@ -3757,6 +4047,31 @@ func sessionTest(_ prefix: String) {
3757
4047
  fflush(stdout); dashboardTest(prefix)
3758
4048
  }
3759
4049
  }
4050
+ /**
4051
+ * Put the real window through its three sizes, as a press on the title bar would: after each one the layout the width picks
4052
+ * and the control have to agree with the size asked for, the frame has to be on the screen, and the content is captured.
4053
+ * Then the pin: the window must float after one press, and goes back to normal after the second.
4054
+ */
4055
+ func dashSizeTest(_ prefix: String, done: @escaping () -> Void) {
4056
+ var todo = DashSize.allCases
4057
+ func next() {
4058
+ guard !todo.isEmpty else {
4059
+ main.toggleDashPin(); print("DASHPIN floating after toggle: \(main.dash?.level == .floating)"); main.toggleDashPin()
4060
+ fflush(stdout); done(); return
4061
+ }
4062
+ let s = todo.removeFirst()
4063
+ main.setDashSize(s, animate: false)
4064
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
4065
+ if let w = main.dash, let v = w.contentView {
4066
+ let vis = (w.screen ?? NSScreen.main)?.visibleFrame ?? .zero
4067
+ print("DASHSIZE \(s.rawValue) window \(Int(w.frame.width))x\(Int(w.frame.height)) | layout \(DashSize.of(v.bounds.width).rawValue) | control \(main.dashUI.size.rawValue) | inside screen \(vis.contains(w.frame))")
4068
+ liveShot(v, "\(prefix)-dashboard-live-\(s.rawValue).png")
4069
+ }
4070
+ next()
4071
+ }
4072
+ }
4073
+ next()
4074
+ }
3760
4075
  func dashboardTest(_ prefix: String) {
3761
4076
  main.showDashboard()
3762
4077
  DispatchQueue.main.asyncAfter(deadline: .now() + 4.0) {
@@ -3768,6 +4083,10 @@ func dashboardTest(_ prefix: String) {
3768
4083
  print("STARTED BY " + (m.parentSession.isEmpty ? "none" : m.parentSession.map { c, up in "\(m.snap?.sessions.first { $0.pid == c }?.name ?? "\(c)") <- \(m.snap?.sessions.first { $0.pid == up }?.name ?? "\(up)")" }.sorted().joined(separator: " | ")))
3769
4084
  print("PHASE " + (m.snap?.sessions ?? []).map { s in "\(s.name ?? s.agent) \(m.phase(s)) [" + m.working(s, max: 3).map { "\($0.name) cpu \(pct1($0.w.cpu * 100)) gpu \(pct1($0.w.gpu * 100)) \(memText($0.w.mem))" }.joined(separator: "; ") + "]" }.joined(separator: " | "))
3770
4085
  render(DashboardView(model: m, still: true).frame(width: 1480), "\(prefix)-dashboard-dark.png")
4086
+ render(DashboardView(model: m, still: true, width: 340).frame(width: 340), "\(prefix)-dashboard-mini-dark.png")
4087
+ render(DashboardView(model: m, still: true, width: 780).frame(width: 780), "\(prefix)-dashboard-medium-dark.png")
4088
+ if let first = m.snap?.sessions.first { render(DashboardView(model: m, still: true, preselect: first.pid, width: 780).frame(width: 780), "\(prefix)-dashboard-medium-open-dark.png") }
4089
+ render(DashboardView(model: m, still: true, detail: true).frame(width: 1480), "\(prefix)-dashboard-detail-dark.png")
3771
4090
  render(DashboardView(model: m, still: true).frame(width: 1480), "\(prefix)-dashboard-light.png", dark: false)
3772
4091
  if let first = m.snap?.sessions.first { render(DashboardView(model: m, still: true, lit: first.pid).frame(width: 1480), "\(prefix)-dashboard-hover-dark.png") }
3773
4092
  print("GPU device \(m.gpu.map { pct1($0 * 100) } ?? "-") | processes on it \(m.gpuUse.count) | sessions on it \(m.gpuBySession.count) | top " + m.gpuUse.prefix(4).map { "\($0.name) \(pct1($0.share * 100)) \($0.session.map { "session \($0)" } ?? "no session")" }.joined(separator: "; "))
@@ -3785,18 +4104,20 @@ func dashboardTest(_ prefix: String) {
3785
4104
  render(DashboardView(model: m, still: true, cleaning: true).frame(width: 1480), "\(prefix)-dashboard-clean-dark.png")
3786
4105
  print("DASHBOARD history samples \(m.samples.count) per-session \(m.hist.values.map { $0.count }.sorted())")
3787
4106
  if let v = main.dash?.contentView { liveShot(v, "\(prefix)-dashboard-live.png") }
3788
- print("LEFTOVER lines \(m.leftoverItems.count) | never cleaned (apps, virtual machines, databases): \(m.leftoverItems.filter { m.leftoverShared.contains($0.id) }.map { $0.name }.joined(separator: ", "))")
3789
- // which session each line would reopen, and the folder that session worked in (the id itself is not printed)
3790
- print("LEFTOVER sessions " + m.leftoverItems.map { it in "\(it.name)=" + (m.leftoverSession[it.id].map { sessionCwd($0).map { ($0 as NSString).lastPathComponent } ?? "no transcript" } ?? "none") }.joined(separator: ", "))
3791
- if let target = selftestClean {
3792
- let hit = m.leftoverItems.first { $0.name == target }
3793
- print("CLEAN target '\(target)': \(hit.map { "\($0.n) process(es)" } ?? "not listed") | protected \(m.leftoverShared.contains(target))")
3794
- print("CLEAN signals sent: \(m.stopLeftovers([target]))")
3795
- }
3796
- main.dash?.close()
3797
- DispatchQueue.main.asyncAfter(deadline: .now() + (selftestClean == nil ? 0.6 : 6.0)) {
3798
- if let target = selftestClean { print("CLEAN afterwards: '\(target)' \(m.leftoverItems.contains { $0.name == target } ? "still listed" : "gone") | leftover lines \(m.leftoverItems.count)") }
3799
- print("DASHBOARD closed, asked for detail \(m.wantDetail) | back to a menu bar app \(NSApp.activationPolicy() == .accessory)"); fflush(stdout); NSApp.terminate(nil)
4107
+ dashSizeTest(prefix) {
4108
+ print("LEFTOVER lines \(m.leftoverItems.count) | never cleaned (apps, virtual machines, databases): \(m.leftoverItems.filter { m.leftoverShared.contains($0.id) }.map { $0.name }.joined(separator: ", "))")
4109
+ // which session each line would reopen, and the folder that session worked in (the id itself is not printed)
4110
+ print("LEFTOVER sessions " + m.leftoverItems.map { it in "\(it.name)=" + (m.leftoverSession[it.id].map { sessionCwd($0).map { ($0 as NSString).lastPathComponent } ?? "no transcript" } ?? "none") }.joined(separator: ", "))
4111
+ if let target = selftestClean {
4112
+ let hit = m.leftoverItems.first { $0.name == target }
4113
+ print("CLEAN target '\(target)': \(hit.map { "\($0.n) process(es)" } ?? "not listed") | protected \(m.leftoverShared.contains(target))")
4114
+ print("CLEAN signals sent: \(m.stopLeftovers([target]))")
4115
+ }
4116
+ main.dash?.close()
4117
+ DispatchQueue.main.asyncAfter(deadline: .now() + (selftestClean == nil ? 0.6 : 6.0)) {
4118
+ if let target = selftestClean { print("CLEAN afterwards: '\(target)' \(m.leftoverItems.contains { $0.name == target } ? "still listed" : "gone") | leftover lines \(m.leftoverItems.count)") }
4119
+ print("DASHBOARD closed, asked for detail \(m.wantDetail) | back to a menu bar app \(NSApp.activationPolicy() == .accessory)"); fflush(stdout); NSApp.terminate(nil)
4120
+ }
3800
4121
  }
3801
4122
  }
3802
4123
  }