diff options
author | 2020-04-16 13:22:55 +0200 | |
---|---|---|
committer | 2020-07-08 10:41:18 +0200 | |
commit | b1afadf1a2162e236525c466b6b3099a2623a2de (patch) | |
tree | e1fa7e7eba82166a9717a4826322340421779ac7 | |
parent | f064f0482b653a1fbee4afbddcecf91e3a399004 (diff) | |
download | iced-b1afadf1a2162e236525c466b6b3099a2623a2de.tar.gz iced-b1afadf1a2162e236525c466b6b3099a2623a2de.tar.bz2 iced-b1afadf1a2162e236525c466b6b3099a2623a2de.zip |
Draft `combo_box` example to test overlay logic
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/combo_box/Cargo.toml | 11 | ||||
-rw-r--r-- | examples/combo_box/README.md | 18 | ||||
-rw-r--r-- | examples/combo_box/src/main.rs | 163 |
4 files changed, 193 insertions, 0 deletions
@@ -56,6 +56,7 @@ members = [ "examples/bezier_tool", "examples/clock", "examples/color_palette", + "examples/combo_box", "examples/counter", "examples/custom_widget", "examples/download_progress", diff --git a/examples/combo_box/Cargo.toml b/examples/combo_box/Cargo.toml new file mode 100644 index 00000000..7e1e4133 --- /dev/null +++ b/examples/combo_box/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "combo_box" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } +iced_native = { path = "../../native" } +iced_wgpu = { path = "../../wgpu" } diff --git a/examples/combo_box/README.md b/examples/combo_box/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/combo_box/README.md @@ -0,0 +1,18 @@ +## Counter + +The classic counter example explained in the [`README`](../../README.md). + +The __[`main`]__ file contains all the code of the example. + +<div align="center"> + <a href="https://gfycat.com/fairdeadcatbird"> + <img src="https://thumbs.gfycat.com/FairDeadCatbird-small.gif"> + </a> +</div> + +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/combo_box/src/main.rs b/examples/combo_box/src/main.rs new file mode 100644 index 00000000..742378c0 --- /dev/null +++ b/examples/combo_box/src/main.rs @@ -0,0 +1,163 @@ +mod combo_box { + use iced_native::{ + layout, mouse, Background, Color, Element, Hasher, Layer, Layout, + Length, Overlay, Point, Size, Vector, Widget, + }; + use iced_wgpu::{Defaults, Primitive, Renderer}; + + pub struct ComboBox; + + impl ComboBox { + pub fn new() -> Self { + Self + } + } + + impl<'a, Message> Widget<'a, Message, Renderer> for ComboBox { + fn width(&self) -> Length { + Length::Shrink + } + + fn height(&self) -> Length { + Length::Shrink + } + + fn layout( + &self, + _renderer: &Renderer, + _limits: &layout::Limits, + ) -> layout::Node { + layout::Node::new(Size::new(50.0, 50.0)) + } + + fn hash_layout(&self, _state: &mut Hasher) {} + + fn draw( + &self, + _renderer: &mut Renderer, + _defaults: &Defaults, + layout: Layout<'_>, + _cursor_position: Point, + ) -> (Primitive, mouse::Interaction) { + let primitive = Primitive::Quad { + bounds: layout.bounds(), + background: Background::Color(Color::BLACK), + border_width: 0, + border_radius: 0, + border_color: Color::TRANSPARENT, + }; + + (primitive, mouse::Interaction::default()) + } + + fn overlay( + &mut self, + layout: Layout<'_>, + ) -> Option<Overlay<'a, Message, Renderer>> { + Some(Overlay::new(layout.position(), Box::new(Menu))) + } + } + + impl<'a, Message> Into<Element<'a, Message, Renderer>> for ComboBox { + fn into(self) -> Element<'a, Message, Renderer> { + Element::new(self) + } + } + + pub struct Menu; + + impl<Message> Layer<Message, Renderer> for Menu { + fn layout( + &self, + _renderer: &Renderer, + _bounds: Size, + position: Point, + ) -> layout::Node { + let mut node = layout::Node::new(Size::new(100.0, 100.0)); + + node.move_to(position + Vector::new(25.0, 25.0)); + + node + } + + fn hash_layout(&self, state: &mut Hasher, position: Point) { + use std::hash::Hash; + + (position.x as u32).hash(state); + (position.y as u32).hash(state); + } + + fn draw( + &self, + _renderer: &mut Renderer, + _defaults: &Defaults, + layout: Layout<'_>, + _cursor_position: Point, + ) -> (Primitive, mouse::Interaction) { + let primitive = Primitive::Quad { + bounds: layout.bounds(), + background: Background::Color(Color { + r: 0.0, + g: 0.0, + b: 1.0, + a: 0.5, + }), + border_width: 0, + border_radius: 0, + border_color: Color::TRANSPARENT, + }; + + (primitive, mouse::Interaction::default()) + } + } +} + +pub use combo_box::ComboBox; + +use iced::{ + button, Button, Column, Container, Element, Length, Sandbox, Settings, Text, +}; + +pub fn main() { + Example::run(Settings::default()) +} + +#[derive(Default)] +struct Example { + button: button::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + ButtonPressed, +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Combo box - Iced") + } + + fn update(&mut self, _message: Message) {} + + fn view(&mut self) -> Element<Message> { + let combo_box = ComboBox::new(); + + let button = Button::new(&mut self.button, Text::new("Press me!")) + .on_press(Message::ButtonPressed); + + let content = Column::new().spacing(10).push(combo_box).push(button); + + Container::new(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} |