use iced::{Font, Pixels, Settings, Size};
use crate::density::{Density, DensityName};
use crate::i18n::{Catalog, Locale};
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 size: Option<Size>,
pub min_size: Option<Size>,
pub max_size: Option<Size>,
pub pointer: Option<(f32, f32)>,
pub displays: Vec<DisplayBounds>,
}
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,
size: None,
min_size: None,
max_size: None,
pointer: None,
displays: Vec::new(),
}
}
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 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
}
}
#[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 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;
let iced_theme = theme::iced_theme(&named.name, tokens);
let iced_settings = Settings {
id: Some(boot.application_id.clone()),
fonts: vec![],
default_font: typo::UI,
default_text_size: Pixels(typo::BODY as f32),
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
},
iced_theme,
}
}
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()
}
}
#[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);
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.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"
);
}
}