capri-ui: add chip module

This commit is contained in:
nocode committed 2026-11-11 20:20:48 +01:00
1 parent 3bd66d8d71
commit 9d45a16ef7
1 file changed
+82
+82
View File
@@ -0,0 +1,82 @@
//! Minimized restore chip: a 52 px badge at the bottom-right of the work area.
use std::time::Instant;
use eframe::egui::{self, Color32, Pos2, Sense, Stroke, ViewportBuilder, ViewportId};
use crate::{geom, i18n::t, theme, App};
const TITLE: &str = "Capri Chip";
const SIZE: f32 = 52.0;
#[derive(Default)]
pub struct Chip {
shown_at: Option<Instant>,
pinned: bool,
}
impl Chip {
pub fn close(&mut self) {
self.shown_at = None;
self.pinned = false;
}
}
impl App {
pub(crate) fn chip_ui(&mut self, ctx: &egui::Context) {
let shown_at = *self.chip.shown_at.get_or_insert_with(|| {
tracing::debug!("restore chip shown");
Instant::now()
});
let work = self.work_area(ctx);
let builder = ViewportBuilder::default()
.with_title(TITLE)
.with_inner_size([SIZE, SIZE])
.with_position(Pos2::new(work.max.x - SIZE - 16.0, work.max.y - SIZE - 16.0))
.with_transparent(true)
.with_decorations(false)
.with_resizable(false)
.with_taskbar(false)
.with_active(false)
.with_window_level(egui::WindowLevel::AlwaysOnTop)
.with_window_type(egui::X11WindowType::Utility);
let restore = ctx.show_viewport_immediate(ViewportId::from_hash_of(TITLE), builder, |ui, _| {
let rect = ui.max_rect();
let response = ui.interact(rect, ui.scope_id().with("chip"), Sense::click_and_drag()).on_hover_text(t("Show Capri"));
if response.drag_started() {
ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);
}
let p = theme::palette();
let r = rect.shrink(3.0);
let cut = 10.0;
let badge = [r.left_top() + egui::vec2(cut, 0.0), r.right_top(), r.right_bottom() - egui::vec2(0.0, cut), r.right_bottom() - egui::vec2(cut, 0.0), r.left_bottom(), r.left_top() + egui::vec2(0.0, cut)];
let painter = ui.painter();
geom::fill(painter, &badge, theme::with(p.background, 0xf2));
painter.add(egui::Shape::closed_line(badge.to_vec(), Stroke::new(1.5, p.cyan)));
// Project symbol: ring, diamond and pink core (the procedural fallback avatar).
let c = rect.center();
painter.circle_stroke(c, 12.0, Stroke::new(1.5, p.cyan));
painter.add(egui::Shape::closed_line([c + egui::vec2(0.0, -16.0), c + egui::vec2(16.0, 0.0), c + egui::vec2(0.0, 16.0), c + egui::vec2(-16.0, 0.0)].to_vec(), Stroke::new(1.0, p.cyan)));
painter.circle_filled(c, 4.0, p.pink);
// 650 ms glint after showing.
let age = shown_at.elapsed().as_secs_f32() / 0.65;
if age < 1.0 && !self.reduced() {
let x = r.left() + r.width() * age;
painter.line_segment([Pos2::new(x, r.top()), Pos2::new(x - 12.0, r.bottom())], Stroke::new(2.0, Color32::WHITE.gamma_multiply(0.5 * (1.0 - age))));
ui.ctx().request_repaint();
}
response.clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter) || i.key_pressed(egui::Key::Space))
});
if !self.chip.pinned && shown_at.elapsed().as_millis() > 300 {
self.chip.pinned = true;
if let Some(x) = &self.x11
&& let Some(w) = x.find_own(TITLE)
{
let _ = x.pin(w, true);
}
}
if restore {
self.hidden = false;
self.focus = false;
}
}
}