diff --git a/examples/floating_element/src/main.rs b/examples/floating_element/src/main.rs index 323abf66..eb79e97b 100644 --- a/examples/floating_element/src/main.rs +++ b/examples/floating_element/src/main.rs @@ -7,7 +7,7 @@ use iced::{ }; use iced_aw::floating_element::Anchor; -use iced_aw::graphics::icons::bootstrap::icon_to_string; +use iced_aw::graphics::icons::icon_to_string; use iced_aw::BootstrapIcon; use iced_aw::{helpers::floating_element, BOOTSTRAP_FONT}; diff --git a/examples/wrap/src/main.rs b/examples/wrap/src/main.rs index 2f166d3b..f47e0aa2 100644 --- a/examples/wrap/src/main.rs +++ b/examples/wrap/src/main.rs @@ -1,8 +1,9 @@ use std::fmt::Display; use iced::{ - widget::{Button, Column, Container, PickList, Row, Text}, - Element, Sandbox, Settings, + alignment, font, + widget::{container, text, Button, Column, Container, PickList, Row, Text}, + Application, Command, Element, Length, Settings, Theme, }; use iced_aw::{NumberInput, Wrap}; use rand::Rng; @@ -11,7 +12,12 @@ fn main() -> iced::Result { RandStrings::run(Settings::default()) } -struct RandStrings { +enum RandStrings { + Loading, + Loaded(State), +} + +struct State { vbuttons: Vec, hbuttons: Vec, spacing: f32, @@ -59,126 +65,166 @@ struct StrButton { } #[allow(clippy::enum_variant_names)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] enum Message { ChangeAlign(WrapAlign), ChangeSpacing(f32), ChangeLineSpacing(f32), ChangeMinimalLength(f32), + Loaded(Result<(), String>), + FontLoaded(Result<(), font::Error>), +} + +async fn load() -> Result<(), String> { + Ok(()) } -impl Sandbox for RandStrings { +impl Application for RandStrings { type Message = Message; + type Theme = Theme; + type Executor = iced::executor::Default; + type Flags = (); - fn new() -> Self { - let mut rng = rand::thread_rng(); - let data: Vec = (0..45) - .map(|s| StrButton { - str: s.to_string(), - size: rng.gen_range(15.0..50.0), - }) - .collect(); - Self { - vbuttons: data.clone(), - hbuttons: data, - align: iced::Alignment::Start, - spacing: 0.0, - line_spacing: 0.0, - line_minimal_length: 10.0, - } + fn new(_flags: ()) -> (Self, Command) { + ( + Self::Loading, + Command::batch(vec![ + font::load(iced_aw::graphics::icons::BOOTSTRAP_FONT_BYTES).map(Message::FontLoaded), + Command::perform(load(), Message::Loaded), + ]), + ) } fn title(&self) -> String { "wrap".to_owned() } - fn update(&mut self, message: Self::Message) { - match message { - Message::ChangeAlign(align) => { - self.align = align.into(); - } - Message::ChangeSpacing(num) => { - self.spacing = num; - } - Message::ChangeLineSpacing(num) => { - self.line_spacing = num; - } - Message::ChangeMinimalLength(num) => { - self.line_minimal_length = num; + fn update(&mut self, message: Self::Message) -> Command { + match self { + Self::Loading => { + if let Message::Loaded(_) = message { + *self = Self::Loaded({ + let mut rng = rand::thread_rng(); + let data: Vec = (0..45) + .map(|s| StrButton { + str: s.to_string(), + size: rng.gen_range(15.0..50.0), + }) + .collect(); + State { + vbuttons: data.clone(), + hbuttons: data, + align: iced::Alignment::Start, + spacing: 0.0, + line_spacing: 0.0, + line_minimal_length: 10.0, + } + }) + } } + Self::Loaded(state) => match message { + Message::ChangeAlign(align) => { + state.align = align.into(); + } + Message::ChangeSpacing(num) => { + state.spacing = num; + } + Message::ChangeLineSpacing(num) => { + state.line_spacing = num; + } + Message::ChangeMinimalLength(num) => { + state.line_minimal_length = num; + } + _ => {} + }, } + + Command::none() } fn view(&self) -> Element<'_, Self::Message> { - let RandStrings { - vbuttons, hbuttons, .. - } = self; - let vertical = Container::new( - vbuttons - .iter() - .fold(Wrap::new_vertical(), |wrap, button| { - let StrButton { str, .. } = button; - wrap.push(Button::new(Text::new(str.as_str()).size(button.size))) - }) - .align_items(self.align) - .spacing(self.spacing) - .line_spacing(self.line_spacing) - .line_minimal_length(self.line_minimal_length), - ) - .width(iced::Length::FillPortion(5)); - let horizontal = Container::new( - hbuttons - .iter() - .fold(Wrap::new(), |wrap, button| { - let StrButton { str, .. } = button; - wrap.push(Button::new(Text::new(str.as_str()).size(button.size))) - }) - .align_items(self.align) - .spacing(self.spacing) - .line_spacing(self.line_spacing) - .line_minimal_length(self.line_minimal_length), - ) - .width(iced::Length::FillPortion(5)); - let align_picklist = PickList::new( - vec![WrapAlign::Start, WrapAlign::Center, WrapAlign::End], - Some(self.align.into()), - Message::ChangeAlign, - ); - let spacing_input = Column::new() - .push(Text::new("spacing")) - .push(NumberInput::new( - self.spacing, - 500.0, - Message::ChangeSpacing, - )); - let line_spacing_input = - Column::new() - .push(Text::new("line spacing")) - .push(NumberInput::new( - self.line_spacing, - 500.0, - Message::ChangeLineSpacing, - )); - let line_minimal_length_input = - Column::new() - .push(Text::new("line minimal length")) - .push(NumberInput::new( - self.line_minimal_length, - 999.0, - Message::ChangeMinimalLength, - )); - let ctrls = Column::new() - .push(align_picklist) - .push(spacing_input) - .push(line_spacing_input) - .push(line_minimal_length_input) - .height(iced::Length::Shrink) - .align_items(iced::Alignment::Center); + match self { + Self::Loading => container( + text("Loading...") + .horizontal_alignment(alignment::Horizontal::Center) + .size(50), + ) + .width(Length::Fill) + .height(Length::Fill) + .center_y() + .center_x() + .into(), + Self::Loaded(state) => { + let vertical = Container::new( + state + .vbuttons + .iter() + .fold(Wrap::new_vertical(), |wrap, button| { + let StrButton { str, .. } = button; + wrap.push(Button::new(Text::new(str.as_str()).size(button.size))) + }) + .align_items(state.align) + .spacing(state.spacing) + .line_spacing(state.line_spacing) + .line_minimal_length(state.line_minimal_length), + ) + .width(iced::Length::FillPortion(5)); + let horizontal = Container::new( + state + .hbuttons + .iter() + .fold(Wrap::new(), |wrap, button| { + let StrButton { str, .. } = button; + wrap.push(Button::new(Text::new(str.as_str()).size(button.size))) + }) + .align_items(state.align) + .spacing(state.spacing) + .line_spacing(state.line_spacing) + .line_minimal_length(state.line_minimal_length), + ) + .width(iced::Length::FillPortion(5)); + let align_picklist = PickList::new( + vec![WrapAlign::Start, WrapAlign::Center, WrapAlign::End], + Some(state.align.into()), + Message::ChangeAlign, + ); + let spacing_input = + Column::new() + .push(Text::new("spacing")) + .push(NumberInput::new( + state.spacing, + 500.0, + Message::ChangeSpacing, + )); + let line_spacing_input = + Column::new() + .push(Text::new("line spacing")) + .push(NumberInput::new( + state.line_spacing, + 500.0, + Message::ChangeLineSpacing, + )); + let line_minimal_length_input = Column::new() + .push(Text::new("line minimal length")) + .push(NumberInput::new( + state.line_minimal_length, + 999.0, + Message::ChangeMinimalLength, + )); + let ctrls = Column::new() + .push(align_picklist) + .push(spacing_input) + .push(line_spacing_input) + .push(line_minimal_length_input) + .height(iced::Length::Shrink) + .align_items(iced::Alignment::Center); - Row::new() - .push(ctrls) - .push(vertical) - .push(horizontal) - .into() + Row::new() + .push(ctrls) + .push(vertical) + .push(horizontal) + .into() + } + } } } diff --git a/src/lib.rs b/src/lib.rs index eb08bdc9..59d7a491 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ mod platform { #[doc(no_inline)] #[cfg(feature = "icons")] pub use { - crate::graphics::icons::{bootstrap::BootstrapIcon, nerd::NerdIcon}, + crate::graphics::icons::{nerd::NerdIcon, BootstrapIcon}, crate::graphics::icons::{BOOTSTRAP_FONT, NERD_FONT}, }; diff --git a/src/native/number_input.rs b/src/native/number_input.rs index f156dd15..a408a689 100644 --- a/src/native/number_input.rs +++ b/src/native/number_input.rs @@ -26,10 +26,7 @@ use num_traits::{Num, NumAssignOps}; use std::{fmt::Display, str::FromStr}; pub use crate::{ - graphics::icons::{ - bootstrap::{icon_to_string, BootstrapIcon}, - BOOTSTRAP_FONT, - }, + graphics::icons::{icon_to_string, BootstrapIcon, BOOTSTRAP_FONT}, style::number_input::{self, Appearance, StyleSheet}, }; diff --git a/src/native/overlay/date_picker.rs b/src/native/overlay/date_picker.rs index 394e3839..f6cc996f 100644 --- a/src/native/overlay/date_picker.rs +++ b/src/native/overlay/date_picker.rs @@ -8,10 +8,7 @@ use crate::{ overlay::Position, }, date_picker, - graphics::icons::{ - bootstrap::{icon_to_string, BootstrapIcon}, - BOOTSTRAP_FONT, - }, + graphics::icons::{icon_to_string, BootstrapIcon, BOOTSTRAP_FONT}, style::{ date_picker::{Appearance, StyleSheet}, style_state::StyleState, diff --git a/src/native/tab_bar.rs b/src/native/tab_bar.rs index 3c49b7a7..bdab406d 100644 --- a/src/native/tab_bar.rs +++ b/src/native/tab_bar.rs @@ -6,10 +6,7 @@ //! *This API requires the following crate features to be activated: `tab_bar`* pub mod tab_label; -use crate::graphics::icons::{ - bootstrap::{icon_to_string, BootstrapIcon}, - BOOTSTRAP_FONT, -}; +use crate::graphics::icons::{icon_to_string, BootstrapIcon, BOOTSTRAP_FONT}; use iced_widget::{ core::{