Skip to content

Commit

Permalink
Menu and ContextMenu is working now
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Honeycutt committed May 18, 2024
1 parent f5e9d54 commit 2a86152
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
49 changes: 36 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{
collections::{HashMap},
env,
process,
};
use crate::fl;
use cosmic::app::{Command, Core};
Expand All @@ -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},
Expand All @@ -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<Entity>),
// Cut(Option<Entity>),
ToggleContextPage(ContextPage),
LaunchUrl(String),
WindowClose,
Expand All @@ -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<Entity>) -> Self::Message {
fn message(&self, _entity_opt: Option<Entity>) -> 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,
}
Expand Down Expand Up @@ -114,15 +116,6 @@ impl App {
.into()
}

fn context_drawer(&self) -> Option<Element<Message>> {
if !self.core.window.show_context {
return None;
}

Some(match self.context_page {
ContextPage::About => self.about(),
})
}
}


Expand Down Expand Up @@ -158,6 +151,16 @@ impl Application for App {
(app, Command::none())
}

fn context_drawer(&self) -> Option<Element<Message>> {
if !self.core.window.show_context {
return None;
}

Some(match self.context_page {
ContextPage::About => self.about(),
})
}

fn view(&self) -> Element<Self::Message> {
widget::container(widget::text::title1(fl!("welcome")))
.width(Length::Fill)
Expand All @@ -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()
Expand Down
18 changes: 9 additions & 9 deletions src/app/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub fn menu_bar<'a>(key_binds: &HashMap<KeyBind, Action>) -> 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(
Expand Down

0 comments on commit 2a86152

Please sign in to comment.