summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Bingus <shankern@protonmail.com>2023-02-17 11:45:34 -0800
committerLibravatar Bingus <shankern@protonmail.com>2023-02-17 11:45:34 -0800
commit744cef5608a91fe55cbbe1adb73a9a0b5e266668 (patch)
treef88ca6ae3c481e2de74178bb3f0d1f1b685e1740 /examples
parent8da098330b58542cc929f4f24d02e26bd654bae4 (diff)
parent7dc1fb488ddbd12519571b51d75ae0c28875911d (diff)
downloadiced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.tar.gz
iced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.tar.bz2
iced-744cef5608a91fe55cbbe1adb73a9a0b5e266668.zip
Merge remote-tracking branch 'origin/master' into feat/multi-window-support
# Conflicts: # winit/src/window.rs
Diffstat (limited to 'examples')
-rw-r--r--examples/checkbox/Cargo.toml9
-rw-r--r--examples/checkbox/README.md12
-rw-r--r--examples/checkbox/fonts/icons.ttfbin0 -> 1272 bytes
-rw-r--r--examples/checkbox/src/main.rs63
-rw-r--r--examples/color_palette/src/main.rs4
-rw-r--r--examples/component/src/main.rs2
-rw-r--r--examples/events/src/main.rs2
-rw-r--r--examples/integration_opengl/src/controls.rs2
-rw-r--r--examples/integration_wgpu/src/controls.rs2
-rw-r--r--examples/modal/src/main.rs2
-rw-r--r--examples/pick_list/src/main.rs4
-rw-r--r--examples/pokedex/src/main.rs2
-rw-r--r--examples/qr_code/src/main.rs14
-rw-r--r--examples/scrollable/src/main.rs22
-rw-r--r--examples/slider/src/main.rs4
-rw-r--r--examples/stopwatch/src/main.rs2
-rw-r--r--examples/styling/src/main.rs12
-rw-r--r--examples/todos/src/main.rs4
-rw-r--r--examples/tour/src/main.rs8
19 files changed, 126 insertions, 44 deletions
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<casper@rogildstorm.com>"]
+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
--- /dev/null
+++ b/examples/checkbox/fonts/icons.ttf
Binary files 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<Message> {
+ 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()
+ }
+}
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<C: ColorSpace + Copy> ColorPicker<C> {
}
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 9f5c9971..b57010c7 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/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")]
diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs
index 6f487e4c..d8041745 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;
}
}
@@ -56,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>(