Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Open files/folder in the file explorer #1117

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 51 additions & 33 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ num-derive = "0.4.2"
num-traits = "0.2.18"
numeric-sort = "0.1.0"
once_cell = "1.19"
open = "5.1"
opener = {version = "0.7.0", features=["reveal"]}
palette = "0.7.5"
parry2d-f64 = { version = "0.13.6", features = ["serde-serialize"] }
path-absolutize = "3.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/rnote-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ clap = { workspace = true }
dialoguer = { workspace = true }
indicatif = { workspace = true }
nalgebra = { workspace = true }
open = { workspace = true }
opener = { workspace = true }
parry2d-f64 = { workspace = true }
smol = { workspace = true }
tracing = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/rnote-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ pub(crate) async fn create_overwrite_file_w_bytes(
}

pub(crate) fn open_file_default_app(file_path: impl AsRef<Path>) -> anyhow::Result<()> {
open::that_detached(file_path.as_ref()).with_context(|| {
// this one may be problematic compared to open::that_detached
opener::open(file_path.as_ref()).with_context(|| {
format!(
"Failed to open output file/folder \"{}\".",
file_path.as_ref().display()
Expand Down
2 changes: 1 addition & 1 deletion crates/rnote-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ num-derive = { workspace = true }
num-traits = { workspace = true }
numeric-sort = { workspace = true }
once_cell = { workspace = true }
open = { workspace = true }
opener = { workspace = true }
palette = { workspace = true }
parry2d-f64 = { workspace = true }
path-absolutize = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/rnote-ui/data/ui/filerow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<attribute name="label" translatable="yes">Open in Default App</attribute>
<attribute name="action">filerow.open-in-default-app</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Open in File Explorer</attribute>
<attribute name="action">filerow.open-in-explorer</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Rename</attribute>
<attribute name="action">filerow.rename-file</attribute>
Expand Down
6 changes: 6 additions & 0 deletions crates/rnote-ui/data/ui/workspacebrowser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<attribute name="action">workspacebrowser.create-folder</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">Open Workspace Folder</attribute>
<attribute name="action">workspacebrowser.open-folder</attribute>
</item>
</section>
</menu>
</object>
</template>
Expand Down
22 changes: 9 additions & 13 deletions crates/rnote-ui/src/dialogs/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,14 @@ pub(crate) async fn dialog_export_doc_w_prefs(appwindow: &RnAppWindow, canvas: &
&gettext("Exported document successfully"),
&gettext("View in file manager"),
clone!(@weak canvas, @weak appwindow => move |_reload_toast| {
let Some(folder_path_string) = file
.parent()
.and_then(|p|
p.path())
.and_then(|p| p.into_os_string().into_string().ok()) else {
tracing::error!("Failed to get the parent folder of the output file `{file:?}.");
appwindow.overlays().dispatch_toast_error(&gettext("Exporting document failed"));
return;
};
let Some(file_path_string) = file.path().and_then(|p| p.into_os_string().into_string().ok()) else {
tracing::error!("Failed to get the path of the file `{file:?}.");
appwindow.overlays().dispatch_toast_error(&gettext("Exporting document failed"));
return;
};

if let Err(e) = open::that(&folder_path_string) {
tracing::error!("Opening the parent folder '{folder_path_string}' in the file manager failed, Err: {e:?}");
if let Err(e) = opener::reveal(&file_path_string) {
tracing::error!("Revealing the file '{file_path_string}' in the file manager failed, Err: {e:?}");
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file manager"));
}
}
Expand Down Expand Up @@ -546,7 +542,7 @@ pub(crate) async fn dialog_export_doc_pages_w_prefs(appwindow: &RnAppWindow, can
return;
};

if let Err(e) = open::that(&folder_path_string) {
if let Err(e) = opener::open(&folder_path_string) {
tracing::error!("Opening the parent folder '{folder_path_string}' in the file manager failed, Err: {e:?}");
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file manager"));
}
Expand Down Expand Up @@ -818,7 +814,7 @@ pub(crate) async fn dialog_export_selection_w_prefs(appwindow: &RnAppWindow, can
return;
};

if let Err(e) = open::that(&folder_path_string) {
if let Err(e) = opener::open(&folder_path_string) {
tracing::error!("Opening the parent folder '{folder_path_string}' in the file manager failed, Err: {e:?}");
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file manager"));
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rnote-ui/src/workspacebrowser/filerow/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
mod duplicate;
mod open;
mod open_in_default_app;
mod open_in_explorer;
mod rename;
mod trash;

// Re-exports
pub(crate) use duplicate::duplicate;
pub(crate) use open::open;
pub(crate) use open_in_default_app::open_in_default_app;
pub(crate) use open_in_explorer::open_in_explorer;
pub(crate) use rename::rename;
pub(crate) use trash::trash;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn open_in_default_app(
let Some(current_file) = filerow.current_file() else {
return;
};
if let Err(e) = open::that(current_file.uri()) {
if let Err(e) = opener::open(current_file.uri()) {
appwindow.overlays().dispatch_toast_error(&gettext("Open the file in the default app failed"));
tracing::debug!("Opening file {} with default app failed, Err: {e:?}", current_file.uri());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Imports
use crate::workspacebrowser::RnFileRow;
use crate::RnAppWindow;
use gettextrs::gettext;
use gtk4::{gio, gio::prelude::FileExt, glib, glib::clone};

/// Create a new `open-in-default-app` action.
pub(crate) fn open_in_explorer(filerow: &RnFileRow, appwindow: &RnAppWindow) -> gio::SimpleAction {
let action_open_in_default = gio::SimpleAction::new("open-in-explorer", None);
action_open_in_default.connect_activate(
clone!(@weak filerow, @weak appwindow => move |_action_open_in_default, _| {
if let Some(current_file) = filerow.current_file() {
// check if the path can be obtained
if let Some(path) = current_file.path() {
if let Err(e) = opener::reveal(path ) {
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file explorer"));
tracing::debug!("opening file {} in the file explorer failed: {e:?}", current_file.uri());
}
}
else {
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file explorer"));
tracing::debug!("opening file {} in the file explorer failed, could not get the path", current_file.uri());
}
}
}),
);

action_open_in_default
}
3 changes: 3 additions & 0 deletions crates/rnote-ui/src/workspacebrowser/filerow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ impl RnFileRow {
self.imp()
.action_group
.add_action(&actions::open_in_default_app(self, appwindow));
self.imp()
.action_group
.add_action(&actions::open_in_explorer(self, appwindow));
self.imp()
.action_group
.add_action(&actions::rename(self, appwindow));
Expand Down
3 changes: 3 additions & 0 deletions crates/rnote-ui/src/workspacebrowser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ impl RnWorkspaceBrowser {
self.imp()
.action_group
.add_action(&workspaceactions::create_folder(self, appwindow));
self.imp()
.action_group
.add_action(&workspaceactions::open_folder(self, appwindow));
}

fn setup_dir_controls(&self, appwindow: &RnAppWindow) {
Expand Down
2 changes: 2 additions & 0 deletions crates/rnote-ui/src/workspacebrowser/workspaceactions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Modules
mod createfolder;
mod openfolder;

// Re-exports
pub(crate) use createfolder::create_folder;
pub(crate) use openfolder::open_folder;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{RnAppWindow, RnWorkspaceBrowser};
use gettextrs::gettext;
use gtk4::{gio, glib, glib::clone, prelude::*};

pub(crate) fn open_folder(
workspacebrowser: &RnWorkspaceBrowser,
appwindow: &RnAppWindow,
) -> gio::SimpleAction {
let open_folder_action = gio::SimpleAction::new("open-folder", None);

open_folder_action.connect_activate(clone!(@weak workspacebrowser, @weak appwindow => move |_, _| {
if let Some(parent_path) = workspacebrowser.dir_list_file().and_then(|f| f.path()) {
if let Err(e) = opener::open(&parent_path) {
let path_string = &parent_path.into_os_string().into_string().ok().unwrap_or(String::from("path not found"));
tracing::error!("Opening the parent folder '{path_string}' in the file manager failed, Err: {e:?}");
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file manager"));
}
} else {
tracing::warn!("No path found");
appwindow.overlays().dispatch_toast_error(&gettext("Failed to open the file in the file manager"));
}
}
));

open_folder_action
}