Skip to content

Commit

Permalink
feat: open with
Browse files Browse the repository at this point in the history
Does not work yet
  • Loading branch information
wiiznokes committed Sep 28, 2024
1 parent dd49980 commit 39481ec
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ tracing-journald = "0.3"
constcat = "0.5"
nucleo = "0.5"
futures = "0.3"
freedesktop-desktop-entry = "0.7.4"
memchr = "2.7.4"

[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic"
Expand Down
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ impl cosmic::Application for AppState {
config_set!(horizontal, horizontal);
}
},
AppMsg::Open(_) => todo!(),
AppMsg::OpenWith { entry, desktop_entry } => todo!(),
}
Command::none()
}
Expand Down
7 changes: 7 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmic::iced::window::Id;
use freedesktop_desktop_entry::DesktopEntry;

use crate::{
clipboard::{self, ClipboardMessage},
Expand All @@ -24,6 +25,12 @@ pub enum AppMsg {
ShowQrCode(Entry),
ReturnToClipboard,
Config(ConfigMsg),
Open(Entry),
OpenWith {
entry: Entry,
desktop_entry: DesktopEntry<'static>,
},

}

#[derive(Clone, Debug)]
Expand Down
26 changes: 26 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ pub fn remove_dir_contents(dir: &Path) {

let _ = inner(dir);
}

pub fn find_x_scheme_handler<'a>(a: &'a str) -> Option<&'a str> {
// Use memchr to find the first occurrence of ':' in the input string.
if let Some(colon_index) = memchr::memchr(b':', a.as_bytes()) {
// Check if the colon is followed by "//" to validate the scheme.
if a[colon_index..].starts_with("://") {
// If valid, return the scheme as a slice from the start up to the colon.
return Some(&a[..colon_index]);
}
}
// If no scheme is found, return None.
None
}

#[test]
fn find_x_scheme_handler_test() {
assert_eq!(
find_x_scheme_handler("https://github.com/wiiznokes/clipboard-manager"),
Some("https")
);

assert_eq!(
find_x_scheme_handler("ddg://query%20terms"),
Some("ddg")
);
}
50 changes: 46 additions & 4 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ use cosmic::{
button::{self},
column, container, context_menu, flex_row, grid,
icon::{self, Handle},
image, menu, mouse_area, row, text, text_input, toggler, Column, Container, Icon,
image, menu, mouse_area, row, text, text_input, toggler, Column, Container, Icon, Lazy,
MouseArea, Space, Text, TextEditor,
},
Element,
};

use freedesktop_desktop_entry as fde;

use anyhow::{anyhow, bail, Result};

use crate::{
Expand All @@ -29,7 +31,7 @@ use crate::{
db::{Content, Entry},
fl,
message::{AppMsg, ConfigMsg},
utils::{formatted_value, horizontal_padding, vertical_padding},
utils::{find_x_scheme_handler, formatted_value, horizontal_padding, vertical_padding},
};

impl AppState {
Expand Down Expand Up @@ -327,6 +329,43 @@ impl AppState {
btn.width(Length::Fill).into()
};

let open_with = Lazy::new(entry, |entry| {
println!("lazy");

let mut mimes = vec![entry.mime.as_ref()];

if let Ok(Content::Text(content)) = entry.get_content() {
if let Some(m) = find_x_scheme_handler(content) {
mimes.push(m);
}
}

let res = fde::DesktopEntry::from_paths::<&str>(
fde::Iter::new(fde::default_paths()),
Some(&[]),
)
.filter_map(|e| {
e.ok().and_then(|e| {
e.mime_type()
.unwrap_or_default()
.iter()
.any(|e| mimes.contains(e))
.then_some(
button(text(e.appid.clone()))
.on_press(AppMsg::OpenWith {
entry: (*entry).clone(),
desktop_entry: e,
})
.width(Length::Fill)
.into(),
)
})
})
.collect::<Vec<_>>();

Column::with_children(res)
});

context_menu(
btn,
Some(vec![
Expand All @@ -339,8 +378,11 @@ impl AppState {
menu::Tree::new(
button::text(fl!("show_qr_code"))
.on_press(AppMsg::ShowQrCode(entry.clone()))
.width(Length::Fill)
.style(Button::Destructive),
.width(Length::Fill),
),
menu::Tree::with_children(
text("Open with").width(Length::Fill),
vec![menu::Tree::new(open_with)],
),
]),
)
Expand Down

0 comments on commit 39481ec

Please sign in to comment.