Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Dec 6, 2023
2 parents b74a813 + 1c8def1 commit 028cff5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
10 changes: 10 additions & 0 deletions crates/rnote-ui/src/canvas/imexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ impl RnCanvas {

crate::utils::create_replace_file_future(export_bytes.await??, file).await?;

self.set_last_export_dir(file.parent());

Ok(())
}

Expand Down Expand Up @@ -347,6 +349,8 @@ impl RnCanvas {
.await?;
}

self.set_last_export_dir(Some(dir.clone()));

Ok(())
}

Expand All @@ -361,6 +365,8 @@ impl RnCanvas {
crate::utils::create_replace_file_future(export_bytes, file).await?;
}

self.set_last_export_dir(file.parent());

Ok(())
}

Expand All @@ -371,6 +377,8 @@ impl RnCanvas {

crate::utils::create_replace_file_future(exported_engine_state.into_bytes(), file).await?;

self.set_last_export_dir(file.parent());

Ok(())
}

Expand All @@ -381,6 +389,8 @@ impl RnCanvas {

crate::utils::create_replace_file_future(exported_engine_config.into_bytes(), file).await?;

self.set_last_export_dir(file.parent());

Ok(())
}
}
12 changes: 12 additions & 0 deletions crates/rnote-ui/src/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ mod imp {
pub(crate) empty: Cell<bool>,
pub(crate) touch_drawing: Cell<bool>,
pub(crate) show_drawing_cursor: Cell<bool>,

pub(crate) last_export_dir: RefCell<Option<gio::File>>,
}

impl Default for RnCanvas {
Expand Down Expand Up @@ -172,6 +174,8 @@ mod imp {
empty: Cell::new(true),
touch_drawing: Cell::new(false),
show_drawing_cursor: Cell::new(false),

last_export_dir: RefCell::new(None),
}
}
}
Expand Down Expand Up @@ -705,6 +709,14 @@ impl RnCanvas {
);
}

pub(crate) fn last_export_dir(&self) -> Option<gio::File> {
self.imp().last_export_dir.borrow().clone()
}

pub(crate) fn set_last_export_dir(&self, dir: Option<gio::File>) {
self.imp().last_export_dir.replace(dir);
}

