diff options
author | 2020-04-18 14:42:48 +0200 | |
---|---|---|
committer | 2020-07-08 10:59:56 +0200 | |
commit | afd9274de26ccf65285df02007b4ddb697bea9a3 (patch) | |
tree | afc8e452251db84a132367753104613d2d4567b7 /graphics | |
parent | b1afadf1a2162e236525c466b6b3099a2623a2de (diff) | |
download | iced-afd9274de26ccf65285df02007b4ddb697bea9a3.tar.gz iced-afd9274de26ccf65285df02007b4ddb697bea9a3.tar.bz2 iced-afd9274de26ccf65285df02007b4ddb697bea9a3.zip |
Draft `ComboBox` and `Menu` layer
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/layer.rs | 2 | ||||
-rw-r--r-- | graphics/src/layer/menu.rs | 102 | ||||
-rw-r--r-- | graphics/src/lib.rs | 2 | ||||
-rw-r--r-- | graphics/src/widget.rs | 1 | ||||
-rw-r--r-- | graphics/src/widget/combo_box.rs | 67 |
5 files changed, 173 insertions, 1 deletions
diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 6aca738e..ddf835a4 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,4 +1,6 @@ //! Organize rendering primitives into a flattened list of layers. +mod menu; + use crate::image; use crate::svg; use crate::triangle; diff --git a/graphics/src/layer/menu.rs b/graphics/src/layer/menu.rs new file mode 100644 index 00000000..e94ef964 --- /dev/null +++ b/graphics/src/layer/menu.rs @@ -0,0 +1,102 @@ +use crate::backend::Backend; +use crate::{Primitive, Renderer}; +use iced_native::{ + layer, mouse, Background, Color, Font, HorizontalAlignment, Point, + Rectangle, VerticalAlignment, +}; + +impl<B> layer::menu::Renderer for Renderer<B> +where + B: Backend, +{ + fn decorate( + &mut self, + bounds: Rectangle, + _cursor_position: Point, + (primitives, mouse_cursor): Self::Output, + ) -> Self::Output { + ( + Primitive::Group { + primitives: vec![ + Primitive::Quad { + bounds, + background: Background::Color( + [0.87, 0.87, 0.87].into(), + ), + border_color: [0.7, 0.7, 0.7].into(), + border_width: 1, + border_radius: 0, + }, + primitives, + ], + }, + mouse_cursor, + ) + } + + fn draw<T: ToString>( + &mut self, + bounds: Rectangle, + cursor_position: Point, + options: &[T], + hovered_option: Option<usize>, + text_size: u16, + padding: u16, + ) -> Self::Output { + use std::f32; + + let is_mouse_over = bounds.contains(cursor_position); + + let mut primitives = Vec::new(); + + for (i, option) in options.iter().enumerate() { + let is_selected = hovered_option == Some(i); + + let bounds = Rectangle { + x: bounds.x, + y: bounds.y + + ((text_size as usize + padding as usize * 2) * i) as f32, + width: bounds.width, + height: f32::from(text_size + padding * 2), + }; + + if is_selected { + primitives.push(Primitive::Quad { + bounds, + background: Background::Color([0.4, 0.4, 1.0].into()), + border_color: Color::TRANSPARENT, + border_width: 0, + border_radius: 0, + }); + } + + primitives.push(Primitive::Text { + content: option.to_string(), + bounds: Rectangle { + x: bounds.x + f32::from(padding), + y: bounds.center_y(), + width: f32::INFINITY, + ..bounds + }, + size: f32::from(text_size), + font: Font::Default, + color: if is_selected { + Color::WHITE + } else { + Color::BLACK + }, + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Center, + }); + } + + ( + Primitive::Group { primitives }, + if is_mouse_over { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + }, + ) + } +} diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 38d8dffa..bad35ccf 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -2,7 +2,7 @@ //! for [`iced`]. //! //! [`iced`]: https://github.com/hecrj/iced -#![deny(missing_docs)] +//#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![deny(unsafe_code)] diff --git a/graphics/src/widget.rs b/graphics/src/widget.rs index 1f6d6559..a0d06999 100644 --- a/graphics/src/widget.rs +++ b/graphics/src/widget.rs @@ -9,6 +9,7 @@ //! ``` pub mod button; pub mod checkbox; +pub mod combo_box; pub mod container; pub mod image; pub mod pane_grid; diff --git a/graphics/src/widget/combo_box.rs b/graphics/src/widget/combo_box.rs new file mode 100644 index 00000000..27ea762a --- /dev/null +++ b/graphics/src/widget/combo_box.rs @@ -0,0 +1,67 @@ +use crate::backend::{self, Backend}; +use crate::{Primitive, Renderer}; +use iced_native::{ + mouse, Background, Color, Font, HorizontalAlignment, Point, Rectangle, + VerticalAlignment, +}; + +pub use iced_native::ComboBox; + +impl<B> iced_native::combo_box::Renderer for Renderer<B> +where + B: Backend + backend::Text, +{ + const DEFAULT_PADDING: u16 = 5; + + fn draw( + &mut self, + bounds: Rectangle, + cursor_position: Point, + selected: Option<String>, + text_size: u16, + padding: u16, + ) -> Self::Output { + let is_mouse_over = bounds.contains(cursor_position); + + let background = Primitive::Quad { + bounds, + background: Background::Color([0.87, 0.87, 0.87].into()), + border_color: if is_mouse_over { + Color::BLACK + } else { + [0.7, 0.7, 0.7].into() + }, + border_width: 1, + border_radius: 0, + }; + + ( + if let Some(label) = selected { + let label = Primitive::Text { + content: label, + size: f32::from(text_size), + font: Font::Default, + color: Color::BLACK, + bounds: Rectangle { + x: bounds.x + f32::from(padding), + y: bounds.center_y(), + ..bounds + }, + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Center, + }; + + Primitive::Group { + primitives: vec![background, label], + } + } else { + background + }, + if is_mouse_over { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + }, + ) + } +} |