From 6f7b7a670dda2c9e20ee288261fbe6cc21316a39 Mon Sep 17 00:00:00 2001 From: Latidoremi Date: Fri, 23 Feb 2024 19:09:29 +0800 Subject: [PATCH] styling --- examples/menu/src/main.rs | 13 ++++++-- src/native/menu/common.rs | 8 ++--- src/native/menu/menu_bar.rs | 6 ++++ src/native/menu/menu_bar_overlay.rs | 52 ++++------------------------- src/style/menu_bar.rs | 6 ++-- 5 files changed, 30 insertions(+), 55 deletions(-) diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index b6df5bca..0cb61846 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -6,7 +6,8 @@ use iced::widget::{column as col, vertical_space}; use iced::{alignment, theme, Application, Border, Color, Element, Length, Pixels, Size}; use iced_aw::graphics::icons::{BootstrapIcon, BOOTSTRAP_FONT, BOOTSTRAP_FONT_BYTES}; -use iced_aw::menu::{Item, Menu}; +use iced_aw::menu::{self, Item, Menu, StyleSheet}; +use iced_aw::style::MenuBarStyle; use iced_aw::{menu_bar, menu_items}; use iced_aw::{native::InnerBounds, quad}; @@ -428,7 +429,15 @@ impl Application for App { (debug_button("MMNN").height(50)) )).width(slider_width * slider_count + (slider_count - 1) * spacing + pad) }) - ); + ) + .draw_path(menu::DrawPath::Backdrop) + .style(|theme:&iced::Theme| menu::Appearance{ + path_border: Border{ + radius: [6.0; 4].into(), + ..Default::default() + }, + ..theme.appearance(&MenuBarStyle::Default) + }); let r = row![ horizontal_space().width(295), diff --git a/src/native/menu/common.rs b/src/native/menu/common.rs index 8a6c5767..f7441c8c 100644 --- a/src/native/menu/common.rs +++ b/src/native/menu/common.rs @@ -17,7 +17,7 @@ pub struct CloseCondition { /// /// ## FakeHovering: /// -/// Places cursors at the path items, +/// Places cursors at the path items, /// useful when you want to customize the styling of each item in the path, /// or you simple want the look of the items when they are hovered over. /// @@ -32,9 +32,9 @@ pub struct CloseCondition { /// useful when you want uniform path styling. /// /// The downside is, -/// depend on the style you're going for, -/// oftentimes manually syncing the path's styling to the path items' is necessary -/// +/// depending on the style you're going for, +/// oftentimes manually syncing the path's styling to the path items' is necessary, +/// the default styling simply can't cover most use cases. pub enum DrawPath { /// FakeHovering FakeHovering, diff --git a/src/native/menu/menu_bar.rs b/src/native/menu/menu_bar.rs index cc5ef37e..a83c6a86 100644 --- a/src/native/menu/menu_bar.rs +++ b/src/native/menu/menu_bar.rs @@ -89,6 +89,12 @@ where self.check_bounds_width = check_bounds_width; self } + + /// Sets the draw path option of the [`MenuBar`] + pub fn draw_path(mut self, draw_path: DrawPath) -> Self { + self.draw_path = draw_path; + self + } /// Sets the padding of the [`MenuBar`]. pub fn padding(mut self, padding: impl Into) -> Self { diff --git a/src/native/menu/menu_bar_overlay.rs b/src/native/menu/menu_bar_overlay.rs index 8dbf7a76..1613a625 100644 --- a/src/native/menu/menu_bar_overlay.rs +++ b/src/native/menu/menu_bar_overlay.rs @@ -217,6 +217,7 @@ where let active_tree = &mut self.tree.children[active]; let mut prev_bounds_list = vec![bar_bounds]; + #[rustfmt::skip] fn rec<'a, 'b, Message, Theme: StyleSheet, Renderer: renderer::Renderer>( tree: &mut Tree, item: &mut Item<'a, Message, Theme, Renderer>, @@ -281,29 +282,13 @@ where RecEvent::Event => RecEvent::Event, RecEvent::Close => { if cursor.is_over(prescroll) { - menu.on_event( - menu_tree, - event, - menu_layout, - cursor, - renderer, - clipboard, - shell, - viewport, - ); + menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport); menu.open_event(menu_tree, menu_layout, cursor); RecEvent::Event } else if cursor.is_over(offset_bounds) { RecEvent::Event } else { - menu.close_event( - menu_tree, - menu_layout, - cursor, - parent_bounds, - prev_bounds_list, - prev, - ); + menu.close_event(menu_tree, menu_layout, cursor, parent_bounds, prev_bounds_list, prev); if prev.is_some() { RecEvent::None } else { @@ -313,16 +298,7 @@ where } RecEvent::None => { if cursor.is_over(prescroll) { - menu.on_event( - menu_tree, - event, - menu_layout, - cursor, - renderer, - clipboard, - shell, - viewport, - ); + menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport); menu.open_event(menu_tree, menu_layout, cursor); RecEvent::Event } else if cursor.is_over(offset_bounds) { @@ -336,29 +312,13 @@ where prev_bounds_list.pop(); if cursor.is_over(prescroll) { - menu.on_event( - menu_tree, - event, - menu_layout, - cursor, - renderer, - clipboard, - shell, - viewport, - ); + menu.on_event(menu_tree, event, menu_layout, cursor, renderer, clipboard, shell, viewport); menu.open_event(menu_tree, menu_layout, cursor); RecEvent::Event } else if cursor.is_over(offset_bounds) { RecEvent::Event } else { - menu.close_event( - menu_tree, - menu_layout, - cursor, - parent_bounds, - prev_bounds_list, - prev, - ); + menu.close_event(menu_tree, menu_layout, cursor, parent_bounds, prev_bounds_list, prev); if prev.is_some() { RecEvent::None } else { diff --git a/src/style/menu_bar.rs b/src/style/menu_bar.rs index 1f355f70..f1f2817e 100644 --- a/src/style/menu_bar.rs +++ b/src/style/menu_bar.rs @@ -78,13 +78,13 @@ pub enum MenuBarStyle { Custom(Box>), } -impl From Appearance> for MenuBarStyle { - fn from(f: fn(&Theme) -> Appearance) -> Self { +impl Appearance + 'static> From for MenuBarStyle { + fn from(f: F) -> Self { Self::Custom(Box::new(f)) } } -impl StyleSheet for fn(&Theme) -> Appearance { +impl Appearance> StyleSheet for F { type Style = Theme; fn appearance(&self, style: &Self::Style) -> Appearance {