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
18 changes: 11 additions & 7 deletions Sources/PureMouseSettingsUI/AppsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct AppsPane: View {
// Tied to the app: switching from one to another (the status menu's "Settings for
// <app>…") starts a page, so no editor, picker, disclosure or icon is left over.
AppPage(key: key, identity: identities.identity(key), segment: list.segment,
select: { list.select($0) }, back: { list.close() }, remove: { remove(key) })
select: { segment in navigate { list.select(segment) } },
back: { navigate { list.close() } }, remove: { navigate { remove(key) } })
.id(key)
} else {
appList
Expand Down Expand Up @@ -80,7 +81,7 @@ struct AppsPane: View {
} else {
ForEach(apps, id: \.self) { id in
AppRow(identity: identities.identity(id), summary: list.summary(id, in: model.config)) {
list.show(id)
navigate { list.show(id) }
}
.contextMenu {
Button("Remove…") { removing = id }
Expand All @@ -104,11 +105,14 @@ struct AppsPane: View {
.onChange(of: apps.count) { settings?.refit() }
}

/// Changes between the list and a page, or a page's segments, as the window's one transition.
private func navigate(_ change: () -> Void) {
if let settings { settings.transition(change) } else { change() }
}

/// Opens the page of the app a request named, listing it first when it is not there.
private func openRequested() {
guard let id = settings?.app else { return }
settings?.app = nil
list.add(id, in: model.config)
settings?.openRequested(in: &list, config: model.config)
}

/// The configured apps and those added here, by name.
Expand All @@ -125,7 +129,7 @@ struct AppsPane: View {
if !running.isEmpty {
Section("Running") {
ForEach(running) { entry in
Button(entry.name) { list.add(entry.id, in: model.config) }
Button(entry.name) { navigate { list.add(entry.id, in: model.config) } }
}
}
}
Expand All @@ -152,7 +156,7 @@ struct AppsPane: View {
Alerts.show(alert.title, alert.message)
return
}
list.add(id, in: model.config)
navigate { list.add(id, in: model.config) }
}
if let window = settings?.window {
panel.beginSheetModal(for: window, completionHandler: finish)
Expand Down
125 changes: 117 additions & 8 deletions Sources/PureMouseSettingsUI/DevCapture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,109 @@ enum DevCapture {
private static var faked = false
private static var shadow = false

/// Captures a page change on an unlocked spare Mac (`PUREMOUSE_DEV_FILMSTRIP=<directory>`): frame
/// 0 before the change, then frames at about 60 Hz for `PUREMOUSE_DEV_FILMSTRIP_SECONDS` (default
/// 0.6), each the window server's image of this process's windows. The frames are taken on a
/// background thread, so an animation the main thread drives (a window frame) shows as it
/// happens, and written once the run ends, with `frames.txt` giving each frame's time in ms from
/// the moment the change began on the main thread (frame 0's is negative). Then it quits.
static func filmstrip(_ environment: [String: String], change: @escaping () -> Void) {
guard let path = environment["PUREMOUSE_DEV_FILMSTRIP"] else { return }
let directory = URL(filePath: path, directoryHint: .isDirectory)
do {
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
} catch {
FileHandle.standardError.write(Data("filmstrip directory failed: \(error)\n".utf8))
NSApp.terminateClosingSheets()
return
}
let delay = environment["PUREMOUSE_DEV_CAPTURE_DELAY"].flatMap(Double.init) ?? 2.5
let seconds = environment["PUREMOUSE_DEV_FILMSTRIP_SECONDS"].flatMap(Double.init) ?? 0.6
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
let film = Film(ids: windowIDs(), shadow: shadow)
let change = Change(run: change)
Thread.detachNewThread {
film.run(seconds: seconds) {
DispatchQueue.main.async {
film.mark(returned: false)
change.run()
film.mark(returned: true)
}
}
DispatchQueue.main.async {
if !film.write(to: directory) {
FileHandle.standardError.write(Data("filmstrip write failed\n".utf8))
}
NSApp.terminateClosingSheets()
}
}
}
}

/// The change a filmstrip films, handed to its thread to start on main.
@MainActor private final class Change {
let run: () -> Void
init(run: @escaping () -> Void) { self.run = run }
}

/// A filmstrip's frames and their times, taken on its own thread; the change's times are set on main.
private final class Film: @unchecked Sendable {
private let ids: [CGWindowID]
private let shadow: Bool
private let lock = NSLock()
private var frames: [(time: UInt64, image: CGImage?)] = []
private var began: UInt64?
private var returned: UInt64?

init(ids: [CGWindowID], shadow: Bool) {
self.ids = ids
self.shadow = shadow
}

func mark(returned isEnd: Bool) {
let now = DispatchTime.now().uptimeNanoseconds
lock.withLock { if isEnd { returned = now } else { began = now } }
}

/// Captures until `seconds` have passed, starting the change once frame 0 is taken.
func run(seconds: Double, startChange: () -> Void) {
let interval = UInt64(1_000_000_000 / 60)
let start = DispatchTime.now().uptimeNanoseconds
let end = start + UInt64(seconds * 1_000_000_000)
var next = start
while next <= end {
let now = DispatchTime.now().uptimeNanoseconds
if now < next { usleep(useconds_t((next - now) / 1000)) }
let time = DispatchTime.now().uptimeNanoseconds
let image = DevCapture.composite(ids, shadow: shadow)
let count = lock.withLock {
frames.append((time, image))
return frames.count
}
if count == 1 { startChange() }
// A slow capture skips the slots it missed rather than bunching up.
repeat { next += interval } while next < DispatchTime.now().uptimeNanoseconds
}
}

@MainActor func write(to directory: URL) -> Bool {
let (frames, began, returned) = lock.withLock { (self.frames, self.began, self.returned) }
let zero = Double(began ?? frames.first?.time ?? 0)
func ms(_ time: UInt64) -> String { String(format: "%.1f", (Double(time) - zero) / 1e6) }
var lines = ["# frame, ms from the change beginning on the main thread"]
if let returned { lines.append("# the change returned at \(ms(returned))") }
for (index, frame) in frames.enumerated() {
let url = directory.appendingPathComponent(String(format: "%03d.png", index))
guard let image = frame.image,
let png = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]),
(try? png.write(to: url)) != nil else { return false }
lines.append(String(format: "%03d ", index) + ms(frame.time))
}
let text = lines.joined(separator: "\n") + "\n"
return (try? text.write(to: directory.appendingPathComponent("frames.txt"), atomically: true, encoding: .utf8)) != nil
}
}

static func schedule(_ environment: [String: String]) {
guard let path = environment["PUREMOUSE_DEV_CAPTURE"] else { return }
let delay = environment["PUREMOUSE_DEV_CAPTURE_DELAY"].flatMap(Double.init) ?? 2.5
Expand Down Expand Up @@ -90,16 +193,19 @@ enum DevCapture {
report("after fake")
}

/// This process's on-screen windows, front to back, composited into one image.
private static func write(to url: URL) -> Bool {
/// This process's on-screen windows, front to back.
private static func windowIDs() -> [CGWindowID] {
let pid = ProcessInfo.processInfo.processIdentifier
let info = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID)
as? [[String: Any]] ?? []
var ids: [UnsafeRawPointer?] = info
return info
.filter { ($0[kCGWindowOwnerPID as String] as? Int).map(pid_t.init) == pid }
.compactMap { ($0[kCGWindowNumber as String] as? Int).flatMap { UnsafeRawPointer(bitPattern: UInt($0)) } }
guard !ids.isEmpty, let windows = CFArrayCreate(nil, &ids, ids.count, nil),
let image = composite(windows),
.compactMap { ($0[kCGWindowNumber as String] as? Int).map(CGWindowID.init) }
}

/// This process's on-screen windows, front to back, composited into one image.
private static func write(to url: URL) -> Bool {
guard let image = composite(windowIDs(), shadow: shadow),
let png = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
else { return false }
return (try? png.write(to: url)) != nil
Expand Down Expand Up @@ -177,9 +283,12 @@ enum DevCapture {

/// `CGWindowListCreateImageFromArray`, looked up at run time: it is deprecated in favour of
/// ScreenCaptureKit, which needs the Screen Recording permission even for a process's own windows.
private static func composite(_ windows: CFArray) -> CGImage? {
/// The filmstrip calls it off the main thread.
nonisolated private static func composite(_ ids: [CGWindowID], shadow: Bool) -> CGImage? {
typealias Create = @convention(c) (CGRect, CFArray, CGWindowImageOption) -> Unmanaged<CGImage>?
guard let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CGWindowListCreateImageFromArray")
var pointers: [UnsafeRawPointer?] = ids.map { UnsafeRawPointer(bitPattern: UInt($0)) }
guard !pointers.isEmpty, let windows = CFArrayCreate(nil, &pointers, pointers.count, nil),
let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CGWindowListCreateImageFromArray")
else { return nil }
let create = unsafeBitCast(symbol, to: Create.self)
let options: CGWindowImageOption = shadow ? [.bestResolution] : [.boundsIgnoreFraming, .bestResolution]
Expand Down
11 changes: 11 additions & 0 deletions Sources/PureMouseSettingsUI/DevHooks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ public enum DevHooks {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { send(key: name) }
}
DevCapture.schedule(environment)
if let target = environment["PUREMOUSE_DEV_FILMSTRIP_TO"] {
DevCapture.filmstrip(environment) {
if let pane = SettingsPane(rawValue: target) {
show(ShowRequest(pane: pane))
} else if target == "back" {
send(key: "esc")
} else if target.hasPrefix("app:") {
show(ShowRequest(app: String(target.dropFirst(4))))
}
}
}
DevAXDump.schedule(environment, show: show)
}
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/PureMouseSettingsUI/MouseTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ final class MouseSelection {
@MainActor
final class MouseTab: NSViewController, IdealSized {
let selection: MouseSelection
/// Called when the segment changes, after its pane shows, so the window can refit to it.
/// Called when the segment changes, after its pane shows.
var didSelect: (MouseSegment) -> Void = { _ in }
/// Runs a segment change on screen as the window's one page transition (`SettingsTabs.transition`).
var transition: (() -> Void) -> Void = { $0() }
/// Called as the segment changes away from Buttons, whichever way the change came (the strip
/// or a request), so Find by Pressing, which belongs to Buttons, ends with it.
var willLeaveButtons: () -> Void = {}
Expand Down Expand Up @@ -68,7 +70,11 @@ final class MouseTab: NSViewController, IdealSized {
guard segment != selection.segment else { return }
if selection.segment == .buttons { willLeaveButtons() }
selection.segment = segment
if isViewLoaded { show(segment) }
if isViewLoaded, view.window != nil {
transition { show(segment) }
} else if isViewLoaded {
show(segment)
}
didSelect(segment)
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/PureMouseSettingsUI/PaneHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct PaneForm<Content: View>: View {
var body: some View {
Form { content }
.formStyle(.grouped)
// Looks the same, but leaves the window's own background (tinted by the desktop)
// showing, so a page change's snapshot matches the page (`PaneTransition`).
.scrollContentBackground(.hidden)
.scrollBounceBehavior(.basedOnSize)
.frame(maxHeight: maxHeight)
.fixedSize(horizontal: false, vertical: true)
Expand Down
Loading
Loading