feat(context_menu): add a context menu widget as menu tree alternative to the popover widget
This commit is contained in:
parent
c0b653f506
commit
9ffb87d21f
9 changed files with 465 additions and 37 deletions
14
examples/context-menu/Cargo.toml
Normal file
14
examples/context-menu/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "context-menu"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1.37"
|
||||
tracing-subscriber = "0.3.17"
|
||||
tracing-log = "0.2.0"
|
||||
|
||||
[dependencies.libcosmic]
|
||||
path = "../../"
|
||||
default-features = false
|
||||
features = ["debug", "winit", "tokio", "xdg-portal"]
|
||||
144
examples/context-menu/src/main.rs
Normal file
144
examples/context-menu/src/main.rs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
// Copyright 2023 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! Application API example
|
||||
|
||||
use cosmic::app::{Command, Core, Settings};
|
||||
use cosmic::iced_core::Size;
|
||||
use cosmic::widget::{menu, segmented_button};
|
||||
use cosmic::{executor, iced, ApplicationExt, Element};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Runs application with these settings
|
||||
#[rustfmt::skip]
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tracing_subscriber::fmt::init();
|
||||
let _ = tracing_log::LogTracer::init();
|
||||
|
||||
let settings = Settings::default()
|
||||
.size(Size::new(1024., 768.));
|
||||
|
||||
cosmic::app::run::<App>(settings, ())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Messages that are used specifically by our [`App`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Message {
|
||||
Clicked,
|
||||
ShowContext,
|
||||
WindowClose,
|
||||
ShowWindowMenu,
|
||||
ToggleHideContent,
|
||||
WindowNew,
|
||||
}
|
||||
|
||||
/// The [`App`] stores application-specific state.
|
||||
pub struct App {
|
||||
core: Core,
|
||||
button_label: String,
|
||||
show_context: bool,
|
||||
hide_content: bool,
|
||||
}
|
||||
|
||||
/// Implement [`cosmic::Application`] to integrate with COSMIC.
|
||||
impl cosmic::Application for App {
|
||||
/// Default async executor to use with the app.
|
||||
type Executor = executor::Default;
|
||||
|
||||
/// Argument received [`cosmic::Application::new`].
|
||||
type Flags = ();
|
||||
|
||||
/// Message type specific to our [`App`].
|
||||
type Message = Message;
|
||||
|
||||
/// The unique application ID to supply to the window manager.
|
||||
const APP_ID: &'static str = "org.cosmic.ContextMenuDemo";
|
||||
|
||||
fn core(&self) -> &Core {
|
||||
&self.core
|
||||
}
|
||||
|
||||
fn core_mut(&mut self) -> &mut Core {
|
||||
&mut self.core
|
||||
}
|
||||
|
||||
/// Creates the application, and optionally emits command on initialize.
|
||||
fn init(core: Core, _input: Self::Flags) -> (Self, Command<Self::Message>) {
|
||||
let mut app = App {
|
||||
core,
|
||||
button_label: String::from("Right click me"),
|
||||
hide_content: false,
|
||||
show_context: false,
|
||||
};
|
||||
|
||||
app.set_header_title("COSMIC Context Menu Demo".into());
|
||||
let command = app.set_window_title("COSMIC Context Menu Demo".into());
|
||||
|
||||
(app, command)
|
||||
}
|
||||
|
||||
/// Handle application events here.
|
||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
||||
self.button_label = format!("Clicked {message:?}");
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
/// Creates a view after each update.
|
||||
fn view(&self) -> Element<Self::Message> {
|
||||
let widget = cosmic::widget::context_menu(
|
||||
cosmic::widget::button::text(&self.button_label).on_press(Message::Clicked),
|
||||
self.context_menu(),
|
||||
);
|
||||
|
||||
let centered = cosmic::widget::container(widget)
|
||||
.width(iced::Length::Fill)
|
||||
.height(iced::Length::Fill)
|
||||
.align_x(iced::alignment::Horizontal::Center)
|
||||
.align_y(iced::alignment::Vertical::Center);
|
||||
|
||||
Element::from(centered)
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn context_menu(&self) -> Option<Vec<menu::Tree<Message>>> {
|
||||
Some(menu::items(
|
||||
&HashMap::new(),
|
||||
vec![
|
||||
menu::Item::Button("New window", ContextMenuAction::WindowNew),
|
||||
menu::Item::Divider,
|
||||
menu::Item::Folder(
|
||||
"View",
|
||||
vec![menu::Item::CheckBox(
|
||||
"Hide content",
|
||||
self.hide_content,
|
||||
ContextMenuAction::ToggleHideContent,
|
||||
)],
|
||||
),
|
||||
menu::Item::Divider,
|
||||
menu::Item::Button("Quit", ContextMenuAction::WindowClose),
|
||||
],
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ContextMenuAction {
|
||||
WindowClose,
|
||||
ToggleHideContent,
|
||||
WindowNew,
|
||||
}
|
||||
|
||||
impl menu::Action for ContextMenuAction {
|
||||
type Message = Message;
|
||||
fn message(&self, _entity_opt: Option<segmented_button::Entity>) -> Self::Message {
|
||||
match self {
|
||||
ContextMenuAction::WindowClose => Message::WindowClose,
|
||||
ContextMenuAction::ToggleHideContent => Message::ToggleHideContent,
|
||||
ContextMenuAction::WindowNew => Message::WindowNew,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,9 +7,7 @@ use std::collections::HashMap;
|
|||
|
||||
use cosmic::app::{Command, Core, Settings};
|
||||
use cosmic::iced_core::Size;
|
||||
use cosmic::widget::menu::action::MenuAction;
|
||||
use cosmic::widget::menu::menu_tree::{menu_items, MenuItem, MenuTree};
|
||||
use cosmic::widget::nav_bar;
|
||||
use cosmic::widget::{menu, nav_bar};
|
||||
use cosmic::{executor, iced, ApplicationExt, Element};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
@ -71,7 +69,7 @@ pub enum NavMenuAction {
|
|||
Delete(nav_bar::Id),
|
||||
}
|
||||
|
||||
impl MenuAction for NavMenuAction {
|
||||
impl menu::Action for NavMenuAction {
|
||||
type Message = cosmic::app::Message<Message>;
|
||||
|
||||
fn message(&self, _entity: Option<cosmic::widget::segmented_button::Entity>) -> Self::Message {
|
||||
|
|
@ -133,13 +131,13 @@ impl cosmic::Application for App {
|
|||
fn nav_context_menu(
|
||||
&self,
|
||||
id: nav_bar::Id,
|
||||
) -> Option<Vec<MenuTree<cosmic::app::Message<Self::Message>, cosmic::Renderer>>> {
|
||||
Some(menu_items(
|
||||
) -> Option<Vec<menu::Tree<cosmic::app::Message<Self::Message>>>> {
|
||||
Some(menu::items(
|
||||
&HashMap::new(),
|
||||
vec![
|
||||
MenuItem::Button("Move Up", NavMenuAction::MoveUp(id)),
|
||||
MenuItem::Button("Move Down", NavMenuAction::MoveDown(id)),
|
||||
MenuItem::Button("Delete", NavMenuAction::Delete(id)),
|
||||
menu::Item::Button("Move Up", NavMenuAction::MoveUp(id)),
|
||||
menu::Item::Button("Move Down", NavMenuAction::MoveDown(id)),
|
||||
menu::Item::Button("Delete", NavMenuAction::Delete(id)),
|
||||
],
|
||||
))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue