2026-10-21 00:54:54 +00:00
|
|
|
import AppKit
|
|
|
|
|
import Foundation
|
|
|
|
|
|
perf: stop sampling when nothing is looking
Reported: "参考nex_squid的代码,进一步降低软件本身占用的系统资源." The
pattern worth copying out of nex_squid's `useSystemSnapshot` is that a
poll stops when nothing can see its answer. Shift had four places that
polled for the life of the process instead.
- `native/macos/drag-bridge.swift` — a separate tracked process polling at
30 Hz forever, and allocating a CGEvent each pass to read a cursor
position nothing wants while no button is held. A/B against the shipped
0.1.11 binary: 20 ms CPU per 6 s idle is now 0 ms, while 12 s of holding
the button still costs 30 ms either way, so a gesture is sampled exactly
as finely as before and an idle Shift stops touching the CPU. It runs at
10 Hz until the button goes down.
- `src-tauri/src/drag_bridge.rs` — the Windows gesture watch slept 33 ms
unconditionally. An *enabled* ring keeps 33 ms, and not from
conservatism: the loop has to observe the press to measure the drag
threshold from where the button went down, so slowing it does not notice
a gesture late, it never records it. Only a switched-off ring can afford
a quarter second, since the one thing it waits for is the switch.
- `src-tauri/src/share.rs` — the discovery loop. Measured on the 0.1.11
bundle: 0.22% CPU with ShiftDrop on, 0.03% with it off. Switching the
feature off means "stop announcing", never "be deaf" — a silent copy
still hears beacons so another machine can still find and send to it, and
expiry must keep running or an hour-old peer pretends to be live forever.
So the saving comes from waking less often while silent and from skipping
bookkeeping when there is no peer to expire, not from probing less.
Announce and sweep intervals are untouched, so discovery stays as fast.
`a_silent_copy_still_knows_and_still_loses_peers` pins that distinction.
- `src/components/MainWindow.tsx` — the 30 s clock ticked for the life of
the app while Shift sits parked in the menu bar with this window hidden,
re-rendering a list nobody could see. It now exists only while the page is
visible, and an empty library is not worth a render at all.
Co-Authored-By: Codex <noreply@openai.com>
2026-11-19 09:50:57 +00:00
|
|
|
// Idle, this process has one job: notice the moment the mouse goes down. It used to do
|
|
|
|
|
// that 30 times a second, and every tick allocated a CGEvent to read a cursor position
|
|
|
|
|
// nothing needs while no button is held. Measured A/B against the 0.1.11 binary on the
|
|
|
|
|
// 2026-11 idle review: 20ms of CPU per 6s idle (~0.3%, and 30 wakeups a second,
|
|
|
|
|
// forever) against 0ms for the same window now, while 12s of holding the button costs
|
|
|
|
|
// 30ms either way. So the gesture is sampled exactly as finely as it ever was and an
|
|
|
|
|
// idle Shift stops touching the CPU at all.
|
|
|
|
|
//
|
|
|
|
|
// 10 Hz while idle delays only the discovery that the button went down. The ring still
|
|
|
|
|
// cannot appear until Finder has published the dragged files on the drag pasteboard,
|
|
|
|
|
// which needs the drag threshold crossed first, and a press is what moves this timer to
|
|
|
|
|
// 30 Hz - so the arming path is unchanged in every respect a user can feel.
|
|
|
|
|
let idleInterval = 1.0 / 10.0
|
|
|
|
|
let gestureInterval = 1.0 / 30.0
|
|
|
|
|
|
2026-10-21 00:54:54 +00:00
|
|
|
struct Payload: Encodable {
|
|
|
|
|
let type: String
|
|
|
|
|
let x: Double?
|
|
|
|
|
let y: Double?
|
|
|
|
|
let files: [String]?
|
|
|
|
|
let tools: Bool?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func emit(_ payload: Payload) {
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
guard let data = try? encoder.encode(payload),
|
|
|
|
|
let line = String(data: data, encoding: .utf8) else { return }
|
|
|
|
|
print(line)
|
|
|
|
|
fflush(stdout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cursorLocation() -> NSPoint {
|
|
|
|
|
if let event = CGEvent(source: nil) {
|
|
|
|
|
return event.location
|
|
|
|
|
}
|
|
|
|
|
return NSEvent.mouseLocation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let dragPasteboard = NSPasteboard(name: .drag)
|
|
|
|
|
var mouseWasDown = false
|
|
|
|
|
var pickerShown = false
|
2026-10-25 04:24:05 +00:00
|
|
|
var cancelled = false
|
2026-10-21 00:54:54 +00:00
|
|
|
var toolsMode = false
|
2026-10-25 04:24:05 +00:00
|
|
|
var missingDragFiles = 0
|
2026-10-21 00:54:54 +00:00
|
|
|
var changeCountAtMouseDown = dragPasteboard.changeCount
|
|
|
|
|
var lastX = Double.nan
|
|
|
|
|
var lastY = Double.nan
|
|
|
|
|
|
|
|
|
|
func draggedFileURLs() -> [String] {
|
|
|
|
|
let objects = dragPasteboard.readObjects(
|
|
|
|
|
forClasses: [NSURL.self],
|
|
|
|
|
options: [.urlReadingFileURLsOnly: true]
|
|
|
|
|
)
|
|
|
|
|
return (objects as? [URL])?.map(\.path) ?? []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tick() {
|
|
|
|
|
if getppid() == 1 {
|
|
|
|
|
exit(0)
|
|
|
|
|
}
|
|
|
|
|
let mouseDown = NSEvent.pressedMouseButtons & 1 != 0
|
perf: stop sampling when nothing is looking
Reported: "参考nex_squid的代码,进一步降低软件本身占用的系统资源." The
pattern worth copying out of nex_squid's `useSystemSnapshot` is that a
poll stops when nothing can see its answer. Shift had four places that
polled for the life of the process instead.
- `native/macos/drag-bridge.swift` — a separate tracked process polling at
30 Hz forever, and allocating a CGEvent each pass to read a cursor
position nothing wants while no button is held. A/B against the shipped
0.1.11 binary: 20 ms CPU per 6 s idle is now 0 ms, while 12 s of holding
the button still costs 30 ms either way, so a gesture is sampled exactly
as finely as before and an idle Shift stops touching the CPU. It runs at
10 Hz until the button goes down.
- `src-tauri/src/drag_bridge.rs` — the Windows gesture watch slept 33 ms
unconditionally. An *enabled* ring keeps 33 ms, and not from
conservatism: the loop has to observe the press to measure the drag
threshold from where the button went down, so slowing it does not notice
a gesture late, it never records it. Only a switched-off ring can afford
a quarter second, since the one thing it waits for is the switch.
- `src-tauri/src/share.rs` — the discovery loop. Measured on the 0.1.11
bundle: 0.22% CPU with ShiftDrop on, 0.03% with it off. Switching the
feature off means "stop announcing", never "be deaf" — a silent copy
still hears beacons so another machine can still find and send to it, and
expiry must keep running or an hour-old peer pretends to be live forever.
So the saving comes from waking less often while silent and from skipping
bookkeeping when there is no peer to expire, not from probing less.
Announce and sweep intervals are untouched, so discovery stays as fast.
`a_silent_copy_still_knows_and_still_loses_peers` pins that distinction.
- `src/components/MainWindow.tsx` — the 30 s clock ticked for the life of
the app while Shift sits parked in the menu bar with this window hidden,
re-rendering a list nobody could see. It now exists only while the page is
visible, and an empty library is not worth a render at all.
Co-Authored-By: Codex <noreply@openai.com>
2026-11-19 09:50:57 +00:00
|
|
|
// Reading the cursor costs a CGEvent allocation, and nothing downstream wants a
|
|
|
|
|
// position unless a press is in progress or has just ended. An idle tick is then
|
|
|
|
|
// one read of `pressedMouseButtons` and a couple of integer tests.
|
|
|
|
|
let pressEnded = mouseWasDown && !mouseDown
|
|
|
|
|
let point = mouseDown || pressEnded ? cursorLocation() : NSPoint(x: 0, y: 0)
|
2026-10-21 00:54:54 +00:00
|
|
|
if !mouseDown {
|
|
|
|
|
changeCountAtMouseDown = dragPasteboard.changeCount
|
2026-10-25 04:24:05 +00:00
|
|
|
cancelled = false
|
|
|
|
|
missingDragFiles = 0
|
2026-10-21 00:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
perf: stop sampling when nothing is looking
Reported: "参考nex_squid的代码,进一步降低软件本身占用的系统资源." The
pattern worth copying out of nex_squid's `useSystemSnapshot` is that a
poll stops when nothing can see its answer. Shift had four places that
polled for the life of the process instead.
- `native/macos/drag-bridge.swift` — a separate tracked process polling at
30 Hz forever, and allocating a CGEvent each pass to read a cursor
position nothing wants while no button is held. A/B against the shipped
0.1.11 binary: 20 ms CPU per 6 s idle is now 0 ms, while 12 s of holding
the button still costs 30 ms either way, so a gesture is sampled exactly
as finely as before and an idle Shift stops touching the CPU. It runs at
10 Hz until the button goes down.
- `src-tauri/src/drag_bridge.rs` — the Windows gesture watch slept 33 ms
unconditionally. An *enabled* ring keeps 33 ms, and not from
conservatism: the loop has to observe the press to measure the drag
threshold from where the button went down, so slowing it does not notice
a gesture late, it never records it. Only a switched-off ring can afford
a quarter second, since the one thing it waits for is the switch.
- `src-tauri/src/share.rs` — the discovery loop. Measured on the 0.1.11
bundle: 0.22% CPU with ShiftDrop on, 0.03% with it off. Switching the
feature off means "stop announcing", never "be deaf" — a silent copy
still hears beacons so another machine can still find and send to it, and
expiry must keep running or an hour-old peer pretends to be live forever.
So the saving comes from waking less often while silent and from skipping
bookkeeping when there is no peer to expire, not from probing less.
Announce and sweep intervals are untouched, so discovery stays as fast.
`a_silent_copy_still_knows_and_still_loses_peers` pins that distinction.
- `src/components/MainWindow.tsx` — the 30 s clock ticked for the life of
the app while Shift sits parked in the menu bar with this window hidden,
re-rendering a list nobody could see. It now exists only while the page is
visible, and an empty library is not worth a render at all.
Co-Authored-By: Codex <noreply@openai.com>
2026-11-19 09:50:57 +00:00
|
|
|
if pressEnded && pickerShown {
|
2026-10-21 00:54:54 +00:00
|
|
|
pickerShown = false
|
|
|
|
|
emit(Payload(type: "end", x: point.x, y: point.y, files: nil, tools: nil))
|
|
|
|
|
}
|
|
|
|
|
mouseWasDown = mouseDown
|
|
|
|
|
|
|
|
|
|
guard mouseDown else { return }
|
|
|
|
|
|
|
|
|
|
let flags = NSEvent.modifierFlags.intersection(.deviceIndependentFlagsMask)
|
|
|
|
|
let shift = flags.contains(.shift)
|
|
|
|
|
let option = flags.contains(.option)
|
|
|
|
|
|
|
|
|
|
if !pickerShown {
|
2026-10-25 04:24:05 +00:00
|
|
|
guard !cancelled else { return }
|
2026-10-21 00:54:54 +00:00
|
|
|
guard shift, dragPasteboard.changeCount != changeCountAtMouseDown else { return }
|
|
|
|
|
let files = draggedFileURLs()
|
|
|
|
|
guard !files.isEmpty else { return }
|
|
|
|
|
pickerShown = true
|
|
|
|
|
toolsMode = option
|
|
|
|
|
lastX = point.x
|
|
|
|
|
lastY = point.y
|
|
|
|
|
emit(Payload(type: "show", x: point.x, y: point.y, files: files, tools: option))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if shift && option != toolsMode {
|
|
|
|
|
toolsMode = option
|
|
|
|
|
emit(Payload(type: "mode", x: point.x, y: point.y, files: nil, tools: option))
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 04:24:05 +00:00
|
|
|
// Some Finder versions also tear down the pasteboard during cancellation.
|
|
|
|
|
// Keep this as a fallback for drag sessions that disappear without a key event.
|
|
|
|
|
if draggedFileURLs().isEmpty {
|
|
|
|
|
missingDragFiles += 1
|
|
|
|
|
if missingDragFiles >= 6 {
|
|
|
|
|
pickerShown = false
|
|
|
|
|
cancelled = true
|
|
|
|
|
emit(Payload(type: "cancel", x: point.x, y: point.y, files: nil, tools: nil))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
missingDragFiles = 0
|
|
|
|
|
|
2026-10-21 00:54:54 +00:00
|
|
|
if abs(point.x - lastX) > 0.5 || abs(point.y - lastY) > 0.5 {
|
|
|
|
|
lastX = point.x
|
|
|
|
|
lastY = point.y
|
|
|
|
|
emit(Payload(type: "move", x: point.x, y: point.y, files: nil, tools: nil))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
perf: stop sampling when nothing is looking
Reported: "参考nex_squid的代码,进一步降低软件本身占用的系统资源." The
pattern worth copying out of nex_squid's `useSystemSnapshot` is that a
poll stops when nothing can see its answer. Shift had four places that
polled for the life of the process instead.
- `native/macos/drag-bridge.swift` — a separate tracked process polling at
30 Hz forever, and allocating a CGEvent each pass to read a cursor
position nothing wants while no button is held. A/B against the shipped
0.1.11 binary: 20 ms CPU per 6 s idle is now 0 ms, while 12 s of holding
the button still costs 30 ms either way, so a gesture is sampled exactly
as finely as before and an idle Shift stops touching the CPU. It runs at
10 Hz until the button goes down.
- `src-tauri/src/drag_bridge.rs` — the Windows gesture watch slept 33 ms
unconditionally. An *enabled* ring keeps 33 ms, and not from
conservatism: the loop has to observe the press to measure the drag
threshold from where the button went down, so slowing it does not notice
a gesture late, it never records it. Only a switched-off ring can afford
a quarter second, since the one thing it waits for is the switch.
- `src-tauri/src/share.rs` — the discovery loop. Measured on the 0.1.11
bundle: 0.22% CPU with ShiftDrop on, 0.03% with it off. Switching the
feature off means "stop announcing", never "be deaf" — a silent copy
still hears beacons so another machine can still find and send to it, and
expiry must keep running or an hour-old peer pretends to be live forever.
So the saving comes from waking less often while silent and from skipping
bookkeeping when there is no peer to expire, not from probing less.
Announce and sweep intervals are untouched, so discovery stays as fast.
`a_silent_copy_still_knows_and_still_loses_peers` pins that distinction.
- `src/components/MainWindow.tsx` — the 30 s clock ticked for the life of
the app while Shift sits parked in the menu bar with this window hidden,
re-rendering a list nobody could see. It now exists only while the page is
visible, and an empty library is not worth a render at all.
Co-Authored-By: Codex <noreply@openai.com>
2026-11-19 09:50:57 +00:00
|
|
|
var timer: Timer?
|
|
|
|
|
var timerIsGesture = false
|
|
|
|
|
|
|
|
|
|
func reschedule(gesture: Bool) {
|
|
|
|
|
guard gesture != timerIsGesture || timer == nil else { return }
|
|
|
|
|
timerIsGesture = gesture
|
|
|
|
|
timer?.invalidate()
|
|
|
|
|
let next = Timer(timeInterval: gesture ? gestureInterval : idleInterval, repeats: true) { _ in
|
|
|
|
|
let down = NSEvent.pressedMouseButtons & 1 != 0
|
|
|
|
|
tick()
|
|
|
|
|
// A press changes the pace on the next tick; the timer is replaced rather
|
|
|
|
|
// than retuned because NSTimer cannot change its own interval.
|
|
|
|
|
if down != timerIsGesture { reschedule(gesture: down) }
|
|
|
|
|
}
|
|
|
|
|
timer = next
|
|
|
|
|
RunLoop.main.add(next, forMode: .common)
|
2026-10-21 00:54:54 +00:00
|
|
|
}
|
perf: stop sampling when nothing is looking
Reported: "参考nex_squid的代码,进一步降低软件本身占用的系统资源." The
pattern worth copying out of nex_squid's `useSystemSnapshot` is that a
poll stops when nothing can see its answer. Shift had four places that
polled for the life of the process instead.
- `native/macos/drag-bridge.swift` — a separate tracked process polling at
30 Hz forever, and allocating a CGEvent each pass to read a cursor
position nothing wants while no button is held. A/B against the shipped
0.1.11 binary: 20 ms CPU per 6 s idle is now 0 ms, while 12 s of holding
the button still costs 30 ms either way, so a gesture is sampled exactly
as finely as before and an idle Shift stops touching the CPU. It runs at
10 Hz until the button goes down.
- `src-tauri/src/drag_bridge.rs` — the Windows gesture watch slept 33 ms
unconditionally. An *enabled* ring keeps 33 ms, and not from
conservatism: the loop has to observe the press to measure the drag
threshold from where the button went down, so slowing it does not notice
a gesture late, it never records it. Only a switched-off ring can afford
a quarter second, since the one thing it waits for is the switch.
- `src-tauri/src/share.rs` — the discovery loop. Measured on the 0.1.11
bundle: 0.22% CPU with ShiftDrop on, 0.03% with it off. Switching the
feature off means "stop announcing", never "be deaf" — a silent copy
still hears beacons so another machine can still find and send to it, and
expiry must keep running or an hour-old peer pretends to be live forever.
So the saving comes from waking less often while silent and from skipping
bookkeeping when there is no peer to expire, not from probing less.
Announce and sweep intervals are untouched, so discovery stays as fast.
`a_silent_copy_still_knows_and_still_loses_peers` pins that distinction.
- `src/components/MainWindow.tsx` — the 30 s clock ticked for the life of
the app while Shift sits parked in the menu bar with this window hidden,
re-rendering a list nobody could see. It now exists only while the page is
visible, and an empty library is not worth a render at all.
Co-Authored-By: Codex <noreply@openai.com>
2026-11-19 09:50:57 +00:00
|
|
|
|
|
|
|
|
reschedule(gesture: NSEvent.pressedMouseButtons & 1 != 0)
|
2026-10-21 00:54:54 +00:00
|
|
|
RunLoop.main.run()
|