feat!(segmented_button): Add context menu support and integrations

This commit is contained in:
Michael Aaron Murphy 2024-04-09 16:54:50 +02:00 committed by Michael Murphy
parent d54af65a2a
commit 59a913c15d
13 changed files with 612 additions and 118 deletions

View file

@ -3,12 +3,13 @@
use std::collections::HashMap;
use crate::config::CosmicTk;
use crate::{config::CosmicTk, widget::nav_bar};
use cosmic_config::CosmicConfigEntry;
use cosmic_theme::ThemeMode;
use iced::window;
use iced_core::window::Id;
use palette::Srgba;
use slotmap::Key;
use crate::Theme;
@ -16,6 +17,7 @@ use crate::Theme;
#[derive(Clone)]
pub struct NavBar {
active: bool,
context_id: crate::widget::nav_bar::Id,
toggled: bool,
toggled_condensed: bool,
}
@ -103,6 +105,7 @@ impl Default for Core {
keyboard_nav: true,
nav_bar: NavBar {
active: true,
context_id: crate::widget::nav_bar::Id::null(),
toggled: true,
toggled_condensed: true,
},
@ -225,6 +228,14 @@ impl Core {
self.nav_bar_set_toggled_condensed(!self.nav_bar.toggled_condensed);
}
pub(crate) fn nav_bar_context(&self) -> nav_bar::Id {
self.nav_bar.context_id
}
pub(crate) fn nav_bar_set_context(&mut self, id: nav_bar::Id) {
self.nav_bar.context_id = id;
}
pub fn nav_bar_set_toggled(&mut self, toggled: bool) {
self.nav_bar.toggled = toggled;
self.nav_bar_set_toggled_condensed(self.nav_bar.toggled);

View file

@ -48,6 +48,8 @@ pub enum Message {
Minimize,
/// Activates a navigation element from the nav bar.
NavBar(nav_bar::Id),
/// Activates a context menu for an item from the nav bar.
NavBarContext(nav_bar::Id),
/// Set scaling factor
ScaleFactor(f32),
/// Notification of system theme changes.
@ -367,7 +369,6 @@ impl<T: Application> Cosmic<T> {
Message::ContextDrawer(show) => {
self.app.core_mut().window.show_context = show;
return self.app.on_context_drawer();
}
Message::Drag => return command::drag(Some(self.app.main_window_id())),
@ -381,6 +382,11 @@ impl<T: Application> Cosmic<T> {
return self.app.on_nav_select(key);
}
Message::NavBarContext(key) => {
self.app.core_mut().nav_bar_set_context(key);
return self.app.on_nav_context(key);
}
Message::ToggleNavBar => {
self.app.core_mut().nav_bar_toggle();
}

View file

@ -52,6 +52,7 @@ use iced::Subscription;
use iced::{multi_window::Application as IcedApplication, window};
#[cfg(any(not(feature = "winit"), not(feature = "multi-window")))]
use iced::{window, Application as IcedApplication};
use iced_core::mouse;
pub use message::Message;
use url::Url;
#[cfg(feature = "single-instance")]
@ -447,7 +448,7 @@ where
window::Id::MAIN
}
/// Allows overriding the default nav bar widget
/// Allows overriding the default nav bar widget.
fn nav_bar(&self) -> Option<Element<Message<Self::Message>>> {
if !self.core().nav_bar_active() {
return None;
@ -455,18 +456,28 @@ where
let nav_model = self.nav_model()?;
let mut nav = crate::widget::nav_bar(nav_model, |entity| {
Message::Cosmic(cosmic::Message::NavBar(entity))
});
let mut nav =
crate::widget::nav_bar(nav_model, |id| Message::Cosmic(cosmic::Message::NavBar(id)))
.on_context(|id| Message::Cosmic(cosmic::Message::NavBarContext(id)))
.context_menu(self.nav_context_menu(self.core().nav_bar_context()))
.into_container()
// XXX both must be shrink to avoid flex layout from ignoring it
.width(iced::Length::Shrink)
.height(iced::Length::Shrink);
if !self.core().is_condensed() {
nav = nav.max_width(280);
}
Some(Element::from(
// XXX both must be shrink to avoid flex layout from ignoring it
nav.width(iced::Length::Shrink).height(iced::Length::Shrink),
))
Some(Element::from(nav))
}
/// Shows a context menu for the active nav bar item.
fn nav_context_menu(
&self,
id: nav_bar::Id,
) -> Option<Vec<crate::widget::menu::MenuTree<Message<Self::Message>, crate::Renderer>>> {
None
}
/// Allows COSMIC to integrate with your application's [`nav_bar::Model`].
@ -482,11 +493,6 @@ where
None
}
// Called when context drawer is toggled
fn on_context_drawer(&mut self) -> iced::Command<Message<Self::Message>> {
iced::Command::none()
}
/// Called when the escape key is pressed.
fn on_escape(&mut self) -> iced::Command<Message<Self::Message>> {
iced::Command::none()
@ -497,6 +503,11 @@ where
iced::Command::none()
}
/// Called when a context menu is requested for a navigation item.
fn on_nav_context(&mut self, id: nav_bar::Id) -> iced::Command<Message<Self::Message>> {
iced::Command::none()
}
/// Called when the search function is requested.
fn on_search(&mut self) -> iced::Command<Message<Self::Message>> {
iced::Command::none()