use std::borrow::Cow;
use iced::{Font, Pixels, Settings, Size};
use crate::density::{Density, DensityName};
use crate::i18n::{Catalog, Locale};
use crate::m3::{ElevationPolicy, ShapePolicy};
use crate::theme::{self, ThemeCatalog, Tokens};
use crate::typo;
use crate::window::{self, DisplayBounds, WindowKind};
#[derive(Debug, Clone)]
pub struct Boot {
pub title: String,
pub application_id: String,
pub window: WindowKind,
pub theme_name: String,
pub locale: Locale,
pub density: DensityName,
pub font_scale: f32,
pub shape: ShapePolicy,
pub elevation: ElevationPolicy,
pub size: Option<Size>,
pub min_size: Option<Size>,
pub max_size: Option<Size>,
pub pointer: Option<(f32, f32)>,
pub displays: Vec<DisplayBounds>,
pub transparent: bool,
pub decorations: Option<bool>,
pub focus: Option<String>,
pub fonts: Vec<Cow<'static, [u8]>>,
}
impl Boot {
pub fn new(title: impl Into<String>, application_id: impl Into<String>) -> Self {
Self {
title: title.into(),
application_id: application_id.into(),
window: WindowKind::Application,
theme_name: "dark".into(),
locale: Locale::default(),
density: DensityName::Default,
font_scale: 1.0,
shape: ShapePolicy::Desktop,
elevation: ElevationPolicy::Desktop,
size: None,
min_size: None,
max_size: None,
pointer: None,
displays: Vec::new(),
transparent: false,
decorations: None,
focus: None,
fonts: Vec::new(),
}
}
pub fn focus(mut self, id: impl Into<String>) -> Self {
self.focus = Some(id.into());
self
}
pub fn overlay(mut self) -> Self {
self.window = WindowKind::Overlay;
self
}
pub fn dialog(mut self) -> Self {
self.window = WindowKind::Dialog;
self
}
pub fn theme(mut self, name: impl Into<String>) -> Self {
self.theme_name = name.into();
self
}
pub fn locale(mut self, locale: Locale) -> Self {
self.locale = locale;
self
}
pub fn density(mut self, density: DensityName) -> Self {
self.density = density;
self
}
pub fn font_scale(mut self, scale: f32) -> Self {
self.font_scale = scale;
self
}
pub fn shape(mut self, shape: ShapePolicy) -> Self {
self.shape = shape;
self
}
pub fn elevation(mut self, elevation: ElevationPolicy) -> Self {
self.elevation = elevation;
self
}
pub fn size(mut self, width: f32, height: f32) -> Self {
self.size = Some(Size::new(width, height));
self
}
pub fn min_size(mut self, width: f32, height: f32) -> Self {
self.min_size = Some(Size::new(width, height));
self
}
pub fn max_size(mut self, width: f32, height: f32) -> Self {
self.max_size = Some(Size::new(width, height));
self
}
pub fn pointer(mut self, x: f32, y: f32) -> Self {
self.pointer = Some((x, y));
self
}
pub fn displays(mut self, displays: impl Into<Vec<DisplayBounds>>) -> Self {
self.displays = displays.into();
self
}
pub fn transparent(mut self, on: bool) -> Self {
self.transparent = on;
self
}
pub fn decorations(mut self, on: bool) -> Self {
self.decorations = Some(on);
self
}
pub fn fonts(mut self, fonts: impl Into<Vec<Cow<'static, [u8]>>>) -> Self {
self.fonts = fonts.into();
self
}
}
#[derive(Debug, Clone)]
pub struct Prepared {
pub title: String,
pub tokens: Tokens,
pub theme_name: String,
pub density: Density,
pub locale: Locale,
pub catalog: Catalog,
pub iced_settings: Settings,
pub window: iced::window::Settings,
pub iced_theme: iced::Theme,
pub focus: Option<iced::widget::Id>,
}
pub fn bootstrap(boot: &Boot) -> Prepared {
bootstrap_with_catalog(boot, &ThemeCatalog::new())
}
pub fn bootstrap_with_catalog(boot: &Boot, themes: &ThemeCatalog) -> Prepared {
let named = themes
.get(&boot.theme_name)
.or_else(|| themes.get("dark"))
.expect("dark theme");
let tokens = named
.tokens
.with_density(Density::named(boot.density))
.with_font_scale(boot.font_scale)
.with_shape(boot.shape)
.with_elevation(boot.elevation)
.with_direction(boot.locale.direction)
.with_clock_digits(crate::i18n::ClockDigits::for_lang(&boot.locale.lang));
let iced_theme = theme::iced_theme(&named.name, tokens);
let iced_settings = Settings {
id: Some(boot.application_id.clone()),
fonts: boot.fonts.clone(),
default_font: typo::UI,
default_text_size: Pixels(tokens.body()),
antialiasing: true,
vsync: true,
};
Prepared {
title: boot.title.clone(),
tokens,
theme_name: named.name,
density: Density::named(boot.density),
locale: boot.locale.clone(),
catalog: Catalog::for_locale(&boot.locale),
iced_settings,
window: {
let mut win = window::settings(boot.window, &boot.application_id);
if let Some(size) = boot.size {
win.size = size;
}
if let Some(min) = boot.min_size {
win.min_size = Some(min);
}
if let Some(max) = boot.max_size {
win.max_size = Some(max);
}
if boot.window == WindowKind::Overlay {
if let Some(pointer) = boot.pointer {
win.position = iced::window::Position::Specific(window::place(
pointer,
win.size,
&boot.displays,
));
}
}
win.transparent = boot.transparent;
if let Some(decorations) = boot.decorations {
win.decorations = decorations;
}
win
},
iced_theme,
focus: boot.focus.clone().map(iced::widget::Id::from),
}
}
impl Prepared {
pub fn direction(&self) -> crate::i18n::Direction {
self.locale.direction
}
pub fn open(&self) -> (iced::window::Id, iced::Task<iced::window::Id>) {
iced::window::open(self.window.clone())
}
pub fn open_desktop(&self) -> (iced::window::Id, iced::Task<iced::window::Id>) {
let mut win = self.window.clone();
let app_id = self.iced_settings.id.as_deref().unwrap_or("");
crate::window::retarget(&mut win, app_id);
iced::window::open(win)
}
}
pub fn default_font() -> Font {
typo::UI
}
#[derive(Debug, Clone)]
pub struct WindowTitle(pub String);
impl<S> iced::application::TitleFn<S> for WindowTitle {
fn title(&self, _state: &S) -> String {
self.0.clone()
}
}
impl<S> iced::daemon::TitleFn<S> for WindowTitle {
fn title(&self, _state: &S, _window: iced::window::Id) -> String {
self.0.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::i18n::Locale;
fn specific_xy(pos: iced::window::Position) -> Option<(f32, f32)> {
match pos {
iced::window::Position::Specific(p) => Some((p.x, p.y)),
iced::window::Position::Centered
| iced::window::Position::Default
| iced::window::Position::SpecificWith(_) => None,
}
}
#[test]
fn bootstrap_loads_theme_and_window_kinds() {
let mut boot = Boot::new("App", "dev.icedtea.app")
.theme("nord")
.density(DensityName::Compact)
.font_scale(1.25)
.shape(ShapePolicy::Material)
.elevation(ElevationPolicy::Flat);
boot = boot.locale(Locale::new("ar"));
let prep = bootstrap(&boot);
assert_eq!(prep.theme_name, "nord");
assert_eq!(prep.density.space, 4);
assert_eq!(prep.tokens.density.space, 4);
assert_eq!(prep.tokens.body(), 18.0);
assert_eq!(prep.tokens.shape, ShapePolicy::Material);
assert_eq!(prep.tokens.elevation, ElevationPolicy::Flat);
assert_eq!(prep.locale.direction, crate::i18n::Direction::Rtl);
assert_eq!(prep.direction(), crate::i18n::Direction::Rtl);
assert_eq!(prep.catalog.t("direction"), "rtl");
assert!(prep.iced_settings.fonts.is_empty());
assert_eq!(default_font(), typo::UI);
let ov = bootstrap(&Boot::new("p", "dev.x").overlay());
assert!(!ov.window.decorations);
assert!(ov.window.max_size.is_none());
let placed = bootstrap(
&Boot::new("p", "dev.x")
.overlay()
.size(900.0, 700.0)
.pointer(100.0, 80.0)
.displays(vec![DisplayBounds {
x: 0.0,
y: 0.0,
width: 1920.0,
height: 1080.0,
}]),
);
assert_eq!(placed.window.size.width, 900.0);
assert_eq!(placed.window.size.height, 700.0);
assert!(placed.window.max_size.is_none());
assert_eq!(specific_xy(placed.window.position), Some((100.0, 80.0)));
assert_eq!(specific_xy(iced::window::Position::Centered), None);
assert_eq!(specific_xy(iced::window::Position::Default), None);
let place: fn(iced::Size, iced::Size) -> iced::Point = |_, _| iced::Point::ORIGIN;
assert_eq!(
place(iced::Size::ZERO, iced::Size::ZERO),
iced::Point::ORIGIN
);
assert_eq!(
specific_xy(iced::window::Position::SpecificWith(place)),
None
);
let dlg = bootstrap(&Boot::new("d", "dev.x").dialog());
assert!(dlg.window.decorations);
let mut cat = ThemeCatalog::new();
cat.register("brand", crate::theme::named("light").tokens, false);
let prep = bootstrap_with_catalog(&Boot::new("a", "dev.x").theme("brand"), &cat);
assert_eq!(prep.theme_name, "brand");
let missing = bootstrap(&Boot::new("a", "dev.x").theme("nope"));
assert_eq!(missing.theme_name, "dark");
let sized = bootstrap(
&Boot::new("tool", "dev.tool")
.size(380.0, 640.0)
.min_size(360.0, 560.0)
.max_size(420.0, 720.0),
);
assert_eq!(sized.window.size.width, 380.0);
assert_eq!(sized.window.size.height, 640.0);
assert_eq!(sized.window.min_size.unwrap().width, 360.0);
assert_eq!(sized.window.min_size.unwrap().height, 560.0);
assert_eq!(sized.window.max_size.unwrap().width, 420.0);
let t = WindowTitle("demo".into());
assert_eq!(
<WindowTitle as iced::application::TitleFn<()>>::title(&t, &()),
"demo"
);
assert_eq!(
<WindowTitle as iced::daemon::TitleFn<()>>::title(&t, &(), iced::window::Id::unique()),
"demo"
);
}
#[test]
fn bootstrap_copies_focus_id() {
let prep = bootstrap(&Boot::new("demo", "dev.icedtea.demo").focus("palette-query"));
assert_eq!(prep.focus, Some(iced::widget::Id::new("palette-query")));
assert!(bootstrap(&Boot::new("demo", "dev.icedtea.demo"))
.focus
.is_none());
}
#[test]
fn bootstrap_loads_application_fonts() {
let bytes: Cow<'static, [u8]> = Cow::Borrowed(&b"\0"[..]);
let prep = bootstrap(&Boot::new("demo", "dev.demo").fonts([bytes]));
assert_eq!(prep.iced_settings.fonts.len(), 1);
assert_eq!(prep.iced_settings.fonts[0].as_ref(), b"\0");
}
#[test]
fn bootstrap_transparent_and_decorations_follow_boot() {
let app = bootstrap(&Boot::new("App", "dev.x"));
assert!(!app.window.transparent);
assert!(app.window.decorations);
let glass = bootstrap(&Boot::new("App", "dev.x").transparent(true));
assert!(glass.window.transparent);
assert!(glass.window.decorations);
let opaque = bootstrap(&Boot::new("App", "dev.x").transparent(false));
assert!(!opaque.window.transparent);
let bare = bootstrap(&Boot::new("App", "dev.x").decorations(false));
assert!(!bare.window.decorations);
assert!(!bare.window.transparent);
let framed = bootstrap(&Boot::new("App", "dev.x").decorations(true));
assert!(framed.window.decorations);
let overlay = bootstrap(&Boot::new("p", "dev.x").overlay());
assert!(!overlay.window.decorations);
assert!(!overlay.window.transparent);
let overlay_chrome = bootstrap(&Boot::new("p", "dev.x").overlay().decorations(true));
assert!(overlay_chrome.window.decorations);
let overlay_glass = bootstrap(&Boot::new("p", "dev.x").overlay().transparent(true));
assert!(overlay_glass.window.transparent);
assert!(!overlay_glass.window.decorations);
}
}