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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Kaern/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
#if DEBUG
// DEV or TEST on the Dock icon, so a Debug build isn't taken for the installed Kaern (DockTag).
DockTag.install()
// Debug commands (stage, commit, discard…) are broadcast to every running Kaern. Only an
// instance on a test session listens, so a Debug build open on your own repositories never
// acts on commands a test run sends to its sample repositories.
Expand Down
19 changes: 19 additions & 0 deletions Kaern/App/DebugDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ final class DebugDriver {
switch command {
case "snapshot":
snapshot(to: args.first ?? "/tmp/kaern.png", window: args.count > 1 ? Int(args[1]) : nil)
case "dockTile":
// dockTile <path> [dev|test]: the Dock tile's drawing; with a kind, that tag's instead of this instance's.
dockTilePicture(to: args.first ?? "/tmp/kaern-dock.png", kind: args.count > 1 ? DockTag(rawValue: args[1].uppercased()) : nil)
case "appearance":
NSApp.appearance = args.first == "dark" ? NSAppearance(named: .darkAqua) : NSAppearance(named: .aqua)
case "activate":
Expand Down Expand Up @@ -983,6 +986,20 @@ final class DebugDriver {
try? data.write(to: URL(filePath: path))
}

/// The Dock tile's drawing (`DockTag`) as a PNG at twice the tile's size, as a Retina Dock draws it.
private func dockTilePicture(to path: String, kind: DockTag?) {
let size = NSApp.dockTile.size
guard let view = kind.map({ DockTagView($0, size: size) }) ?? NSApp.dockTile.contentView,
let rep = NSBitmapImageRep(
bitmapDataPlanes: nil, pixelsWide: Int(size.width * 2), pixelsHigh: Int(size.height * 2), bitsPerSample: 8,
samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)
else { return }
rep.size = size
view.cacheDisplay(in: view.bounds, to: rep)
guard let data = rep.representation(using: .png, properties: [:]) else { return }
try? data.write(to: URL(filePath: path))
}

private func dump(to path: String) {
var windows: [[String: Any]] = []
for controller in app.windows {
Expand Down Expand Up @@ -1142,6 +1159,8 @@ final class DebugDriver {
"fieldText": editor?.string ?? "",
"fieldPlaceholder": field?.placeholderString ?? field?.placeholderAttributedString?.string ?? "",
"appearance": NSApp.effectiveAppearance.isDark ? "dark" : "light",
// The tag on the Dock icon (DockTag): TEST on a test instance.
"dockTag": DockTag.shown?.rawValue ?? "",
"pointerBusy": pointerTasks > 0,
"drag": ["sequence": lastDragSequence, "handle": lastResizeHandle, "value": lastResizeValue,
"storedAfterChange": sizesAfterDragChange, "storedAfterMouseUp": sizesAfterMouseUp] as [String: Any],
Expand Down
86 changes: 86 additions & 0 deletions Kaern/App/DockTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#if DEBUG
import AppKit

/// A tag on a Debug build's Dock icon, so it isn't taken for the installed Kaern beside it: DEV on a build you run
/// yourself (build_and_run.sh, Xcode), TEST on an instance of a test session (`-KaernSessionFile`: the flow checks,
/// checks/launch.sh). Release builds have none of it.
///
/// Only the Dock tile changes (`NSDockTile.contentView`): the app's icon (`NSApp.applicationIconImage`) stays as it
/// is, so the alerts drawn in the window, and the checks' pictures of them, keep the plain one.
enum DockTag: String {
case dev = "DEV"
case test = "TEST"

/// This instance's: a test session's, or a build of your own.
static var current: DockTag { SessionStore.testSessionPath != nil ? .test : .dev }

/// The tag on this instance's Dock tile, once `install()` has run.
private(set) static var shown: DockTag?

var color: NSColor {
switch self {
case .dev: NSColor(srgbRed: 0.96, green: 0.45, blue: 0.05, alpha: 1)
case .test: NSColor(srgbRed: 0.53, green: 0.32, blue: 0.92, alpha: 1)
}
}

static func install() {
let tile = NSApp.dockTile
tile.contentView = DockTagView(current, size: tile.size)
tile.display()
shown = current
}
}

/// The app's icon with a tag on its top right corner. Drawn in the icon's own grid (an 824-unit tile in a 1024-unit
/// canvas, `script/make_icon.swift`), the tag and its shadow inside the canvas: the Dock shows nothing past it.
final class DockTagView: NSView {
let kind: DockTag

init(_ kind: DockTag, size: NSSize) {
self.kind = kind
super.init(frame: NSRect(origin: .zero, size: size))
}

@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }

override func draw(_ dirtyRect: NSRect) {
guard let context = NSGraphicsContext.current else { return }
context.imageInterpolation = .high
NSApp.applicationIconImage?.draw(in: bounds)

NSGraphicsContext.saveGraphicsState()
let grid = NSAffineTransform()
grid.scale(by: bounds.width / 1024)
grid.concat()
let kern: CGFloat = 6
let font = NSFont.systemFont(ofSize: 162, weight: .heavy)
let text = NSAttributedString(string: kind.rawValue, attributes: [.font: font, .foregroundColor: NSColor.white, .kern: kern])
let textWidth = text.size().width - kern // no kern after the last letter
let height: CGFloat = 270
let tag = NSRect(x: 990 - (textWidth + 120), y: 990 - height, width: textWidth + 120, height: height)
let path = NSBezierPath(roundedRect: tag, xRadius: 68, yRadius: 68)

// A shadow's blur and offset are in device pixels whatever the drawing's scale: scaled to the grid here, or a
// small tile's shadow would spread past the canvas and be cut off at its edge.
NSGraphicsContext.saveGraphicsState()
let scale = hypot(context.cgContext.ctm.a, context.cgContext.ctm.b)
let shadow = NSShadow()
shadow.shadowColor = NSColor(white: 0, alpha: 0.28)
shadow.shadowBlurRadius = 12 * scale
shadow.shadowOffset = NSSize(width: 0, height: -6 * scale)
shadow.set()
kind.color.setFill()
path.fill()
NSGraphicsContext.restoreGraphicsState()

NSColor.white.setStroke()
path.lineWidth = 22
path.stroke()
// The capitals centred by their cap height.
text.draw(at: NSPoint(x: tag.midX - textWidth / 2, y: tag.midY - font.capHeight / 2 + font.descender))
NSGraphicsContext.restoreGraphicsState()
}
}
#endif
4 changes: 4 additions & 0 deletions checks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ are the same. The menus and popovers a check opens come up beside them, where th
instance's screens answer from there as well, `TestSession.moveScreensAside`), so the pictures have them too.
`KAERN_ON_SCREEN=1` keeps the windows on the screen, to watch a check.

A test instance's Dock icon has a purple TEST tag, and a Debug build you run yourself an orange DEV one
(`Kaern/App/DockTag.swift`), so neither is taken for the Kaern you use. `t_isolation` checks it and keeps a picture
of each tag in `shots/`.

## Where things are

`python3 checks/paths.py` prints the work folder and the app. The work folder is in the temporary folder, one per
Expand Down
7 changes: 7 additions & 0 deletions checks/t_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def real_state():
real_before = real_state()
start_app('light', columns=False, git_config=False)
d = wait_launched()
# Its Dock icon has the TEST tag, so it isn't taken for your own Kaern, or for a Debug build you run yourself (DEV).
tag = d['driver']['dockTag']; print('dock tag:', tag)
assert tag == 'TEST', 'a test instance shows %r on its Dock icon' % tag
for kind in ('test', 'dev'):
tile = shot_path('isolation-dock-tile-' + kind, 'light')
k('dockTile', tile, kind); sync()
assert os.path.getsize(tile) > 0, 'no picture of the %s Dock tile' % kind
genv = d['gitConfigEnvironment']; print('git config environment:', genv)
assert genv.get('GIT_CONFIG_GLOBAL') == STEM + '.gitconfig'
assert '' in genv.values() and any(v.startswith('store --file=') for v in genv.values())
Expand Down