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
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ impl WinitApp {
self.dispatch_desktop_wrapper_message(message);
}
}
DesktopFrontendMessage::PersistenceWritePreferences { preferences } => {
self.persistent_data.write_preferences(preferences);
}
DesktopFrontendMessage::PersistenceLoadPreferences => {
if let Some(preferences) = self.persistent_data.load_preferences() {
let message = DesktopWrapperMessage::LoadPreferences { preferences };
self.dispatch_desktop_wrapper_message(message);
}
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion desktop/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub(crate) static APP_NAME: &str = "Graphite";
pub(crate) static APP_ID: &str = "rs.graphite.GraphiteEditor";

pub(crate) static APP_DIRECTORY_NAME: &str = "graphite-editor";
pub(crate) static APP_AUTOSAVE_DIRECTORY_NAME: &str = "documents";
pub(crate) static APP_STATE_FILE_NAME: &str = "state.ron";
pub(crate) static APP_PREFERENCES_FILE_NAME: &str = "preferences.ron";
pub(crate) static APP_DOCUMENTS_DIRECTORY_NAME: &str = "documents";

// CEF configuration constants
pub(crate) const CEF_WINDOWLESS_FRAME_RATE: i32 = 60;
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::create_dir_all;
use std::path::PathBuf;

use crate::consts::{APP_AUTOSAVE_DIRECTORY_NAME, APP_DIRECTORY_NAME};
use crate::consts::{APP_DIRECTORY_NAME, APP_DOCUMENTS_DIRECTORY_NAME};

pub(crate) fn ensure_dir_exists(path: &PathBuf) {
if !path.exists() {
Expand All @@ -16,7 +16,7 @@ pub(crate) fn graphite_data_dir() -> PathBuf {
}

pub(crate) fn graphite_autosave_documents_dir() -> PathBuf {
let path = graphite_data_dir().join(APP_AUTOSAVE_DIRECTORY_NAME);
let path = graphite_data_dir().join(APP_DOCUMENTS_DIRECTORY_NAME);
ensure_dir_exists(&path);
path
}
35 changes: 29 additions & 6 deletions desktop/src/persist.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use graphite_desktop_wrapper::messages::{Document, DocumentId};
use graphite_desktop_wrapper::messages::{Document, DocumentId, Preferences};

#[derive(Default, serde::Serialize, serde::Deserialize)]
pub(crate) struct PersistentData {
documents: DocumentStore,
current_document: Option<DocumentId>,
#[serde(skip)]
document_order: Option<Vec<DocumentId>>,
preferences: Option<Preferences>,
}

impl PersistentData {
Expand Down Expand Up @@ -72,21 +73,37 @@ impl PersistentData {
self.flush();
}

pub(crate) fn write_preferences(&mut self, preferences: Preferences) {
let Ok(preferences) = ron::ser::to_string_pretty(&preferences, Default::default()) else {
tracing::error!("Failed to serialize preferences");
return;
};
std::fs::write(Self::preferences_file_path(), &preferences).unwrap_or_else(|e| {
tracing::error!("Failed to write preferences to disk: {e}");
});
}

pub(crate) fn load_preferences(&self) -> Option<Preferences> {
let data = std::fs::read_to_string(Self::preferences_file_path()).ok()?;
let preferences = ron::from_str(&data).ok()?;
Some(preferences)
}

fn flush(&self) {
let data = match ron::to_string(self) {
let data = match ron::ser::to_string_pretty(self, Default::default()) {
Ok(d) => d,
Err(e) => {
tracing::error!("Failed to serialize persistent data: {e}");
return;
}
};
if let Err(e) = std::fs::write(Self::persistence_file_path(), data) {
if let Err(e) = std::fs::write(Self::state_file_path(), data) {
tracing::error!("Failed to write persistent data to disk: {e}");
}
}

pub(crate) fn load_from_disk(&mut self) {
let path = Self::persistence_file_path();
let path = Self::state_file_path();
let data = match std::fs::read_to_string(&path) {
Ok(d) => d,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
Expand All @@ -108,9 +125,15 @@ impl PersistentData {
*self = loaded;
}

fn persistence_file_path() -> std::path::PathBuf {
fn state_file_path() -> std::path::PathBuf {
let mut path = crate::dirs::graphite_data_dir();
path.push(crate::consts::APP_STATE_FILE_NAME);
path
}

fn preferences_file_path() -> std::path::PathBuf {
let mut path = crate::dirs::graphite_data_dir();
path.push(format!("{}.ron", crate::consts::APP_AUTOSAVE_DIRECTORY_NAME));
path.push(crate::consts::APP_PREFERENCES_FILE_NAME);
path
}
}
Expand Down
6 changes: 5 additions & 1 deletion desktop/wrapper/src/handle_desktop_wrapper_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use graphene_std::Color;
use graphene_std::raster::Image;
use graphite_editor::messages::app_window::app_window_message_handler::AppWindowPlatform;
use graphite_editor::messages::prelude::{AppWindowMessage, DocumentMessage, PortfolioMessage};
use graphite_editor::messages::prelude::{AppWindowMessage, DocumentMessage, PortfolioMessage, PreferencesMessage};

use crate::messages::Platform;

Expand Down Expand Up @@ -140,5 +140,9 @@ pub(super) fn handle_desktop_wrapper_message(dispatcher: &mut DesktopWrapperMess
let message = PortfolioMessage::SelectDocument { document_id: id };
dispatcher.queue_editor_message(message.into());
}
DesktopWrapperMessage::LoadPreferences { preferences } => {
let message = PreferencesMessage::Load { preferences };
dispatcher.queue_editor_message(message.into());
}
}
}
6 changes: 6 additions & 0 deletions desktop/wrapper/src/intercept_frontend_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ pub(super) fn intercept_frontend_message(dispatcher: &mut DesktopWrapperMessageD
FrontendMessage::TriggerLoadRestAutoSaveDocuments => {
dispatcher.respond(DesktopFrontendMessage::PersistenceLoadRemainingDocuments);
}
FrontendMessage::TriggerSavePreferences { preferences } => {
dispatcher.respond(DesktopFrontendMessage::PersistenceWritePreferences { preferences });
}
FrontendMessage::TriggerLoadPreferences => {
dispatcher.respond(DesktopFrontendMessage::PersistenceLoadPreferences);
}
m => return Some(m),
}
None
Expand Down
12 changes: 11 additions & 1 deletion desktop/wrapper/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub use graphite_editor::messages::prelude::DocumentId;
use graphite_editor::messages::prelude::FrontendMessage;
pub(crate) use graphite_editor::messages::prelude::Message as EditorMessage;
use std::path::PathBuf;

pub(crate) use graphite_editor::messages::prelude::Message as EditorMessage;

pub use graphite_editor::messages::prelude::PreferencesMessageHandler as Preferences;

pub enum DesktopFrontendMessage {
ToWeb(Vec<FrontendMessage>),
OpenFileDialog {
Expand Down Expand Up @@ -48,6 +51,10 @@ pub enum DesktopFrontendMessage {
PersistenceUpdateDocumentsList {
ids: Vec<DocumentId>,
},
PersistenceWritePreferences {
preferences: Preferences,
},
PersistenceLoadPreferences,
CloseWindow,
}

Expand Down Expand Up @@ -93,6 +100,9 @@ pub enum DesktopWrapperMessage {
SelectDocument {
id: DocumentId,
},
LoadPreferences {
preferences: Preferences,
},
}

#[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion editor/src/messages/preferences/preferences_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::messages::prelude::*;
#[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum PreferencesMessage {
// Management messages
Load { preferences: String },
Load { preferences: PreferencesMessageHandler },
ResetToDefaults,

// Per-preference messages
Expand Down
14 changes: 6 additions & 8 deletions editor/src/messages/preferences/preferences_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ impl MessageHandler<PreferencesMessage, ()> for PreferencesMessageHandler {
match message {
// Management messages
PreferencesMessage::Load { preferences } => {
if let Ok(deserialized_preferences) = serde_json::from_str::<PreferencesMessageHandler>(&preferences) {
*self = deserialized_preferences;
*self = preferences;

responses.add(PortfolioMessage::EditorPreferences);
responses.add(PortfolioMessage::UpdateVelloPreference);
responses.add(PreferencesMessage::ModifyLayout {
zoom_with_scroll: self.zoom_with_scroll,
});
}
responses.add(PortfolioMessage::EditorPreferences);
responses.add(PortfolioMessage::UpdateVelloPreference);
responses.add(PreferencesMessage::ModifyLayout {
zoom_with_scroll: self.zoom_with_scroll,
});
}
PreferencesMessage::ResetToDefaults => {
refresh_dialog(responses);
Expand Down
1 change: 1 addition & 0 deletions frontend/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ math-parser = { workspace = true }
wgpu = { workspace = true }
web-sys = { workspace = true }
ron = { workspace = true }
serde_json = { workspace = true }

[package.metadata.wasm-pack.profile.dev]
wasm-opt = false
Expand Down
5 changes: 5 additions & 0 deletions frontend/wasm/src/editor_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ impl EditorHandle {

#[wasm_bindgen(js_name = loadPreferences)]
pub fn load_preferences(&self, preferences: String) {
let Ok(preferences) = serde_json::from_str(&preferences) else {
log::error!("Failed to deserialize preferences");
return;
};

let message = PreferencesMessage::Load { preferences };

self.dispatch(message);
Expand Down