summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-03-27 15:43:52 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-10 10:01:41 +0100
commit0322e820eb40d36a7425246278b7bcb22b7010aa (patch)
tree5bf2c46cdfc8883643141312c3373c8ea1e172dc
parentfd8f980b88df260ce49d46ed6c514f6e382c6494 (diff)
downloadiced-0322e820eb40d36a7425246278b7bcb22b7010aa.tar.gz
iced-0322e820eb40d36a7425246278b7bcb22b7010aa.tar.bz2
iced-0322e820eb40d36a7425246278b7bcb22b7010aa.zip
Create `layout` example
-rw-r--r--examples/layout/Cargo.toml9
-rw-r--r--examples/layout/src/main.rs123
-rw-r--r--src/time.rs1
-rw-r--r--widget/src/column.rs2
-rw-r--r--widget/src/row.rs2
5 files changed, 135 insertions, 2 deletions
diff --git a/examples/layout/Cargo.toml b/examples/layout/Cargo.toml
new file mode 100644
index 00000000..c2c3f49b
--- /dev/null
+++ b/examples/layout/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "layout"
+version = "0.1.0"
+authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
+edition = "2021"
+publish = false
+
+[dependencies]
+iced = { path = "../.." }
diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs
new file mode 100644
index 00000000..1b0c0c94
--- /dev/null
+++ b/examples/layout/src/main.rs
@@ -0,0 +1,123 @@
+use iced::executor;
+use iced::widget::{column, container, row, text, vertical_rule};
+use iced::{
+ Application, Command, Element, Length, Settings, Subscription, Theme,
+};
+
+pub fn main() -> iced::Result {
+ Layout::run(Settings::default())
+}
+
+#[derive(Debug)]
+struct Layout {
+ previous: Vec<Example>,
+ current: Example,
+ next: Vec<Example>,
+}
+
+#[derive(Debug, Clone, Copy)]
+enum Message {
+ Next,
+ Previous,
+}
+
+impl Application for Layout {
+ type Message = Message;
+ type Theme = Theme;
+ type Executor = executor::Default;
+ type Flags = ();
+
+ fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
+ (
+ Self {
+ previous: vec![],
+ current: Example::Centered,
+ next: vec![Example::NestedQuotes],
+ },
+ Command::none(),
+ )
+ }
+
+ fn title(&self) -> String {
+ String::from("Counter - Iced")
+ }
+
+ fn update(&mut self, message: Self::Message) -> Command<Message> {
+ match message {
+ Message::Next => {
+ if !self.next.is_empty() {
+ let previous = std::mem::replace(
+ &mut self.current,
+ self.next.remove(0),
+ );
+
+ self.previous.push(previous);
+ }
+ }
+ Message::Previous => {
+ if let Some(previous) = self.previous.pop() {
+ let next = std::mem::replace(&mut self.current, previous);
+
+ self.next.insert(0, next);
+ }
+ }
+ }
+
+ Command::none()
+ }
+
+ fn subscription(&self) -> Subscription<Message> {
+ use iced::event::{self, Event};
+ use iced::keyboard;
+
+ event::listen_with(|event, status| match event {
+ Event::Keyboard(keyboard::Event::KeyReleased {
+ key_code, ..
+ }) if status == event::Status::Ignored => match key_code {
+ keyboard::KeyCode::Left => Some(Message::Previous),
+ keyboard::KeyCode::Right => Some(Message::Next),
+ _ => None,
+ },
+ _ => None,
+ })
+ }
+
+ fn view(&self) -> Element<Message> {
+ self.current.view()
+ }
+}
+
+#[derive(Debug)]
+enum Example {
+ Centered,
+ NestedQuotes,
+}
+
+impl Example {
+ fn view(&self) -> Element<Message> {
+ match self {
+ Self::Centered => container(text("I am centered!").size(50))
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .center_x()
+ .center_y()
+ .into(),
+ Self::NestedQuotes => container((1..5).fold(
+ column![text("Original text")].padding(10),
+ |quotes, i| {
+ column![
+ row![vertical_rule(2), quotes],
+ text(format!("Reply {i}"))
+ ]
+ .spacing(10)
+ .padding(10)
+ },
+ ))
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .center_x()
+ .center_y()
+ .into(),
+ }
+ }
+}
diff --git a/src/time.rs b/src/time.rs
index 37d454ed..f10f7a5e 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -1,4 +1,5 @@
//! Listen and react to time.
pub use iced_core::time::{Duration, Instant};
+#[allow(unused_imports)]
pub use iced_futures::backend::default::time::*;
diff --git a/widget/src/column.rs b/widget/src/column.rs
index 526509bb..80327458 100644
--- a/widget/src/column.rs
+++ b/widget/src/column.rs
@@ -35,7 +35,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
Column {
spacing: 0.0,
padding: Padding::ZERO,
- width: Length::Fill,
+ width: Length::Shrink,
height: Length::Shrink,
max_width: f32::INFINITY,
align_items: Alignment::Start,
diff --git a/widget/src/row.rs b/widget/src/row.rs
index c4a1db56..50fc4de0 100644
--- a/widget/src/row.rs
+++ b/widget/src/row.rs
@@ -34,7 +34,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
Row {
spacing: 0.0,
padding: Padding::ZERO,
- width: Length::Fill,
+ width: Length::Shrink,
height: Length::Shrink,
align_items: Alignment::Start,
children,