/// Returns the saved date time for the modification time of the output file when it was set by the app.
///
/// It might not be correct if the content of the output file has changed from outside the app.
Expand Down
68 changes: 38 additions & 30 deletions crates/rnote-ui/src/dialogs/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(crate) async fn dialog_export_doc_w_prefs(appwindow: &RnAppWindow, canvas: &

let doc_export_prefs = canvas.engine_mut().export_prefs.doc_export_prefs;
let filedialog =
create_filedialog_export_doc(&appwindow, canvas.output_file(), &doc_export_prefs);
create_filedialog_export_doc(&appwindow, &canvas, &doc_export_prefs);
match filedialog.save_future(Some(&appwindow)).await {
Ok(f) => {
if let Some(path_string) = f.path().map(|p| p.to_string_lossy().to_string()) {
Expand Down Expand Up @@ -261,7 +261,7 @@ pub(crate) async fn dialog_export_doc_w_prefs(appwindow: &RnAppWindow, canvas: &

fn create_filedialog_export_doc(
appwindow: &RnAppWindow,
output_file: Option<gio::File>,
canvas: &RnCanvas,
doc_export_prefs: &DocExportPrefs,
) -> FileDialog {
let filedialog = FileDialog::builder()
Expand Down Expand Up @@ -290,16 +290,14 @@ fn create_filedialog_export_doc(
}
let file_ext = doc_export_prefs.export_format.file_ext();
let file_name = crate::utils::default_file_title_for_export(
output_file,
canvas.output_file(),
Some(&canvas::OUTPUT_FILE_NEW_TITLE),
Some(&(String::from(".") + &file_ext)),
);

filedialog.set_default_filter(Some(&filter));
filedialog.set_initial_name(Some(&file_name));
if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dirlist_dir() {
filedialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)));
}
filedialog.set_initial_folder(get_initial_folder_for_export(appwindow, canvas).as_ref());

filedialog
}
Expand Down Expand Up @@ -398,7 +396,7 @@ pub(crate) async fn dialog_export_doc_pages_w_prefs(appwindow: &RnAppWindow, can
let doc_pages_export_prefs = canvas.engine_mut().export_prefs.doc_pages_export_prefs;
let filedialog = create_filedialog_export_doc_pages(
&appwindow,
canvas.output_file(),
&canvas,
&doc_pages_export_prefs,
);
match filedialog.select_folder_future(Some(&appwindow)).await {
Expand Down Expand Up @@ -560,7 +558,7 @@ pub(crate) async fn dialog_export_doc_pages_w_prefs(appwindow: &RnAppWindow, can

fn create_filedialog_export_doc_pages(
appwindow: &RnAppWindow,
output_file: Option<gio::File>,
canvas: &RnCanvas,
doc_pages_export_prefs: &DocPagesExportPrefs,
) -> FileDialog {
let filedialog = FileDialog::builder()
Expand All @@ -569,16 +567,7 @@ fn create_filedialog_export_doc_pages(
.accept_label(gettext("Select"))
.build();

let initial_folder = if let Some(output_parent_dir) = output_file.and_then(|f| f.parent()) {
Some(output_parent_dir)
} else {
appwindow
.sidebar()
.workspacebrowser()
.dirlist_dir()
.map(gio::File::for_path)
};
filedialog.set_initial_folder(initial_folder.as_ref());
filedialog.set_initial_folder(get_initial_folder_for_export(appwindow, canvas).as_ref());

let filter = FileFilter::new();
// We always need to be able to select folders
Expand Down Expand Up @@ -688,7 +677,7 @@ pub(crate) async fn dialog_export_selection_w_prefs(appwindow: &RnAppWindow, can
.selection_export_prefs;
let filedialog = create_filedialog_export_selection(
&appwindow,
canvas.output_file(),
&canvas,
&selection_export_prefs,
);
match filedialog.save_future(Some(&appwindow)).await {
Expand Down Expand Up @@ -836,9 +825,34 @@ pub(crate) async fn dialog_export_selection_w_prefs(appwindow: &RnAppWindow, can
}
}

/// Returns (if possible) a "reasonable" folder for export operations
/// concerning the specified `appwindow` and `canvas`. The main goal
/// of this function is to provide a "good" initial folder for the
/// file chooser dialog when a canvas is exported.
///
/// The following locations are checked:
///
/// 1. the last export directory of `canvas`
/// 2. the parent directory of the output file of `canvas`
/// 3. the directory shown in the sidebar of the window
///
/// The first available will be returned.
fn get_initial_folder_for_export(appwindow: &RnAppWindow, canvas: &RnCanvas) -> Option<gio::File> {
canvas
.last_export_dir()
.or_else(|| canvas.output_file().and_then(|p| p.parent()))
.or_else(|| {
appwindow
.sidebar()
.workspacebrowser()
.dirlist_dir()
.map(gio::File::for_path)
})
}

fn create_filedialog_export_selection(
appwindow: &RnAppWindow,
output_file: Option<gio::File>,
canvas: &RnCanvas,
selection_export_prefs: &SelectionExportPrefs,
) -> FileDialog {
let filedialog = FileDialog::builder()
Expand All @@ -847,9 +861,7 @@ fn create_filedialog_export_selection(
.accept_label(gettext("Select"))
.build();

if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dirlist_dir() {
filedialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)));
}
filedialog.set_initial_folder(get_initial_folder_for_export(appwindow, canvas).as_ref());

let filter = FileFilter::new();
match selection_export_prefs.export_format {
Expand All @@ -872,7 +884,7 @@ fn create_filedialog_export_selection(
}
let file_ext = selection_export_prefs.export_format.file_ext();
let file_name = crate::utils::default_file_title_for_export(
output_file,
canvas.output_file(),
Some(&canvas::OUTPUT_FILE_NEW_TITLE),
Some(&(String::from(" - Selection") + "." + &file_ext)),
);
Expand Down Expand Up @@ -902,9 +914,7 @@ pub(crate) async fn filechooser_export_engine_state(appwindow: &RnAppWindow, can
.initial_name(&initial_name)
.build();

if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dirlist_dir() {
filedialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)))
}
filedialog.set_initial_folder(get_initial_folder_for_export(appwindow, canvas).as_ref());

match filedialog.save_future(Some(appwindow)).await {
Ok(selected_file) => {
Expand Down Expand Up @@ -949,9 +959,7 @@ pub(crate) async fn filechooser_export_engine_config(appwindow: &RnAppWindow, ca
.initial_name(&initial_name)
.build();

if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dirlist_dir() {
filedialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)));
}
filedialog.set_initial_folder(get_initial_folder_for_export(appwindow, canvas).as_ref());

match filedialog.save_future(Some(appwindow)).await {
Ok(selected_file) => {
Expand Down

0 comments on commit 028cff5

Please sign in to comment.