Documentation
¶
Overview ¶
Package wait provides condition-based waits that replace the hand-tuned `wait { duration_ms }` heuristics that v0.1 batches relied on. Three primitives are exposed:
- WaitForWindow: poll EWMH _NET_CLIENT_LIST until a window matching a WindowTarget appears (or disappears, when Present is false).
- WaitForPixelChange: capture a baseline of a screen region and poll until pixel content changes (mode "any") or stops changing (mode "stable").
- WaitForText: poll an OCR-driven find_text query until the text appears (or vanishes, when Present is false).
Each primitive honors ctx.Done() (batch-level cancel) and its own timeout. Timeouts return contract.Precondition with code WAIT_TIMEOUT and a last_state snapshot under details so callers can debug from the response alone. Cancellation via ctx.Done() returns contract.Cancelled (exit 6).
Design choices:
- WaitForWindow uses a polling loop, not a PropertyNotify subscription. Polling at 100ms is sufficient for desktop launch and close events; subscription would require a long-lived X11 connection plus an event pump, doubling the implementation size for marginal latency wins. The cadence is documented in the result so debugging is straightforward.
- WaitForPixelChange uses a 16x16 dhash signature for the baseline and a Hamming-distance ratio as the diff metric. dhash is cheap to compute on a captured RGBA buffer and robust to anti-aliasing jitter at idle.
- WaitForText reuses imageutil.FindText and screen.CaptureRGBA so OCR logic is not duplicated.
Index ¶
Constants ¶
const ( DefaultWindowTimeoutMS = 5000 DefaultWindowPollMS = 100 DefaultPixelTimeoutMS = 5000 DefaultPixelPollMS = 100 DefaultPixelThreshold = 0.02 DefaultPixelStableMS = 300 PixelModeAny = "any" PixelModeStable = "stable" DefaultTextTimeoutMS = 8000 DefaultTextPollMS = 250 DefaultTextMinConfidence = 0.6 )
Default cadences and timeouts.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PixelRequest ¶
type PixelRequest struct {
Region contract.RegionRef `` /* 127-byte string literal not displayed */
Threshold float64 `json:"threshold,omitempty"`
TimeoutMS int `json:"timeout_ms,omitempty"`
PollMS int `json:"poll_ms,omitempty"`
Mode string `json:"mode,omitempty"`
StableMS int `json:"stable_ms,omitempty"`
}
PixelRequest carries the inputs for WaitForPixelChange. Region accepts both bare {x,y,width,height} (v0.1/v0.2 screen-space) and the task-12 extended shape ({...,space:"window"|"window_frame"|"monitor", target?,monitor_index?}). The region is resolved to screen-space once per call before pixel capture begins.
type PixelResult ¶
type PixelResult struct {
Changed bool `json:"changed"`
Diff float64 `json:"diff"`
ElapsedMS int `json:"elapsed_ms"`
Polls int `json:"polls"`
LastState map[string]any `json:"last_state"`
// RegionClamped reports that the request's region exceeded the
// resolved-space bounds (window client_bounds, monitor bounds, or
// screen bounds) and was silently shrunk to fit before polling
// began. Omitted when no clamp occurred so existing in-bounds calls
// return the same wire shape.
RegionClamped bool `json:"region_clamped,omitempty"`
// OriginalRegion is the local (pre-resolution) rectangle the caller
// requested. Present only when RegionClamped is true.
OriginalRegion *contract.Bounds `json:"original_region,omitempty"`
// ClampedRegion is the local (pre-resolution) rectangle actually
// polled after the clamp. Present only when RegionClamped is true.
ClampedRegion *contract.Bounds `json:"clamped_region,omitempty"`
}
PixelResult is the success envelope for WaitForPixelChange.
func WaitForPixelChange ¶
func WaitForPixelChange(ctx context.Context, req PixelRequest) (PixelResult, error)
WaitForPixelChange captures a baseline of the requested screen region, then polls until the region's dhash signature diverges from the baseline (mode "any") or has been stable for StableMS (mode "stable").
type TextRequest ¶
type TextRequest struct {
Query string `json:"query"`
Region contract.RegionRef `` /* 135-byte string literal not displayed */
Present *bool `json:"present,omitempty"`
TimeoutMS int `json:"timeout_ms,omitempty"`
PollMS int `json:"poll_ms,omitempty"`
MinConfidence float64 `json:"min_confidence,omitempty"`
Lang string `json:"lang,omitempty"`
CaseSensitive bool `json:"case_sensitive,omitempty"`
Regex bool `json:"regex,omitempty"`
}
TextRequest carries the inputs for WaitForText. Region accepts the same dual shape as PixelRequest.Region: bare bounds for screen-space (v0.1/v0.2 wire compat) or the extended {space,target?,monitor_index?} envelope for window-space / monitor-space polling.
type TextResult ¶
type TextResult struct {
Found bool `json:"found"`
Candidate *contract.FindCandidate `json:"candidate"`
ElapsedMS int `json:"elapsed_ms"`
Polls int `json:"polls"`
LastState map[string]any `json:"last_state"`
// RegionClamped reports that the request's region exceeded the
// resolved-space bounds and was silently shrunk to fit before
// polling began. Omitted when no clamp occurred so existing
// in-bounds calls return the same wire shape.
RegionClamped bool `json:"region_clamped,omitempty"`
// OriginalRegion is the local (pre-resolution) rectangle the caller
// requested. Present only when RegionClamped is true.
OriginalRegion *contract.Bounds `json:"original_region,omitempty"`
// ClampedRegion is the local (pre-resolution) rectangle actually
// polled after the clamp. Present only when RegionClamped is true.
ClampedRegion *contract.Bounds `json:"clamped_region,omitempty"`
}
TextResult is the success envelope for WaitForText.
func WaitForText ¶
func WaitForText(ctx context.Context, req TextRequest) (TextResult, error)
WaitForText polls find_text until the requested query is found (Present=true) or vanishes (Present=false). Reuses imageutil.FindText — no OCR logic is duplicated here.
type WindowRequest ¶
type WindowRequest struct {
Match contract.WindowTarget `json:"match"`
Present *bool `json:"present,omitempty"`
Focused *bool `json:"focused,omitempty"`
TimeoutMS int `json:"timeout_ms,omitempty"`
PollMS int `json:"poll_ms,omitempty"`
}
WindowRequest carries the inputs for WaitForWindow.
type WindowResult ¶
type WindowResult struct {
Matched *contract.WindowInfo `json:"matched"`
ElapsedMS int `json:"elapsed_ms"`
Polls int `json:"polls"`
LastState map[string]any `json:"last_state"`
}
WindowResult is the success envelope for WaitForWindow.
func WaitForWindow ¶
func WaitForWindow(ctx context.Context, req WindowRequest) (WindowResult, error)
WaitForWindow polls EWMH _NET_CLIENT_LIST until a window matching req.Match appears (Present=true) or disappears (Present=false). The matcher is contract.MatchWindowInfo so behavior is identical to the focus_window and window_* verbs. When req.Focused is set, the matched window must additionally satisfy the requested focused state.