diff --git a/Cargo.toml b/Cargo.toml index 9ab57bc8..c62b5c9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] +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. + +
+ + + +
+ +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> { + Some(Overlay::new(layout.position(), Box::new(Menu))) + } + } + + impl<'a, Message> Into> for ComboBox { + fn into(self) -> Element<'a, Message, Renderer> { + Element::new(self) + } + } + + pub struct Menu; + + impl Layer 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 { + 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() + } +}