//! sf-ui: the SmartFox TV UI kit for Slint apps. //! //! - **Liquid Glass** ([`Glass`]): the refracting, frosted glass of the //! SmartFox launcher, drawn by an OpenGL layer under your Slint UI, over an //! animated palette backdrop, a picture or a playing video. //! - **Slint components** (`@sf-ui/sf-ui.slint`): design tokens (`Tv`), text //! styles, glass panes, buttons, chips, a progress bar, an on-screen //! keyboard, icons and the Onest font. //! - **Images** ([`images::Images`], feature `images`): artwork over HTTP, //! decoded and scaled off the UI thread, cached. //! - **Video** (`video::Player`, feature `video`, Linux): GStreamer playback //! with hardware decoding, its frames shown as the glass backdrop. //! //! Guide: `docs/sf-ui.md` in the SmartFox repository. pub mod render; mod dev; mod glass; #[cfg(feature = "images")] pub mod images; pub mod text; #[cfg(feature = "video")] pub mod video; pub use glass::{Backdrop, Glass, Panels, VideoSource}; pub use render::{Matrix, Nv12Frame, Panel, Quality}; /// Selects Slint's OpenGL renderer (FemtoVG), which the glass layer draws /// with. Call it first in `main`, before creating any window. pub fn init() -> anyhow::Result<()> { slint::BackendSelector::new() .renderer_name("femtovg".into()) .select() .map_err(|e| anyhow::anyhow!("selecting the femtovg (OpenGL) renderer: {e}")) } /// The backdrop gradient's three colours: highlight, mid, deep. Use the /// `palette` of your app.toml, or one per screen / per focused item. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Palette { pub highlight: slint::Color, pub mid: slint::Color, pub deep: slint::Color, } impl Palette { /// The neutral system palette (no app colour). pub const NEUTRAL: Palette = Palette { highlight: slint::Color::from_rgb_u8(0x9a, 0xa4, 0xb5), mid: slint::Color::from_rgb_u8(0x2a, 0x31, 0x40), deep: slint::Color::from_rgb_u8(0x06, 0x07, 0x0a), }; pub fn new(highlight: slint::Color, mid: slint::Color, deep: slint::Color) -> Palette { Palette { highlight, mid, deep } } /// From three "#rrggbb" strings (as in app.toml); bad ones fall back to /// the neutral palette's colour. pub fn from_hex(hex: [&str; 3]) -> Palette { let n = Palette::NEUTRAL; Palette { highlight: parse_hex(hex[0]).unwrap_or(n.highlight), mid: parse_hex(hex[1]).unwrap_or(n.mid), deep: parse_hex(hex[2]).unwrap_or(n.deep), } } /// A palette from one colour, e.g. the average colour of a poster: /// a lighter highlight, the colour itself, a near-black deep tone. pub fn from_color(c: slint::Color) -> Palette { let [r, g, b] = rgb(c); let mix = |k: f32, t: f32| -> slint::Color { let f = |v: f32| ((v * k + t * (1.0 - k)).clamp(0.0, 1.0) * 255.0) as u8; slint::Color::from_rgb_u8(f(r), f(g), f(b)) }; Palette { highlight: mix(0.7, 1.0), mid: mix(0.55, 0.0), deep: mix(0.08, 0.0) } } /// rgb 0..1 for the renderer. pub fn to_floats(self) -> [f32; 9] { let mut out = [0.0; 9]; for (i, c) in [self.highlight, self.mid, self.deep].into_iter().enumerate() { out[i * 3..i * 3 + 3].copy_from_slice(&rgb(c)); } out } } /// "#rrggbb" or "rrggbb". pub fn parse_hex(hex: &str) -> Option { let h = hex.trim().trim_start_matches('#'); let v = u32::from_str_radix(h.get(..6)?, 16).ok()?; Some(slint::Color::from_rgb_u8((v >> 16) as u8, (v >> 8) as u8, v as u8)) } fn rgb(c: slint::Color) -> [f32; 3] { [c.red() as f32 / 255.0, c.green() as f32 / 255.0, c.blue() as f32 / 255.0] } impl Panel { /// From the fields of a Slint `GlassRect` (used by [`glue!`]). #[allow(clippy::too_many_arguments)] pub fn from_slint( x: f32, y: f32, w: f32, h: f32, r: f32, focus: f32, alpha: f32, blur: f32, glow: slint::Color, tint: slint::Color, group: i32, ) -> Panel { let [tr, tg, tb] = rgb(tint); Panel { x, y, w, h, radius: r, focus, alpha, blur, glow: rgb(glow), group: group as f32, tint: [tr, tg, tb, tint.alpha() as f32 / 255.0], } } } /// Connects the types Slint generated in *your* crate to sf-ui. Put it once /// next to `slint::include_modules!()`, with your root component's name: /// /// ```ignore /// slint::include_modules!(); /// sf_ui::glue!(App); /// ``` /// /// It makes `GlassRect` convertible to [`Panel`] (so `panels.extend(...)` /// takes your `[GlassRect]` arrays) and adds `App::sf_ui_setup()`, which /// wires the Slint helper callbacks (`SfText`). Your main .slint file must /// export the helpers: `export { Tv, SfText } from "@sf-ui/sf-ui.slint";`. #[macro_export] macro_rules! glue { ($app:ident) => { impl ::core::convert::From for $crate::Panel { fn from(g: GlassRect) -> Self { $crate::Panel::from_slint(g.x, g.y, g.w, g.h, g.r, g.focus, g.alpha, g.blur, g.glow.color(), g.tint.color(), g.group) } } impl $app { /// Wires the sf-ui Slint helpers (text editing for the keyboard). /// Call it once after creating the window. #[allow(dead_code)] pub fn sf_ui_setup(&self) { use ::slint::ComponentHandle as _; let t = self.global::(); t.on_drop_last(|s| $crate::text::drop_last(&s).into()); t.on_dots(|n| $crate::text::dots(n.max(0) as usize).into()); t.on_truncate(|s, n| $crate::text::truncate(&s, n.max(0) as usize).into()); t.on_typable(|s| $crate::text::typable(&s).into()); } } }; } #[cfg(test)] mod tests { use super::*; #[test] fn hex_colours() { assert_eq!(parse_hex("#ff7a35"), Some(slint::Color::from_rgb_u8(0xff, 0x7a, 0x35))); assert_eq!(parse_hex("121317"), Some(slint::Color::from_rgb_u8(0x12, 0x13, 0x17))); assert_eq!(parse_hex("#12"), None); let p = Palette::from_hex(["#ffffff", "nope", "#000000"]); assert_eq!(p.mid, Palette::NEUTRAL.mid); assert_eq!(p.to_floats()[0], 1.0); } #[test] fn panel_scaling() { let p = Panel { x: 10.0, y: 20.0, w: 30.0, h: 40.0, radius: 5.0, alpha: 1.0, ..Panel::default() }.scaled(2.0); assert_eq!((p.x, p.y, p.w, p.h, p.radius), (20.0, 40.0, 60.0, 80.0, 10.0)); } }