Files
capri-core-rust/crates/capri-ui/src/overlay.rs
T
2026-11-11 20:20:48 +01:00

186 lines
10 KiB
Rust

//! Computer-control indicator, ported from Capri's `renderer/control-overlay.{css,mjs}`:
//! one transparent, click-through, always-on-top window per monitor with border, caption tab,
//! edge effects and a reticle that follows the real pointer.
//! The Stop button lives in the companion (click-through windows cannot be clicked).
use std::f32::consts::PI;
use std::time::Instant;
use eframe::egui::{self, Align2, Color32, FontId, Painter, Pos2, Rect, Shape, Stroke, ViewportBuilder, ViewportId};
use crate::hooks::ControlOverlay;
use crate::App;
fn hex(rgb: u32, alpha: f32) -> Color32 {
let a = (alpha.clamp(0.0, 1.0) * 255.0) as u8;
Color32::from_rgba_unmultiplied((rgb >> 16) as u8, (rgb >> 8) as u8, rgb as u8, a)
}
fn polyline(p: &Painter, points: &[(f32, f32)], color: u32, alpha: f32, weight: f32) {
p.add(Shape::line(points.iter().map(|(x, y)| Pos2::new(*x, *y)).collect(), Stroke::new(weight, hex(color, alpha))));
}
/// Dynamic state copied out of the shared lock for one frame.
struct Frame {
pointer: Option<Pos2>,
trail: Vec<(Pos2, f32)>,
acting: bool,
pulse_ms: f32,
}
/// Draws one monitor's overlay. `origin` = monitor position in desktop pixels.
fn draw(painter: &Painter, rect: Rect, overlay: &ControlOverlay, f: &Frame, origin: Pos2, time: f32) {
let (w, h) = (rect.width(), rect.height());
let colors: [u32; 3] = if overlay.blackwall { [0xff2858, 0xff619c, 0xaf54ff] } else { [0xff2070, 0x70ff4a, 0x5ae6ff] };
let border = if overlay.blackwall { 0xff2858 } else { 0xff3c8d };
// Border 3 px + inset glow (`box-shadow: inset 0 0 15px`).
for i in 0..6 {
let inset = 3.0 + i as f32 * 2.5;
painter.rect_stroke(rect.shrink(inset), 0.0, Stroke::new(2.5, hex(border, 0.32 * (1.0 - i as f32 / 6.0))), egui::StrokeKind::Inside);
}
painter.rect_stroke(rect.shrink(1.5), 0.0, Stroke::new(3.0, hex(border, 1.0)), egui::StrokeKind::Middle);
let amount = if overlay.effects { overlay.intensity / 50.0 } else { 0.0 };
if amount > 0.0 {
let count = 14 + (amount * 24.0).round() as usize;
let depth = 9.0 + amount.min(1.0) * 43.0;
for i in 0..count {
let fi = i as f32;
let seed = (fi * 91.37 + 0.17).sin() * 43758.54;
let noise = seed - seed.floor();
let y = (noise * h + time * (8.0 + noise * 20.0)).rem_euclid(h + 100.0) - 50.0;
let reach = 4.0 + depth * (0.3 + 0.7 * (fi * 5.2 + time * 0.4).sin().powi(2));
let alpha = amount * (0.12 + 0.3 * (fi + time).sin().powi(2));
let (x, sign) = if i % 2 == 1 { (w - 3.0, -1.0) } else { (3.0, 1.0) };
if overlay.blackwall {
let column = x + sign * (3.0 + noise * depth);
polyline(painter, &[(column, y - 45.0 - noise * 90.0), (column, y + 35.0), (column + sign * 9.0, y + 35.0), (column + sign * 9.0, y + 50.0)], colors[i % 3], alpha * 1.25, if i % 4 == 0 { 2.0 } else { 1.0 });
polyline(painter, &[(x, y), (x + sign * reach, y)], colors[0], alpha * 0.8, 1.0);
} else {
polyline(painter, &[(x, y - 28.0), (x + sign * reach, y - 11.0), (x + sign * reach, y + 8.0), (x + sign * 3.0, y + 21.0)], colors[i % 3], alpha, if i % 5 == 0 { 2.0 } else { 1.0 });
}
if !overlay.blackwall && i % 7 == 0 {
let ex = x + sign * (10.0 + depth * 0.4);
polyline(painter, &[(ex - 7.0, y), (ex, y - 4.0), (ex + 7.0, y), (ex, y + 4.0), (ex - 7.0, y)], colors[1], amount * 0.7, 1.0);
painter.rect_filled(Rect::from_min_size(Pos2::new(ex - 1.0, y - 1.0), egui::vec2(2.0, 2.0)), 0.0, hex(colors[2], amount * 0.8));
}
let top = (noise * w + time * 16.0).rem_euclid(w);
polyline(painter, &[(top, 3.0), (top + 18.0, 3.0 + reach * 0.5), (top + 33.0, 3.0 + reach * 0.5)], colors[i % 3], alpha, 1.0);
polyline(painter, &[(w - top, h - 3.0), (w - top - 18.0, h - 3.0 - reach * 0.5)], colors[(i + 1) % 3], alpha, 1.0);
}
// Brief chromatic fractures at the edges, never over the centre.
if (time * 1.37).sin() > 0.985 || f.pulse_ms < 160.0 {
for i in 0..7 {
let y = (i as f32 * 173.0 + time * 110.0).rem_euclid(h);
let side = if i % 2 == 1 { w - 70.0 } else { 0.0 };
painter.rect_filled(Rect::from_min_size(Pos2::new(side, y), egui::vec2(16.0 + i as f32 * 5.0, 1.0 + (i % 3) as f32)), 0.0, hex(colors[i % 3], amount * 0.65));
}
}
let corner = 30.0 + amount.min(1.0) * 25.0;
for (x, y, sx, sy) in [(8.0, 8.0, 1.0, 1.0), (w - 8.0, 8.0, -1.0, 1.0), (8.0, h - 8.0, 1.0, -1.0), (w - 8.0, h - 8.0, -1.0, -1.0)] {
polyline(painter, &[(x, y + sy * corner), (x, y + sy * 9.0), (x + sx * 9.0, y), (x + sx * corner, y)], if f.acting { colors[1] } else { colors[0] }, 0.45 + amount * 0.5, 2.0);
}
}
// Caption tab (`.control-label`): yellow monospace on dark, pink border without top edge.
let font = FontId::monospace(12.0);
let galley = painter.layout_no_wrap(overlay.caption.clone(), font.clone(), hex(0xffe948, 1.0));
let tab = Rect::from_min_size(Pos2::new(w / 2.0 - galley.size().x / 2.0 - 18.0, 0.0), galley.size() + egui::vec2(36.0, 16.0));
painter.rect_filled(tab, 0.0, hex(0x101426, 0.96));
let pink = Stroke::new(2.0, hex(0xff3c8d, 1.0));
painter.add(Shape::line(vec![tab.left_top(), tab.left_bottom(), tab.right_bottom(), tab.right_top()], pink));
painter.text(tab.center(), Align2::CENTER_CENTER, &overlay.caption, font, hex(0xffe948, 1.0));
// Reticle and trail at the real pointer.
if overlay.cursor
&& let Some(p) = f.pointer
{
let p = p - origin.to_vec2();
if !rect.expand(40.0).contains(p) {
return;
}
for (t, age) in &f.trail {
let t = *t - origin.to_vec2();
painter.rect_filled(Rect::from_center_size(t, egui::vec2(4.0, 4.0)), 0.0, hex(colors[0], 0.35 * (1.0 - age / 350.0).max(0.0)));
}
let pulse = (1.0 - f.pulse_ms / 450.0).max(0.0);
let r = 12.0 + pulse * 12.0;
let start = time * 0.4;
let arc: Vec<Pos2> = (0..=32).map(|k| start + PI * 1.6 * k as f32 / 32.0).map(|a| Pos2::new(p.x + r * a.cos(), p.y + r * a.sin())).collect();
painter.add(Shape::line(arc, Stroke::new(1.4, hex(colors[2], 0.85))));
polyline(painter, &[(p.x - r - 6.0, p.y), (p.x - r, p.y)], colors[1], 0.9, 2.0);
polyline(painter, &[(p.x, p.y + r + 5.0), (p.x, p.y + r)], colors[0], 0.9, 2.0);
polyline(painter, &[(p.x + r, p.y), (p.x + r + 5.0, p.y - 5.0), (p.x + r + 5.0, p.y + 5.0), (p.x + r, p.y)], colors[0], 0.9, 1.5);
polyline(painter, &[(p.x - 5.0, p.y), (p.x, p.y - 4.0), (p.x + 5.0, p.y), (p.x, p.y + 4.0), (p.x - 5.0, p.y)], colors[1], 0.8, 1.0);
painter.rect_filled(Rect::from_center_size(p, egui::vec2(2.0, 2.0)), 0.0, hex(colors[2], 0.9));
}
}
impl App {
pub(crate) fn control_overlay(&mut self, ctx: &egui::Context) {
let pin_key = egui::Id::NULL.with("capri-control-pinned");
let (overlay, frame) = {
let mut s = self.ui.0.state.lock();
let Some(overlay) = s.overlay.clone() else {
drop(s);
ctx.data_mut(|d| d.remove::<usize>(pin_key));
return;
};
let now = Instant::now();
s.control_trail.retain(|(_, at)| now.duration_since(*at).as_millis() < 350);
let frame = Frame {
pointer: s.control_pointer.map(|p| Pos2::new(p[0], p[1])),
trail: s.control_trail.iter().map(|(p, at)| (Pos2::new(p[0], p[1]), now.duration_since(*at).as_secs_f32() * 1000.0)).collect(),
acting: s.control_phase == "acting",
pulse_ms: s.control_pulse.map_or(f32::MAX, |at| now.duration_since(at).as_secs_f32() * 1000.0),
};
(overlay, frame)
};
let reduced = self.reduced();
let time = if reduced { 0.0 } else { self.epoch.elapsed().as_secs_f32() };
for (i, m) in overlay.monitors.iter().enumerate() {
let title = format!("Capri control {i}");
let builder = ViewportBuilder::default()
.with_title(&title)
.with_position(Pos2::new(m[0], m[1]))
.with_inner_size([m[2], m[3]])
.with_transparent(true)
.with_decorations(false)
.with_resizable(false)
.with_taskbar(false)
.with_active(false)
.with_mouse_passthrough(true)
.with_window_level(egui::WindowLevel::AlwaysOnTop)
.with_window_type(egui::X11WindowType::Notification);
ctx.show_viewport_immediate(ViewportId::from_hash_of(&title), builder, |ui, _| {
draw(ui.painter(), ui.max_rect(), &overlay, &frame, Pos2::new(m[0], m[1]), time);
});
}
let pinned = ctx.data(|d| d.get_temp::<usize>(pin_key)).unwrap_or(0);
if pinned < overlay.monitors.len()
&& let Some(x) = &self.x11
{
let found: Vec<_> = (0..overlay.monitors.len()).filter_map(|i| x.find_own(&format!("Capri control {i}"))).collect();
for w in &found {
let _ = x.pin(*w, true);
}
ctx.data_mut(|d| d.insert_temp(pin_key, found.len()));
}
// Visible once every overlay window exists (without X11 inspection: after one frame).
let drawn = self.x11.is_none() || ctx.data(|d| d.get_temp::<usize>(pin_key)).unwrap_or(0) >= overlay.monitors.len();
let mut state = self.ui.0.state.lock();
if state.overlay.as_ref() == Some(&overlay) {
state.overlay_drawn = drawn;
}
drop(state);
ctx.request_repaint_after(std::time::Duration::from_millis(if reduced { 250 } else { 33 }));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colours() {
assert_eq!(hex(0xff2070, 1.0), Color32::from_rgb(0xff, 0x20, 0x70));
assert_eq!(hex(0x5ae6ff, 0.0).a(), 0);
}
}