Skip to main content

material_ui_rs/
application.rs

1//! Helpers for bootstrapping `iced` applications with Material defaults.
2
3use iced::{Size, window as iced_window};
4use iced_winit::program;
5
6use crate::{Theme, fonts};
7
8/// Creates an `iced` application with the bundled Material fonts preloaded.
9///
10/// This is equivalent to [`iced::application()`] followed by
11/// [`with_material_fonts`].
12pub fn application<State, Message, Renderer>(
13    boot: impl iced::application::BootFn<State, Message>,
14    update: impl iced::application::UpdateFn<State, Message>,
15    view: impl for<'a> iced::application::ViewFn<'a, State, Message, Theme, Renderer>,
16) -> iced::Application<impl iced::Program<State = State, Message = Message, Theme = Theme>>
17where
18    State: 'static,
19    Message: Send + 'static,
20    Renderer: program::Renderer,
21{
22    with_material_fonts(iced::application(boot, update, view))
23}
24
25/// Adds the bundled Material fonts to an existing `iced` application.
26pub fn with_material_fonts<P>(application: iced::Application<P>) -> iced::Application<P>
27where
28    P: iced::Program,
29{
30    fonts::all()
31        .into_iter()
32        .fold(application, iced::Application::font)
33        .default_font(fonts::ROBOTO)
34}
35
36/// Returns centered window settings for the provided size.
37pub fn window(size: Size) -> iced_window::Settings {
38    window_settings(size, None)
39}
40
41/// Returns centered window settings with the provided minimum size.
42pub fn window_with_min_size(size: Size, min_size: Size) -> iced_window::Settings {
43    window_settings(size, Some(min_size))
44}
45
46/// Returns centered window settings with the provided size constraints.
47pub fn window_settings(size: Size, min_size: Option<Size>) -> iced_window::Settings {
48    iced_window::Settings {
49        size,
50        min_size,
51        position: iced_window::Position::Centered,
52        ..iced_window::Settings::default()
53    }
54}
55
56#[cfg(test)]
57#[path = "../tests/application.rs"]
58mod tests;