use crate::input::core::coordinator::LayerId;
use crate::input::{InputCoordinator, Sense, WidgetKind};
use crate::render::{RenderContext, TextAlign};
use crate::ui::css_text::{fill_text_css, INTER};
use crate::types::{Rect, WidgetId};
use super::critter::{draw_critter, CritterView};
#[derive(Clone, Debug)]
pub struct SessionPillView<'a> {
pub label: &'a str,
pub critter: Option<CritterView>,
pub hovered: bool,
pub open: bool,
}
#[derive(Clone, Debug)]
pub struct SessionPillStyle {
pub bg: &'static str,
pub bg_hover: &'static str,
pub border: &'static str,
pub text: &'static str,
pub sub: &'static str,
pub font: &'static str,
pub font_px: f64,
pub height: f64,
pub top: f64,
pub pad_l: f64,
pub pad_r: f64,
pub avatar: f64,
pub gap: f64,
pub shadow: Option<(f64, f64, &'static str)>,
pub chevron_on_hover: bool,
}
impl Default for SessionPillStyle {
fn default() -> Self {
Self {
bg: "#1f1f1f",
bg_hover: "#262626",
border: "#2b2b2b",
text: "#f5f5f5",
sub: "#acacac",
font: "600 15px Inter, sans-serif",
font_px: 15.0,
height: 40.0,
top: 8.0,
pad_l: 7.0,
pad_r: 13.0,
avatar: 24.0,
gap: 10.0,
shadow: Some((2.0, 10.0, "rgba(0,0,0,0.333)")),
chevron_on_hover: true,
}
}
}
const CHEV_W: f64 = 14.0;
fn chevron(view: &SessionPillView<'_>, style: &SessionPillStyle) -> bool {
!style.chevron_on_hover || view.hovered || view.open
}
pub fn session_pill_width(ctx: &mut dyn RenderContext, view: &SessionPillView<'_>, style: &SessionPillStyle) -> f64 {
ctx.set_font(style.font);
let t = ctx.measure_text(view.label);
let av = if view.critter.is_some() { style.avatar + style.gap } else { 0.0 };
let ch = if chevron(view, style) { CHEV_W } else { 0.0 };
2.0 + style.pad_l + av + t + ch + style.pad_r
}
pub fn session_pill_rect(area: Rect, width: f64, style: &SessionPillStyle) -> Rect {
let w = width.min(area.width - 24.0).max(40.0);
Rect::new(area.x + (area.width - w) / 2.0, area.y + style.top, w, style.height)
}
fn round_rect(ctx: &mut dyn RenderContext, r: Rect, rad: f64) {
let rad = rad.min(r.height / 2.0).min(r.width / 2.0).max(0.0);
let k = rad * 0.552_284_749_8;
let (x0, y0, x1, y1) = (r.x, r.y, r.x + r.width, r.y + r.height);
ctx.begin_path();
ctx.move_to(x0 + rad, y0);
ctx.line_to(x1 - rad, y0);
ctx.bezier_curve_to(x1 - rad + k, y0, x1, y0 + rad - k, x1, y0 + rad);
ctx.line_to(x1, y1 - rad);
ctx.bezier_curve_to(x1, y1 - rad + k, x1 - rad + k, y1, x1 - rad, y1);
ctx.line_to(x0 + rad, y1);
ctx.bezier_curve_to(x0 + rad - k, y1, x0, y1 - rad + k, x0, y1 - rad);
ctx.line_to(x0, y0 + rad);
ctx.bezier_curve_to(x0, y0 + rad - k, x0 + rad - k, y0, x0 + rad, y0);
ctx.close_path();
}
pub fn draw_session_pill(ctx: &mut dyn RenderContext, rect: Rect, view: &SessionPillView<'_>, style: &SessionPillStyle) {
let r = rect.height / 2.0;
if let Some((dy, blur, col)) = style.shadow {
ctx.set_shadow(0.0, dy, blur, col);
}
round_rect(ctx, rect, r);
ctx.set_fill_color(style.border);
ctx.fill();
ctx.clear_shadow();
round_rect(ctx, Rect::new(rect.x + 1.0, rect.y + 1.0, rect.width - 2.0, rect.height - 2.0), r - 1.0);
ctx.set_fill_color(if view.hovered || view.open { style.bg_hover } else { style.bg });
ctx.fill();
let mut x = rect.x + 1.0 + style.pad_l;
let cy = rect.y + rect.height / 2.0;
if let Some(c) = &view.critter {
draw_critter(ctx, Rect::new(x, cy - style.avatar / 2.0, style.avatar, style.avatar), c);
x += style.avatar + style.gap;
}
ctx.set_font(style.font);
ctx.set_fill_color(style.text);
let lh = style.font_px * 1.5;
fill_text_css(ctx, view.label, x, cy - lh / 2.0, style.font_px, lh, INTER, TextAlign::Left);
if chevron(view, style) {
let chx = rect.x + rect.width - 1.0 - style.pad_r - 8.0;
ctx.begin_path();
ctx.move_to(chx, cy - 4.0);
ctx.line_to(chx + 4.0, cy);
ctx.line_to(chx, cy + 4.0);
ctx.set_stroke_color(style.sub);
ctx.set_stroke_width(1.6);
ctx.stroke();
}
}
pub fn register_session_pill(coord: &mut InputCoordinator, id: impl Into<WidgetId>, rect: Rect, layer: &LayerId) {
coord.register_atomic(id, WidgetKind::Button, rect, Sense::CLICK, layer);
}
pub fn path_round_rect(ctx: &mut dyn RenderContext, r: Rect, rad: f64) {
round_rect(ctx, r, rad);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rect_is_centred() {
let s = SessionPillStyle::default();
let r = session_pill_rect(Rect::new(100.0, 0.0, 600.0, 400.0), 200.0, &s);
assert_eq!(r.x, 300.0);
assert_eq!(r.height, 40.0);
assert_eq!(r.y, 8.0);
}
}