refactor: responsive headers should allow some options
This commit is contained in:
parent
99d2478d98
commit
bbcd874d9c
4 changed files with 142 additions and 86 deletions
|
|
@ -103,7 +103,7 @@ pub(crate) mod responsive_container;
|
|||
mod responsive_menu_bar;
|
||||
#[cfg(feature = "surface-message")]
|
||||
#[doc(inline)]
|
||||
pub use responsive_menu_bar::responsive_menu_bar;
|
||||
pub use responsive_menu_bar::{ResponsiveMenuBar, responsive_menu_bar};
|
||||
|
||||
pub mod button;
|
||||
#[doc(inline)]
|
||||
|
|
|
|||
|
|
@ -7,72 +7,121 @@ use crate::{
|
|||
widget::{button, icon, responsive_container},
|
||||
};
|
||||
|
||||
use super::menu;
|
||||
use super::menu::{self, ItemHeight, ItemWidth};
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// Will panic if the menu bar collapses without tracking the size
|
||||
pub fn responsive_menu_bar<'a, Message: Clone + 'static, A: menu::Action<Message = Message>>(
|
||||
core: &Core,
|
||||
key_binds: &HashMap<menu::KeyBind, A>,
|
||||
id: crate::widget::Id,
|
||||
action_message: impl Fn(crate::surface::Action) -> Message + 'static,
|
||||
trees: Vec<(
|
||||
std::borrow::Cow<'static, str>,
|
||||
Vec<menu::Item<A, std::borrow::Cow<'static, str>>>,
|
||||
)>,
|
||||
) -> Element<'a, Message> {
|
||||
use crate::widget::id_container;
|
||||
pub fn responsive_menu_bar() -> ResponsiveMenuBar {
|
||||
ResponsiveMenuBar::default()
|
||||
}
|
||||
|
||||
let menu_bar_size = core.menu_bars.get(&id);
|
||||
pub struct ResponsiveMenuBar {
|
||||
item_width: ItemWidth,
|
||||
item_height: ItemHeight,
|
||||
spacing: f32,
|
||||
}
|
||||
|
||||
#[allow(clippy::if_not_else)]
|
||||
if !menu_bar_size.is_some_and(|(limits, size)| {
|
||||
let max_size = limits.max();
|
||||
max_size.width < size.width
|
||||
}) {
|
||||
responsive_container::responsive_container(
|
||||
id_container(
|
||||
menu::bar(
|
||||
trees
|
||||
.into_iter()
|
||||
.map(|mt| {
|
||||
menu::Tree::<_>::with_children(
|
||||
menu::root(mt.0),
|
||||
menu::items(key_binds, mt.1),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
|
||||
),
|
||||
id,
|
||||
action_message,
|
||||
)
|
||||
.apply(Element::from)
|
||||
} else {
|
||||
responsive_container::responsive_container(
|
||||
id_container(
|
||||
menu::bar(vec![menu::Tree::<_>::with_children(
|
||||
Element::from(
|
||||
button::icon(icon::from_name("open-menu-symbolic"))
|
||||
.padding([4, 12])
|
||||
.class(crate::theme::Button::MenuRoot),
|
||||
),
|
||||
menu::items(
|
||||
key_binds,
|
||||
trees
|
||||
.into_iter()
|
||||
.map(|mt| menu::Item::Folder(mt.0, mt.1))
|
||||
.collect(),
|
||||
),
|
||||
)]),
|
||||
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
|
||||
),
|
||||
id,
|
||||
action_message,
|
||||
)
|
||||
.size(menu_bar_size.unwrap().1)
|
||||
.apply(Element::from)
|
||||
impl Default for ResponsiveMenuBar {
|
||||
fn default() -> ResponsiveMenuBar {
|
||||
ResponsiveMenuBar {
|
||||
item_width: ItemWidth::Uniform(150),
|
||||
item_height: ItemHeight::Uniform(30),
|
||||
spacing: 0.,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponsiveMenuBar {
|
||||
/// Set the item width
|
||||
pub fn item_width(mut self, item_width: ItemWidth) -> Self {
|
||||
self.item_width = item_width;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the item height
|
||||
pub fn item_height(mut self, item_height: ItemHeight) -> Self {
|
||||
self.item_height = item_height;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the spacing
|
||||
pub fn spacing(mut self, spacing: f32) -> Self {
|
||||
self.spacing = spacing;
|
||||
self
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// Will panic if the menu bar collapses without tracking the size
|
||||
pub fn into_element<
|
||||
'a,
|
||||
Message: Clone + 'static,
|
||||
A: menu::Action<Message = Message>,
|
||||
S: Into<std::borrow::Cow<'static, str>> + 'static,
|
||||
>(
|
||||
self,
|
||||
core: &Core,
|
||||
key_binds: &HashMap<menu::KeyBind, A>,
|
||||
id: crate::widget::Id,
|
||||
action_message: impl Fn(crate::surface::Action) -> Message + 'static,
|
||||
trees: Vec<(S, Vec<menu::Item<A, S>>)>,
|
||||
) -> Element<'a, Message> {
|
||||
use crate::widget::id_container;
|
||||
|
||||
let menu_bar_size = core.menu_bars.get(&id);
|
||||
|
||||
#[allow(clippy::if_not_else)]
|
||||
if !menu_bar_size.is_some_and(|(limits, size)| {
|
||||
let max_size = limits.max();
|
||||
max_size.width < size.width
|
||||
}) {
|
||||
responsive_container::responsive_container(
|
||||
id_container(
|
||||
menu::bar(
|
||||
trees
|
||||
.into_iter()
|
||||
.map(|mt| {
|
||||
menu::Tree::<_>::with_children(
|
||||
menu::root(mt.0),
|
||||
menu::items(key_binds, mt.1.into()),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
.item_width(self.item_width)
|
||||
.item_height(self.item_height)
|
||||
.spacing(self.spacing),
|
||||
crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
|
||||
),
|
||||
id,
|
||||
action_message,
|
||||
)
|
||||
.apply(Element::from)
|
||||
} else {
|
||||
responsive_container::responsive_container(
|
||||
id_container(
|
||||
menu::bar(vec![menu::Tree::<_>::with_children(
|
||||
Element::from(
|
||||
button::icon(icon::from_name("open-menu-symbolic"))
|
||||
.padding([4, 12])
|
||||
.class(crate::theme::Button::MenuRoot),
|
||||
),
|
||||
menu::items(
|
||||
key_binds,
|
||||
trees
|
||||
.into_iter()
|
||||
.map(|mt| menu::Item::Folder(mt.0, mt.1.into()))
|
||||
.collect(),
|
||||
),
|
||||
)])
|
||||
.item_height(self.item_height)
|
||||
.item_width(self.item_width)
|
||||
.spacing(self.spacing),
|
||||
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
|
||||
),
|
||||
id,
|
||||
action_message,
|
||||
)
|
||||
.size(menu_bar_size.unwrap().1)
|
||||
.apply(Element::from)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue