Quince/src/ui/chrome.rs
astar76 d2bf8007b2 Add the --gallery page and release packaging
- quince --gallery shows every control in every state, light/dark/AMOLED
  and four seeds; "--gallery over" runs the real chrome on a made-up film.
- Fix: a toast's action button was drawn under the toast; the disabled
  play triangle showed a seam where its halves overlap.
- tools/package.sh (from Mulberry's): tar.gz, AppImage, Arch, .deb, Nix,
  Windows installer and portable zip; installer.sh, LICENSE, build.rs for
  the exe's icon and version, Inno Setup script, PKGBUILD.
- Release notes for 0.1.0 in docs/releases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-27 07:10:38 -06:00

583 lines
31 KiB
Rust

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! What floats over the video: the top bar, the control dock with the wavy seek bar, the on-screen messages, toasts,
//! and the screens shown when nothing is playing or a file has only sound.
use crate::app::{Act, MenuKind, Sheet, Toast, cmd, set};
use crate::fonts;
use crate::motion::{spring, spring_from};
use crate::mpv::{self, trim_num};
use crate::shape::{self, Form};
use crate::theme::{alpha, mix};
use crate::thumbs::Thumbs;
use crate::tokens::spring::{DEFAULT, EFFECT};
use crate::tokens::{radius, size, space, text, time};
use crate::ui::View;
use crate::ui::controls::{self, clock, play_button, seek_bar, slider};
use crate::ui::glyphs as g;
use crate::widgets::{Kind, button, cr, feel, floating_shadow, icon, icon_button, line, scale_rect};
use eframe::egui::{self, Align2, Area, Color32, Id, Order, Pos2, Rect, Sense, Ui, pos2, vec2};
const DOCK_MAX: f32 = 1040.0;
const DOCK_H: f32 = 116.0;
const BAR_H: f32 = 56.0;
fn area(ctx: &egui::Context, name: &str, rect: Rect, order: Order, f: impl FnOnce(&mut Ui)) {
Area::new(Id::new(name)).order(order).fixed_pos(rect.min).constrain(false).show(ctx, |ui| {
ui.set_min_size(rect.size());
ui.set_max_size(rect.size());
f(ui)
});
}
fn ib(ui: &mut Ui, v: &View, key: &str, at: Pos2, side: f32, glyph: &str, name: &str, keys: &str, on: bool) -> egui::Response {
let _ = v;
icon_button(ui, v.t, Id::new(("ib", key)), Rect::from_center_size(at, vec2(side, side)), glyph, name, keys, Kind::Standard, on)
}
/// The bar along the top: what is playing, and buttons to open and for the menu.
pub fn top_bar(ctx: &egui::Context, v: &mut View, sheet_w: f32) -> Vec<Rect> {
let screen = ctx.content_rect();
let t = v.t;
let lift = (1.0 - v.vis) * -12.0;
let right_w = 3.0 * size::CONTROL + 2.0 * space::S;
let right = Rect::from_min_size(pos2(screen.right() - space::L - sheet_w - right_w, screen.top() + space::L + lift), vec2(right_w, BAR_H));
// title pill
let title = if v.s.title.is_empty() { mpv::display_name(&v.s.path) } else { v.s.title.clone() };
let mut detail = vec![];
if v.s.width > 0 {
detail.push(format!("{}×{}", v.s.width, v.s.height));
}
if !v.s.video_codec.is_empty() {
detail.push(v.s.video_codec.split_whitespace().next().unwrap_or("").to_uppercase());
}
if v.s.fps > 0.0 {
detail.push(format!("{} fps", trim_num(v.s.fps, 3)));
}
if !v.s.hwdec_current.is_empty() && v.s.hwdec_current != "no" {
detail.push(format!("hardware ({})", v.s.hwdec_current));
}
if v.s.playlist.len() > 1 {
detail.push(format!("{} of {}", v.s.playlist_pos + 1, v.s.playlist.len()));
}
let detail = detail.join(" · ");
let room = (right.left() - space::S - (screen.left() + space::L)).max(120.0);
let p = ctx.layer_painter(egui::LayerId::new(Order::Middle, Id::new("top-measure")));
let tw = p.layout_no_wrap(title.clone(), fonts::semi(text::BODY + 2.0), t.on_surface).size().x;
let dw = p.layout_no_wrap(detail.clone(), fonts::ui(text::LABEL), t.on_variant).size().x;
let w = (tw.max(dw) + 2.0 * space::XL).min(room).max(120.0);
let left = Rect::from_min_size(pos2(screen.left() + space::L, screen.top() + space::L + lift), vec2(w, BAR_H));
let vis = v.vis;
area(ctx, "top-left", left, Order::Middle, |ui| {
ui.multiply_opacity(vis);
let p = ui.painter();
floating_shadow(p, left, cr(radius::XL), t.dark);
p.rect_filled(left, cr(radius::XL), t.container);
let inner = left.shrink2(vec2(space::XL, 0.0));
let a = line(p, &title, fonts::semi(text::BODY + 2.0), t.on_surface, inner.width());
let b = line(p, &detail, fonts::ui(text::LABEL), t.on_variant, inner.width());
let total = a.size().y + if detail.is_empty() { 0.0 } else { b.size().y };
let y = left.center().y - total / 2.0;
p.galley(pos2(inner.left(), y), a.clone(), t.on_surface);
if !detail.is_empty() {
p.galley(pos2(inner.left(), y + a.size().y), b, t.on_variant);
}
});
let acts = &mut *v.acts;
let vv = View { t: v.t, s: v.s, cfg: v.cfg, acts, now: v.now, vis: v.vis };
let mut out_acts = vec![];
area(ctx, "top-right", right, Order::Middle, |ui| {
ui.multiply_opacity(vis);
let p = ui.painter();
floating_shadow(p, right, cr(radius::XL), t.dark);
p.rect_filled(right, cr(radius::XL), t.container);
let y = right.center().y;
let mut x = right.left() + space::S + size::CONTROL / 2.0;
if ib(ui, &vv, "open", pos2(x, y), size::CONTROL, g::FOLDER_OPEN, "Open file", "Ctrl+O", false).clicked() {
out_acts.push(Act::OpenFiles);
}
x += size::CONTROL;
if ib(ui, &vv, "link", pos2(x, y), size::CONTROL, g::LINK, "Open link", "Ctrl+L", false).clicked() {
out_acts.push(Act::OpenUrl);
}
x += size::CONTROL;
let r = ib(ui, &vv, "more", pos2(x, y), size::CONTROL, g::MORE_VERT, "Menu", "", false);
if r.clicked() {
out_acts.push(Act::Menu(MenuKind::Context, pos2(r.rect.right(), r.rect.bottom() + space::XS), false));
}
});
v.acts.extend(out_acts);
vec![left, right]
}
/// Buttons in the corner of the empty screen.
pub fn top_bar_idle(ctx: &egui::Context, v: &mut View) -> Vec<Rect> {
let screen = ctx.content_rect();
let w = 2.0 * size::CONTROL + 2.0 * space::S;
let r = Rect::from_min_size(pos2(screen.right() - space::L - w, screen.top() + space::L), vec2(w, BAR_H));
let mut out = vec![];
let t = v.t;
area(ctx, "idle-top", r, Order::Middle, |ui| {
let y = r.center().y;
let x = r.left() + space::S + size::CONTROL / 2.0;
if icon_button(ui, t, Id::new("idle-settings"), Rect::from_center_size(pos2(x, y), vec2(40.0, 40.0)), g::SETTINGS, "Settings", "Ctrl+,", Kind::Standard, false).clicked() {
out.push(Act::Settings);
}
let m = icon_button(ui, t, Id::new("idle-more"), Rect::from_center_size(pos2(x + size::CONTROL, y), vec2(40.0, 40.0)), g::MORE_VERT, "Menu", "", Kind::Standard, false);
if m.clicked() {
out.push(Act::Menu(MenuKind::Context, pos2(m.rect.right(), m.rect.bottom() + space::XS), false));
}
});
v.acts.extend(out);
vec![r]
}
/// The control dock: seek bar with times, transport, volume, tracks, speed, sheets and fullscreen.
/// Returns its rect and whether the seek bar is being dragged.
pub fn dock(ctx: &egui::Context, v: &mut View, thumbs: &mut Thumbs, sheet_w: f32) -> (Rect, bool) {
let screen = ctx.content_rect();
let t = v.t;
let s = v.s;
let avail_l = screen.left() + space::L;
let avail_r = screen.right() - space::L - sheet_w;
let w = (avail_r - avail_l).min(DOCK_MAX).max(320.0);
let x0 = avail_l + ((avail_r - avail_l) - w) / 2.0;
let y0 = screen.bottom() - space::L - DOCK_H + (1.0 - v.vis) * 16.0;
let rect = Rect::from_min_size(pos2(x0, y0), vec2(w, DOCK_H));
let narrow = w < 640.0;
let vis = v.vis;
let mut acts = vec![];
let mut dragging = false;
let mut hover: Option<(f64, f32)> = None;
let chapters: Vec<f64> = s.chapters.iter().map(|c| c.time).collect();
let playing = !s.pause && !s.eof;
area(ctx, "dock", rect, Order::Middle, |ui| {
ui.multiply_opacity(vis);
let p = ui.painter().clone();
floating_shadow(&p, rect, cr(radius::XL), t.dark);
p.rect_filled(rect, cr(radius::XL), t.container);
// row 1: times and seek bar
let r1 = Rect::from_min_size(pos2(rect.left() + space::L, rect.top() + space::M), vec2(w - 2.0 * space::L, 32.0));
let time_w = if s.duration >= 3600.0 { 64.0 } else { 48.0 };
let shown_time = if v.cfg.remaining_time { s.time } else { s.time };
p.text(pos2(r1.left(), r1.center().y), Align2::LEFT_CENTER, clock(shown_time), fonts::med(text::LABEL + 1.0), t.on_surface);
let right_txt = if v.cfg.remaining_time { format!("-{}", clock((s.duration - s.time).max(0.0) / s.speed.max(0.01))) } else { clock(s.duration) };
let rt = Rect::from_min_size(pos2(r1.right() - time_w, r1.top()), vec2(time_w, 32.0));
let rr = ui.interact(rt, Id::new("dock-remaining"), Sense::click());
let rf = feel(&rr, true);
if rf.tint > 0.001 {
p.rect_filled(rt, cr(radius::S), alpha(t.on_surface, rf.tint));
}
p.text(pos2(rt.right(), rt.center().y), Align2::RIGHT_CENTER, right_txt, fonts::med(text::LABEL + 1.0), t.on_variant);
if rr.clicked() {
acts.push(Act::ToggleRemaining);
}
let rr = rr.on_hover_text(if v.cfg.remaining_time { "Show length" } else { "Show time left" });
let _ = rr;
let bar = Rect::from_min_max(pos2(r1.left() + time_w + space::S, r1.top()), pos2(rt.left() - space::S, r1.bottom()));
let sk = seek_bar(ui, t, Id::new("seek"), bar, s.time, s.duration, s.cache_time, &chapters, playing, v.cfg.wave);
if let Some(to) = sk.seek {
acts.push(Act::Seek(to));
}
dragging = sk.dragging.is_some();
hover = sk.hover.or(sk.dragging.map(|d| (d, bar.left() + 8.0 + (bar.width() - 16.0) * (d / s.duration.max(1.0)) as f32)));
// row 2
let y = rect.top() + space::M + 32.0 + space::XS + BAR_H / 2.0;
let mut x = rect.left() + space::M + size::CONTROL / 2.0;
let has_list = s.playlist.len() > 1;
let prev = ui.add_enabled_ui(has_list || s.chapters.len() > 1, |ui| ib(ui, v, "prev", pos2(x, y), size::CONTROL, g::SKIP_PREVIOUS, "Previous", "P", false)).inner;
if prev.clicked() {
acts.push(if has_list { cmd(&["playlist-prev"]) } else { cmd(&["add", "chapter", "-1"]) });
}
x += size::CONTROL / 2.0 + space::XS + size::LARGE / 2.0;
if play_button(ui, t, Id::new("play"), Rect::from_center_size(pos2(x, y), vec2(size::LARGE, size::LARGE)), playing).clicked() {
acts.push(Act::TogglePause);
}
x += size::LARGE / 2.0 + space::XS + size::CONTROL / 2.0;
let next = ui.add_enabled_ui(has_list || s.chapters.len() > 1, |ui| ib(ui, v, "next", pos2(x, y), size::CONTROL, g::SKIP_NEXT, "Next", "N", false)).inner;
if next.clicked() {
acts.push(if has_list { cmd(&["playlist-next"]) } else { cmd(&["add", "chapter", "1"]) });
}
x += size::CONTROL / 2.0 + space::S + size::CONTROL / 2.0;
let vol_glyph = if s.mute || s.volume <= 0.0 { g::VOLUME_OFF } else if s.volume < 50.0 { g::VOLUME_DOWN } else { g::VOLUME_UP };
if ib(ui, v, "mute", pos2(x, y), size::CONTROL, vol_glyph, if s.mute { "Sound on" } else { "Mute" }, "M", s.mute).clicked() {
acts.push(cmd(&["cycle", "mute"]));
}
let mut left_end = x + size::CONTROL / 2.0;
if !narrow {
let sr = Rect::from_min_size(pos2(left_end + space::XS, y - 16.0), vec2(120.0, 32.0));
let mut vol = if s.mute { 0.0 } else { s.volume };
if slider(ui, t, Id::new("volume"), sr, &mut vol, 0.0, 150.0, 1.0, None, &|v| format!("{} %", v.round())) {
acts.push(Act::Volume(vol));
}
left_end = sr.right();
}
// right side, from the right edge
let mut rx = rect.right() - space::M - size::CONTROL / 2.0;
let fs = ctx.input(|i| i.viewport().fullscreen.unwrap_or(false));
if ib(ui, v, "fs", pos2(rx, y), size::CONTROL, if fs { g::FULLSCREEN_EXIT } else { g::FULLSCREEN }, if fs { "Leave full screen" } else { "Full screen" }, "F", false).clicked() {
acts.push(Act::Fullscreen);
}
rx -= size::CONTROL;
if ib(ui, v, "playlist", pos2(rx, y), size::CONTROL, g::PLAYLIST_PLAY, "Playlist", "Ctrl+P", false).clicked() {
acts.push(Act::Sheet(Sheet::Playlist));
}
rx -= size::CONTROL;
if !narrow {
if ib(ui, v, "adjust", pos2(rx, y), size::CONTROL, g::TUNE, "Picture, sound and subtitles", "Ctrl+E", false).clicked() {
acts.push(Act::Sheet(Sheet::Adjust));
}
rx -= size::CONTROL;
}
let subs_on = s.sid != "no" && !s.sid.is_empty() && s.sub_visible;
let sr = ib(ui, v, "subs", pos2(rx, y), size::CONTROL, if subs_on { g::SUBTITLES } else { g::SUBTITLES_OFF }, "Subtitles", "J", false);
if sr.clicked() {
acts.push(Act::Menu(MenuKind::Subtitles, pos2(sr.rect.left(), rect.top() - space::S), true));
}
rx -= size::CONTROL;
let ar = ib(ui, v, "audio", pos2(rx, y), size::CONTROL, g::MUSIC_NOTE, "Sound track", "A", false);
if ar.clicked() {
acts.push(Act::Menu(MenuKind::Audio, pos2(ar.rect.left(), rect.top() - space::S), true));
}
rx -= size::CONTROL / 2.0 + space::XS;
// speed as a small pill showing the value
let label = format!("{}×", trim_num(s.speed, 2));
let gw = p.layout_no_wrap(label.clone(), fonts::med(text::BODY), t.on_variant).size().x;
let pw = (gw + 2.0 * space::M).max(48.0);
let pr = Rect::from_min_size(pos2(rx - pw, y - 16.0), vec2(pw, 32.0));
let changed_speed = (s.speed - 1.0).abs() > 0.001;
let resp = ui.interact(pr, Id::new("speed"), Sense::click());
let f = feel(&resp, true);
let look_fill = if changed_speed { t.secondary_container } else { Color32::TRANSPARENT };
let ink = if changed_speed { t.on_secondary_container } else { t.on_variant };
let shape = scale_rect(pr, 1.0 - 0.05 * f.press);
p.rect_filled(shape, cr(radius::S), look_fill);
if !changed_speed {
p.rect_stroke(shape, cr(radius::S), egui::Stroke::new(1.0, t.outline_variant), egui::StrokeKind::Inside);
}
if f.tint > 0.001 {
p.rect_filled(shape, cr(radius::S), alpha(ink, f.tint));
}
if f.focus {
crate::widgets::ring(&p, t, shape, cr(radius::S));
}
p.text(pr.center(), Align2::CENTER_CENTER, &label, fonts::med(text::BODY), ink);
if resp.clicked() {
acts.push(Act::Menu(MenuKind::Speed, pos2(pr.left(), rect.top() - space::S), true));
}
if resp.secondary_clicked() {
acts.push(set("speed", "1"));
}
let _ = resp.on_hover_text("Speed ([ and ])");
rx = pr.left() - space::XS;
if !s.chapters.is_empty() {
rx -= size::CONTROL / 2.0;
let cr_ = ib(ui, v, "chapters", pos2(rx, y), size::CONTROL, g::BOOKMARKS, "Chapters", "PgUp, PgDn", false);
if cr_.clicked() {
acts.push(Act::Menu(MenuKind::Chapters, pos2(cr_.rect.left(), rect.top() - space::S), true));
}
rx -= size::CONTROL / 2.0;
}
// the middle: chapter name, or loop state
let mid_l = left_end + space::L;
let mid_r = rx - space::M;
if !narrow && mid_r - mid_l > 80.0 {
let mut what = String::new();
if let Some(c) = usize::try_from(s.chapter).ok().and_then(|i| s.chapters.get(i)) {
what = c.title.clone().unwrap_or_else(|| format!("Chapter {}", s.chapter + 1));
}
if let (Some(a), b) = (s.ab_a, s.ab_b) {
what = match b {
Some(b) => format!("Looping {} to {}", clock(a), clock(b)),
None => format!("Loop from {}", clock(a)),
};
} else if s.loop_file {
what = if what.is_empty() { "Repeating".into() } else { format!("{what} · repeating") };
}
if s.buffering {
what = "Loading".into();
}
let gal = line(&p, &what, fonts::ui(text::BODY), t.on_variant, mid_r - mid_l);
p.galley(pos2((mid_l + mid_r) / 2.0 - gal.size().x / 2.0, y - gal.size().y / 2.0), gal, t.on_variant);
}
});
// preview above the bar
if let Some((at, x)) = hover {
preview(ctx, v, thumbs, at, x, rect.top() - space::S);
}
v.acts.extend(acts);
(rect, dragging)
}
fn preview(ctx: &egui::Context, v: &View, thumbs: &mut Thumbs, at: f64, x: f32, bottom: f32) {
let t = v.t;
let s = v.s;
let tex = if v.cfg.thumbnails && s.has_video() { thumbs.get(ctx, &s.path, s.duration, at) } else { None };
let chapter = s.chapters.iter().rev().find(|c| c.time <= at).and_then(|c| c.title.clone());
let p = ctx.layer_painter(egui::LayerId::new(Order::Tooltip, Id::new("seek-preview")));
let label = clock(at);
let tg = p.layout_no_wrap(label, fonts::med(text::BODY), t.on_surface);
let cg = chapter.as_ref().map(|c| line(&p, c, fonts::ui(text::LABEL), t.on_variant, 200.0));
let (tw, th) = tex.as_ref().map(|tx| {
let sz = tx.size_vec2();
(sz.x * (90.0 / sz.y.max(1.0)), 90.0)
}).unwrap_or((0.0, 0.0));
let w = tw.max(tg.size().x + 2.0 * space::M).max(cg.as_ref().map(|c| c.size().x + 2.0 * space::M).unwrap_or(0.0));
let h = th + if th > 0.0 { space::S } else { 0.0 } + tg.size().y + cg.as_ref().map(|c| c.size().y).unwrap_or(0.0) + 2.0 * space::S;
let screen = ctx.content_rect();
let left = (x - w / 2.0).clamp(screen.left() + space::S, screen.right() - space::S - w);
let r = Rect::from_min_size(pos2(left, bottom - h), vec2(w, h));
let rise = spring_from("preview-rise", 6.0, 0.0, DEFAULT);
let r = r.translate(vec2(0.0, rise));
floating_shadow(&p, r, cr(radius::M), t.dark);
p.rect_filled(r, cr(radius::M), t.high);
let mut y = r.top() + space::S;
if let Some(tx) = &tex {
let ir = Rect::from_min_size(pos2(r.center().x - tw / 2.0, y), vec2(tw, th)).shrink2(vec2(space::S, 0.0));
p.add(egui::epaint::RectShape::filled(ir, cr(radius::S), Color32::WHITE).with_texture(tx.id(), Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0))));
y += th + space::S;
}
p.galley(pos2(r.center().x - tg.size().x / 2.0, y), tg.clone(), t.on_surface);
y += tg.size().y;
if let Some(c) = cg {
p.galley(pos2(r.center().x - c.size().x / 2.0, y), c, t.on_variant);
}
}
/// Short message at the top for volume, seeking, speed and the like. A level shows as a bar under the words.
pub fn osd(ctx: &egui::Context, v: &mut View, o: Option<&crate::app::Osd>) {
let Some(o) = o else { return };
let age = v.now - o.at;
let alive = age < 1.3;
let k = spring_from(("osd", (o.at * 1000.0) as i64), 0.0, f32::from(alive), EFFECT);
if alive {
ctx.request_repaint_after(std::time::Duration::from_secs_f64(1.31 - age));
}
if k < 0.01 {
return;
}
let t = v.t;
let screen = ctx.content_rect();
let p = ctx.layer_painter(egui::LayerId::new(Order::Foreground, Id::new("osd")));
let gal = p.layout_no_wrap(o.text.clone(), fonts::med(text::BODY), t.on_surface);
let w = (gal.size().x + 2.0 * space::XL).max(if o.level.is_some() { 200.0 } else { 0.0 });
let h = if o.level.is_some() { 64.0 } else { 44.0 };
let top = screen.top() + if v.vis > 0.5 { space::L + BAR_H + space::M } else { space::XL };
let rise = spring_from(("osd-rise", (o.at * 1000.0) as i64), -8.0, 0.0, DEFAULT);
let r = Rect::from_min_size(pos2(screen.center().x - w / 2.0, top + rise), vec2(w, h));
let mut p = p.clone();
p.set_opacity(k);
floating_shadow(&p, r, cr(radius::FULL), t.dark);
p.rect_filled(r, cr(radius::FULL), t.high);
if let Some(l) = o.level {
p.galley(pos2(r.center().x - gal.size().x / 2.0, r.top() + 10.0), gal.clone(), t.on_surface);
let bar = Rect::from_min_size(pos2(r.left() + space::XL, r.bottom() - 22.0), vec2(w - 2.0 * space::XL, 12.0));
crate::wave::bar(&egui::Ui::new(ctx.clone(), Id::new("osd-ui"), egui::UiBuilder::new().layer_id(egui::LayerId::new(Order::Foreground, Id::new("osd"))).max_rect(r)), t, Id::new("osd-bar"), bar, l.clamp(0.0, 1.0), 0.0, t.primary, t.secondary_container);
} else {
p.galley(r.center() - gal.size() / 2.0, gal, t.on_surface);
}
}
/// Toasts at the bottom centre, above the dock when it shows. Newest at the bottom, older ones pushed up.
pub fn toasts(ctx: &egui::Context, v: &mut View, list: &mut Vec<Toast>) {
let now = v.now;
list.retain(|t| now - t.at < if t.action.is_some() { time::TOAST_ACTION } else { time::TOAST } + 0.4);
if list.is_empty() {
return;
}
let t = v.t;
let screen = ctx.content_rect();
let mut bottom = screen.bottom() - space::L - if v.vis > 0.5 && v.s.loaded() { DOCK_H + space::M } else { 0.0 };
let mut acts = vec![];
let n = list.len();
for (i, to) in list.iter().enumerate().rev() {
let life = if to.action.is_some() { time::TOAST_ACTION } else { time::TOAST };
let age = now - to.at;
let alive = age < life;
ctx.request_repaint_after(std::time::Duration::from_secs_f64((life - age).max(0.0) + 0.02));
let key = (to.at * 1000.0) as i64;
let k = spring_from(("toast", key), 0.0, f32::from(alive), EFFECT).clamp(0.0, 1.0);
let rise = spring_from(("toast-rise", key), 24.0, 0.0, DEFAULT);
let p = ctx.layer_painter(egui::LayerId::new(Order::Foreground, Id::new(("toast", key))));
let max_w = size::TOAST.min(screen.width() - 2.0 * space::L);
let action_w = to.action.as_ref().map(|(l, _)| p.layout_no_wrap(l.clone(), fonts::med(text::BODY), t.inverse_primary).size().x + 2.0 * space::M + space::S).unwrap_or(0.0);
let job = {
let mut j = egui::text::LayoutJob::simple(to.text.clone(), fonts::ui(text::BODY), t.inverse_on_surface, max_w - 2.0 * space::L - action_w);
j.wrap.max_rows = 3;
j
};
let gal = p.layout_job(job);
let w = (gal.size().x + 2.0 * space::L + action_w).min(max_w);
let h = (gal.size().y + 2.0 * space::M + 4.0).max(48.0);
let r = Rect::from_min_size(pos2(screen.center().x - w / 2.0, bottom - h + rise), vec2(w, h));
bottom = r.top() - space::S;
let mut p2 = p.clone();
p2.set_opacity(k);
floating_shadow(&p2, r, cr(radius::M), t.dark);
p2.rect_filled(r, cr(radius::M), t.inverse_surface);
p2.galley(pos2(r.left() + space::L, r.center().y - gal.size().y / 2.0), gal, t.inverse_on_surface);
if let Some((label, act)) = &to.action {
let br = Rect::from_min_size(pos2(r.right() - action_w, r.center().y - 18.0), vec2(action_w - space::S, 36.0));
let mut clicked = false;
area(ctx, &format!("toast-btn-{key}"), br, Order::Foreground, |ui| {
let resp = ui.interact(br, Id::new(("toast-btn", key)), Sense::click());
let f = feel(&resp, true);
// painted on the toast's own layer: the area's layer may sit under it
if f.tint > 0.001 {
p2.rect_filled(br, cr(radius::FULL), alpha(t.inverse_primary, f.tint));
}
if f.focus {
crate::widgets::ring(&p2, t, br, cr(radius::FULL));
}
p2.text(br.center(), Align2::CENTER_CENTER, label, fonts::med(text::BODY), t.inverse_primary);
clicked = resp.clicked();
});
if clicked {
acts.push(act.clone());
}
}
let _ = (i, n);
}
v.acts.extend(acts);
}
/// What shows when nothing is open: a shape, a line of what to do, two buttons, and the recent files.
pub fn empty(ctx: &egui::Context, v: &mut View, error: Option<&str>) {
let t = v.t;
let screen = ctx.content_rect();
let recent: Vec<_> = v.cfg.recent.iter().take(5).cloned().collect();
let col_w = 440.0f32.min(screen.width() - 2.0 * space::L);
let recent_h = if recent.is_empty() { 0.0 } else { space::XXL + 32.0 + recent.len() as f32 * 56.0 + 2.0 * space::S };
let err_h = if error.is_some() { 72.0 + space::L } else { 0.0 };
let total = err_h + 96.0 + space::XL + 32.0 + space::S + 20.0 + space::XL + 40.0 + recent_h;
let top = (screen.center().y - total / 2.0).max(screen.top() + 80.0);
let rect = Rect::from_min_size(pos2(screen.center().x - col_w / 2.0, top), vec2(col_w, total));
let mut acts = vec![];
area(ctx, "empty", rect, Order::Middle, |ui| {
let p = ui.painter().clone();
let mut y = rect.top();
if let Some(e) = error {
let er = Rect::from_min_size(pos2(rect.left(), y), vec2(col_w, 72.0));
p.rect_filled(er, cr(radius::L), t.error_container);
icon(&p, pos2(er.left() + space::L + 12.0, er.center().y), 24.0, g::ERROR, t.on_error_container);
let gal = p.layout(e.to_string(), fonts::ui(text::BODY), t.on_error_container, col_w - 3.0 * space::L - 24.0);
p.galley(pos2(er.left() + 2.0 * space::L + 24.0, er.center().y - gal.size().y / 2.0), gal, t.on_error_container);
y += 72.0 + space::L;
}
// the shape: turns a little when hovered, opens files when clicked
let c = pos2(rect.center().x, y + 48.0);
let sr = Rect::from_center_size(c, vec2(96.0, 96.0));
let resp = ui.interact(sr, Id::new("empty-shape"), Sense::click());
let f = feel(&resp, true);
let turn = spring("empty-turn", if resp.hovered() { 0.35 } else { 0.0 }, DEFAULT);
let pts = shape::outline(Form::Cookie12, c, 48.0 * (1.0 - 0.06 * f.press), turn);
shape::fill(&p, c, &pts, t.primary_container);
if f.tint > 0.001 {
shape::fill(&p, c, &pts, alpha(t.on_primary_container, f.tint));
}
icon(&p, c, 40.0, g::MOVIE, t.on_primary_container);
if resp.clicked() {
acts.push(Act::OpenFiles);
}
y += 96.0 + space::XL;
p.text(pos2(rect.center().x, y), Align2::CENTER_TOP, "Nothing playing", fonts::semi(text::HEADLINE), t.on_surface);
y += 32.0 + space::S;
p.text(pos2(rect.center().x, y), Align2::CENTER_TOP, "Open a video or a link, or drop files here.", fonts::ui(text::BODY), t.on_variant);
y += 20.0 + space::XL;
let bw = 148.0;
let b1 = Rect::from_min_size(pos2(rect.center().x - bw - space::S / 2.0, y), vec2(bw, 40.0));
let b2 = Rect::from_min_size(pos2(rect.center().x + space::S / 2.0, y), vec2(bw, 40.0));
if button(ui, t, Id::new("empty-open"), b1, g::FOLDER_OPEN, "Open file", Kind::Filled).on_hover_text("Open file (Ctrl+O)").clicked() {
acts.push(Act::OpenFiles);
}
if button(ui, t, Id::new("empty-link"), b2, g::LINK, "Open link", Kind::Tonal).on_hover_text("Open link (Ctrl+L)").clicked() {
acts.push(Act::OpenUrl);
}
y += 40.0 + space::XXL;
if !recent.is_empty() {
p.text(pos2(rect.left() + space::S, y + 16.0), Align2::LEFT_CENTER, "Recent", fonts::med(text::BODY), t.primary);
let cl = Rect::from_min_size(pos2(rect.right() - 72.0, y), vec2(72.0, 32.0));
if button(ui, t, Id::new("recent-clear"), cl, "", "Clear", Kind::Text).clicked() {
acts.push(Act::ClearRecent);
}
y += 32.0;
let card = Rect::from_min_size(pos2(rect.left(), y), vec2(col_w, recent.len() as f32 * 56.0 + 2.0 * space::S));
p.rect_filled(card, cr(radius::L), t.low);
let mut ry = card.top() + space::S;
for r in &recent {
let row_r = Rect::from_min_size(pos2(card.left() + space::S, ry), vec2(col_w - 2.0 * space::S, 56.0));
let name = if r.title.is_empty() || r.title == mpv::display_name(&r.path) { mpv::display_name(&r.path) } else { r.title.clone() };
let detail = if r.duration > 0.0 && r.position > 5.0 && r.position < r.duration - 10.0 {
format!("Stopped at {} of {}", clock(r.position), clock(r.duration))
} else if r.path.contains("://") {
r.path.split('/').nth(2).unwrap_or("").to_string()
} else {
std::path::Path::new(&r.path).parent().map(|d| short(&d.to_string_lossy())).unwrap_or_default()
};
let id = Id::new(("recent", &r.path));
let resp = controls::row(ui, t, id, row_r.shrink2(vec2(0.0, 2.0)), if r.path.contains("://") { g::PUBLIC } else { g::VIDEO_FILE }, &name, &detail, false);
// the remove button shows on hover without moving anything
let xr = Rect::from_center_size(pos2(row_r.right() - 24.0, row_r.center().y), vec2(32.0, 32.0));
let show_x = spring((id, "x"), f32::from(resp.hovered() || ui.rect_contains_pointer(xr)), EFFECT);
let mut removed = false;
if show_x > 0.01 {
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(xr));
child.multiply_opacity(show_x);
if icon_button(&mut child, t, id.with("x"), xr, g::CLOSE, "Remove from recent", "", Kind::Standard, false).clicked() {
acts.push(Act::ForgetRecent(r.path.clone()));
removed = true;
}
}
if resp.clicked() && !removed {
acts.push(Act::Load(r.path.clone()));
}
ry += 56.0;
}
}
});
v.acts.extend(acts);
}
fn short(p: &str) -> String {
let home = dirs::home_dir().map(|h| h.to_string_lossy().into_owned()).unwrap_or_default();
if !home.is_empty() && p.starts_with(&home) { format!("~{}", &p[home.len()..]) } else { p.to_string() }
}
/// A file with sound and no picture: a shape that is round while paused and a cookie while playing, and the title.
pub fn sound_only(ctx: &egui::Context, v: &mut View) {
let t = v.t;
let screen = ctx.content_rect();
let c = pos2(screen.center().x, screen.center().y - 60.0);
let playing = !v.s.pause && !v.s.eof;
let p = ctx.layer_painter(egui::LayerId::background());
p.rect_filled(screen, 0.0, t.surface);
let m = crate::wave::with_state(ctx, Id::new("sound-shape"), |m: &mut Option<shape::Morpher>, _| {
let m = m.get_or_insert_with(|| shape::Morpher::new(Form::Circle));
m.morph_to(if playing { Form::Cookie12 } else { Form::Circle });
if playing && controls::travels() {
m.rotation.target += crate::motion::dt() * 0.25;
crate::motion::keep_alive();
}
m.step();
m.clone()
});
let r = (screen.height() * 0.18).clamp(56.0, 120.0);
let pts = m.points(c, r);
shape::fill(&p, c, &pts, t.primary_container);
icon(&p, c, r * 0.7, g::MUSIC_NOTE, t.on_primary_container);
let title = if v.s.title.is_empty() { mpv::display_name(&v.s.path) } else { v.s.title.clone() };
let gal = line(&p, &title, fonts::semi(text::HEADLINE), t.on_surface, screen.width() - 64.0);
p.galley(pos2(screen.center().x - gal.size().x / 2.0, c.y + r * 1.25 + space::L), gal, t.on_surface);
let detail = [v.s.audio_codec.to_uppercase(), v.s.file_format.clone()].into_iter().filter(|s| !s.is_empty()).collect::<Vec<_>>().join(" · ");
p.text(pos2(screen.center().x, c.y + r * 1.25 + space::L + 36.0), Align2::CENTER_TOP, detail, fonts::ui(text::BODY), t.on_variant);
let _ = mix(t.surface, t.surface, 0.0);
}
/// Quince's logo, the same drawing as the app icon: a leaf-green clover with a dark play mark. `spin` turns it.
pub fn logo(p: &egui::Painter, c: Pos2, r: f32, spin: f32) {
let base = Color32::from_rgb(0x94, 0xbb, 0x3b);
let ink = Color32::from_rgb(0x2b, 0x39, 0x0b);
let pts = shape::outline(Form::Clover, c, r, std::f32::consts::FRAC_PI_4 + spin);
shape::fill(p, c, &pts, base);
controls::play_pause_glyph(p, c + vec2(r * 0.06, 0.0), r * 0.95, 0.0, ink);
}