From 2a86152364e5e0081d985f641f37f5263f06e940 Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Fri, 17 May 2024 19:21:04 -0600 Subject: [PATCH] Menu and ContextMenu is working now --- Cargo.toml | 2 ++ src/app.rs | 49 ++++++++++++++++++++++++++++++++++++------------- src/app/menu.rs | 18 +++++++++--------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b41f95..e68e242 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ vergen = { version = "8", features = ["git", "gitcl"] } i18n-embed-fl = "0.8" once_cell = "1.19.0" rust-embed = "8.3.0" +log = "0.4" +open = "5.0.2" [dependencies.libcosmic] git = "https://github.com/pop-os/libcosmic.git" diff --git a/src/app.rs b/src/app.rs index 13fc705..a84ff60 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,6 +3,7 @@ use std::{ collections::{HashMap}, env, + process, }; use crate::fl; use cosmic::app::{Command, Core}; @@ -11,6 +12,7 @@ use cosmic::{ iced::{Alignment, Length}, }; use cosmic::iced::alignment::{Horizontal, Vertical}; +use cosmic::iced::window; use cosmic::iced_core::keyboard::Key; use cosmic::widget::menu::{ action::{MenuAction}, @@ -37,7 +39,7 @@ pub struct App { /// If your application does not need to send messages, you can use an empty enum or `()`. #[derive(Debug, Clone)] pub enum Message { - Cut(Option), + // Cut(Option), ToggleContextPage(ContextPage), LaunchUrl(String), WindowClose, @@ -60,17 +62,17 @@ impl ContextPage { #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Action { About, - Cut, + // Cut, WindowClose, WindowNew, } impl MenuAction for Action { type Message = Message; - fn message(&self, entity_opt: Option) -> Self::Message { + fn message(&self, _entity_opt: Option) -> Self::Message { match self { Action::About => Message::ToggleContextPage(ContextPage::About), - Action::Cut => Message::Cut(entity_opt), + // Action::Cut => Message::Cut(entity_opt), Action::WindowClose => Message::WindowClose, Action::WindowNew => Message::WindowNew, } @@ -114,15 +116,6 @@ impl App { .into() } - fn context_drawer(&self) -> Option> { - if !self.core.window.show_context { - return None; - } - - Some(match self.context_page { - ContextPage::About => self.about(), - }) - } } @@ -158,6 +151,16 @@ impl Application for App { (app, Command::none()) } + fn context_drawer(&self) -> Option> { + if !self.core.window.show_context { + return None; + } + + Some(match self.context_page { + ContextPage::About => self.about(), + }) + } + fn view(&self) -> Element { widget::container(widget::text::title1(fl!("welcome"))) .width(Length::Fill) @@ -181,6 +184,26 @@ impl Application for App { } self.set_context_title(context_page.title()); } + Message::WindowClose => { + return window::close(window::Id::MAIN); + } + Message::WindowNew => match env::current_exe() { + Ok(exe) => match process::Command::new(&exe).spawn() { + Ok(_child) => {} + Err(err) => { + eprintln!("failed to execute {:?}: {}", exe, err); + } + }, + Err(err) => { + eprintln!("failed to get current executable path: {}", err); + } + }, + Message::LaunchUrl(url) => match open::that_detached(&url) { + Ok(()) => {} + Err(err) => { + log::warn!("failed to open {:?}: {}", url, err); + } + }, } Command::none() diff --git a/src/app/menu.rs b/src/app/menu.rs index 97db2b4..8d1532f 100644 --- a/src/app/menu.rs +++ b/src/app/menu.rs @@ -24,15 +24,15 @@ pub fn menu_bar<'a>(key_binds: &HashMap) -> Element<'a, Message ], ), ), - Tree::with_children( - root(fl!("edit")), - items( - key_binds, - vec![ - Item::Button(fl!("cut"), Action::Cut), - ], - ), - ), + // Tree::with_children( + // root(fl!("edit")), + // items( + // key_binds, + // vec![ + // Item::Button(fl!("cut"), Action::Cut), + // ], + // ), + // ), Tree::with_children( root(fl!("view")), items(