Skip to content
Open
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
9 changes: 7 additions & 2 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::cli::Cli;
use crate::consts::CEF_MESSAGE_LOOP_MAX_ITERATIONS;
use crate::event::{AppEvent, AppEventScheduler};
use crate::persist::PersistentData;
use crate::preferences;
use crate::render::{RenderError, RenderState};
use crate::window::Window;
use crate::wrapper::messages::{DesktopFrontendMessage, DesktopWrapperMessage, InputMessage, MouseKeys, MouseState};
Expand Down Expand Up @@ -277,10 +278,10 @@ impl App {
self.persistent_data.set_document_order(ids);
}
DesktopFrontendMessage::PersistenceWritePreferences { preferences } => {
self.persistent_data.write_preferences(preferences);
preferences::write(preferences);
}
DesktopFrontendMessage::PersistenceLoadPreferences => {
let preferences = self.persistent_data.load_preferences();
let preferences = preferences::read();
let message = DesktopWrapperMessage::LoadPreferences { preferences };
responses.push(message);
}
Expand Down Expand Up @@ -398,6 +399,9 @@ impl App {
window.show_all();
}
}
DesktopFrontendMessage::Restart => {
self.exit(Some(ExitReason::Restart));
}
}
}

Expand Down Expand Up @@ -663,5 +667,6 @@ impl ApplicationHandler for App {

pub(crate) enum ExitReason {
Shutdown,
Restart,
UiAccelerationFailure,
}
45 changes: 27 additions & 18 deletions desktop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
use crate::app::App;
use crate::cef::CefHandler;
use crate::cli::Cli;
use crate::consts::APP_LOCK_FILE_NAME;
use crate::event::CreateAppEventSchedulerEventLoopExt;
use clap::Parser;
use std::io::Write;
use std::process::exit;
use tracing_subscriber::EnvFilter;
use winit::event_loop::EventLoop;

pub(crate) mod consts;
pub(crate) use graphite_desktop_wrapper as wrapper;

mod app;
mod cef;
mod cli;
mod dirs;
mod event;
mod gpu_context;
mod persist;
mod preferences;
mod render;
mod window;

mod gpu_context;

pub(crate) use graphite_desktop_wrapper as wrapper;

use app::App;
use cef::CefHandler;
use cli::Cli;
use event::CreateAppEventSchedulerEventLoopExt;

use crate::consts::APP_LOCK_FILE_NAME;
pub(crate) mod consts;

pub fn start() {
tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init();
Expand Down Expand Up @@ -77,12 +75,13 @@ pub fn start() {

let (cef_view_info_sender, cef_view_info_receiver) = std::sync::mpsc::channel();

if cli.disable_ui_acceleration {
let disable_ui_acceleration = preferences::read().disable_ui_acceleration || cli.disable_ui_acceleration;
if disable_ui_acceleration {
println!("UI acceleration is disabled");
}

let cef_handler = cef::CefHandler::new(wgpu_context.clone(), app_event_scheduler.clone(), cef_view_info_receiver);
let cef_context = match cef_context_builder.initialize(cef_handler, cli.disable_ui_acceleration) {
let cef_context = match cef_context_builder.initialize(cef_handler, disable_ui_acceleration) {
Ok(context) => {
tracing::info!("CEF initialized successfully");
context
Expand All @@ -109,16 +108,26 @@ pub fn start() {

let exit_reason = app.run(event_loop);

// If exiting due to a UI acceleration failure, update preferences to disable it for next launch
if matches!(exit_reason, app::ExitReason::UiAccelerationFailure) {
tracing::error!("Disabling UI acceleration");
preferences::modify(|prefs| {
prefs.disable_ui_acceleration = true;
});
}

// Explicitly drop the instance lock
drop(lock);

match exit_reason {
#[cfg(target_os = "linux")]
app::ExitReason::UiAccelerationFailure => {
use std::os::unix::process::CommandExt;

tracing::error!("Restarting application without UI acceleration");
let _ = std::process::Command::new(std::env::current_exe().unwrap()).arg("--disable-ui-acceleration").exec();
app::ExitReason::Restart | app::ExitReason::UiAccelerationFailure => {
tracing::error!("Restarting application");
let mut command = std::process::Command::new(std::env::current_exe().unwrap());
#[cfg(target_family = "unix")]
let _ = std::os::unix::process::CommandExt::exec(&mut command);
#[cfg(not(target_family = "unix"))]
let _ = command.spawn();
tracing::error!("Failed to restart application");
}
_ => {}
Expand Down
24 changes: 1 addition & 23 deletions desktop/src/persist.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::wrapper::messages::{Document, DocumentId, Preferences};
use crate::wrapper::messages::{Document, DocumentId};

#[derive(Default, serde::Serialize, serde::Deserialize)]
pub(crate) struct PersistentData {
Expand Down Expand Up @@ -72,22 +72,6 @@ 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::ser::to_string_pretty(self, Default::default()) {
Ok(d) => d,
Expand Down Expand Up @@ -129,12 +113,6 @@ impl PersistentData {
path.push(crate::consts::APP_STATE_FILE_NAME);
path
}

fn preferences_file_path() -> std::path::PathBuf {
let mut path = crate::dirs::app_data_dir();
path.push(crate::consts::APP_PREFERENCES_FILE_NAME);
path
}
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
Expand Down
33 changes: 33 additions & 0 deletions desktop/src/preferences.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use graphite_desktop_wrapper::messages::Preferences;

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

pub(crate) fn read() -> Preferences {
let Ok(data) = std::fs::read_to_string(file_path()) else {
return Preferences::default();
};
let Ok(preferences) = ron::from_str(&data) else {
return Preferences::default();
};
preferences
}

pub(crate) fn modify(f: impl FnOnce(&mut Preferences)) {
let mut preferences = read();
f(&mut preferences);
write(preferences);
}

fn file_path() -> std::path::PathBuf {
let mut path = crate::dirs::app_data_dir();
path.push(crate::consts::APP_PREFERENCES_FILE_NAME);
path
}
2 changes: 1 addition & 1 deletion desktop/src/render/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl RenderState {
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&overlays_texture_view.as_ref()),
resource: wgpu::BindingResource::TextureView(overlays_texture_view.as_ref()),
},
wgpu::BindGroupEntry {
binding: 2,
Expand Down
3 changes: 3 additions & 0 deletions desktop/wrapper/src/intercept_frontend_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ pub(super) fn intercept_frontend_message(dispatcher: &mut DesktopWrapperMessageD
FrontendMessage::WindowShowAll => {
dispatcher.respond(DesktopFrontendMessage::WindowShowAll);
}
FrontendMessage::WindowRestart => {
dispatcher.respond(DesktopFrontendMessage::Restart);
}
m => return Some(m),
}
None
Expand Down
12 changes: 4 additions & 8 deletions desktop/wrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
use graph_craft::wasm_application_io::WasmApplicationIo;
use graphite_editor::application::{Editor, Environment, Host, Platform};
use graphite_editor::messages::prelude::{FrontendMessage, Message};
use message_dispatcher::DesktopWrapperMessageDispatcher;
use messages::{DesktopFrontendMessage, DesktopWrapperMessage};

pub use graphite_editor::consts::FILE_EXTENSION;

pub use wgpu_executor::TargetTexture;
pub use wgpu_executor::WgpuContext;
pub use wgpu_executor::WgpuContextBuilder;
pub use wgpu_executor::WgpuExecutor;
pub use wgpu_executor::WgpuFeatures;

pub mod messages;
use messages::{DesktopFrontendMessage, DesktopWrapperMessage};

mod message_dispatcher;
use message_dispatcher::DesktopWrapperMessageDispatcher;

mod handle_desktop_wrapper_message;
mod intercept_editor_message;
mod intercept_frontend_message;

mod message_dispatcher;
pub mod messages;
pub(crate) mod utils;

pub struct DesktopWrapper {
Expand Down
3 changes: 2 additions & 1 deletion desktop/wrapper/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum DesktopFrontendMessage {
WindowHide,
WindowHideOthers,
WindowShowAll,
Restart,
}

pub enum DesktopWrapperMessage {
Expand Down Expand Up @@ -113,7 +114,7 @@ pub enum DesktopWrapperMessage {
id: DocumentId,
},
LoadPreferences {
preferences: Option<Preferences>,
preferences: Preferences,
},
MenuEvent {
id: String,
Expand Down
1 change: 1 addition & 0 deletions editor/src/messages/app_window/app_window_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::messages::prelude::*;
pub enum AppWindowMessage {
PointerLock,
PointerLockMove { x: f64, y: f64 },
Restart,
Close,
Minimize,
Maximize,
Expand Down
4 changes: 4 additions & 0 deletions editor/src/messages/app_window/app_window_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl MessageHandler<AppWindowMessage, ()> for AppWindowMessageHandler {
AppWindowMessage::ShowAll => {
responses.add(FrontendMessage::WindowShowAll);
}
AppWindowMessage::Restart => {
responses.add(PortfolioMessage::AutoSaveAllDocuments);
responses.add(FrontendMessage::WindowRestart);
}
}
}
advertise_actions!(AppWindowMessageDiscriminant;
Expand Down
7 changes: 5 additions & 2 deletions editor/src/messages/dialog/dialog_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ pub enum DialogMessage {
PreferencesDialog(PreferencesDialogMessage),

// Messages
CloseAllDocumentsWithConfirmation,
CloseDialogAndThen {
Dismiss,
Close,
CloseAndThen {
followups: Vec<Message>,
},
CloseAllDocumentsWithConfirmation,
DisplayDialogError {
title: String,
description: String,
Expand All @@ -35,4 +37,5 @@ pub enum DialogMessage {
},
RequestNewDocumentDialog,
RequestPreferencesDialog,
RequestConfirmRestartDialog,
}
Loading