From ccd02c525bd5751d828bb6ab7b656d88526b2b55 Mon Sep 17 00:00:00 2001 From: Jedsek Date: Fri, 20 Jan 2023 13:46:09 +0800 Subject: Update main.rs The app should not render qr_code when data is empty --- examples/qr_code/src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 6f487e4c..27b41fdc 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -21,10 +21,7 @@ impl Sandbox for QRGenerator { type Message = Message; fn new() -> Self { - QRGenerator { - qr_code: qr_code::State::new("").ok(), - ..Self::default() - } + QRGenerator::default() } fn title(&self) -> String { @@ -36,7 +33,12 @@ impl Sandbox for QRGenerator { Message::DataChanged(mut data) => { data.truncate(100); - self.qr_code = qr_code::State::new(&data).ok(); + self.qr_code = if data.is_empty() { + None + } else { + qr_code::State::new(&data).ok() + } + self.data = data; } } -- cgit From 06fb7e0b51e0750122e6775ab5cec59d6e3431d4 Mon Sep 17 00:00:00 2001 From: Jedsek Date: Fri, 20 Jan 2023 13:56:02 +0800 Subject: Update main.rs --- examples/qr_code/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 27b41fdc..42b58a21 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -37,7 +37,7 @@ impl Sandbox for QRGenerator { None } else { qr_code::State::new(&data).ok() - } + }; self.data = data; } -- cgit From 4fb0be179359097db4aaf6d88dff1367201727dd Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Thu, 16 Feb 2023 14:13:04 +0100 Subject: Added the ability to change checkbox icon --- examples/checkbox/Cargo.toml | 9 ++++++ examples/checkbox/README.md | 12 ++++++++ examples/checkbox/fonts/icons.ttf | Bin 0 -> 1272 bytes examples/checkbox/src/main.rs | 63 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 examples/checkbox/Cargo.toml create mode 100644 examples/checkbox/README.md create mode 100644 examples/checkbox/fonts/icons.ttf create mode 100644 examples/checkbox/src/main.rs (limited to 'examples') diff --git a/examples/checkbox/Cargo.toml b/examples/checkbox/Cargo.toml new file mode 100644 index 00000000..dde8f910 --- /dev/null +++ b/examples/checkbox/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "checkbox" +version = "0.1.0" +authors = ["Casper Rogild Storm"] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/checkbox/README.md b/examples/checkbox/README.md new file mode 100644 index 00000000..b7f85684 --- /dev/null +++ b/examples/checkbox/README.md @@ -0,0 +1,12 @@ +## Checkbox + +A box that can be checked. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package pick_list +``` + +[`main`]: src/main.rs diff --git a/examples/checkbox/fonts/icons.ttf b/examples/checkbox/fonts/icons.ttf new file mode 100644 index 00000000..a2046844 Binary files /dev/null and b/examples/checkbox/fonts/icons.ttf differ diff --git a/examples/checkbox/src/main.rs b/examples/checkbox/src/main.rs new file mode 100644 index 00000000..09950bb8 --- /dev/null +++ b/examples/checkbox/src/main.rs @@ -0,0 +1,63 @@ +use iced::widget::{checkbox, column, container}; +use iced::{Element, Font, Length, Sandbox, Settings}; + +const ICON_FONT: Font = Font::External { + name: "Icons", + bytes: include_bytes!("../fonts/icons.ttf"), +}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +#[derive(Default)] +struct Example { + default_checkbox: bool, + custom_checkbox: bool, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + DefaultChecked(bool), + CustomChecked(bool), +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Default::default() + } + + fn title(&self) -> String { + String::from("Checkbox - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::DefaultChecked(value) => self.default_checkbox = value, + Message::CustomChecked(value) => self.custom_checkbox = value, + } + } + + fn view(&self) -> Element { + let default_checkbox = + checkbox("Default", self.default_checkbox, Message::DefaultChecked); + let custom_checkbox = + checkbox("Custom", self.custom_checkbox, Message::CustomChecked) + .icon(checkbox::Icon { + font: ICON_FONT, + code_point: '\u{e901}', + size: None, + }); + + let content = column![default_checkbox, custom_checkbox].spacing(22); + + container(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} -- cgit From ac1945404efde5fbec7f3004f688dfd08f28cf55 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 13:57:04 +0100 Subject: Run `cargo fmt` --- examples/qr_code/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 42b58a21..c10c665b 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -38,7 +38,7 @@ impl Sandbox for QRGenerator { } else { qr_code::State::new(&data).ok() }; - + self.data = data; } } -- cgit From d2996f3ed86e14ece846f595bc7d3133ce106eb3 Mon Sep 17 00:00:00 2001 From: Sebastian Dröge Date: Sun, 20 Nov 2022 17:30:11 +0200 Subject: image: Allow any kind of data that implements `AsRef<[u8]>` for the image data It's not required anywhere for it to be a plain slice or a `Vec` and this makes it possible to use data allocated in a different way without copying. --- examples/pokedex/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 748acae0..1873b674 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -193,7 +193,7 @@ impl Pokemon { { let bytes = reqwest::get(&url).await?.bytes().await?; - Ok(image::Handle::from_memory(bytes.as_ref().to_vec())) + Ok(image::Handle::from_memory(bytes)) } #[cfg(target_arch = "wasm32")] -- cgit From 7b8b01f560569ae18d9337a31ba94f6c1c2ba0dd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Feb 2023 12:24:13 +0100 Subject: Use `f32` in `Length::Units` and rename it to `Fixed` --- examples/color_palette/src/main.rs | 4 ++-- examples/component/src/main.rs | 2 +- examples/events/src/main.rs | 2 +- examples/integration_opengl/src/controls.rs | 2 +- examples/integration_wgpu/src/controls.rs | 2 +- examples/modal/src/main.rs | 2 +- examples/pick_list/src/main.rs | 4 ++-- examples/qr_code/src/main.rs | 2 +- examples/scrollable/src/main.rs | 22 +++++++++++----------- examples/slider/src/main.rs | 4 ++-- examples/stopwatch/src/main.rs | 2 +- examples/styling/src/main.rs | 12 ++++-------- examples/todos/src/main.rs | 4 ++-- examples/tour/src/main.rs | 8 ++++---- 14 files changed, 34 insertions(+), 38 deletions(-) (limited to 'examples') diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 42149965..a2df36c2 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -301,11 +301,11 @@ impl ColorPicker { } row![ - text(C::LABEL).width(Length::Units(50)), + text(C::LABEL).width(50), slider(cr1, c1, move |v| C::new(v, c2, c3)), slider(cr2, c2, move |v| C::new(c1, v, c3)), slider(cr3, c3, move |v| C::new(c1, c2, v)), - text(color.to_string()).width(Length::Units(185)).size(14), + text(color.to_string()).width(185).size(14), ] .spacing(10) .align_items(Alignment::Center) diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index 06b1e53a..c407bb06 100644 --- a/examples/component/src/main.rs +++ b/examples/component/src/main.rs @@ -127,7 +127,7 @@ mod numeric_input { .horizontal_alignment(alignment::Horizontal::Center) .vertical_alignment(alignment::Vertical::Center), ) - .width(Length::Units(50)) + .width(50) .on_press(on_press) }; diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 0e583479..1b97018e 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -93,7 +93,7 @@ impl Application for Events { .width(Length::Fill) .horizontal_alignment(alignment::Horizontal::Center), ) - .width(Length::Units(100)) + .width(100) .padding(10) .on_press(Message::Exit); diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs index 22c41066..c3648f44 100644 --- a/examples/integration_opengl/src/controls.rs +++ b/examples/integration_opengl/src/controls.rs @@ -42,7 +42,7 @@ impl Program for Controls { let background_color = self.background_color; let sliders = Row::new() - .width(Length::Units(500)) + .width(500) .spacing(20) .push( Slider::new(0.0..=1.0, background_color.r, move |r| { diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 92300a45..533cb6e2 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -48,7 +48,7 @@ impl Program for Controls { let text = &self.text; let sliders = Row::new() - .width(Length::Units(500)) + .width(500) .spacing(20) .push( slider(0.0..=1.0, background_color.r, move |r| { diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 5afafd0d..54555684 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -156,7 +156,7 @@ impl Application for App { ] .spacing(20), ) - .width(Length::Units(300)) + .width(300) .padding(10) .style(theme::Container::Box); diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 9df1f5c7..62a4ef88 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -43,10 +43,10 @@ impl Sandbox for Example { .placeholder("Choose a language..."); let content = column![ - vertical_space(Length::Units(600)), + vertical_space(600), "Which is your favorite language?", pick_list, - vertical_space(Length::Units(600)), + vertical_space(600), ] .width(Length::Fill) .align_items(Alignment::Center) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index c10c665b..d8041745 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -58,7 +58,7 @@ impl Sandbox for QRGenerator { .padding(15); let mut content = column![title, input] - .width(Length::Units(700)) + .width(700) .spacing(20) .align_items(Alignment::Center); diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 128d98b2..7c858961 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -187,9 +187,9 @@ impl Application for ScrollableDemo { column![ scroll_to_end_button(), text("Beginning!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Middle!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("End!"), scroll_to_beginning_button(), ] @@ -211,13 +211,13 @@ impl Application for ScrollableDemo { row![ scroll_to_end_button(), text("Beginning!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("Middle!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("End!"), scroll_to_beginning_button(), ] - .height(Length::Units(450)) + .height(450) .align_items(Alignment::Center) .padding([0, 40, 0, 40]) .spacing(40), @@ -237,26 +237,26 @@ impl Application for ScrollableDemo { row![ column![ text("Let's do some scrolling!"), - vertical_space(Length::Units(2400)) + vertical_space(2400) ], scroll_to_end_button(), text("Horizontal - Beginning!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), //vertical content column![ text("Horizontal - Middle!"), scroll_to_end_button(), text("Vertical - Beginning!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Vertical - Middle!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Vertical - End!"), scroll_to_beginning_button(), - vertical_space(Length::Units(40)), + vertical_space(40), ] .align_items(Alignment::Fill) .spacing(40), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("Horizontal - End!"), scroll_to_beginning_button(), ] diff --git a/examples/slider/src/main.rs b/examples/slider/src/main.rs index 6286d625..e83804c2 100644 --- a/examples/slider/src/main.rs +++ b/examples/slider/src/main.rs @@ -38,11 +38,11 @@ impl Sandbox for Slider { let h_slider = container(slider(0..=100, value, Message::SliderChanged)) - .width(Length::Units(250)); + .width(250); let v_slider = container(vertical_slider(0..=100, value, Message::SliderChanged)) - .height(Length::Units(200)); + .height(200); let text = text(format!("{value}")); diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index b8cee807..9581a3ce 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -105,7 +105,7 @@ impl Application for Stopwatch { text(label).horizontal_alignment(alignment::Horizontal::Center), ) .padding(10) - .width(Length::Units(80)) + .width(80) }; let toggle_button = { diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 49bedce7..448c9792 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -108,14 +108,10 @@ impl Sandbox for Styling { let progress_bar = progress_bar(0.0..=100.0, self.slider_value); let scrollable = scrollable( - column![ - "Scroll me!", - vertical_space(Length::Units(800)), - "You did it!" - ] - .width(Length::Fill), + column!["Scroll me!", vertical_space(800), "You did it!"] + .width(Length::Fill), ) - .height(Length::Units(100)); + .height(100); let checkbox = checkbox( "Check me!", @@ -143,7 +139,7 @@ impl Sandbox for Styling { column![checkbox, toggler].spacing(20) ] .spacing(10) - .height(Length::Units(100)) + .height(100) .align_items(Alignment::Center), ] .spacing(20) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 04411ed7..6408f09c 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -460,7 +460,7 @@ fn empty_message(message: &str) -> Element<'_, Message> { .style(Color::from([0.7, 0.7, 0.7])), ) .width(Length::Fill) - .height(Length::Units(200)) + .height(200) .center_y() .into() } @@ -474,7 +474,7 @@ const ICONS: Font = Font::External { fn icon(unicode: char) -> Text<'static> { text(unicode.to_string()) .font(ICONS) - .width(Length::Units(20)) + .width(20) .horizontal_alignment(alignment::Horizontal::Center) .size(20) } diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 5ee65562..de063d00 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -513,14 +513,14 @@ impl<'a> Step { text("Tip: You can use the scrollbar to scroll down faster!") .size(16), ) - .push(vertical_space(Length::Units(4096))) + .push(vertical_space(4096)) .push( text("You are halfway there!") .width(Length::Fill) .size(30) .horizontal_alignment(alignment::Horizontal::Center), ) - .push(vertical_space(Length::Units(4096))) + .push(vertical_space(4096)) .push(ferris(300)) .push( text("You made it!") @@ -605,7 +605,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> { } else { image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR"))) } - .width(Length::Units(width)), + .width(width), ) .width(Length::Fill) .center_x() @@ -616,7 +616,7 @@ fn button<'a, Message: Clone>(label: &str) -> Button<'a, Message> { text(label).horizontal_alignment(alignment::Horizontal::Center), ) .padding(12) - .width(Length::Units(100)) + .width(100) } fn color_slider<'a>( -- cgit