Skip to content
Merged
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
3 changes: 2 additions & 1 deletion apps/notecraft/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ impl eframe::App for Shell {

fn notebooks_dir(args: &[String]) -> PathBuf {
let configured = notecraft_store::settings::configured_notebooks_dir(&Settings::default_location());
// The development folder counts only in a debug build; a production install never reads it.
let local = PathBuf::from("my-notebooks");
let local = local.is_dir().then_some(local);
let local = (cfg!(debug_assertions) && local.is_dir()).then_some(local);
notebooks_dir_from(args, configured, local, notecraft_store::settings::default_notebooks_dir())
}

Expand Down
24 changes: 24 additions & 0 deletions crates/engine/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ impl Session {
&self.root
}

/// Moves the session to another notebooks folder and lists the notebooks in it. The folder
/// is created when it does not exist yet, as at start.
pub fn set_root(&mut self, root: &Path) -> Result<(), EngineError> {
std::fs::create_dir_all(root).map_err(|e| StoreError::io("create", root, e))?;
self.root = std::fs::canonicalize(root).map_err(|e| StoreError::io("open", root, e))?;
self.refresh();
Ok(())
}

pub fn reviewed_export(&self) -> Option<&notecraft_store::export::Prepared> {
self.export.as_ref()
}
Expand Down Expand Up @@ -208,6 +217,21 @@ fn first_page(n: &Node) -> Option<PathBuf> {
mod tests {
use super::*;

#[test]
fn changing_the_root_shows_the_notebooks_in_the_new_folder() {
let t = notecraft_store::testing::TempDir::new("session-set-root");
let settings = notecraft_store::Settings::at(t.path().join("settings.toml"));
let first = t.path().join("first");
let second = t.path().join("second");
notecraft_store::ops::create_notebook(&second, "Work", None, &settings).expect("second notebook");
let mut session = Session::new(&first, settings).expect("session");

session.set_root(&second).expect("change the root");

assert_eq!(session.root(), std::fs::canonicalize(&second).expect("canonical").as_path());
assert!(session.tree().iter().any(|n| n.path.ends_with("work")), "the new folder's notebooks are listed");
}

#[test]
fn a_panic_becomes_an_error() {
let r: Result<(), EngineError> = guarded(|| panic!("boom"));
Expand Down
19 changes: 19 additions & 0 deletions crates/store/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub fn default_notebooks_dir() -> PathBuf {
default_notebooks_dir_for_home(home)
}

/// The notebooks folder this build starts with: the development folder in a debug build, and the
/// per-user folder in a production build. "Use the default folder" goes back to this.
pub fn build_default_notebooks_dir() -> PathBuf {
if cfg!(debug_assertions) { PathBuf::from("my-notebooks") } else { default_notebooks_dir() }
}

/// The per-user notebook folder, kept separate so every platform can test the same contract.
pub fn default_notebooks_dir_for_home(home: impl AsRef<Path>) -> PathBuf {
home.as_ref().join("Documents").join("NoteCraft")
Expand Down Expand Up @@ -191,6 +197,19 @@ mod tests {
assert_eq!(configured_notebooks_dir(&s), Some(PathBuf::from("Notes/Research")));
}

#[test]
fn a_debug_build_defaults_to_the_development_folder() {
let expected = if cfg!(debug_assertions) { PathBuf::from("my-notebooks") } else { default_notebooks_dir() };
assert_eq!(build_default_notebooks_dir(), expected);
}

#[test]
fn a_debug_build_never_resets_to_the_production_folder() {
if cfg!(debug_assertions) {
assert_ne!(build_default_notebooks_dir(), default_notebooks_dir(), "a dev build resets to my-notebooks, not Documents");
}
}

#[test]
fn a_cleared_notebooks_root_means_the_default_folder() {
let t = TempDir::new("settings-notebook-root-cleared");
Expand Down
68 changes: 47 additions & 21 deletions crates/ui-egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,6 @@ impl NoteCraftApp {
self.ui_scale
}

/// Draws the whole window larger or smaller, for a screen where the system's own scale
/// leaves the ribbon and the menus too small to read, and remembers the choice.
/// Saves the notebooks folder NoteCraft uses from the next start. `None` goes back to the
/// default folder. The current session keeps its folder until then.
pub fn save_notebooks_root(&mut self, folder: Option<&std::path::Path>) -> Result<(), String> {
Expand All @@ -1565,22 +1563,36 @@ impl NoteCraftApp {
}

fn choose_default_notebooks_folder(&mut self) {
let Some(picked) = pick_notebooks_folder() else {
// The dialog opens where the notebooks are now, not in Documents.
let Some(picked) = pick_notebooks_folder(self.session.root()) else {
return;
};
self.apply_notebooks_root_choice(Some(&picked));
}

/// Moves NoteCraft to the chosen notebooks folder now, or to the default folder for this build
/// when `folder` is None, and remembers the choice for the next start.
fn apply_notebooks_root_choice(&mut self, folder: Option<&std::path::Path>) {
match self.save_notebooks_root(folder) {
Ok(()) => self.say(crate::tl_id!("ui.notebooks_folder_next_start")),
Err(error) => {
let message = crate::i18n::format_message_id("ui.could_not_save_the_notebooks_folder_error", &[("error", &error)]);
self.complain(&message);
}
let target = folder.map_or_else(notecraft_store::settings::build_default_notebooks_dir, std::path::Path::to_path_buf);
if let Err(error) = self.save_notebooks_root(folder) {
let message = crate::i18n::format_message_id("ui.could_not_save_the_notebooks_folder_error", &[("error", &error)]);
self.complain(&message);
return;
}
if let Err(error) = self.session.set_root(&target) {
let error = error.to_string();
let message = crate::i18n::format_message_id("ui.could_not_open_the_notebooks_folder_error", &[("error", &error)]);
self.complain(&message);
return;
}
self.editor = None;
self.pad = None;
let shown = target.display().to_string();
self.say(crate::i18n::format_message_id("ui.notebooks_folder_changed", &[("folder", &shown)]));
}

/// Draws the whole window larger or smaller, for a screen where the system's own scale
/// leaves the ribbon and the menus too small to read, and remembers the choice.
pub fn set_ui_scale(&mut self, percent: u16) {
if let Err(error) = self.session.settings.set(UI_SCALE_KEY, &percent.to_string()) {
let error = error.to_string();
Expand Down Expand Up @@ -11048,12 +11060,12 @@ fn drawing_help_page(notebooks: &[PathBuf], language: &str) -> Option<PathBuf> {

/// The notebooks folder chooser. The web build has no native folder dialog, so it chooses nothing.
#[cfg(not(target_arch = "wasm32"))]
fn pick_notebooks_folder() -> Option<PathBuf> {
rfd::FileDialog::new().set_title(crate::tl_id!("ui.choose_the_notebooks_folder")).pick_folder()
fn pick_notebooks_folder(start: &std::path::Path) -> Option<PathBuf> {
rfd::FileDialog::new().set_title(crate::tl_id!("ui.choose_the_notebooks_folder")).set_directory(start).pick_folder()
}

#[cfg(target_arch = "wasm32")]
fn pick_notebooks_folder() -> Option<PathBuf> {
fn pick_notebooks_folder(_start: &std::path::Path) -> Option<PathBuf> {
None
}

Expand Down Expand Up @@ -11264,22 +11276,36 @@ mod alert_tests {
}

#[test]
fn choosing_a_notebooks_folder_saves_it_for_the_next_start() {
fn choosing_a_notebooks_folder_moves_to_it_now() {
let t = notecraft_store::testing::TempDir::new("notebooks-folder-choice");
let settings = notecraft_store::Settings::at(t.path().join("settings.toml"));
settings.set("privacy_welcome_seen", "true").expect("settings");
let session = Session::new(&t.path().join("notebooks"), settings.clone()).expect("session");
let mut app = NoteCraftApp::new(session);
let chosen = t.path().join("chosen");

app.save_notebooks_root(Some(std::path::Path::new("Notes/Research"))).expect("save the folder");
assert_eq!(
notecraft_store::settings::configured_notebooks_dir(&settings),
Some(std::path::PathBuf::from("Notes/Research")),
"the chosen folder is used at the next start"
);
app.apply_notebooks_root_choice(Some(&chosen));

assert_eq!(app.session().root(), std::fs::canonicalize(&chosen).expect("canonical").as_path(), "the window moves to the chosen folder");
assert_eq!(settings.get("last_notebooks_root"), Some(chosen.to_string_lossy().into_owned()), "the choice is remembered");
}

#[test]
fn after_a_change_the_window_lists_the_notebooks_in_the_new_folder() {
let t = notecraft_store::testing::TempDir::new("notebooks-folder-list");
let settings = notecraft_store::Settings::at(t.path().join("settings.toml"));
settings.set("privacy_welcome_seen", "true").expect("settings");
let session = Session::new(&t.path().join("notebooks"), settings.clone()).expect("session");
let mut app = NoteCraftApp::new(session);
let chosen = t.path().join("chosen");
notecraft_store::ops::create_notebook(&chosen, "Research", None, &settings).expect("a notebook in the chosen folder");

app.save_notebooks_root(None).expect("use the default folder");
assert_eq!(notecraft_store::settings::configured_notebooks_dir(&settings), None, "the default folder is used again");
app.apply_notebooks_root_choice(Some(&chosen));

assert!(
app.session().tree().iter().any(|n| n.path.ends_with("research")),
"the notebook in the chosen folder is listed at once, not after a restart"
);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion i18n/ar-XB.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@
@id ui.change_notebooks_folder [[RLI]]$$$Change notebooks folder...@@@[[PDI]]
@id ui.use_the_default_notebooks_folder [[RLI]]$$$Use the default folder@@@[[PDI]]
@id ui.choose_the_notebooks_folder [[RLI]]$$$Choose the notebooks folder@@@[[PDI]]
@id ui.notebooks_folder_next_start [[RLI]]$$$The notebooks folder changes the next time NoteCraft starts.@@@[[PDI]]
@id ui.could_not_save_the_notebooks_folder_error [[RLI]]$$$Could not save the notebooks folder: {error}@@@[[PDI]]
@id ui.notebooks_folder_current [[RLI]]$$$NoteCraft keeps new notebooks in {folder}.@@@[[PDI]]
@id ui.notebooks_folder_changed [[RLI]]$$$The notebooks folder is now {folder}.@@@[[PDI]]
@id ui.could_not_open_the_notebooks_folder_error [[RLI]]$$$Could not open the notebooks folder: {error}@@@[[PDI]]
3 changes: 2 additions & 1 deletion i18n/en-XA.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@
@id ui.change_notebooks_folder $$$Change notebooks folder...@@@
@id ui.use_the_default_notebooks_folder $$$Use the default folder@@@
@id ui.choose_the_notebooks_folder $$$Choose the notebooks folder@@@
@id ui.notebooks_folder_next_start $$$The notebooks folder changes the next time NoteCraft starts.@@@
@id ui.could_not_save_the_notebooks_folder_error $$$Could not save the notebooks folder: {error}@@@
@id ui.notebooks_folder_current $$$NoteCraft keeps new notebooks in {folder}.@@@
@id ui.notebooks_folder_changed $$$The notebooks folder is now {folder}.@@@
@id ui.could_not_open_the_notebooks_folder_error $$$Could not open the notebooks folder: {error}@@@
3 changes: 2 additions & 1 deletion i18n/en.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@
@id ui.change_notebooks_folder Change notebooks folder...
@id ui.use_the_default_notebooks_folder Use the default folder
@id ui.choose_the_notebooks_folder Choose the notebooks folder
@id ui.notebooks_folder_next_start The notebooks folder changes the next time NoteCraft starts.
@id ui.could_not_save_the_notebooks_folder_error Could not save the notebooks folder: {error}
@id ui.notebooks_folder_current NoteCraft keeps new notebooks in {folder}.
@id ui.notebooks_folder_changed The notebooks folder is now {folder}.
@id ui.could_not_open_the_notebooks_folder_error Could not open the notebooks folder: {error}
Loading