Skip to content

Commit

Permalink
updated menu widget
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Jul 26, 2023
1 parent c3e94af commit 5b2c65d
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ default = [
"wrap",
"selection_list",
"split",
#"menu",
"menu",
"quad",
"context_menu",
"spinner",
Expand Down Expand Up @@ -108,7 +108,7 @@ members = [
"examples/selection_list",
"examples/split",
"examples/split_scroller",
#"examples/menu",
"examples/menu",
"examples/spinner",
"examples/context_menu"
]
Expand Down
10 changes: 5 additions & 5 deletions examples/menu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl button::StyleSheet for ButtonStyle {
fn active(&self, style: &Self::Style) -> button::Appearance {
button::Appearance {
text_color: style.extended_palette().background.base.text,
border_radius: 4.0,
border_radius: [4.0; 4].into(),
background: Some(Color::TRANSPARENT.into()),
..Default::default()
}
Expand Down Expand Up @@ -323,7 +323,7 @@ fn debug_sub_menu<'a>(
fn separator<'a>() -> MenuTree<'a, Message, iced::Renderer> {
menu_tree!(quad::Quad {
color: [0.5; 3].into(),
border_radius: 4.0.into(),
border_radius: [4.0; 4].into(),

Check failure on line 326 in examples/menu/src/main.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `[f32; 4]`
inner_bounds: quad::InnerBounds::Ratio(0.98, 0.1),
..Default::default()
})
Expand All @@ -341,13 +341,13 @@ fn dot_separator<'a>() -> MenuTree<'a, Message, iced::Renderer> {
fn labeled_separator(label: &'_ str) -> MenuTree<'_, Message, iced::Renderer> {
let q_1 = quad::Quad {
color: [0.5; 3].into(),
border_radius: 4.0.into(),
border_radius: [4.0; 4].into(),

Check failure on line 344 in examples/menu/src/main.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `[f32; 4]`
inner_bounds: quad::InnerBounds::Ratio(0.98, 0.1),
..Default::default()
};
let q_2 = quad::Quad {
color: [0.5; 3].into(),
border_radius: 4.0.into(),
border_radius: [4.0; 4].into(),

Check failure on line 350 in examples/menu/src/main.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `[f32; 4]`
inner_bounds: quad::InnerBounds::Ratio(0.98, 0.1),
..Default::default()
};
Expand All @@ -367,7 +367,7 @@ fn circle(color: Color) -> quad::Quad {
quad::Quad {
color,
inner_bounds: quad::InnerBounds::Square(radius * 2.0),
border_radius: radius.into(),
border_radius: [radius; 4].into(),

Check failure on line 370 in examples/menu/src/main.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `[f32; 4]`
..Default::default()
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/core/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ impl From<NaiveDate> for Date {
}
}

/// # Panics
/// Creates a date with the previous month based on the given date.

/// panics if year, month or day doesnt exist.
#[must_use]
pub fn pred_month(date: NaiveDate) -> NaiveDate {
let (year, month) = if date.month() == 1 {
Expand All @@ -68,8 +69,9 @@ pub fn pred_month(date: NaiveDate) -> NaiveDate {
NaiveDate::from_ymd_opt(year, month, day).expect("Year, Month or Day doesnt Exist")
}

/// # Panics
/// Creates a date with the next month based on given date.

/// panics if year, month or day doesnt exist.
#[must_use]
pub fn succ_month(date: NaiveDate) -> NaiveDate {
let (year, month) = if date.month() == 12 {
Expand All @@ -83,7 +85,9 @@ pub fn succ_month(date: NaiveDate) -> NaiveDate {
NaiveDate::from_ymd_opt(year, month, day).expect("Year, Month or Day doesnt Exist")
}

/// # Panics
/// Creates a date with the previous year based on the given date.
// panics if year, month or day doesnt exist.

#[must_use]
pub fn pred_year(date: NaiveDate) -> NaiveDate {
Expand All @@ -93,7 +97,9 @@ pub fn pred_year(date: NaiveDate) -> NaiveDate {
NaiveDate::from_ymd_opt(year, date.month(), day).expect("Year, Month or Day doesnt Exist")
}

/// # Panics
/// Creates a date with the next year based on the given date.
// panics if year, month or day doesnt exist.

#[must_use]
pub fn succ_year(date: NaiveDate) -> NaiveDate {
Expand Down Expand Up @@ -146,9 +152,10 @@ pub enum IsInMonth {
Next,
}

/// # Panics
/// Calculates the day number at the given position in the calendar table based
/// on the given year and month.

/// panics if year, month or day does not exist.
#[must_use]
pub fn position_to_day(x: usize, y: usize, year: i32, month: u32) -> (usize, IsInMonth) {
let (x, y) = (x as isize, y as isize);
Expand Down
4 changes: 2 additions & 2 deletions src/native/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn menu_bar<Message, Renderer>(
menu_roots: Vec<crate::menu::menu_tree::MenuTree<Message, Renderer>>,
) -> crate::menu::menu_bar::MenuBar<Message, Renderer>
where
Renderer: iced_native::Renderer,
Renderer: core::Renderer,
Renderer::Theme: crate::style::menu_bar::StyleSheet,
{
crate::menu::menu_bar::MenuBar::new(menu_roots)
Expand All @@ -98,7 +98,7 @@ pub fn menu_tree<'a, Message, Renderer>(
children: Vec<impl Into<crate::menu::menu_tree::MenuTree<'a, Message, Renderer>>>,
) -> crate::menu::menu_tree::MenuTree<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
Renderer: core::Renderer,
{
crate::menu::menu_tree::MenuTree::with_children(item, children)
}
Expand Down
6 changes: 2 additions & 4 deletions src/native/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ pub mod menu_tree;

pub use crate::style::menu_bar::{Appearance, StyleSheet};
/// A `MenuBar` collects `MenuTree`s and handles
pub type MenuBar<'a, Message, Backend, Theme> =
menu_bar::MenuBar<'a, Message, iced_graphics::Renderer<Backend, Theme>>;
pub type MenuBar<'a, Message, Renderer> = menu_bar::MenuBar<'a, Message, Renderer>;
pub use menu_inner::{CloseCondition, ItemHeight, ItemWidth, PathHighlight};
/// Nested menu is essentially a tree of items, a menu is a collection of items
pub type MenuTree<'a, Message, Backend, Theme> =
menu_tree::MenuTree<'a, Message, iced_graphics::Renderer<Backend, Theme>>;
pub type MenuTree<'a, Message, Renderer> = menu_tree::MenuTree<'a, Message, Renderer>;
8 changes: 5 additions & 3 deletions src/native/menu/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use iced_native::layout::{Limits, Node};
use iced_native::{renderer, Alignment, Element, Padding, Point, Size};
use iced_widget::core::{
layout::{Limits, Node},
renderer, Alignment, Element, Padding, Point, Size,
};

/// The main axis of a flex layout.
#[derive(Debug)]
Expand Down Expand Up @@ -88,7 +90,7 @@ where
if align_items == Alignment::Center {
let mut fill_cross = axis.cross(limits.min());

for child in items.iter() {
for child in items {
let child = child.borrow();
let cross_fill_factor = match axis {
Axis::Horizontal => child.as_widget().height(),
Expand Down
44 changes: 26 additions & 18 deletions src/native/menu/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ use super::menu_inner::{
};
use super::menu_tree::MenuTree;
use crate::style::menu_bar::StyleSheet;
use iced_native::widget::{tree, Tree};
use iced_native::{
event, layout, mouse, overlay, renderer, touch, Alignment, Clipboard, Color, Element, Length,
Padding, Point, Rectangle, Shell, Widget,

use iced_widget::core::{
event,
layout::{Limits, Node},
mouse::{self, Cursor},
overlay, renderer, touch,
widget::{tree, Tree},
Alignment, Clipboard, Color, Element, Layout, Length, Padding, Rectangle, Shell, Widget,
};

pub(super) struct MenuBarState {
pub(super) pressed: bool,
pub(super) view_cursor: Point,
pub(super) view_cursor: Cursor,
pub(super) open: bool,
pub(super) active_root: Option<usize>,
pub(super) horizontal_direction: Direction,
Expand All @@ -38,7 +42,7 @@ impl Default for MenuBarState {
fn default() -> Self {
Self {
pressed: false,
view_cursor: Point::new(-0.5, -0.5),
view_cursor: Cursor::Available([-0.5, -0.5].into()),
open: false,
active_root: None,
horizontal_direction: Direction::Positive,
Expand All @@ -51,7 +55,7 @@ impl Default for MenuBarState {
/// A `MenuBar` collects `MenuTree`s and handles
/// all the layout, event processing and drawing
#[allow(missing_debug_implementations)]
pub struct MenuBar<'a, Message, Renderer>
pub struct MenuBar<'a, Message, Renderer = crate::Renderer>
where
Renderer: renderer::Renderer,
Renderer::Theme: StyleSheet,
Expand Down Expand Up @@ -247,7 +251,7 @@ where
.collect()
}

fn layout(&self, renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
fn layout(&self, renderer: &Renderer, limits: &Limits) -> Node {
use super::flex;

let limits = limits.width(self.width).height(self.height);
Expand All @@ -271,11 +275,12 @@ where
&mut self,
tree: &mut Tree,
event: event::Event,
layout: layout::Layout<'_>,
view_cursor: Point,
layout: Layout<'_>,
view_cursor: Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) -> event::Status {
use event::Event::{Mouse, Touch};
use mouse::{Button::Left, Event::ButtonReleased};
Expand All @@ -290,13 +295,14 @@ where
renderer,
clipboard,
shell,
viewport,
);

let state = tree.state.downcast_mut::<MenuBarState>();

match event {
Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. } | FingerLost { .. }) => {
if state.menu_states.is_empty() && layout.bounds().contains(view_cursor) {
if state.menu_states.is_empty() && view_cursor.is_over(layout.bounds()) {
state.view_cursor = view_cursor;
state.open = true;
}
Expand All @@ -312,13 +318,13 @@ where
renderer: &mut Renderer,
theme: &<Renderer as renderer::Renderer>::Theme,
style: &renderer::Style,
layout: layout::Layout<'_>,
view_cursor: Point,
layout: Layout<'_>,
view_cursor: Cursor,
viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<MenuBarState>();

let position = if state.open && (view_cursor.x < 0.0 || view_cursor.y < 0.0) {
let cursor_pos = view_cursor.position().unwrap_or_default();
let position = if state.open && (cursor_pos.x < 0.0 || cursor_pos.y < 0.0) {
state.view_cursor
} else {
view_cursor
Expand Down Expand Up @@ -364,7 +370,7 @@ where
fn overlay<'b>(
&'b mut self,
tree: &'b mut Tree,
layout: layout::Layout<'_>,
layout: Layout<'_>,
_renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
let state = tree.state.downcast_ref::<MenuBarState>();
Expand Down Expand Up @@ -403,13 +409,14 @@ where
#[allow(unused_results, clippy::too_many_arguments)]
fn process_root_events<Message, Renderer>(
menu_roots: &mut [MenuTree<'_, Message, Renderer>],
view_cursor: Point,
view_cursor: Cursor,
tree: &mut Tree,
event: &event::Event,
layout: layout::Layout<'_>,
layout: Layout<'_>,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) -> event::Status
where
Renderer: renderer::Renderer,
Expand All @@ -428,6 +435,7 @@ where
renderer,
clipboard,
shell,
viewport,
)
})
.fold(event::Status::Ignored, event::Status::merge)
Expand Down
Loading

0 comments on commit 5b2c65d

Please sign in to comment.