Skip to content

Commit b9dde9a

Browse files
Merge branch 'master' into provide-cargo-rust-gpu-to-nix-devs
2 parents 6ca7587 + 81abfe1 commit b9dde9a

File tree

11 files changed

+76
-31
lines changed

11 files changed

+76
-31
lines changed

editor/src/dispatcher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl Dispatcher {
143143
let context = DialogMessageContext {
144144
portfolio: &self.message_handlers.portfolio_message_handler,
145145
preferences: &self.message_handlers.preferences_message_handler,
146+
viewport_bounds: &self.message_handlers.input_preprocessor_message_handler.viewport_bounds,
146147
};
147148
self.message_handlers.dialog_message_handler.process_message(message, &mut queue, context);
148149
}

editor/src/messages/defer/defer_message_handler.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl MessageHandler<DeferMessage, DeferMessageContext<'_>> for DeferMessageHandl
1919
DeferMessage::AfterGraphRun { mut messages } => {
2020
let after_graph_run = self.after_graph_run.entry(context.portfolio.active_document_id.unwrap_or(DocumentId(0))).or_default();
2121
after_graph_run.extend(messages.drain(..).map(|m| (self.current_graph_submission_id, m)));
22+
responses.add(NodeGraphMessage::RunDocumentGraph);
2223
}
2324
DeferMessage::AfterNavigationReady { messages } => {
2425
self.after_viewport_resize.extend_from_slice(&messages);
@@ -37,8 +38,10 @@ impl MessageHandler<DeferMessage, DeferMessageContext<'_>> for DeferMessageHandl
3738
for (_, message) in elements.rev() {
3839
responses.add_front(message);
3940
}
40-
if !after_graph_run.is_empty() {
41-
responses.add(NodeGraphMessage::RunDocumentGraph);
41+
for (id, messages) in self.after_graph_run.iter() {
42+
if !messages.is_empty() {
43+
responses.add(PortfolioMessage::SubmitGraphRender { document_id: *id, ignore_hash: false });
44+
}
4245
}
4346
}
4447
DeferMessage::TriggerNavigationReady => {

editor/src/messages/dialog/dialog_message_handler.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use super::new_document_dialog::NewDocumentDialogMessageContext;
12
use super::simple_dialogs::{self, AboutGraphiteDialog, ComingSoonDialog, DemoArtworkDialog, LicensesDialog};
3+
use crate::messages::input_mapper::utility_types::input_mouse::ViewportBounds;
24
use crate::messages::layout::utility_types::widget_prelude::*;
35
use crate::messages::prelude::*;
46

57
#[derive(ExtractField)]
68
pub struct DialogMessageContext<'a> {
79
pub portfolio: &'a PortfolioMessageHandler,
10+
pub viewport_bounds: &'a ViewportBounds,
811
pub preferences: &'a PreferencesMessageHandler,
912
}
1013

@@ -19,11 +22,15 @@ pub struct DialogMessageHandler {
1922
#[message_handler_data]
2023
impl MessageHandler<DialogMessage, DialogMessageContext<'_>> for DialogMessageHandler {
2124
fn process_message(&mut self, message: DialogMessage, responses: &mut VecDeque<Message>, context: DialogMessageContext) {
22-
let DialogMessageContext { portfolio, preferences } = context;
25+
let DialogMessageContext {
26+
portfolio,
27+
preferences,
28+
viewport_bounds,
29+
} = context;
2330

2431
match message {
2532
DialogMessage::ExportDialog(message) => self.export_dialog.process_message(message, responses, ExportDialogMessageContext { portfolio }),
26-
DialogMessage::NewDocumentDialog(message) => self.new_document_dialog.process_message(message, responses, ()),
33+
DialogMessage::NewDocumentDialog(message) => self.new_document_dialog.process_message(message, responses, NewDocumentDialogMessageContext { viewport_bounds }),
2734
DialogMessage::PreferencesDialog(message) => self.preferences_dialog.process_message(message, responses, PreferencesDialogMessageContext { preferences }),
2835

2936
DialogMessage::CloseAllDocumentsWithConfirmation => {

editor/src/messages/dialog/new_document_dialog/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ mod new_document_dialog_message_handler;
44
#[doc(inline)]
55
pub use new_document_dialog_message::{NewDocumentDialogMessage, NewDocumentDialogMessageDiscriminant};
66
#[doc(inline)]
7-
pub use new_document_dialog_message_handler::NewDocumentDialogMessageHandler;
7+
pub use new_document_dialog_message_handler::{NewDocumentDialogMessageContext, NewDocumentDialogMessageHandler};

editor/src/messages/dialog/new_document_dialog/new_document_dialog_message_handler.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
use crate::messages::layout::utility_types::widget_prelude::*;
21
use crate::messages::prelude::*;
2+
use crate::messages::{input_mapper::utility_types::input_mouse::ViewportBounds, layout::utility_types::widget_prelude::*};
33
use glam::{IVec2, UVec2};
44
use graph_craft::document::NodeId;
55

6+
#[derive(ExtractField)]
7+
pub struct NewDocumentDialogMessageContext<'a> {
8+
pub viewport_bounds: &'a ViewportBounds,
9+
}
10+
611
/// A dialog to allow users to set some initial options about a new document.
712
#[derive(Debug, Clone, Default, ExtractField)]
813
pub struct NewDocumentDialogMessageHandler {
@@ -12,8 +17,8 @@ pub struct NewDocumentDialogMessageHandler {
1217
}
1318

1419
#[message_handler_data]
15-
impl MessageHandler<NewDocumentDialogMessage, ()> for NewDocumentDialogMessageHandler {
16-
fn process_message(&mut self, message: NewDocumentDialogMessage, responses: &mut VecDeque<Message>, _: ()) {
20+
impl<'a> MessageHandler<NewDocumentDialogMessage, NewDocumentDialogMessageContext<'a>> for NewDocumentDialogMessageHandler {
21+
fn process_message(&mut self, message: NewDocumentDialogMessage, responses: &mut VecDeque<Message>, context: NewDocumentDialogMessageContext<'a>) {
1722
match message {
1823
NewDocumentDialogMessage::Name(name) => self.name = name,
1924
NewDocumentDialogMessage::Infinite(infinite) => self.infinite = infinite,
@@ -24,21 +29,18 @@ impl MessageHandler<NewDocumentDialogMessage, ()> for NewDocumentDialogMessageHa
2429

2530
let create_artboard = !self.infinite && self.dimensions.x > 0 && self.dimensions.y > 0;
2631
if create_artboard {
27-
responses.add(NodeGraphMessage::RunDocumentGraph);
28-
responses.add(DeferMessage::AfterGraphRun {
29-
messages: vec![
30-
GraphOperationMessage::NewArtboard {
31-
id: NodeId::new(),
32-
artboard: graphene_std::Artboard::new(IVec2::ZERO, self.dimensions.as_ivec2()),
33-
}
34-
.into(),
35-
NodeGraphMessage::ForceRunDocumentGraph.into(),
36-
DeferMessage::AfterGraphRun {
37-
messages: vec![DeferMessage::TriggerNavigationReady.into()],
38-
}
39-
.into(),
40-
],
32+
responses.add(GraphOperationMessage::NewArtboard {
33+
id: NodeId::new(),
34+
artboard: graphene_std::Artboard::new(IVec2::ZERO, self.dimensions.as_ivec2()),
4135
});
36+
responses.add(NavigationMessage::CanvasPan { delta: self.dimensions.as_dvec2() });
37+
responses.add(NodeGraphMessage::RunDocumentGraph);
38+
// If we already have bounds, we won't receive a viewport bounds update so we just fabricate one ourselves
39+
if *context.viewport_bounds != ViewportBounds::default() {
40+
responses.add(InputPreprocessorMessage::BoundsOfViewports {
41+
bounds_of_viewports: vec![context.viewport_bounds.clone()],
42+
});
43+
}
4244
responses.add(DeferMessage::AfterNavigationReady {
4345
messages: vec![DocumentMessage::ZoomCanvasToFitAll.into(), DocumentMessage::DeselectAllLayers.into()],
4446
});

editor/src/messages/input_preprocessor/input_preprocessor_message_handler.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ impl MessageHandler<InputPreprocessorMessage, InputPreprocessorMessageContext> f
3636
responses.add(NavigationMessage::CanvasPan { delta: DVec2::ZERO });
3737
responses.add(NodeGraphMessage::SetGridAlignedEdges);
3838
}
39+
responses.add(DeferMessage::AfterGraphRun {
40+
messages: vec![
41+
DeferMessage::AfterGraphRun {
42+
messages: vec![DeferMessage::TriggerNavigationReady.into()],
43+
}
44+
.into(),
45+
],
46+
});
3947
}
4048
InputPreprocessorMessage::DoubleClick { editor_mouse_state, modifier_keys } => {
4149
self.update_states_of_modifier_keys(modifier_keys, keyboard_platform, responses);

frontend/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
"profiling": "npm run setup && npm run wasm:build-profiling && concurrently -k -n \"VITE,RUST\" \"vite\" \"npm run wasm:watch-profiling\"",
1212
"production": "npm run setup && npm run wasm:build-production && concurrently -k -n \"VITE,RUST\" \"vite\" \"npm run wasm:watch-production\"",
1313
"---------- BUILDS ----------": "",
14+
"build": "npm run wasm:build-production && vite build",
1415
"build-dev": "npm run wasm:build-dev && vite build",
15-
"build-native": "npm run native:build-dev && vite build",
16+
"build-native": "npm run native:build-production && vite build",
17+
"build-native-dev": "npm run native:build-dev && vite build",
1618
"build-profiling": "npm run wasm:build-profiling && vite build",
17-
"build": "npm run wasm:build-production && vite build",
1819
"---------- UTILITIES ----------": "",
1920
"lint": "eslint . && tsc --noEmit",
2021
"lint-fix": "eslint . --fix && tsc --noEmit",
2122
"---------- INTERNAL ----------": "",
2223
"setup": "node package-installer.js",
2324
"native:build-dev": "wasm-pack build ./wasm --dev --target=web --features native",
25+
"native:build-production": "wasm-pack build ./wasm --release --target=web --features native",
2426
"wasm:build-dev": "wasm-pack build ./wasm --dev --target=web",
2527
"wasm:build-profiling": "wasm-pack build ./wasm --profiling --target=web",
2628
"wasm:build-production": "wasm-pack build ./wasm --release --target=web",

frontend/wasm/src/editor_api.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// on the dispatcher messaging system and more complex Rust data types.
66
//
77
use crate::helpers::translate_key;
8-
use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER};
9-
use editor::application::Editor;
8+
use crate::{EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER};
109
use editor::consts::FILE_SAVE_SUFFIX;
1110
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
1211
use editor::messages::input_mapper::utility_types::input_mouse::{EditorMouseState, ScrollDelta, ViewportBounds};
@@ -28,6 +27,11 @@ use wasm_bindgen::JsCast;
2827
use wasm_bindgen::prelude::*;
2928
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, ImageData, window};
3029

30+
#[cfg(not(feature = "native"))]
31+
use crate::EDITOR;
32+
#[cfg(not(feature = "native"))]
33+
use editor::application::Editor;
34+
3135
static IMAGE_DATA_HASH: AtomicU64 = AtomicU64::new(0);
3236

3337
fn calculate_hash<T: std::hash::Hash>(t: &T) -> u64 {
@@ -140,6 +144,7 @@ impl EditorHandle {
140144

141145
#[wasm_bindgen]
142146
impl EditorHandle {
147+
#[cfg(not(feature = "native"))]
143148
#[wasm_bindgen(constructor)]
144149
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
145150
let editor = Editor::new();
@@ -153,6 +158,16 @@ impl EditorHandle {
153158
editor_handle
154159
}
155160

161+
#[cfg(feature = "native")]
162+
#[wasm_bindgen(constructor)]
163+
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
164+
let editor_handle = EditorHandle { frontend_message_handler_callback };
165+
if EDITOR_HANDLE.with(|handle| handle.lock().ok().map(|mut guard| *guard = Some(editor_handle.clone()))).is_none() {
166+
log::error!("Attempted to initialize the editor handle more than once");
167+
}
168+
editor_handle
169+
}
170+
156171
// Sends a message to the dispatcher in the Editor Backend
157172
#[cfg(not(feature = "native"))]
158173
fn dispatch<T: Into<Message>>(&self, message: T) {
@@ -247,6 +262,7 @@ impl EditorHandle {
247262
let g = f.clone();
248263

249264
*g.borrow_mut() = Some(Closure::new(move |_timestamp| {
265+
#[cfg(not(feature = "native"))]
250266
wasm_bindgen_futures::spawn_local(poll_node_graph_evaluation());
251267

252268
if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
@@ -929,6 +945,7 @@ fn set_timeout(f: &Closure<dyn FnMut()>, delay: Duration) {
929945
}
930946

931947
/// Provides access to the `Editor` by calling the given closure with it as an argument.
948+
#[cfg(not(feature = "native"))]
932949
fn editor<T: Default>(callback: impl FnOnce(&mut editor::application::Editor) -> T) -> T {
933950
EDITOR.with(|editor| {
934951
let mut guard = editor.try_lock();
@@ -942,6 +959,7 @@ fn editor<T: Default>(callback: impl FnOnce(&mut editor::application::Editor) ->
942959
}
943960

944961
/// Provides access to the `Editor` and its `EditorHandle` by calling the given closure with them as arguments.
962+
#[cfg(not(feature = "native"))]
945963
pub(crate) fn editor_and_handle(callback: impl FnOnce(&mut Editor, &mut EditorHandle)) {
946964
handle(|editor_handle| {
947965
editor(|editor| {
@@ -964,6 +982,7 @@ pub(crate) fn handle(callback: impl FnOnce(&mut EditorHandle)) {
964982
});
965983
}
966984

985+
#[cfg(not(feature = "native"))]
967986
async fn poll_node_graph_evaluation() {
968987
// Process no further messages after a crash to avoid spamming the console
969988
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {

frontend/wasm/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub static NODE_GRAPH_ERROR_DISPLAYED: AtomicBool = AtomicBool::new(false);
2020
pub static LOGGER: WasmLog = WasmLog;
2121

2222
thread_local! {
23+
#[cfg(not(feature = "native"))]
2324
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
2425
pub static MESSAGE_BUFFER: std::cell::RefCell<Vec<Message>> = const { std::cell::RefCell::new(Vec::new()) };
2526
pub static EDITOR_HANDLE: Mutex<Option<editor_api::EditorHandle>> = const { Mutex::new(None) };
@@ -50,7 +51,7 @@ pub fn panic_hook(info: &panic::PanicHookInfo) {
5051

5152
if !NODE_GRAPH_ERROR_DISPLAYED.load(Ordering::SeqCst) {
5253
NODE_GRAPH_ERROR_DISPLAYED.store(true, Ordering::SeqCst);
53-
editor_api::editor_and_handle(|_, handle| {
54+
editor_api::handle(|handle| {
5455
let error = r#"
5556
<rect x="50%" y="50%" width="600" height="100" transform="translate(-300 -50)" rx="4" fill="var(--color-error-red)" />
5657
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="18" fill="var(--color-2-mildblack)">

frontend/wasm/src/native_communcation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use editor::{application::Editor, messages::prelude::FrontendMessage};
1+
use editor::messages::prelude::FrontendMessage;
22
use js_sys::{ArrayBuffer, Uint8Array};
33
use wasm_bindgen::prelude::*;
44

@@ -9,12 +9,12 @@ pub fn receive_native_message(buffer: ArrayBuffer) {
99
let buffer = Uint8Array::new(buffer.as_ref()).to_vec();
1010
match ron::from_str::<Vec<FrontendMessage>>(str::from_utf8(buffer.as_slice()).unwrap()) {
1111
Ok(messages) => {
12-
let callback = move |_: &mut Editor, handle: &mut EditorHandle| {
12+
let callback = move |handle: &mut EditorHandle| {
1313
for message in messages {
1414
handle.send_frontend_message_to_js_rust_proxy(message);
1515
}
1616
};
17-
editor_api::editor_and_handle(callback);
17+
editor_api::handle(callback);
1818
}
1919
Err(e) => log::error!("Failed to deserialize frontend messages: {e:?}"),
2020
}

0 commit comments

Comments
 (0)