Skip to content

Computer use CLI for AI agents

feat: draw the cursor label as a pill that fits its text (#357)

Chris Tate9aa4d6052148
diff --git a/apps/docs/content/docs/commands/system.mdx b/apps/docs/content/docs/commands/system.mdx
index 9d2aa2da0..57b1d5fbd 100644
--- a/apps/docs/content/docs/commands/system.mdx
+++ b/apps/docs/content/docs/commands/system.mdx
@@ -128,7 +128,7 @@ agent-desktop --session "$AGENT_DESKTOP_SESSION" cursor-overlay disable
</thead>
<tbody>
<tr><td><code>--multi-agent</code></td><td>off</td><td>Require an agent id for desktop actions. Rejected together with <code>--agent-id</code>.</td></tr>
- <tr><td><code>--label TEXT</code>, <code>--max-words N</code></td><td>none, <code>6</code></td><td>Intent text beside the cursor, limited to 1 to 12 words.</td></tr>
+ <tr><td><code>--label TEXT</code>, <code>--max-words N</code></td><td>none, <code>6</code></td><td>Intent text beside the cursor, limited to 1 to 12 words, drawn as a black pill sized to the text.</td></tr>
<tr><td><code>--fill HEX</code>, <code>--rim HEX</code>, <code>--accent HEX</code></td><td><code>#FFFFFF</code>, <code>#111318</code>, <code>#4299FF</code></td><td>Cursor body, outline, and ripple colors.</td></tr>
<tr><td><code>--size N</code></td><td><code>1.0</code></td><td>Size multiplier, 0.5 to 4.0.</td></tr>
<tr><td><code>--no-ripple</code>, <code>--no-highlight</code></td><td>off</td><td>Turn off the click ripple or the element outline.</td></tr>
diff --git a/crates/core/src/cursor_overlay/label_pill.rs b/crates/core/src/cursor_overlay/label_pill.rs
new file mode 100644
index 000000000..baeb10138
--- /dev/null
+++ b/crates/core/src/cursor_overlay/label_pill.rs
@@ -0,0 +1,86 @@
+//! The cursor label's pill: a black, fully rounded tag that fits its text.
+//!
+//! Each renderer measures the label in its own system font and takes every
+//! other dimension from here, so the macOS and Windows overlays draw the same
+//! pill, with the text centred in it both ways.
+
+use crate::Rect;
+
+/// The label's type size at the default cursor size.
+pub const LABEL_FONT_POINTS: f64 = 12.5;
+/// The pill's height; its corner radius is half of it.
+pub const LABEL_PILL_HEIGHT: f64 = 24.0;
+/// Space between the text and each end of the pill.
+pub const LABEL_PILL_PADDING: f64 = 10.0;
+/// The widest the pill grows; longer text is cut short with an ellipsis.
+pub const LABEL_PILL_MAX_WIDTH: f64 = 360.0;
+/// The pill's shadow: black at this opacity, offset down and blurred.
+pub const LABEL_SHADOW_OPACITY: f64 = 0.31;
+pub const LABEL_SHADOW_OFFSET: f64 = 2.0;
+pub const LABEL_SHADOW_BLUR: f64 = 2.5;
+
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct LabelPill {
+ width: f64,
+ height: f64,
+ text_width: f64,
+}
+
+impl LabelPill {
+ /// The pill for `label`, whose text measures `text_width` when set at
+ /// [`LABEL_FONT_POINTS`] times `scale`. `None` when the label says
+ /// nothing, so an empty label shows no pill.
+ pub fn for_label(label: &str, text_width: f64, scale: f64) -> Option<Self> {
+ if label.trim().is_empty() || !text_width.is_finite() || !scale.is_finite() || scale <= 0.0
+ {
+ return None;
+ }
+ let padding = LABEL_PILL_PADDING * scale;
+ let widest_text = LABEL_PILL_MAX_WIDTH * scale - padding * 2.0;
+ let text_width = text_width.clamp(0.0, widest_text);
+ Some(Self {
+ width: text_width + padding * 2.0,
+ height: LABEL_PILL_HEIGHT * scale,
+ text_width,
+ })
+ }
+
+ pub const fn size(&self) -> (f64, f64) {
+ (self.width, self.height)
+ }
+
+ pub fn corner_radius(&self) -> f64 {
+ self.height / 2.0
+ }
+
+ /// The pill cut down to at most `width`, as placing it on a narrow screen
+ /// does. It keeps its padding, so the text gets what is left and is cut
+ /// short with an ellipsis rather than running past the pill's ends.
+ pub fn within(self, width: f64) -> Self {
+ if width.is_nan() || width >= self.width {
+ return self;
+ }
+ let padding = (self.width - self.text_width) / 2.0;
+ let width = width.max(padding * 2.0);
+ Self {
+ width,
+ text_width: width - padding * 2.0,
+ ..self
+ }
+ }
+
+ /// Where a line of text `line_height` tall sits inside the pill, relative
+ /// to its top-left corner: centred across and down.
+ pub fn text_frame(&self, line_height: f64) -> Rect {
+ Rect {
+ x: (self.width - self.text_width) / 2.0,
+ y: (self.height - line_height) / 2.0,
+ width: self.text_width,
+ height: line_height,
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "label_pill_tests.rs"]
+mod tests;
diff --git a/crates/core/src/cursor_overlay/label_pill_tests.rs b/crates/core/src/cursor_overlay/label_pill_tests.rs
new file mode 100644
index 000000000..74dbe0265
--- /dev/null
+++ b/crates/core/src/cursor_overlay/label_pill_tests.rs
@@ -0,0 +1,70 @@
+use super::*;
+
+#[test]
+fn the_pill_grows_with_its_text_and_keeps_its_padding() {
+ let short = LabelPill::for_label("Go", 14.0, 1.0).expect("a pill");
+ let long = LabelPill::for_label("Opening the release notes", 160.0, 1.0).expect("a pill");
+
+ assert_eq!(short.size(), (34.0, LABEL_PILL_HEIGHT));
+ assert_eq!(long.size(), (180.0, LABEL_PILL_HEIGHT));
+ assert_eq!(long.corner_radius(), LABEL_PILL_HEIGHT / 2.0);
+}
+
+#[test]
+fn the_text_is_centred_across_and_down() {
+ let pill = LabelPill::for_label("Opening menu", 80.0, 1.0).expect("a pill");
+ let text = pill.text_frame(16.0);
+ let (width, height) = pill.size();
+
+ assert_eq!(text.x, LABEL_PILL_PADDING);
+ assert_eq!(text.x + text.width + text.x, width);
+ assert_eq!(text.y + text.height + text.y, height);
+}
+
+#[test]
+fn an_empty_label_shows_no_pill() {
+ assert_eq!(LabelPill::for_label("", 0.0, 1.0), None);
+ assert_eq!(LabelPill::for_label(" ", 12.0, 1.0), None);
+}
+
+#[test]
+fn a_long_label_stops_at_the_widest_pill_and_scales_with_the_cursor() {
+ let capped = LabelPill::for_label("x", 5_000.0, 1.0).expect("a pill");
+ assert_eq!(capped.size().0, LABEL_PILL_MAX_WIDTH);
+ let doubled = LabelPill::for_label("Go", 28.0, 2.0).expect("a pill");
+ assert_eq!(doubled.size(), (68.0, LABEL_PILL_HEIGHT * 2.0));
+ assert_eq!(doubled.text_frame(30.0).x, LABEL_PILL_PADDING * 2.0);
+}
+
+#[test]
+fn the_pill_sits_right_of_and_below_the_tip() {
+ let screen = Rect {
+ x: 0.0,
+ y: 0.0,
+ width: 1000.0,
+ height: 800.0,
+ };
+ let pill = LabelPill::for_label("Opening menu", 80.0, 1.0).expect("a pill");
+ let placed = crate::place_label(&crate::Point { x: 100.0, y: 100.0 }, pill.size(), &screen);
+
+ assert_eq!((placed.x, placed.y), (120.0, 130.0));
+ assert_eq!((placed.width, placed.height), pill.size());
+}
+
+/// A pill clamped to a narrow screen keeps its padding and gives the text
+/// only the room left, so the text and its ellipsis stay inside the pill.
+#[test]
+fn a_pill_cut_down_to_the_screen_cuts_its_text_room_too() {
+ let pill = LabelPill::for_label("a long label", 300.0, 2.0).expect("a pill");
+ let cut = pill.within(200.0);
+ let text = cut.text_frame(20.0);
+
+ assert_eq!(cut.size().0, 200.0);
+ assert_eq!(text.x, LABEL_PILL_PADDING * 2.0);
+ assert_eq!(text.x + text.width + text.x, 200.0);
+ assert_eq!(
+ pill.within(1_000.0),
+ pill,
+ "a wide enough screen leaves it alone"
+ );
+}
diff --git a/crates/core/src/cursor_overlay/layout.rs b/crates/core/src/cursor_overlay/layout.rs
index 93f29d670..8733ea6c1 100644
--- a/crates/core/src/cursor_overlay/layout.rs
+++ b/crates/core/src/cursor_overlay/layout.rs
@@ -3,23 +3,26 @@ use crate::{Point, Rect};
/// How close, in points, two cursor positions are to count as the same place.
pub const CURSOR_SAME_POINT: f64 = 1.5;
-const DESTINATION_GAP: f64 = 18.0;
+/// How far the label sits right of and below the point the cursor targets,
+/// which clears the dart drawn below and right of its tip.
+const LABEL_OFFSET_X: f64 = 20.0;
+const LABEL_OFFSET_Y: f64 = 30.0;
pub fn place_label(destination: &Point, size: (f64, f64), screen: &Rect) -> Rect {
let (width, height) = size;
let right = screen.x + screen.width;
let bottom = screen.y + screen.height;
- let preferred_x = destination.x + DESTINATION_GAP;
- let preferred_y = destination.y + DESTINATION_GAP;
+ let preferred_x = destination.x + LABEL_OFFSET_X;
+ let preferred_y = destination.y + LABEL_OFFSET_Y;
let x = if preferred_x + width <= right {
preferred_x
} else {
- destination.x - width - DESTINATION_GAP
+ destination.x - width - LABEL_OFFSET_X
};
let y = if preferred_y + height <= bottom {
preferred_y
} else {
- destination.y - height - DESTINATION_GAP
+ destination.y - height - LABEL_OFFSET_Y
};
Rect {
x: x.clamp(screen.x, (right - width).max(screen.x)),
diff --git a/crates/core/src/cursor_overlay/mod.rs b/crates/core/src/cursor_overlay/mod.rs
index 3838af04b..2a838663b 100644
--- a/crates/core/src/cursor_overlay/mod.rs
+++ b/crates/core/src/cursor_overlay/mod.rs
@@ -6,6 +6,7 @@ mod hand_path;
mod image;
mod images;
mod instruction;
+mod label_pill;
mod layout;
mod motion;
mod motion_profile;
@@ -25,6 +26,10 @@ pub use image::{
};
pub use images::CursorImages;
pub use instruction::CursorOverlayInstruction;
+pub use label_pill::{
+ LABEL_FONT_POINTS, LABEL_PILL_HEIGHT, LABEL_PILL_MAX_WIDTH, LABEL_PILL_PADDING,
+ LABEL_SHADOW_BLUR, LABEL_SHADOW_OFFSET, LABEL_SHADOW_OPACITY, LabelPill,
+};
pub use layout::{CURSOR_SAME_POINT, place_label};
pub use motion::CursorMotion;
pub use motion_profile::CursorMotionProfile;
diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs
index ae07d6714..bdfecfb29 100644
--- a/crates/core/src/lib.rs
+++ b/crates/core/src/lib.rs
@@ -46,8 +46,10 @@ pub use cursor_overlay::{
CURSOR_LABEL_REVEAL_MS, CURSOR_MOTION_BUDGET_MS, CURSOR_OVERLAY_GREETING, CURSOR_REST_FADE_MS,
CURSOR_SAME_POINT, CursorImage, CursorImages, CursorMotion, CursorMotionProfile,
CursorOverlayConfig, CursorOverlayControl, CursorOverlayInstruction, CursorOverlayStyle,
- CursorPhase, CursorPose, MAX_CURSOR_IMAGE_BYTES, MAX_CURSOR_IMAGE_PATH_BYTES,
- MAX_CURSOR_IMAGE_PIXELS, MAX_CURSOR_LABEL_BYTES, MAX_CURSOR_LABEL_WORDS, place_label,
+ CursorPhase, CursorPose, LABEL_FONT_POINTS, LABEL_PILL_HEIGHT, LABEL_PILL_MAX_WIDTH,
+ LABEL_PILL_PADDING, LABEL_SHADOW_BLUR, LABEL_SHADOW_OFFSET, LABEL_SHADOW_OPACITY, LabelPill,
+ MAX_CURSOR_IMAGE_BYTES, MAX_CURSOR_IMAGE_PATH_BYTES, MAX_CURSOR_IMAGE_PIXELS,
+ MAX_CURSOR_LABEL_BYTES, MAX_CURSOR_LABEL_WORDS, place_label,
};
pub use input::clipboard_content::ClipboardContent;
pub use input::clipboard_format::ClipboardFormat;
diff --git a/crates/macos/src/system/cursor_overlay/bridge.rs b/crates/macos/src/system/cursor_overlay/bridge.rs
index 0a2f05cdb..18b4f0ad1 100644
--- a/crates/macos/src/system/cursor_overlay/bridge.rs
+++ b/crates/macos/src/system/cursor_overlay/bridge.rs
@@ -1,6 +1,6 @@
use agent_desktop_core::{
AdapterError, CURSOR_HIGHLIGHT_HOLD_MS, CursorImage, CursorOverlayInstruction,
- CursorOverlayStyle, CursorPhase, CursorPose, ErrorCode, Point, Rect,
+ CursorOverlayStyle, CursorPhase, CursorPose, ErrorCode, LabelPill, Point, Rect, place_label,
};
use std::ffi::{CString, c_char};
@@ -31,8 +31,7 @@ struct NativeCursorFrame {
struct NativeRenderConfig {
frame_seconds: f64,
label: *const c_char,
- bubble_x: f64,
- bubble_y: f64,
+ bubble: [f64; 4],
target: [f64; 4],
highlight_seconds: f64,
flags: u8,
@@ -57,6 +56,7 @@ unsafe extern "C" {
hotspot_y: f64,
) -> bool;
fn agent_desktop_cursor_overlay_idle();
+ fn agent_desktop_cursor_overlay_label_width(label: *const c_char) -> f64;
fn agent_desktop_cursor_overlay_caret(x: f64, y: f64, width: f64, height: f64);
fn agent_desktop_cursor_overlay_caret_clear();
fn agent_desktop_cursor_overlay_caret_follow(
@@ -160,7 +160,7 @@ pub(super) fn run(
fps: u32,
instruction: &CursorOverlayInstruction,
reduce_motion: bool,
- bubble: &Rect,
+ screen: &Rect,
) -> Result<(), AdapterError> {
let frames = poses
.iter()
@@ -175,6 +175,20 @@ pub(super) fn run(
.map(|value| CString::new(value.replace('\0', " ")))
.transpose()
.map_err(|_| AdapterError::internal("Cursor overlay label encoding failed"))?;
+ let anchor = poses
+ .last()
+ .map_or(instruction.destination(), |pose| &pose.point);
+ let bubble = label
+ .as_ref()
+ .zip(instruction.label())
+ .and_then(|(encoded, text)| {
+ let width = unsafe { agent_desktop_cursor_overlay_label_width(encoded.as_ptr()) };
+ LabelPill::for_label(text, width, 1.0)
+ })
+ .map_or([0.0; 4], |pill| {
+ let placed = place_label(anchor, pill.size(), screen);
+ [placed.x, placed.y, placed.width, placed.height]
+ });
let mut flags = 0;
if reduce_motion {
flags |= REDUCE_MOTION;
@@ -191,8 +205,7 @@ pub(super) fn run(
label: label
.as_ref()
.map_or(std::ptr::null(), |value| value.as_ptr()),
- bubble_x: bubble.x,
- bubble_y: bubble.y,
+ bubble,
target: target.unwrap_or_default(),
highlight_seconds: CURSOR_HIGHLIGHT_HOLD_MS as f64 / 1_000.0,
flags,
diff --git a/crates/macos/src/system/cursor_overlay/child.rs b/crates/macos/src/system/cursor_overlay/child.rs
index c8d315a06..722d20218 100644
--- a/crates/macos/src/system/cursor_overlay/child.rs
+++ b/crates/macos/src/system/cursor_overlay/child.rs
@@ -1,7 +1,7 @@
use agent_desktop_core::{
AdapterError, CURSOR_IDLE_REST_MS, CursorMotion, CursorMotionProfile, CursorOverlayConfig,
CursorOverlayControl, CursorOverlayInstruction, CursorOverlayStyle, CursorPhase, CursorPose,
- ErrorCode, Point, place_label,
+ ErrorCode, Point,
};
use std::io::{Read, Write};
use std::os::unix::fs::PermissionsExt;
@@ -14,7 +14,6 @@ use super::bridge;
pub(super) const MARKER: &str = "AGENT_DESKTOP_CURSOR_OVERLAY_CHILD";
pub(super) const SOCKET_ENV: &str = "AGENT_DESKTOP_CURSOR_OVERLAY_SOCKET";
const MAX_INSTRUCTION_BYTES: u64 = 4 * 1024;
-const BUBBLE_SIZE: (f64, f64) = (232.0, 38.0);
#[derive(Default)]
struct OverlayState {
@@ -243,13 +242,12 @@ fn render(
if instruction.phase() == CursorPhase::Drag {
let from = instruction.drag_from().unwrap_or(instruction.destination());
let (screen, fps, reduce_motion) = bridge::screen_at(from)?;
- let bubble = place_label(from, BUBBLE_SIZE, &screen);
bridge::run(
&[CursorPose::still(from.clone())],
fps,
instruction,
reduce_motion,
- &bubble,
+ &screen,
)?;
bridge::begin_drag(from, state.style.ripple() && !reduce_motion);
return Ok(());
@@ -258,14 +256,13 @@ fn render(
bridge::end_drag(instruction.destination(), instruction.drag_from().is_some());
}
let (screen, fps, reduce_motion) = bridge::screen_at(instruction.destination())?;
- let bubble = place_label(instruction.destination(), BUBBLE_SIZE, &screen);
let shown = if state.style.highlight() {
instruction.clone()
} else {
instruction.clone().with_target(None)
};
let frames = frames_for(state, &shown, &screen, fps, reduce_motion);
- bridge::run(&frames, fps, &shown, reduce_motion, &bubble)
+ bridge::run(&frames, fps, &shown, reduce_motion, &screen)
}
fn frames_for(
diff --git a/crates/macos/src/system/cursor_overlay/cursor_overlay_bridge.m b/crates/macos/src/system/cursor_overlay/cursor_overlay_bridge.m
index ed8ccc19b..aa8e8dcfa 100644
--- a/crates/macos/src/system/cursor_overlay/cursor_overlay_bridge.m
+++ b/crates/macos/src/system/cursor_overlay/cursor_overlay_bridge.m
@@ -13,8 +13,7 @@ typedef struct {
typedef struct {
double frameSeconds;
const char *label;
- double bubbleX;
- double bubbleY;
+ double bubble[4];
double target[4];
double highlightSeconds;
uint8_t flags;
@@ -31,8 +30,13 @@ static const CGFloat ADBoxWidth = 32.0;
static const CGFloat ADBoxHeight = 40.0;
static const CGFloat ADTipX = 88.0;
static const CGFloat ADTipY = 172.0;
-static const CGFloat ADBubbleWidth = 232.0;
-static const CGFloat ADBubbleHeight = 38.0;
+static const CGFloat ADBubbleMargin = 8.0;
+static const CGFloat ADLabelPadding = 10.0;
+static const CGFloat ADLabelPoints = 12.5;
+static const CGFloat ADTextCellInset = 6.0;
+static const float ADLabelShadowOpacity = 0.31f;
+static const CGFloat ADLabelShadowOffset = 2.0;
+static const CGFloat ADLabelShadowBlur = 2.5;
static const CGFloat ADHighlightPad = 5.0;
static __strong NSWindow *ADCursorWindow = nil;
@@ -40,6 +44,7 @@ static __strong CALayer *ADPointer = nil;
static __strong NSWindow *ADRipple = nil;
static __strong NSWindow *ADBubbleWindow = nil;
static __strong NSTextField *ADBubbleText = nil;
+static __strong CALayer *ADBubblePill = nil;
static bool ADDragArmed = false;
static bool ADDragStarted = false;
static bool ADDragTrail = false;
@@ -157,21 +162,72 @@ static void ADMoveCursor(const AgentDesktopCursorFrame *frame, double mainHeight
[ADCursorWindow setFrameOrigin:NSMakePoint(frame->x - ADTipX, mainHeight - frame->y - ADTipY)];
}
+static NSFont *ADLabelFont(void) {
+ static NSFont *font = nil;
+ if (font == nil) {
+ font = [NSFont systemFontOfSize:ADLabelPoints weight:NSFontWeightMedium];
+ }
+ return font;
+}
+
+/// The label pill: a black, fully rounded tag with a soft shadow, in a window
+/// `ADBubbleMargin` larger on every side so the shadow is not clipped. Its
+/// size comes from the label's measured width; see `ADLayoutBubble`.
static NSWindow *ADBubble(void) {
- NSWindow *window = ADWindow(NSMakeRect(0.0, 0.0, ADBubbleWidth, ADBubbleHeight));
- CALayer *surface = window.contentView.layer;
- surface.backgroundColor = NSColor.whiteColor.CGColor;
- surface.cornerRadius = 10.0;
- surface.borderWidth = 1.5;
- surface.borderColor = [NSColor colorWithSRGBRed:0.08 green:0.08 blue:0.09 alpha:1.0].CGColor;
+ NSWindow *window = ADWindow(NSMakeRect(0.0, 0.0, 1.0, 1.0));
+ ADBubblePill = [CALayer layer];
+ ADBubblePill.backgroundColor = NSColor.blackColor.CGColor;
+ ADBubblePill.shadowColor = NSColor.blackColor.CGColor;
+ ADBubblePill.shadowOpacity = ADLabelShadowOpacity;
+ ADBubblePill.shadowRadius = ADLabelShadowBlur;
+ ADBubblePill.shadowOffset = CGSizeMake(0.0, -ADLabelShadowOffset);
+ ADFreezeLayer(ADBubblePill);
+ [window.contentView.layer addSublayer:ADBubblePill];
ADBubbleText = [NSTextField labelWithString:@""];
- ADBubbleText.frame = NSMakeRect(13.0, 7.0, 206.0, 23.0);
- ADBubbleText.textColor = [NSColor colorWithSRGBRed:0.16 green:0.12 blue:0.06 alpha:1.0];
- ADBubbleText.font = [NSFont systemFontOfSize:12.5];
+ ADBubbleText.textColor = NSColor.whiteColor;
+ ADBubbleText.font = ADLabelFont();
+ ADBubbleText.alignment = NSTextAlignmentCenter;
+ ADBubbleText.lineBreakMode = NSLineBreakByTruncatingTail;
+ ADBubbleText.maximumNumberOfLines = 1;
[window.contentView addSubview:ADBubbleText];
return window;
}
+/// Sizes the pill to `pill` and centres the text in it across and down, the
+/// way `LabelPill::text_frame` places it. The text field spans the padded
+/// interior, widened on each side by more than the cell's own inset so the
+/// measured text is never cut short, and centres its line itself; its height is the font's line, so
+/// the line sits at the pill's vertical middle.
+static void ADLayoutBubble(NSSize pill) {
+ ADBubblePill.frame = CGRectMake(ADBubbleMargin, ADBubbleMargin, pill.width, pill.height);
+ ADBubblePill.cornerRadius = pill.height / 2.0;
+ CGPathRef shadow = CGPathCreateWithRoundedRect(CGRectMake(0.0, 0.0, pill.width, pill.height),
+ pill.height / 2.0, pill.height / 2.0, NULL);
+ ADBubblePill.shadowPath = shadow;
+ CGPathRelease(shadow);
+ CGFloat line = ceil([ADBubbleText.cell cellSizeForBounds:NSMakeRect(0.0, 0.0, CGFLOAT_MAX,
+ CGFLOAT_MAX)]
+ .height);
+ ADBubbleText.frame = NSMakeRect(ADBubbleMargin + ADLabelPadding - ADTextCellInset,
+ ADBubbleMargin + (pill.height - line) / 2.0,
+ MAX(pill.width - ADLabelPadding * 2.0, 0.0) +
+ ADTextCellInset * 2.0,
+ line);
+}
+
+double agent_desktop_cursor_overlay_label_width(const char *label) {
+ if (label == NULL) {
+ return 0.0;
+ }
+ @autoreleasepool {
+ NSString *text = [NSString stringWithUTF8String:label];
+ if (text == nil) {
+ return 0.0;
+ }
+ return ceil([text sizeWithAttributes:@{NSFontAttributeName : ADLabelFont()}].width);
+ }
+}
+
void agent_desktop_cursor_overlay_idle(void) {
@autoreleasepool {
ADDragPoll();
@@ -192,6 +248,7 @@ void agent_desktop_cursor_overlay_stop(void) {
ADRipple = nil;
ADBubbleWindow = nil;
ADBubbleText = nil;
+ ADBubblePill = nil;
}
void agent_desktop_cursor_overlay_caret(double x, double y, double width, double height) {
@@ -348,17 +405,21 @@ bool agent_desktop_cursor_overlay_run(const AgentDesktopCursorFrame *frames,
bool pointsOnArrival = (config->flags & ADPointOnArrival) != 0;
[ADCursorWindow orderFrontRegardless];
- bool showsBubble = config->label != NULL && config->label[0] != '\0';
+ bool showsBubble =
+ config->label != NULL && config->label[0] != '\0' && config->bubble[2] > 0.0;
NSString *nextLabel = showsBubble ? @(config->label) : @"";
bool changedLabel = ![ADBubbleText.stringValue isEqualToString:nextLabel];
ADBubbleText.stringValue = nextLabel;
- if (!showsBubble) {
+ if (!showsBubble || changedLabel) {
[ADBubbleWindow orderOut:nil];
}
- NSRect bubbleFrame = NSMakeRect(config->bubbleX,
- mainHeight - config->bubbleY - ADBubbleHeight,
- ADBubbleWidth,
- ADBubbleHeight);
+ NSRect pillFrame = NSMakeRect(config->bubble[0],
+ mainHeight - config->bubble[1] - config->bubble[3],
+ config->bubble[2], config->bubble[3]);
+ NSRect bubbleFrame = NSInsetRect(pillFrame, -ADBubbleMargin, -ADBubbleMargin);
+ if (showsBubble) {
+ ADLayoutBubble(pillFrame.size);
+ }
bool followsBubble = showsBubble && !changedLabel && ADBubbleWindow.isVisible;
[ADRipple setFrameOrigin:NSMakePoint(last->x - ADRippleSize * 0.5,
mainHeight - last->y - ADRippleSize * 0.5)];
diff --git a/crates/windows/src/system/cursor_overlay/geometry.rs b/crates/windows/src/system/cursor_overlay/geometry.rs
index 3284ad7e1..e86e16ecf 100644
--- a/crates/windows/src/system/cursor_overlay/geometry.rs
+++ b/crates/windows/src/system/cursor_overlay/geometry.rs
@@ -28,16 +28,6 @@ pub(crate) const RIPPLE_SIZE: f64 = 108.0;
pub(crate) const RIPPLE_CORE_RADIUS: f64 = 19.0;
pub(crate) const RIPPLE_RING_INSET: f64 = 4.0;
-/// The label bubble. Its corner radius and border are why the bubble body is
-/// composited per-pixel rather than alpha-forced across its bounding
-/// rectangle: forcing a rectangle would paint opaque square corners.
-pub(crate) const BUBBLE_WIDTH: f64 = 232.0;
-pub(crate) const BUBBLE_HEIGHT: f64 = 38.0;
-pub(crate) const BUBBLE_CORNER_RADIUS: f64 = 10.0;
-pub(crate) const BUBBLE_BORDER_WIDTH: f64 = 1.5;
-pub(crate) const BUBBLE_FONT_POINTS: f64 = 12.5;
-pub(crate) const BUBBLE_TEXT_INSET: f64 = 10.0;
-
/// The highlight drawn around the element being acted on.
pub(crate) const HIGHLIGHT_BORDER_WIDTH: f64 = 2.5;
pub(crate) const HIGHLIGHT_CORNER_RADIUS: f64 = 8.0;
@@ -65,8 +55,17 @@ pub(crate) fn ripple_rect(tip: &Point, scale: f64) -> Rect {
}
}
-pub(crate) fn bubble_size(scale: f64) -> (f64, f64) {
- (BUBBLE_WIDTH * scale, BUBBLE_HEIGHT * scale)
+/// The label pill's shadow extent: the pill's rectangle moved down by the
+/// shadow's offset and grown by its blur on every side, so the follower
+/// surface and the card's fade both cover the whole shadow.
+pub(crate) fn label_shadow_rect(pill: &Rect, scale: f64) -> Rect {
+ let blur = agent_desktop_core::LABEL_SHADOW_BLUR * scale * 2.0;
+ Rect {
+ x: pill.x - blur,
+ y: pill.y + agent_desktop_core::LABEL_SHADOW_OFFSET * scale - blur,
+ width: pill.width + blur * 2.0,
+ height: pill.height + blur * 2.0,
+ }
}
/// The highlight's rectangle: the target's bounds, padded, so the outline
diff --git a/crates/windows/src/system/cursor_overlay/geometry_tests.rs b/crates/windows/src/system/cursor_overlay/geometry_tests.rs
index d29846858..2400dcd15 100644
--- a/crates/windows/src/system/cursor_overlay/geometry_tests.rs
+++ b/crates/windows/src/system/cursor_overlay/geometry_tests.rs
@@ -1,6 +1,6 @@
use super::{
- BUBBLE_CORNER_RADIUS, BUBBLE_HEIGHT, BUBBLE_WIDTH, DART, GLYPH_HEIGHT, GLYPH_WIDTH,
- RIPPLE_SIZE, bubble_size, follower_rect, glyph_rect, highlight_rect, ripple_rect,
+ DART, GLYPH_HEIGHT, GLYPH_WIDTH, RIPPLE_SIZE, follower_rect, glyph_rect, highlight_rect,
+ label_shadow_rect, ripple_rect,
};
use agent_desktop_core::{Point, Rect};
@@ -14,8 +14,22 @@ fn tip() -> Point {
fn the_ported_dimensions_match_the_reference() {
assert_eq!((GLYPH_WIDTH, GLYPH_HEIGHT), (32.0, 40.0));
assert_eq!(RIPPLE_SIZE, 108.0);
- assert_eq!((BUBBLE_WIDTH, BUBBLE_HEIGHT), (232.0, 38.0));
- assert_eq!(BUBBLE_CORNER_RADIUS, 10.0);
+}
+
+/// The shadow falls below the pill and spreads past it, so a surface sized to
+/// the pill alone would cut it off.
+#[test]
+fn the_label_shadow_extends_below_and_around_the_pill() {
+ let pill = Rect {
+ x: 100.0,
+ y: 100.0,
+ width: 80.0,
+ height: 24.0,
+ };
+ let shadow = label_shadow_rect(&pill, 1.0);
+
+ assert!(shadow.x < pill.x && shadow.x + shadow.width > pill.x + pill.width);
+ assert!(shadow.y + shadow.height > pill.y + pill.height + 2.0);
}
/// The pose names where the cursor points, not where its box starts, so the
@@ -46,7 +60,6 @@ fn every_dimension_scales_with_the_sessions_style_size() {
assert_eq!((doubled.width, doubled.height), (64.0, 80.0));
assert_eq!(ripple.width, RIPPLE_SIZE * 2.0);
- assert_eq!(bubble_size(2.0), (BUBBLE_WIDTH * 2.0, BUBBLE_HEIGHT * 2.0));
assert_eq!(doubled.x + DART[0].0 * 2.0, tip().x, "the tip stays put");
}
@@ -85,8 +98,8 @@ fn the_follower_surface_contains_the_glyph_the_label_and_the_highlight() {
let label = Rect {
x: 900.0,
y: 700.0,
- width: BUBBLE_WIDTH,
- height: BUBBLE_HEIGHT,
+ width: 120.0,
+ height: 24.0,
};
let highlight = Rect {
x: 20.0,
diff --git a/crates/windows/src/system/cursor_overlay/presentation.rs b/crates/windows/src/system/cursor_overlay/presentation.rs
index f68d545f5..d95b26e5b 100644
--- a/crates/windows/src/system/cursor_overlay/presentation.rs
+++ b/crates/windows/src/system/cursor_overlay/presentation.rs
@@ -10,7 +10,9 @@ pub(crate) use imp::{Presentation, RestState};
#[cfg(target_os = "windows")]
mod imp {
use crate::system::cursor_overlay::reveal::Reveal;
- use agent_desktop_core::{CursorOverlayControl, CursorOverlayStyle, Point};
+ use agent_desktop_core::{
+ CursorOverlayControl, CursorOverlayStyle, LABEL_FONT_POINTS, LabelPill, Point,
+ };
use std::time::Instant;
/// The three things a frame is composed from, plus when the card began
@@ -58,6 +60,18 @@ mod imp {
self.pointing = std::mem::take(&mut self.arrives_pointing);
}
+ /// The label's pill, sized to the text as it is set at this size, or
+ /// nothing when there is no label or it cannot be measured.
+ pub(crate) fn label_pill(&self) -> Option<LabelPill> {
+ let label = self.label.as_deref()?;
+ let scale = self.style.size();
+ let width = crate::system::cursor_overlay::text::measure_label(
+ label,
+ LABEL_FONT_POINTS * scale,
+ )?;
+ LabelPill::for_label(label, width, scale)
+ }
+
/// The style a frame is drawn with: the session's own, with the
/// pointer image standing in for the arrow while the cursor rests on
/// a pressable control.
diff --git a/crates/windows/src/system/cursor_overlay/raster.rs b/crates/windows/src/system/cursor_overlay/raster.rs
index 52b748681..299376bcf 100644
--- a/crates/windows/src/system/cursor_overlay/raster.rs
+++ b/crates/windows/src/system/cursor_overlay/raster.rs
@@ -80,7 +80,7 @@ impl Surface {
}
/// Composites one sample over what is already there, premultiplied.
- fn blend(&mut self, x: i32, y: i32, paint: &Paint, coverage: f64) {
+ pub(super) fn blend(&mut self, x: i32, y: i32, paint: &Paint, coverage: f64) {
if coverage <= 0.0 || x < 0 || y < 0 || x >= self.width || y >= self.height {
return;
}
diff --git a/crates/windows/src/system/cursor_overlay/render.rs b/crates/windows/src/system/cursor_overlay/render.rs
index 156414c17..73064b318 100644
--- a/crates/windows/src/system/cursor_overlay/render.rs
+++ b/crates/windows/src/system/cursor_overlay/render.rs
@@ -1,10 +1,10 @@
//! One frame: where the follower surface sits, and what is on it.
//!
-//! Pure apart from the label's text, which the caller draws afterwards onto
-//! the bubble body this composes. Keeping the composition itself free of
+//! Pure apart from the label's text, which the caller measures beforehand
+//! and draws afterwards onto the pill this composes. Keeping the composition itself free of
//! Win32 is what lets the whole visual layer be asserted without a desktop.
-use agent_desktop_core::{CursorOverlayStyle, Point, Rect, place_label};
+use agent_desktop_core::{CursorOverlayStyle, LabelPill, Point, Rect, place_label};
use super::raster::{self, Surface, SurfaceMapping};
use super::{cursor_image, geometry, rounded};
@@ -17,7 +17,8 @@ pub(crate) struct Frame<'a> {
pub(crate) ripple_phase: f64,
pub(crate) target: Option<Rect>,
pub(crate) highlight_opacity: f64,
- pub(crate) label: Option<&'a str>,
+ /// The label's pill, sized to its measured text, when there is a label.
+ pub(crate) label: Option<LabelPill>,
pub(crate) screen: Rect,
}
@@ -28,13 +29,18 @@ pub(crate) struct Composed {
/// one. The caller draws into it and then forces it opaque, which is the
/// only region GDI is allowed to touch.
pub(crate) text_rect: Option<Rect>,
- /// The whole card, in surface coordinates, when there is one. The text
- /// rectangle above is only its inset interior; a caller fading the card
- /// in has to dim the body and its border with the text, or the card
- /// arrives already drawn with only its writing catching up.
+ /// The whole card and its shadow, in surface coordinates, when there is
+ /// one. The text rectangle above is only its interior; a caller fading
+ /// the card in has to dim the pill and its shadow with the text, or the
+ /// card arrives already drawn with only its writing catching up.
pub(crate) card_rect: Option<Rect>,
}
+/// How far the text rectangle stays inside the pill's top and bottom edges.
+/// GDI's text is copied in at full opacity across that whole rectangle, so it
+/// has to lie where the rounded body is already opaque.
+const TEXT_VERTICAL_INSET: f64 = 2.0;
+
pub(crate) fn compose(frame: &Frame<'_>) -> Composed {
let scale = frame.style.size();
let image = frame.style.image().and_then(|image| {
@@ -44,7 +50,9 @@ pub(crate) fn compose(frame: &Frame<'_>) -> Composed {
});
let label_rect = frame
.label
- .map(|_| place_label(&frame.tip, geometry::bubble_size(scale), &frame.screen));
+ .map(|pill| place_label(&frame.tip, pill.size(), &frame.screen));
+ let card_bounds = label_rect
+ .map(|rect| geometry::union_of(&rect, &geometry::label_shadow_rect(&rect, scale)));
let highlighted = frame
.target
.as_ref()
@@ -54,7 +62,7 @@ pub(crate) fn compose(frame: &Frame<'_>) -> Composed {
let mut bounds = geometry::follower_rect(
&frame.tip,
scale,
- label_rect.as_ref(),
+ card_bounds.as_ref(),
highlight_rect.as_ref(),
);
if let Some((_, rect)) = &image {
@@ -97,22 +105,17 @@ pub(crate) fn compose(frame: &Frame<'_>) -> Composed {
);
}
- let mut card_rect = None;
- let text_rect = label_rect.map(|rect| {
+ let card_rect = card_bounds.map(|card| mapping.to_local(&card));
+ let text_rect = label_rect.zip(frame.label).map(|(rect, pill)| {
let local = mapping.to_local(&rect);
- card_rect = Some(local);
- rounded::draw_bubble(
- &mut surface,
- &local,
- frame.style.fill_rgb(),
- frame.style.rim_rgb(),
- );
- let inset = geometry::BUBBLE_TEXT_INSET * scale;
+ rounded::draw_label_pill(&mut surface, &local, scale);
+ let line = pill
+ .within(local.width)
+ .text_frame(local.height - TEXT_VERTICAL_INSET * 2.0 * scale);
Rect {
- x: local.x + inset,
- y: local.y + inset,
- width: (local.width - inset * 2.0).max(0.0),
- height: (local.height - inset * 2.0).max(0.0),
+ x: local.x + line.x,
+ y: local.y + line.y,
+ ..line
}
});
diff --git a/crates/windows/src/system/cursor_overlay/render_label_tests.rs b/crates/windows/src/system/cursor_overlay/render_label_tests.rs
new file mode 100644
index 000000000..369bbc69d
--- /dev/null
+++ b/crates/windows/src/system/cursor_overlay/render_label_tests.rs
@@ -0,0 +1,116 @@
+use super::super::compose;
+use super::{frame, pill, screen};
+use agent_desktop_core::{CursorOverlayStyle, Point};
+
+/// The label's rectangle is where GDI is allowed to write, and it has to sit
+/// inside the surface or the text lands outside the window entirely.
+#[test]
+fn a_label_yields_a_text_rectangle_inside_the_surface() {
+ let style = CursorOverlayStyle::default();
+
+ let composed = compose(&frame(&style, Some(pill(80.0))));
+
+ let text = composed.text_rect.expect("a label yields a text rectangle");
+ assert!(text.x >= 0.0 && text.y >= 0.0);
+ assert!(text.x + text.width <= f64::from(composed.surface.width));
+ assert!(text.y + text.height <= f64::from(composed.surface.height));
+ assert!(text.width > 0.0 && text.height > 0.0);
+}
+
+/// The pill fits its text: a longer label yields a wider card, and the text
+/// sits centred in it rather than against its left end.
+#[test]
+fn the_card_fits_its_text_and_centres_it() {
+ let style = CursorOverlayStyle::default();
+
+ let short = compose(&frame(&style, Some(pill(30.0))));
+ let long = compose(&frame(&style, Some(pill(200.0))));
+
+ let card = |composed: &super::super::Composed| composed.card_rect.expect("a card");
+ assert!(card(&long).width > card(&short).width + 150.0);
+ for composed in [&short, &long] {
+ let text = composed.text_rect.expect("a text rectangle");
+ let card = card(composed);
+ let centre = |x: f64, width: f64| x + width / 2.0;
+ assert!((centre(text.x, text.width) - centre(card.x, card.width)).abs() < 0.01);
+ }
+}
+
+/// The bubble body must be opaque under the text, because that is what makes
+/// forcing the text rectangle's alpha correct rather than a workaround.
+#[test]
+fn the_bubble_body_is_opaque_beneath_where_the_text_will_go() {
+ let style = CursorOverlayStyle::default();
+
+ let composed = compose(&frame(&style, Some(pill(80.0))));
+ let text = composed.text_rect.expect("a text rectangle");
+
+ let alpha = composed.surface.alpha_at(
+ (text.x + text.width / 2.0) as i32,
+ (text.y + text.height / 2.0) as i32,
+ );
+ assert_eq!(
+ alpha, 255,
+ "GDI writes RGB without alpha, so it may only draw where the body is already opaque"
+ );
+ for (x, y) in [
+ (text.x, text.y),
+ (text.x + text.width - 1.0, text.y),
+ (text.x, text.y + text.height - 1.0),
+ (text.x + text.width - 1.0, text.y + text.height - 1.0),
+ ] {
+ assert_eq!(
+ composed.surface.alpha_at(x.ceil() as i32, y.ceil() as i32),
+ 255,
+ "the text rectangle's corner ({x}, {y}) lies where the rounded body is opaque"
+ );
+ }
+}
+
+/// A destination near the screen edge must not push the bubble off it: core
+/// places the label, and the surface has to follow that placement.
+#[test]
+fn a_label_near_the_screen_edge_stays_on_screen() {
+ let style = CursorOverlayStyle::default();
+ let mut edge = frame(&style, Some(pill(80.0)));
+ edge.tip = Point {
+ x: 1600.0,
+ y: 770.0,
+ };
+
+ let composed = compose(&edge);
+ let text = composed.text_rect.expect("a text rectangle");
+
+ let absolute_right = composed.origin.x + text.x + text.width;
+ let absolute_bottom = composed.origin.y + text.y + text.height;
+ assert!(
+ absolute_right <= screen().width + 1.0,
+ "{absolute_right} runs off the right"
+ );
+ assert!(
+ absolute_bottom <= screen().height + 1.0,
+ "{absolute_bottom} runs off the bottom"
+ );
+}
+
+/// A label wider than the screen is clamped with its pill, so the text and
+/// its ellipsis stay inside the pill rather than running past its end.
+#[test]
+fn a_label_wider_than_the_screen_keeps_its_text_inside_the_clamped_pill() {
+ let mut style = CursorOverlayStyle::default();
+ style.set_size(2.0);
+ let wide = agent_desktop_core::LabelPill::for_label("a long label", 600.0, 2.0)
+ .expect("a label yields a pill");
+ let mut narrow = frame(&style, Some(wide));
+ narrow.screen.width = 500.0;
+
+ let composed = compose(&narrow);
+ let text = composed.text_rect.expect("a text rectangle");
+
+ let absolute_right = composed.origin.x + text.x + text.width;
+ assert!(
+ absolute_right <= narrow.screen.width,
+ "{absolute_right} runs past the {} wide screen",
+ narrow.screen.width
+ );
+}
diff --git a/crates/windows/src/system/cursor_overlay/render_tests.rs b/crates/windows/src/system/cursor_overlay/render_tests.rs
index 9c055afdc..cc44c8cc2 100644
--- a/crates/windows/src/system/cursor_overlay/render_tests.rs
+++ b/crates/windows/src/system/cursor_overlay/render_tests.rs
@@ -1,7 +1,15 @@
use super::{Frame, compose};
-use agent_desktop_core::{CursorOverlayStyle, Point, Rect};
-fn screen() -> Rect {
+#[path = "render_label_tests.rs"]
+mod label;
+use agent_desktop_core::{CursorOverlayStyle, LabelPill, Point, Rect};
+
+/// A pill for text that measures `width` at the default size.
+pub(super) fn pill(width: f64) -> LabelPill {
+ LabelPill::for_label("a label", width, 1.0).expect("a label yields a pill")
+}
+
+pub(super) fn screen() -> Rect {
Rect {
x: 0.0,
y: 0.0,
@@ -10,7 +18,7 @@ fn screen() -> Rect {
}
}
-fn frame<'a>(style: &'a CursorOverlayStyle, label: Option<&'a str>) -> Frame<'a> {
+pub(super) fn frame(style: &CursorOverlayStyle, label: Option<LabelPill>) -> Frame<'_> {
Frame {
tip: Point { x: 400.0, y: 300.0 },
style,
@@ -112,66 +120,6 @@ fn disabling_the_highlight_removes_it() {
assert!(painted_pixels(&compose(&shown)) > painted_pixels(&compose(&hidden)));
}
-/// The label's rectangle is where GDI is allowed to write, and it has to sit
-/// inside the surface or the text lands outside the window entirely.
-#[test]
-fn a_label_yields_a_text_rectangle_inside_the_surface() {
- let style = CursorOverlayStyle::default();
-
- let composed = compose(&frame(&style, Some("open the file")));
-
- let text = composed.text_rect.expect("a label yields a text rectangle");
- assert!(text.x >= 0.0 && text.y >= 0.0);
- assert!(text.x + text.width <= f64::from(composed.surface.width));
- assert!(text.y + text.height <= f64::from(composed.surface.height));
- assert!(text.width > 0.0 && text.height > 0.0);
-}
-
-/// The bubble body must be opaque under the text, because that is what makes
-/// forcing the text rectangle's alpha correct rather than a workaround.
-#[test]
-fn the_bubble_body_is_opaque_beneath_where_the_text_will_go() {
- let style = CursorOverlayStyle::default();
-
- let composed = compose(&frame(&style, Some("open the file")));
- let text = composed.text_rect.expect("a text rectangle");
-
- let alpha = composed.surface.alpha_at(
- (text.x + text.width / 2.0) as i32,
- (text.y + text.height / 2.0) as i32,
- );
- assert_eq!(
- alpha, 255,
- "GDI writes RGB without alpha, so it may only draw where the body is already opaque"
- );
-}
-
-/// A destination near the screen edge must not push the bubble off it: core
-/// places the label, and the surface has to follow that placement.
-#[test]
-fn a_label_near_the_screen_edge_stays_on_screen() {
- let style = CursorOverlayStyle::default();
- let mut edge = frame(&style, Some("open the file"));
- edge.tip = Point {
- x: 1600.0,
- y: 770.0,
- };
-
- let composed = compose(&edge);
- let text = composed.text_rect.expect("a text rectangle");
-
- let absolute_right = composed.origin.x + text.x + text.width;
- let absolute_bottom = composed.origin.y + text.y + text.height;
- assert!(
- absolute_right <= screen().width + 1.0,
- "{absolute_right} runs off the right"
- );
- assert!(
- absolute_bottom <= screen().height + 1.0,
- "{absolute_bottom} runs off the bottom"
- );
-}
-
/// The surface follows the pose rather than spanning the screen; painting a
/// virtual screen every frame is a frame budget rather than a rounding error.
#[test]
@@ -242,7 +190,7 @@ fn composing_the_busiest_frame_fits_well_inside_a_display_frame() {
ripple_phase: 0.5,
target: Some(target),
highlight_opacity: 1.0,
- label: Some("Click the Submit button"),
+ label: Some(pill(150.0)),
screen: screen(),
};
@@ -291,7 +239,7 @@ fn composing_a_travel_frame_leaves_most_of_the_display_frame_unspent() {
ripple_phase: 0.0,
target: Some(target),
highlight_opacity: 0.0,
- label: Some("Click the Submit button"),
+ label: Some(pill(150.0)),
screen: screen(),
};
@@ -349,7 +297,7 @@ fn composing_frames_around_large_targets_fits_inside_a_display_frame() {
ripple_phase: 0.5,
target: Some(target),
highlight_opacity: 1.0,
- label: Some("Click the Submit button"),
+ label: Some(pill(150.0)),
screen: screen(),
};
diff --git a/crates/windows/src/system/cursor_overlay/rounded.rs b/crates/windows/src/system/cursor_overlay/rounded.rs
index 3ecb154b3..46144d897 100644
--- a/crates/windows/src/system/cursor_overlay/rounded.rs
+++ b/crates/windows/src/system/cursor_overlay/rounded.rs
@@ -1,5 +1,5 @@
//! The two rounded rectangles the overlay draws: the outline around the
-//! element being acted on, and the body the label's text sits on.
+//! element being acted on, and the pill the label's text sits on.
//!
//! They share a shape and a hazard. Both are composited per-pixel for the
//! reason `raster` exists — GDI leaves the alpha byte at zero and forcing
@@ -83,38 +83,82 @@ fn hollow_interior(outer: &Rect, border: f64, radius: f64) -> Rect {
}
}
-/// The label bubble's body: an opaque rounded rectangle with a rim, drawn
-/// per-pixel so its corners keep their coverage. GDI writes the text on top.
-pub(crate) fn draw_bubble(surface: &mut Surface, rect: &Rect, fill: [f64; 3], rim: [f64; 3]) {
- let radius = geometry::BUBBLE_CORNER_RADIUS;
- let border = geometry::BUBBLE_BORDER_WIDTH;
- let inner = Rect {
- x: rect.x + border,
- y: rect.y + border,
- width: (rect.width - border * 2.0).max(0.0),
- height: (rect.height - border * 2.0).max(0.0),
+/// The label pill: a soft shadow, then an opaque black, fully rounded body,
+/// drawn per-pixel so its ends keep their coverage. GDI writes the text on
+/// top, so the body has to be opaque wherever the text can land.
+pub(crate) fn draw_label_pill(surface: &mut Surface, rect: &Rect, scale: f64) {
+ let radius = rect.height / 2.0;
+ let offset = agent_desktop_core::LABEL_SHADOW_OFFSET * scale;
+ let blur = (agent_desktop_core::LABEL_SHADOW_BLUR * scale).max(0.5);
+ let shadow = Rect {
+ y: rect.y + offset,
+ ..*rect
};
- let outer = *rect;
- raster::fill_region(
+ fill_graded(
surface,
- &outer,
+ &geometry::label_shadow_rect(rect, scale),
&Paint {
- rgb: rim,
- alpha: 1.0,
+ rgb: [0.0, 0.0, 0.0],
+ alpha: agent_desktop_core::LABEL_SHADOW_OPACITY,
},
- |x, y| in_rounded_rect(&outer, radius, x, y),
+ |x, y| shadow_strength(rounded_rect_distance(&shadow, radius, x, y), blur),
);
+ let body = *rect;
raster::fill_region(
surface,
- &inner,
+ &body,
&Paint {
- rgb: fill,
+ rgb: [0.0, 0.0, 0.0],
alpha: 1.0,
},
- |x, y| in_rounded_rect(&inner, (radius - border).max(0.0), x, y),
+ |x, y| in_rounded_rect(&body, radius, x, y),
);
}
+/// Paints `paint` across `bounds` at the opacity `strength` gives each pixel's
+/// centre, for a soft edge such as a shadow, where coverage is a gradient
+/// rather than a shape.
+fn fill_graded(
+ surface: &mut Surface,
+ bounds: &Rect,
+ paint: &Paint,
+ strength: impl Fn(f64, f64) -> f64,
+) {
+ let x0 = bounds.x.floor().max(0.0) as i32;
+ let y0 = bounds.y.floor().max(0.0) as i32;
+ let x1 = (bounds.x + bounds.width)
+ .ceil()
+ .min(f64::from(surface.width)) as i32;
+ let y1 = (bounds.y + bounds.height)
+ .ceil()
+ .min(f64::from(surface.height)) as i32;
+ for y in y0..y1 {
+ for x in x0..x1 {
+ let value = strength(f64::from(x) + 0.5, f64::from(y) + 0.5);
+ surface.blend(x, y, paint, value);
+ }
+ }
+}
+
+/// How strongly a shadow covers a point `distance` outside its shape (negative
+/// inside): full well inside, none a blur's width outside, easing between.
+pub(super) fn shadow_strength(distance: f64, blur: f64) -> f64 {
+ let t = ((distance + blur) / (blur * 2.0)).clamp(0.0, 1.0);
+ 1.0 - t * t * (3.0 - 2.0 * t)
+}
+
+/// The signed distance from a point to a rounded rectangle's edge: negative
+/// inside, positive outside.
+pub(super) fn rounded_rect_distance(rect: &Rect, radius: f64, x: f64, y: f64) -> f64 {
+ let radius = radius.min(rect.width / 2.0).min(rect.height / 2.0).max(0.0);
+ let half_width = rect.width / 2.0 - radius;
+ let half_height = rect.height / 2.0 - radius;
+ let dx = (x - (rect.x + rect.width / 2.0)).abs() - half_width;
+ let dy = (y - (rect.y + rect.height / 2.0)).abs() - half_height;
+ let outside = dx.max(0.0).hypot(dy.max(0.0));
+ outside + dx.max(dy).min(0.0) - radius
+}
+
pub(super) fn in_rounded_rect(rect: &Rect, radius: f64, x: f64, y: f64) -> bool {
if rect.width <= 0.0 || rect.height <= 0.0 {
return false;
diff --git a/crates/windows/src/system/cursor_overlay/rounded_tests.rs b/crates/windows/src/system/cursor_overlay/rounded_tests.rs
index 0e04484a4..2786f11d6 100644
--- a/crates/windows/src/system/cursor_overlay/rounded_tests.rs
+++ b/crates/windows/src/system/cursor_overlay/rounded_tests.rs
@@ -1,10 +1,7 @@
-use super::super::geometry;
use super::super::raster::{Surface, SurfaceMapping};
-use super::{draw_bubble, draw_highlight};
+use super::{draw_highlight, draw_label_pill, rounded_rect_distance, shadow_strength};
use agent_desktop_core::{Point, Rect};
-const FILL: [f64; 3] = [1.0, 1.0, 1.0];
-const RIM: [f64; 3] = [0.07, 0.07, 0.09];
const ACCENT: [f64; 3] = [0.26, 0.60, 1.0];
fn surface() -> Surface {
@@ -91,35 +88,55 @@ fn a_highlight_at_zero_opacity_paints_nothing() {
assert!(surface.pixels.iter().all(|pixel| *pixel == 0));
}
-/// The bubble's corners are why its body is composited rather than
-/// alpha-forced across its bounding rectangle: forcing would paint opaque
-/// square corners and the bubble would read unlike the reference.
+/// The pill's ends are why its body is composited rather than alpha-forced
+/// across its bounding rectangle: forcing would paint opaque square corners.
#[test]
-fn the_bubbles_corners_are_rounded_rather_than_squared_off() {
+fn the_label_pill_is_fully_rounded_black_and_opaque_in_its_body() {
let mut surface = surface();
let rect = Rect {
x: 10.0,
y: 10.0,
- width: geometry::BUBBLE_WIDTH.min(180.0),
- height: geometry::BUBBLE_HEIGHT,
+ width: 120.0,
+ height: 24.0,
};
- draw_bubble(&mut surface, &rect, FILL, RIM);
+ draw_label_pill(&mut surface, &rect, 1.0);
- assert_eq!(
- surface.alpha_at(rect.x as i32, rect.y as i32),
- 0,
- "the very corner of the bounding rectangle is outside a rounded bubble"
+ assert!(
+ surface.alpha_at(rect.x as i32, rect.y as i32) < 128,
+ "the very corner of the bounding rectangle is outside a fully rounded pill"
);
- assert_eq!(
- surface.alpha_at(
+ let centre = surface
+ .pixel_at(
(rect.x + rect.width / 2.0) as i32,
- (rect.y + rect.height / 2.0) as i32
- ),
- 255,
- "the bubble's body is opaque, which is what lets GDI draw text onto it"
+ (rect.y + rect.height / 2.0) as i32,
+ )
+ .expect("inside the surface");
+ assert_eq!(centre, 0xFF00_0000, "the body is opaque black");
+ let below = surface.alpha_at(
+ (rect.x + rect.width / 2.0) as i32,
+ (rect.y + rect.height + 1.5) as i32,
+ );
+ assert!(
+ below > 0 && below < 128,
+ "a soft shadow falls below the pill"
);
}
+#[test]
+fn the_shadow_fades_from_full_inside_to_none_a_blur_outside() {
+ let rect = Rect {
+ x: 0.0,
+ y: 0.0,
+ width: 100.0,
+ height: 24.0,
+ };
+ assert!(rounded_rect_distance(&rect, 12.0, 50.0, 12.0) < 0.0);
+ assert_eq!(rounded_rect_distance(&rect, 12.0, 50.0, 30.0), 6.0);
+ assert_eq!(shadow_strength(-5.0, 2.5), 1.0);
+ assert_eq!(shadow_strength(5.0, 2.5), 0.0);
+ assert_eq!(shadow_strength(0.0, 2.5), 0.5);
+}
+
#[path = "rounded_equivalence_tests.rs"]
mod equivalence;
diff --git a/crates/windows/src/system/cursor_overlay/surface_host.rs b/crates/windows/src/system/cursor_overlay/surface_host.rs
index dc21024b5..5c0987c0f 100644
--- a/crates/windows/src/system/cursor_overlay/surface_host.rs
+++ b/crates/windows/src/system/cursor_overlay/surface_host.rs
@@ -17,13 +17,13 @@
use super::presentation::{Presentation, RestState};
#[cfg(target_os = "windows")]
use crate::system::cursor_overlay::{
- animation, caret_mark::CaretMark, fade, geometry, label, monitors, render, reveal::Reveal,
- schedule, session_state, text, topology::DisplayTopology, window::OverlayWindow,
+ animation, caret_mark::CaretMark, fade, label, monitors, render, reveal::Reveal, schedule,
+ session_state, text, topology::DisplayTopology, window::OverlayWindow,
};
#[cfg(target_os = "windows")]
use agent_desktop_core::{
CURSOR_HIGHLIGHT_HOLD_MS, CURSOR_IDLE_REST_MS, CURSOR_REST_FADE_MS, CursorMotion,
- CursorOverlayControl, CursorPhase, Point, Rect,
+ CursorOverlayControl, CursorPhase, LABEL_FONT_POINTS, Point, Rect,
};
#[cfg(target_os = "windows")]
use std::time::{Duration, Instant};
@@ -261,7 +261,7 @@ impl SurfaceHost {
ripple_phase,
target: target.cloned(),
highlight_opacity,
- label: self.presentation.label.as_deref(),
+ label: self.presentation.label_pill(),
screen,
};
let mut composed = render::compose(&frame);
@@ -272,8 +272,8 @@ impl SurfaceHost {
&mut composed.surface,
&text_rect,
label,
- self.presentation.style.rim_rgb(),
- geometry::BUBBLE_FONT_POINTS * self.presentation.style.size(),
+ [1.0, 1.0, 1.0],
+ LABEL_FONT_POINTS * self.presentation.style.size(),
);
}
if let Some(card) = composed.card_rect {
diff --git a/crates/windows/src/system/cursor_overlay/text.rs b/crates/windows/src/system/cursor_overlay/text.rs
index 2787f3eff..674b7a570 100644
--- a/crates/windows/src/system/cursor_overlay/text.rs
+++ b/crates/windows/src/system/cursor_overlay/text.rs
@@ -1,5 +1,9 @@
//! The label's text, and the one place GDI is allowed to draw.
//!
+//! The text is set in Segoe UI at semibold and measured in that same font,
+//! so the pill core sizes from the measurement fits what is drawn, and is
+//! centred in it across and down.
+//!
//! GDI writes RGB and leaves the alpha byte at zero, which is why every other
//! element is composited by hand. Text is the exception because a glyph
//! rasterizer is not something to hand-roll: it is drawn into a scratch
@@ -13,12 +17,17 @@
//! against transparency fringes with colour.
#[cfg(target_os = "windows")]
-pub(crate) use imp::draw_label;
+pub(crate) use imp::{draw_label, measure_label};
#[cfg(all(test, target_os = "windows"))]
#[path = "text_tests.rs"]
mod tests;
+#[cfg(not(target_os = "windows"))]
+pub(crate) fn measure_label(_label: &str, _points: f64) -> Option<f64> {
+ None
+}
+
#[cfg(not(target_os = "windows"))]
pub(crate) fn draw_label(
_surface: &mut super::raster::Surface,
@@ -35,12 +44,57 @@ mod imp {
use crate::system::cursor_overlay::raster::Surface;
use crate::system::cursor_overlay::wide::wide;
use agent_desktop_core::Rect;
+ use windows_sys::Win32::Foundation::SIZE;
use windows_sys::Win32::Graphics::Gdi::{
- ANTIALIASED_QUALITY, CLIP_DEFAULT_PRECIS, CreateFontW, DEFAULT_CHARSET, DEFAULT_PITCH,
- DT_END_ELLIPSIS, DT_LEFT, DT_SINGLELINE, DT_VCENTER, DeleteObject, DrawTextW, FW_NORMAL,
- GdiFlush, OUT_DEFAULT_PRECIS, SelectObject, SetBkMode, SetTextColor, TRANSPARENT,
+ ANTIALIASED_QUALITY, CLIP_DEFAULT_PRECIS, CreateCompatibleDC, CreateFontW, DEFAULT_CHARSET,
+ DEFAULT_PITCH, DT_CENTER, DT_END_ELLIPSIS, DT_SINGLELINE, DT_VCENTER, DeleteDC,
+ DeleteObject, DrawTextW, FW_SEMIBOLD, GdiFlush, GetTextExtentPoint32W, HFONT,
+ OUT_DEFAULT_PRECIS, SelectObject, SetBkMode, SetTextColor, TRANSPARENT,
};
+ fn label_font(points: f64) -> HFONT {
+ let face = wide("Segoe UI");
+ unsafe {
+ CreateFontW(
+ -(points.round() as i32),
+ 0,
+ 0,
+ 0,
+ FW_SEMIBOLD as i32,
+ 0,
+ 0,
+ 0,
+ DEFAULT_CHARSET as u32,
+ OUT_DEFAULT_PRECIS as u32,
+ CLIP_DEFAULT_PRECIS as u32,
+ ANTIALIASED_QUALITY as u32,
+ DEFAULT_PITCH as u32,
+ face.as_ptr(),
+ )
+ }
+ }
+
+ /// The label's width in pixels when set at `points`, or `None` when it
+ /// cannot be measured; a label that cannot be measured is not drawn.
+ pub(crate) fn measure_label(label: &str, points: f64) -> Option<f64> {
+ let text: Vec<u16> = label.encode_utf16().collect();
+ let length = i32::try_from(text.len()).ok()?;
+ let memory = unsafe { CreateCompatibleDC(std::ptr::null_mut()) };
+ if memory.is_null() {
+ return None;
+ }
+ let font = label_font(points);
+ let previous = unsafe { SelectObject(memory, font) };
+ let mut size = SIZE { cx: 0, cy: 0 };
+ let measured = unsafe { GetTextExtentPoint32W(memory, text.as_ptr(), length, &mut size) };
+ unsafe {
+ SelectObject(memory, previous);
+ DeleteObject(font);
+ DeleteDC(memory);
+ }
+ (measured != 0).then(|| f64::from(size.cx))
+ }
+
fn colorref(rgb: [f64; 3]) -> u32 {
let channel = |value: f64| (value.clamp(0.0, 1.0) * 255.0).round() as u32;
channel(rgb[0]) | (channel(rgb[1]) << 8) | (channel(rgb[2]) << 16)
@@ -85,25 +139,7 @@ mod imp {
}
}
- let face = wide("Segoe UI");
- let font = unsafe {
- CreateFontW(
- -(points.round() as i32),
- 0,
- 0,
- 0,
- FW_NORMAL as i32,
- 0,
- 0,
- 0,
- DEFAULT_CHARSET as u32,
- OUT_DEFAULT_PRECIS as u32,
- CLIP_DEFAULT_PRECIS as u32,
- ANTIALIASED_QUALITY as u32,
- DEFAULT_PITCH as u32,
- face.as_ptr(),
- )
- };
+ let font = label_font(points);
let previous_font = unsafe { SelectObject(memory, font) };
unsafe {
SetBkMode(memory, TRANSPARENT as i32);
@@ -123,7 +159,7 @@ mod imp {
text.as_mut_ptr(),
-1,
&mut bounds,
- DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS,
+ DT_CENTER | DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS,
);
}
diff --git a/crates/windows/src/system/cursor_overlay/text_tests.rs b/crates/windows/src/system/cursor_overlay/text_tests.rs
index 8dd5a4c2f..a7e26e79a 100644
--- a/crates/windows/src/system/cursor_overlay/text_tests.rs
+++ b/crates/windows/src/system/cursor_overlay/text_tests.rs
@@ -1,4 +1,4 @@
-use super::draw_label;
+use super::{draw_label, measure_label};
use crate::system::cursor_overlay::raster::Surface;
use agent_desktop_core::Rect;
@@ -83,3 +83,47 @@ fn the_ink_is_opaque_where_it_landed() {
"every pixel the label was copied over must stay opaque"
);
}
+
+/// The pill is sized from this measurement, so it has to grow with the text.
+#[test]
+fn a_longer_label_measures_wider() {
+ let short = measure_label("Go", 12.5).expect("a measurable label");
+ let long = measure_label("Opening the menu", 12.5).expect("a measurable label");
+
+ assert!(short > 0.0);
+ assert!(long > short * 3.0, "{long} is not much wider than {short}");
+}
+
+/// The ink is centred in its rectangle across and down, rather than starting
+/// at the left edge.
+#[test]
+fn the_label_is_centred_in_its_rectangle() {
+ let rect = Rect {
+ x: 0.0,
+ y: 0.0,
+ width: 200.0,
+ height: 30.0,
+ };
+ let mut surface = opaque_card(200, 30);
+ draw_label(&mut surface, &rect, "Go", INK, 16.0);
+
+ let inked: Vec<(i32, i32)> = (0..30)
+ .flat_map(|y| (0..200).map(move |x| (x, y)))
+ .filter(|(x, y)| surface.pixel_at(*x, *y).is_some_and(|value| value != BODY))
+ .collect();
+ assert!(!inked.is_empty(), "the label drew no ink");
+ let (left, right) = inked.iter().fold((i32::MAX, i32::MIN), |(l, r), (x, _)| {
+ (l.min(*x), r.max(*x))
+ });
+ let (top, bottom) = inked.iter().fold((i32::MAX, i32::MIN), |(t, b), (_, y)| {
+ (t.min(*y), b.max(*y))
+ });
+ assert!(
+ ((left + right) as f64 / 2.0 - 100.0).abs() <= 2.0,
+ "{left}..{right}"
+ );
+ assert!(
+ ((top + bottom) as f64 / 2.0 - 15.0).abs() <= 3.0,
+ "{top}..{bottom}"
+ );
+}
diff --git a/skills/agent-desktop-windows/references/shell-and-overlay.md b/skills/agent-desktop-windows/references/shell-and-overlay.md
index c937dae39..a974f4eee 100644
--- a/skills/agent-desktop-windows/references/shell-and-overlay.md
+++ b/skills/agent-desktop-windows/references/shell-and-overlay.md
@@ -111,12 +111,12 @@ agent-desktop cursor-overlay disable
- **It ignores the "Show animations in Windows" setting.** Windows reports
animations off by default on a stock Windows Server host, so the explicit
`enable` counts as the opt-in. macOS honours Reduce Motion instead.
-- **`--fill` and `--rim` also colour the label card** (its body and its text).
- A fill that matches the window behind it leaves the card nearly invisible.
-- **The card shows only when there is something to say.** `enable` without
+- **The label is a black pill with white Segoe UI text**, sized to the text.
+ `--fill` and `--rim` colour only the cursor, not the label.
+- **The label shows only when there is something to say.** `enable` without
`--label` greets once, and with `--image` it does not greet. After that,
- only an action that carries a description draws the card. Each `enable` and
- each action replaces the card text.
+ only an action that carries a description draws the label. Each `enable`
+ and each action replaces the label text.
- **It fades out after 6 s with no instruction** and comes straight back on the
next command.
- **The caret comes from the Text pattern.** After text entry, the agent's caret
diff --git a/skills/agent-desktop/references/commands-system.md b/skills/agent-desktop/references/commands-system.md
index c86ad54f2..517d776da 100644
--- a/skills/agent-desktop/references/commands-system.md
+++ b/skills/agent-desktop/references/commands-system.md
@@ -193,6 +193,7 @@ On macOS:
On macOS and Windows:
+- The label is a black pill with white text in the system font (SF Pro on macOS, Segoe UI on Windows), sized to the text and set below and right of the cursor's tip. A label wider than 360 pt is cut with an ellipsis, and an empty label shows no pill.
- `--image` and `--pointer-image` take a PNG of at most 2 MiB and 1024 pixels per side, drawn at `--size` with the hotspot on the point the cursor targets. Relative paths resolve at `enable`. `--fill` and `--rim` conflict with `--image`. A missing or invalid image falls back to the arrow. With `--image`, `enable` draws no greeting.
- The pointer image shows on arrival at a pressable control. Text fields keep the arrow.