mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Desktop: Add "Disable UI Acceleration" to preferences (#3774)
* Deskltop: Add Disable UI Accelaration preference * Fixup * Fix typo * Code review and update strings --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
@@ -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};
|
||||
@@ -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);
|
||||
}
|
||||
@@ -398,6 +399,9 @@ impl App {
|
||||
window.show_all();
|
||||
}
|
||||
}
|
||||
DesktopFrontendMessage::Restart => {
|
||||
self.exit(Some(ExitReason::Restart));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,5 +667,6 @@ impl ApplicationHandler for App {
|
||||
|
||||
pub(crate) enum ExitReason {
|
||||
Shutdown,
|
||||
Restart,
|
||||
UiAccelerationFailure,
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
@@ -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
|
||||
@@ -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");
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
@@ -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)]
|
||||
|
||||
33
desktop/src/preferences.rs
Normal file
33
desktop/src/preferences.rs
Normal file
@@ -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
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user