From 0ff5a02550e5d5de8fb5fd0643ea424d9e508888 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 23 May 2020 01:07:59 +0200 Subject: Rename `Layer` to `overlay::Content` --- graphics/src/overlay/menu.rs | 102 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 graphics/src/overlay/menu.rs (limited to 'graphics/src/overlay/menu.rs') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs new file mode 100644 index 00000000..f4204f25 --- /dev/null +++ b/graphics/src/overlay/menu.rs @@ -0,0 +1,102 @@ +use crate::backend::Backend; +use crate::{Primitive, Renderer}; +use iced_native::{ + mouse, overlay, Background, Color, Font, HorizontalAlignment, Point, + Rectangle, VerticalAlignment, +}; + +impl overlay::menu::Renderer for Renderer +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( + &mut self, + bounds: Rectangle, + cursor_position: Point, + options: &[T], + hovered_option: Option, + 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() + }, + ) + } +} -- cgit From 61f22b1db23f3495145a9a4f7255311fe8381998 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Jun 2020 20:41:11 +0200 Subject: Add styling support for `ComboBox` and `Menu` --- graphics/src/overlay/menu.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'graphics/src/overlay/menu.rs') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index f4204f25..13065645 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,18 +1,23 @@ use crate::backend::Backend; use crate::{Primitive, Renderer}; use iced_native::{ - mouse, overlay, Background, Color, Font, HorizontalAlignment, Point, - Rectangle, VerticalAlignment, + mouse, overlay, Color, Font, HorizontalAlignment, Point, Rectangle, + VerticalAlignment, }; +pub use iced_style::menu::Style; + impl overlay::menu::Renderer for Renderer where B: Backend, { + type Style = Style; + fn decorate( &mut self, bounds: Rectangle, _cursor_position: Point, + style: &Style, (primitives, mouse_cursor): Self::Output, ) -> Self::Output { ( @@ -20,11 +25,9 @@ where 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, + background: style.background, + border_color: style.border_color, + border_width: style.border_width, border_radius: 0, }, primitives, @@ -42,6 +45,7 @@ where hovered_option: Option, text_size: u16, padding: u16, + style: &Style, ) -> Self::Output { use std::f32; @@ -63,7 +67,7 @@ where if is_selected { primitives.push(Primitive::Quad { bounds, - background: Background::Color([0.4, 0.4, 1.0].into()), + background: style.selected_background, border_color: Color::TRANSPARENT, border_width: 0, border_radius: 0, @@ -81,9 +85,9 @@ where size: f32::from(text_size), font: Font::Default, color: if is_selected { - Color::WHITE + style.selected_text_color } else { - Color::BLACK + style.text_color }, horizontal_alignment: HorizontalAlignment::Left, vertical_alignment: VerticalAlignment::Center, -- cgit From 625979b6652a8a14a0eaf6bd62f1e9a8da0ae421 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Jul 2020 05:44:10 +0200 Subject: Draft `Widget::overlay` idempotency --- graphics/src/overlay/menu.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'graphics/src/overlay/menu.rs') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 13065645..914baa6a 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,4 +1,4 @@ -use crate::backend::Backend; +use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; use iced_native::{ mouse, overlay, Color, Font, HorizontalAlignment, Point, Rectangle, @@ -9,7 +9,7 @@ pub use iced_style::menu::Style; impl overlay::menu::Renderer for Renderer where - B: Backend, + B: Backend + backend::Text, { type Style = Style; -- cgit From 69ac47f463fd5c392f1f8e788fcf89b1a76abcae Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 8 Jul 2020 07:04:20 +0200 Subject: Implement `font` method for `ComboBox` --- graphics/src/overlay/menu.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'graphics/src/overlay/menu.rs') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 914baa6a..89a9cd03 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -43,8 +43,9 @@ where cursor_position: Point, options: &[T], hovered_option: Option, - text_size: u16, padding: u16, + text_size: u16, + font: Font, style: &Style, ) -> Self::Output { use std::f32; @@ -83,7 +84,7 @@ where ..bounds }, size: f32::from(text_size), - font: Font::Default, + font, color: if is_selected { style.selected_text_color } else { -- cgit From 2118a726f8b6134820e1ca5b7b802fa1344e453a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 10 Jul 2020 02:39:12 +0200 Subject: Write documentation for the new `overlay` API --- graphics/src/overlay/menu.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'graphics/src/overlay/menu.rs') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 89a9cd03..a952f065 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,3 +1,4 @@ +//! Build and show dropdown menus. use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; use iced_native::{ -- cgit