From 719c073fc67c87d6b2da1bc01b74751d3f5e59f0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 25 Oct 2019 03:47:34 +0200 Subject: Draft `Scrollable` widget (no clipping yet!) --- examples/scroll.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 examples/scroll.rs (limited to 'examples/scroll.rs') diff --git a/examples/scroll.rs b/examples/scroll.rs new file mode 100644 index 00000000..648b1d8f --- /dev/null +++ b/examples/scroll.rs @@ -0,0 +1,69 @@ +use iced::{ + button, scrollable, Align, Application, Button, Color, Element, Image, + Length, Scrollable, Text, +}; + +pub fn main() { + Example::default().run() +} + +#[derive(Default)] +struct Example { + paragraph_count: u16, + + scroll: scrollable::State, + add_button: button::State, +} + +#[derive(Debug, Clone, Copy)] +pub enum Message { + AddParagraph, +} + +impl Application for Example { + type Message = Message; + + fn update(&mut self, message: Message) { + match message { + Message::AddParagraph => { + self.paragraph_count += 1; + } + } + } + + fn view(&mut self) -> Element { + let content = Scrollable::new(&mut self.scroll) + .width(Length::Fill) + .max_width(Length::Units(600)) + .spacing(20) + .padding(20) + .align_self(Align::Center); + + //let content = (0..self.paragraph_count) + // .fold(content, |column, _| column.push(lorem_ipsum())) + // .push( + // Button::new(&mut self.add_button, Text::new("Add paragraph")) + // .on_press(Message::AddParagraph) + // .padding(20) + // .border_radius(5) + // .align_self(Align::Center), + // ); + + (0..10) + .fold(content, |content, _| { + content.push( + Image::new(format!( + "{}/examples/resources/ferris.png", + env!("CARGO_MANIFEST_DIR") + )) + .width(Length::Units(400)) + .align_self(Align::Center), + ) + }) + .into() + } +} + +fn lorem_ipsum() -> Text { + Text::new("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dui vel massa blandit interdum. Quisque placerat, odio ut vulputate sagittis, augue est facilisis ex, eget euismod felis magna in sapien. Nullam luctus consequat massa, ac interdum mauris blandit pellentesque. Nullam in est urna. Aliquam tristique lectus ac luctus feugiat. Aenean libero diam, euismod facilisis consequat quis, pellentesque luctus erat. Praesent vel tincidunt elit.") +} -- cgit From 09bd2c46c06eba72da40852d82a52e7353cc9e9b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 27 Oct 2019 01:24:08 +0200 Subject: Expose scrollable offset properly --- examples/scroll.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'examples/scroll.rs') diff --git a/examples/scroll.rs b/examples/scroll.rs index 648b1d8f..2f250ff8 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -1,6 +1,6 @@ use iced::{ - button, scrollable, Align, Application, Button, Color, Element, Image, - Length, Scrollable, Text, + button, scrollable, Align, Application, Button, Color, Column, Element, + Image, Justify, Length, Scrollable, Text, }; pub fn main() { @@ -32,12 +32,7 @@ impl Application for Example { } fn view(&mut self) -> Element { - let content = Scrollable::new(&mut self.scroll) - .width(Length::Fill) - .max_width(Length::Units(600)) - .spacing(20) - .padding(20) - .align_self(Align::Center); + let content = Scrollable::new(&mut self.scroll).spacing(20).padding(20); //let content = (0..self.paragraph_count) // .fold(content, |column, _| column.push(lorem_ipsum())) @@ -49,8 +44,12 @@ impl Application for Example { // .align_self(Align::Center), // ); - (0..10) - .fold(content, |content, _| { + Column::new() + .height(Length::Fill) + .max_width(Length::Units(600)) + .align_self(Align::Center) + .justify_content(Justify::Center) + .push((0..3).fold(content, |content, _| { content.push( Image::new(format!( "{}/examples/resources/ferris.png", @@ -59,7 +58,7 @@ impl Application for Example { .width(Length::Units(400)) .align_self(Align::Center), ) - }) + })) .into() } } -- cgit From 0a0aa3edd9c5185551040c75a934f12d3bce7618 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 27 Oct 2019 02:29:23 +0100 Subject: Implement clipping for images --- examples/scroll.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'examples/scroll.rs') diff --git a/examples/scroll.rs b/examples/scroll.rs index 2f250ff8..450dae11 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -32,7 +32,19 @@ impl Application for Example { } fn view(&mut self) -> Element { - let content = Scrollable::new(&mut self.scroll).spacing(20).padding(20); + let content = (0..3).fold( + Scrollable::new(&mut self.scroll).spacing(20).padding(20), + |content, _| { + content.push( + Image::new(format!( + "{}/examples/resources/ferris.png", + env!("CARGO_MANIFEST_DIR") + )) + .width(Length::Units(400)) + .align_self(Align::Center), + ) + }, + ); //let content = (0..self.paragraph_count) // .fold(content, |column, _| column.push(lorem_ipsum())) @@ -46,19 +58,9 @@ impl Application for Example { Column::new() .height(Length::Fill) - .max_width(Length::Units(600)) - .align_self(Align::Center) .justify_content(Justify::Center) - .push((0..3).fold(content, |content, _| { - content.push( - Image::new(format!( - "{}/examples/resources/ferris.png", - env!("CARGO_MANIFEST_DIR") - )) - .width(Length::Units(400)) - .align_self(Align::Center), - ) - })) + .padding(20) + .push(content) .into() } } -- cgit From e21890168f3db64fb6bb9aa5e1de974f5fad1c68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 27 Oct 2019 03:04:07 +0100 Subject: Improve `scroll` example --- examples/scroll.rs | 67 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 28 deletions(-) (limited to 'examples/scroll.rs') diff --git a/examples/scroll.rs b/examples/scroll.rs index 450dae11..b1e427bf 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -1,6 +1,6 @@ use iced::{ - button, scrollable, Align, Application, Button, Color, Column, Element, - Image, Justify, Length, Scrollable, Text, + button, scrollable, Align, Application, Button, Column, Element, Image, + Justify, Length, Scrollable, Text, }; pub fn main() { @@ -9,7 +9,7 @@ pub fn main() { #[derive(Default)] struct Example { - paragraph_count: u16, + item_count: u16, scroll: scrollable::State, add_button: button::State, @@ -17,7 +17,7 @@ struct Example { #[derive(Debug, Clone, Copy)] pub enum Message { - AddParagraph, + AddItem, } impl Application for Example { @@ -25,36 +25,47 @@ impl Application for Example { fn update(&mut self, message: Message) { match message { - Message::AddParagraph => { - self.paragraph_count += 1; + Message::AddItem => { + self.item_count += 1; } } } fn view(&mut self) -> Element { - let content = (0..3).fold( - Scrollable::new(&mut self.scroll).spacing(20).padding(20), - |content, _| { - content.push( - Image::new(format!( - "{}/examples/resources/ferris.png", - env!("CARGO_MANIFEST_DIR") - )) - .width(Length::Units(400)) - .align_self(Align::Center), - ) - }, - ); + //let content = (0..3).fold( + // Scrollable::new(&mut self.scroll).spacing(20).padding(20), + // |content, _| { + // content.push( + // ) + // }, + //); - //let content = (0..self.paragraph_count) - // .fold(content, |column, _| column.push(lorem_ipsum())) - // .push( - // Button::new(&mut self.add_button, Text::new("Add paragraph")) - // .on_press(Message::AddParagraph) - // .padding(20) - // .border_radius(5) - // .align_self(Align::Center), - // ); + let content = (0..self.item_count) + .fold( + Scrollable::new(&mut self.scroll) + .spacing(20) + .padding(20) + .align_items(Align::Center), + |column, i| { + if i % 2 == 0 { + column.push(lorem_ipsum().width(Length::Units(600))) + } else { + column.push( + Image::new(format!( + "{}/examples/resources/ferris.png", + env!("CARGO_MANIFEST_DIR") + )) + .width(Length::Units(400)), + ) + } + }, + ) + .push( + Button::new(&mut self.add_button, Text::new("Add item")) + .on_press(Message::AddItem) + .padding(20) + .border_radius(5), + ); Column::new() .height(Length::Fill) -- cgit From 2b23e0986c532dbacd89ccd73bb603db558cbdaf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 28 Oct 2019 04:28:21 +0100 Subject: Implement text clipping (caching still broken) --- examples/scroll.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'examples/scroll.rs') diff --git a/examples/scroll.rs b/examples/scroll.rs index b1e427bf..608923fe 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -4,6 +4,8 @@ use iced::{ }; pub fn main() { + env_logger::init(); + Example::default().run() } @@ -32,25 +34,17 @@ impl Application for Example { } fn view(&mut self) -> Element { - //let content = (0..3).fold( - // Scrollable::new(&mut self.scroll).spacing(20).padding(20), - // |content, _| { - // content.push( - // ) - // }, - //); - let content = (0..self.item_count) .fold( Scrollable::new(&mut self.scroll) .spacing(20) .padding(20) .align_items(Align::Center), - |column, i| { + |scrollable, i| { if i % 2 == 0 { - column.push(lorem_ipsum().width(Length::Units(600))) + scrollable.push(lorem_ipsum().width(Length::Units(600))) } else { - column.push( + scrollable.push( Image::new(format!( "{}/examples/resources/ferris.png", env!("CARGO_MANIFEST_DIR") -- cgit