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
52 changes: 40 additions & 12 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use graphene_std::raster::Image;
use graphite_editor::application::Editor;
use graphite_editor::consts::DEFAULT_DOCUMENT_NAME;
use graphite_editor::messages::prelude::*;
use std::env;
use std::fs;
use std::sync::Arc;
use std::sync::mpsc::Sender;
Expand Down Expand Up @@ -221,6 +222,32 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
self.dispatch_message(message);
}
CustomEvent::MessageReceived(message) => {
if let Message::Portfolio(PortfolioMessage::Init) = &message {
let args: Vec<String> = env::args().collect();

// Try to open a file passed as the first command line argument
if args.len() > 1 {
let event_loop_proxy = self.event_loop_proxy.clone();
let path = std::path::PathBuf::from(&args[1]);

if !path.exists() {
tracing::warn!("The first argument is not a valid path or doesn't exist: {}", path.display());
return;
}

let name = path.file_stem().and_then(|s| s.to_str()).map(|s| s.to_string());

// Load the file in a separate thread to avoid blocking the main thread
let _ = thread::spawn(move || {
let Some(content) = load_string(&path) else { return };
let message = PortfolioMessage::OpenDocumentFile {
document_name: name.unwrap_or(DEFAULT_DOCUMENT_NAME.to_string()),
document_serialized_content: content,
};
let _ = event_loop_proxy.send_event(CustomEvent::DispatchMessage(message.into()));
});
}
}
if let Message::InputPreprocessor(_) = &message {
if let Some(window) = &self.window {
window.request_redraw();
Expand Down Expand Up @@ -276,18 +303,6 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
// Fine to early return since we don't need to do cef work in this case
return;
};
let load_string = |path: &std::path::PathBuf| {
let Ok(content) = fs::read_to_string(path) else {
tracing::error!("Failed to read file: {}", path.display());
return None;
};

if content.is_empty() {
tracing::warn!("Dropped file is empty: {}", path.display());
return None;
}
Some(content)
};
// TODO: Consider moving this logic to the editor so we have one message to load data which is then demultiplexed in the portfolio message handler
match extension {
"graphite" => {
Expand Down Expand Up @@ -368,3 +383,16 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
self.cef_context.work();
}
}

fn load_string(path: &std::path::PathBuf) -> Option<String> {
let Ok(content) = fs::read_to_string(path) else {
tracing::error!("Failed to read file: {}", path.display());
return None;
};

if content.is_empty() {
tracing::warn!("Dropped file is empty: {}", path.display());
return None;
}
Some(content)
}
2 changes: 1 addition & 1 deletion desktop/src/cef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use winit::event_loop::EventLoopProxy;
pub(crate) trait CefEventHandler: Clone {
fn window_size(&self) -> WindowSize;
fn draw<'a>(&self, frame_buffer: FrameBufferRef<'a>);
/// Scheudule the main event loop to run the cef event loop after the timeout
/// Schedule the main event loop to run the cef event loop after the timeout
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
fn receive_web_message(&self, message: &[u8]);
Expand Down
Loading