Skip to content

Commit dc3ac63

Browse files
Improved desktop entry menu
Currently, desktop entries display a generic menu with items that aren't relevant to apps. This patch improves the menu by removing the unneeded items and listing desktop specific entries such as icons. Should compose well with pop-os/cosmic-applibrary#179
1 parent 20f9292 commit dc3ac63

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub enum Action {
9797
EditHistory,
9898
EditLocation,
9999
EmptyTrash,
100+
ExecEntryAction(usize),
100101
ExtractHere,
101102
Gallery,
102103
HistoryNext,
@@ -158,6 +159,9 @@ impl Action {
158159
}
159160
Action::EmptyTrash => Message::TabMessage(None, tab::Message::EmptyTrash),
160161
Action::ExtractHere => Message::ExtractHere(entity_opt),
162+
Action::ExecEntryAction(&action) => {
163+
Message::TabMessage(entity_opt, tab::Message::ExecEntryAction(action))
164+
}
161165
Action::Gallery => Message::TabMessage(entity_opt, tab::Message::GalleryToggle),
162166
Action::HistoryNext => Message::TabMessage(entity_opt, tab::Message::GoNext),
163167
Action::HistoryPrevious => Message::TabMessage(entity_opt, tab::Message::GoPrevious),

src/menu.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn context_menu<'a>(
8686
let mut selected_dir = 0;
8787
let mut selected = 0;
8888
let mut selected_trash_only = false;
89+
let mut selected_desktop_entry = None;
8990
let mut selected_types: Vec<Mime> = vec![];
9091
tab.items_opt().map(|items| {
9192
for item in items.iter() {
@@ -94,8 +95,16 @@ pub fn context_menu<'a>(
9495
if item.metadata.is_dir() {
9596
selected_dir += 1;
9697
}
97-
if item.location_opt == Some(Location::Trash) {
98-
selected_trash_only = true;
98+
match &item.location_opt {
99+
Some(Location::Trash) => selected_trash_only = true,
100+
Some(Location::Path(path)) => {
101+
if selected == 1
102+
&& path.extension().and_then(|s| s.to_str()) == Some("desktop")
103+
{
104+
selected_desktop_entry = Some(&**path);
105+
}
106+
}
107+
_ => (),
99108
}
100109
selected_types.push(item.mime.clone());
101110
}
@@ -104,6 +113,15 @@ pub fn context_menu<'a>(
104113
selected_types.sort_unstable();
105114
selected_types.dedup();
106115
selected_trash_only = selected_trash_only && selected == 1;
116+
// Recheck that only one item, the desktop entry, is selected
117+
let selected_desktop_entry = selected_desktop_entry.and_then(|path| {
118+
if selected == 1 {
119+
// Cache?
120+
cosmic::desktop::load_desktop_file(None, path)
121+
} else {
122+
None
123+
}
124+
});
107125

108126
let mut children: Vec<Element<_>> = Vec::new();
109127
match (&tab.mode, &tab.location) {
@@ -116,6 +134,15 @@ pub fn context_menu<'a>(
116134
if tab::trash_entries() > 0 {
117135
children.push(menu_item(fl!("empty-trash"), Action::EmptyTrash).into());
118136
}
137+
} else if let Some(entry) = selected_desktop_entry {
138+
children.push(menu_item(fl!("open"), Action::Open).into());
139+
for (i, action) in entry.desktop_actions.into_iter().enumerate() {
140+
children.push(menu_item(action.name, Action::ExecEntryAction(i)).into())
141+
}
142+
children.push(divider::horizontal::light().into());
143+
children.push(menu_item(fl!("rename"), Action::Rename).into());
144+
// Should this simply bypass trash and remove the shortcut?
145+
children.push(menu_item(fl!("move-to-trash"), Action::MoveToTrash).into());
119146
} else if selected > 0 {
120147
if selected_dir == 1 && selected == 1 || selected_dir == 0 {
121148
children.push(menu_item(fl!("open"), Action::Open).into());

src/tab.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ pub enum Message {
10341034
EditLocationEnable,
10351035
OpenInNewTab(PathBuf),
10361036
EmptyTrash,
1037+
ExecEntryAction(usize),
10371038
Gallery(bool),
10381039
GalleryPrevious,
10391040
GalleryNext,
@@ -2312,6 +2313,9 @@ impl Tab {
23122313
Message::EmptyTrash => {
23132314
commands.push(Command::EmptyTrash);
23142315
}
2316+
Message::ExecEntryAction(action) => {
2317+
commands.push(Command::ExecEntryAction(action));
2318+
}
23152319
Message::Gallery(gallery) => {
23162320
self.gallery = gallery;
23172321
}

0 commit comments

Comments
 (0)