Skip to content

Commit

Permalink
updated number input to newest style
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed May 21, 2024
1 parent f215636 commit b802445
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 202 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ drop_down = []
default = [
"badge",
"card",
#"number_input",
"number_input",
#"date_picker",
#"color_picker",
#"grid",
Expand Down Expand Up @@ -79,7 +79,7 @@ opt-level = 2
members = [
"examples/badge",
"examples/card",
#"examples/number_input",
"examples/number_input",
#"examples/color_picker",
#"examples/font_loading",
#"examples/grid",
Expand Down
129 changes: 35 additions & 94 deletions examples/number_input/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,56 @@
use iced::{
alignment, font,
theme::Theme,
widget::{container, text, Container, Row, Text},
window, Alignment, Application, Command, Element, Length, Settings,
widget::{Container, Row, Text},
Alignment, Element, Length,
};
use iced_aw::{number_input, style::NumberInputStyles};

#[derive(Debug)]
enum NumberInputDemo {
Loading,
Loaded(State),
}
use iced_aw::number_input;

#[derive(Default, Debug)]
pub struct State {
pub struct NumberInputDemo {
value: f32,
}

#[derive(Debug, Clone)]
pub enum Message {
NumInpChanged(f32),
Loaded(Result<(), String>),
FontLoaded(Result<(), font::Error>),
}

fn main() -> iced::Result {
NumberInputDemo::run(Settings {
default_text_size: iced::Pixels(12.0),
window: window::Settings {
size: iced::Size {
width: 250.0,
height: 200.0,
},
..Default::default()
},
..Settings::default()
iced::program(
"Number Input example",
NumberInputDemo::update,
NumberInputDemo::view,
)
.window_size(iced::Size {
width: 250.0,
height: 200.0,
})
.font(iced_aw::BOOTSTRAP_FONT_BYTES)
.run()
}

async fn load() -> Result<(), String> {
Ok(())
}

impl Application for NumberInputDemo {
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();

fn new(_flags: ()) -> (NumberInputDemo, Command<Message>) {
(
NumberInputDemo::Loading,
Command::batch(vec![
font::load(iced_aw::BOOTSTRAP_FONT_BYTES).map(Message::FontLoaded),
Command::perform(load(), Message::Loaded),
]),
)
}

fn title(&self) -> String {
String::from("Number Input Demo")
}

fn update(&mut self, message: self::Message) -> Command<Message> {
match self {
NumberInputDemo::Loading => {
if let Message::Loaded(_) = message {
*self = NumberInputDemo::Loaded(State { value: 27.0 })
}
}
NumberInputDemo::Loaded(State { value }) => {
if let Message::NumInpChanged(val) = message {
*value = val;
}
}
}

Command::none()
impl NumberInputDemo {
fn update(&mut self, message: self::Message) {
let Message::NumInpChanged(val) = message;
self.value = val;
}

fn view(&self) -> Element<Message> {
match self {
NumberInputDemo::Loading => container(
text("Loading...")
.horizontal_alignment(alignment::Horizontal::Center)
.size(50),
)
.width(Length::Fill)
.height(Length::Fill)
.center_y()
.center_x()
.into(),
NumberInputDemo::Loaded(State { value }) => {
let lb_minute = Text::new("Number Input:");
let txt_minute = number_input(*value, 0.0..250.0, Message::NumInpChanged)
.style(NumberInputStyles::Default)
.step(0.5);

Container::new(
Row::new()
.spacing(10)
.align_items(Alignment::Center)
.push(lb_minute)
.push(txt_minute),
)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
let lb_minute = Text::new("Number Input:");
let txt_minute = number_input(self.value, 0.0..250.0, Message::NumInpChanged)
.style(number_input::number_input::primary)
.step(0.5);

Container::new(
Row::new()
.spacing(10)
.align_items(Alignment::Center)
.push(lb_minute)
.push(txt_minute),
)
.width(Length::Fill)
.height(Length::Fill)
.center_x(Length::Fill)
.center_y(Length::Fill)
.into()
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ mod platform {

#[doc(no_inline)]
#[cfg(feature = "number_input")]
pub use {
crate::style::NumberInputStyles, crate::widgets::number_input, number_input::NumberInput,
};
pub use {crate::widgets::number_input, number_input::NumberInput};

#[doc(no_inline)]
#[cfg(feature = "selection_list")]
Expand Down
4 changes: 2 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod colors;
pub mod status;
pub mod style_state;

pub use status::{Status, StyleFn};

#[cfg(feature = "badge")]
pub mod badge;

Expand Down Expand Up @@ -32,8 +34,6 @@ pub use time_picker::TimePickerStyle;

#[cfg(feature = "number_input")]
pub mod number_input;
#[cfg(feature = "number_input")]
pub use number_input::NumberInputStyles;

#[cfg(feature = "selection_list")]
pub mod selection_list;
Expand Down
5 changes: 1 addition & 4 deletions src/style/badge.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Use a badge for color highlighting important information.
//!
//! *This API requires the following crate features to be activated: badge*
use super::{
colors,
status::{Status, StyleFn},
};
use super::{colors, Status, StyleFn};
use iced::{theme::palette, Background, Color, Theme};

/// The style of a [`Badge`](crate::native::badge::Badge).
Expand Down
5 changes: 1 addition & 4 deletions src/style/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
//!
//! *This API requires the following crate features to be activated: card*
use super::{
colors,
status::{Status, StyleFn},
};
use super::{colors, Status, StyleFn};
use iced::{Background, Color, Theme};

/// The appearance of a [`Card`](crate::native::card::Card).
Expand Down
99 changes: 44 additions & 55 deletions src/style/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
//!
//! *This API requires the following crate features to be activated: `number_input`*
use super::{Status, StyleFn};
use iced::{Background, Color, Theme};

/// The appearance of a [`NumberInput`](crate::native::number_input::NumberInput).
#[derive(Clone, Copy, Debug)]
pub struct Appearance {
pub struct Style {
/// The background of the [`NumberInput`](crate::native::number_input::NumberInput).
pub button_background: Option<Background>,
/// The Color of the arrows of [`NumberInput`](crate::native::number_input::NumberInput).
pub icon_color: Color,
}

impl Default for Appearance {
impl Default for Style {
fn default() -> Self {
Self {
button_background: None,
Expand All @@ -22,79 +23,67 @@ impl Default for Appearance {
}
}

/// The appearance of a [`NumberInput`](crate::native::number_input::NumberInput).
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
pub trait StyleSheet {
type Style: Default;
/// The normal appearance of a [`NumberInput`](crate::native::number_input::NumberInput).
fn active(&self, style: &Self::Style) -> Appearance;
/// The Catalog of a [`NumberInput`](crate::native::number_input::NumberInput).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;

/// The appearance when the [`NumberInput`](crate::native::number_input::NumberInput) is pressed.
fn pressed(&self, style: &Self::Style) -> Appearance;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;

/// The appearance when the [`NumberInput`](crate::native::number_input::NumberInput) is disabled.
fn disabled(&self, style: &Self::Style) -> Appearance;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}

#[derive(Default)]
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
/// Default Prebuilt ``NumberInput`` Styles
pub enum NumberInputStyles {
#[default]
Default,
Custom(Box<dyn StyleSheet<Style = Theme>>),
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;

impl NumberInputStyles {
/// Creates a custom [`NumberInputStyles`] style variant.
pub fn custom(style_sheet: impl StyleSheet<Style = Theme> + 'static) -> Self {
Self::Custom(Box::new(style_sheet))
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
}

impl StyleSheet for Theme {
type Style = NumberInputStyles;

fn active(&self, style: &Self::Style) -> Appearance {
if let NumberInputStyles::Custom(custom) = style {
return custom.active(self);
}

let palette = self.extended_palette();

Appearance {
button_background: Some(palette.primary.strong.color.into()),
icon_color: palette.primary.strong.text,
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}

/// The appearance of a [`NumberInput`](crate::native::number_input::NumberInput).
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
pub trait StyleSheet {
type Style: Default;
/// The normal appearance of a [`NumberInput`](crate::native::number_input::NumberInput).
fn active(&self, style: &Self::Style) -> Style;

/// The appearance when the [`NumberInput`](crate::native::number_input::NumberInput) is pressed.
fn pressed(&self, style: &Self::Style) -> Appearance {
if let NumberInputStyles::Custom(custom) = style {
return custom.pressed(self);
}
self.active(style)
}
fn pressed(&self, style: &Self::Style) -> Style;

/// The appearance when the [`NumberInput`](crate::native::number_input::NumberInput) is disabled.
fn disabled(&self, style: &Self::Style) -> Appearance {
if let NumberInputStyles::Custom(custom) = style {
return custom.disabled(self);
}
fn disabled(&self, style: &Self::Style) -> Style;
}

/// The primary theme of a [`Badge`](crate::native::badge::Badge).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = Style {
button_background: Some(palette.primary.strong.color.into()),
icon_color: palette.primary.strong.text,
};

let active = self.active(style);
Appearance {
button_background: active.button_background.map(|bg| match bg {
match status {
Status::Active | Status::Pressed | Status::Focused | Status::Hovered => base,
Status::Disabled => Style {
button_background: base.button_background.map(|bg| match bg {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
Background::Gradient(grad) => Background::Gradient(grad),
}),
icon_color: Color {
a: active.icon_color.a * 0.5,
..active.icon_color
a: base.icon_color.a * 0.5,
..base.icon_color
},
}
},
}
}
8 changes: 4 additions & 4 deletions src/widgets/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ pub fn number_input<'a, T, Message, Theme, Renderer, F>(
where
Message: Clone,
Renderer: iced::advanced::text::Renderer<Font = iced::Font>,
Theme: crate::style::number_input::StyleSheet
+ iced::widget::text_input::StyleSheet
+ iced::widget::container::StyleSheet
+ iced::widget::text::StyleSheet,
Theme: crate::style::number_input::Catalog
+ iced::widget::text_input::Catalog
+ iced::widget::container::Catalog
+ iced::widget::text::Catalog,
F: 'static + Fn(T) -> Message + Copy,
T: 'static
+ num_traits::Num
Expand Down
Loading

0 comments on commit b802445

Please sign in to comment.