mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-07-24 16:13:44 +00:00

* Node graph API stub * Rename and fix SetInputValue * Get list of links from network * Test populating node graph UI * Node properties * Fix viewport bounds * Slightly change promise usage * A tiny bit of cleanup I did while reading code * Cleanup and work towards hooking up node links in Vue template * Add the brighten colour node * Run cargo fmt * Add to and from hsla * GrayscaleImage node with small perf improvement * Fix gutter panel resizing * Display node links from backend * Add support for connecting node links * Use existing message * Fix formatting error * Add a (currently crashing) brighten node * Replace brighten node with proto node implementation * Add support for connecting node links * Update watch dirs * Add hue shift node * Add create_node function to editor api * Basic insert node UI * Fix broken names * Add log * Fix positioning * Set connector index to 0 * Add properties for Heu shift / brighten * Allow deselecting nodes * Redesign Properties panel collapsible sections Co-authored-by: Keavon Chambers <keavon@keavon.com> Co-authored-by: Dennis Kobert <dennis@kobert.dev>
632 lines
24 KiB
Rust
632 lines
24 KiB
Rust
//! This file is where functions are defined to be called directly from JS.
|
|
//! It serves as a thin wrapper over the editor backend API that relies
|
|
//! on the dispatcher messaging system and more complex Rust data types.
|
|
|
|
use crate::helpers::{translate_key, Error};
|
|
use crate::{EDITOR_HAS_CRASHED, EDITOR_INSTANCES, JS_EDITOR_HANDLES};
|
|
|
|
use editor::application::generate_uuid;
|
|
use editor::application::Editor;
|
|
use editor::consts::{FILE_SAVE_SUFFIX, GRAPHITE_DOCUMENT_VERSION};
|
|
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
|
|
use editor::messages::input_mapper::utility_types::input_mouse::{EditorMouseState, ScrollDelta, ViewportBounds};
|
|
use editor::messages::portfolio::utility_types::{ImaginateServerStatus, Platform};
|
|
use editor::messages::prelude::*;
|
|
use graphene::color::Color;
|
|
use graphene::layers::imaginate_layer::ImaginateStatus;
|
|
use graphene::LayerId;
|
|
|
|
use serde::Serialize;
|
|
use serde_wasm_bindgen::{self, from_value};
|
|
use std::sync::atomic::Ordering;
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
/// Set the random seed used by the editor by calling this from JS upon initialization.
|
|
/// This is necessary because WASM doesn't have a random number generator.
|
|
#[wasm_bindgen(js_name = setRandomSeed)]
|
|
pub fn set_random_seed(seed: u64) {
|
|
editor::application::set_uuid_seed(seed);
|
|
}
|
|
|
|
/// We directly interface with the updateImage JS function for massively increased performance over serializing and deserializing.
|
|
/// This avoids creating a json with a list millions of numbers long.
|
|
#[wasm_bindgen(module = "@/wasm-communication/editor")]
|
|
extern "C" {
|
|
fn updateImage(path: Vec<u64>, mime: String, imageData: &[u8], document_id: u64);
|
|
}
|
|
|
|
/// Provides a handle to access the raw WASM memory
|
|
#[wasm_bindgen(js_name = wasmMemory)]
|
|
pub fn wasm_memory() -> JsValue {
|
|
wasm_bindgen::memory()
|
|
}
|
|
|
|
// To avoid wasm-bindgen from checking mutable reference issues using WasmRefCell we must make all methods take a non mutable reference to self.
|
|
// Not doing this creates an issue when rust calls into JS which calls back to rust in the same call stack.
|
|
#[wasm_bindgen]
|
|
#[derive(Clone)]
|
|
pub struct JsEditorHandle {
|
|
editor_id: u64,
|
|
frontend_message_handler_callback: js_sys::Function,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
#[allow(clippy::too_many_arguments)]
|
|
impl JsEditorHandle {
|
|
#[wasm_bindgen(constructor)]
|
|
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
|
|
let editor_id = generate_uuid();
|
|
let editor = Editor::new();
|
|
let editor_handle = JsEditorHandle {
|
|
editor_id,
|
|
frontend_message_handler_callback,
|
|
};
|
|
EDITOR_INSTANCES.with(|instances| instances.borrow_mut().insert(editor_id, editor));
|
|
JS_EDITOR_HANDLES.with(|instances| instances.borrow_mut().insert(editor_id, editor_handle.clone()));
|
|
editor_handle
|
|
}
|
|
|
|
// Sends a message to the dispatcher in the Editor Backend
|
|
fn dispatch<T: Into<Message>>(&self, message: T) {
|
|
// Process no further messages after a crash to avoid spamming the console
|
|
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
|
return;
|
|
}
|
|
|
|
// Get the editor instances, dispatch the message, and store the `FrontendMessage` queue response
|
|
let frontend_messages = EDITOR_INSTANCES.with(|instances| {
|
|
// Mutably borrow the editors, and if successful, we can access them in the closure
|
|
instances.try_borrow_mut().map(|mut editors| {
|
|
// Get the editor instance for this editor ID, then dispatch the message to the backend, and return its response `FrontendMessage` queue
|
|
editors
|
|
.get_mut(&self.editor_id)
|
|
.expect("EDITOR_INSTANCES does not contain the current editor_id")
|
|
.handle_message(message.into())
|
|
})
|
|
});
|
|
|
|
// Process any `FrontendMessage` responses resulting from the backend processing the dispatched message
|
|
if let Ok(frontend_messages) = frontend_messages {
|
|
// Send each `FrontendMessage` to the JavaScript frontend
|
|
for message in frontend_messages.into_iter() {
|
|
self.send_frontend_message_to_js(message);
|
|
}
|
|
}
|
|
// If the editor cannot be borrowed then it has encountered a panic - we should just ignore new dispatches
|
|
}
|
|
|
|
// Sends a FrontendMessage to JavaScript
|
|
fn send_frontend_message_to_js(&self, message: FrontendMessage) {
|
|
// Special case for update image data to avoid serialization times.
|
|
if let FrontendMessage::UpdateImageData { document_id, image_data } = message {
|
|
for image in image_data {
|
|
updateImage(image.path, image.mime, &image.image_data, document_id);
|
|
}
|
|
return;
|
|
}
|
|
|
|
let message_type = message.to_discriminant().local_name();
|
|
|
|
let serializer = serde_wasm_bindgen::Serializer::new().serialize_large_number_types_as_bigints(true);
|
|
let message_data = message.serialize(&serializer).expect("Failed to serialize FrontendMessage");
|
|
|
|
let js_return_value = self.frontend_message_handler_callback.call2(&JsValue::null(), &JsValue::from(message_type), &message_data);
|
|
|
|
if let Err(error) = js_return_value {
|
|
error!(
|
|
"While handling FrontendMessage \"{:?}\", JavaScript threw an error: {:?}",
|
|
message.to_discriminant().local_name(),
|
|
error,
|
|
)
|
|
}
|
|
}
|
|
|
|
// ========================================================================
|
|
// Add additional JS -> Rust wrapper functions below as needed for calling
|
|
// the backend from the web frontend.
|
|
// ========================================================================
|
|
|
|
#[wasm_bindgen(js_name = initAfterFrontendReady)]
|
|
pub fn init_after_frontend_ready(&self, platform: String) {
|
|
let platform = match platform.as_str() {
|
|
"Windows" => Platform::Windows,
|
|
"Mac" => Platform::Mac,
|
|
"Linux" => Platform::Linux,
|
|
_ => Platform::Unknown,
|
|
};
|
|
|
|
self.dispatch(GlobalsMessage::SetPlatform { platform });
|
|
self.dispatch(Message::Init);
|
|
}
|
|
|
|
/// Displays a dialog with an error message
|
|
#[wasm_bindgen(js_name = errorDialog)]
|
|
pub fn error_dialog(&self, title: String, description: String) {
|
|
let message = DialogMessage::DisplayDialogError { title, description };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Answer whether or not the editor has crashed
|
|
#[wasm_bindgen(js_name = hasCrashed)]
|
|
pub fn has_crashed(&self) -> bool {
|
|
EDITOR_HAS_CRASHED.load(Ordering::SeqCst)
|
|
}
|
|
|
|
/// Answer whether or not the editor is in development mode
|
|
#[wasm_bindgen(js_name = inDevelopmentMode)]
|
|
pub fn in_development_mode(&self) -> bool {
|
|
cfg!(debug_assertions)
|
|
}
|
|
|
|
/// Get the constant `FILE_SAVE_SUFFIX`
|
|
#[wasm_bindgen(js_name = fileSaveSuffix)]
|
|
pub fn file_save_suffix(&self) -> String {
|
|
FILE_SAVE_SUFFIX.into()
|
|
}
|
|
|
|
/// Get the constant `GRAPHITE_DOCUMENT_VERSION`
|
|
#[wasm_bindgen(js_name = graphiteDocumentVersion)]
|
|
pub fn graphite_document_version(&self) -> String {
|
|
GRAPHITE_DOCUMENT_VERSION.to_string()
|
|
}
|
|
|
|
/// Update layout of a given UI
|
|
#[wasm_bindgen(js_name = updateLayout)]
|
|
pub fn update_layout(&self, layout_target: JsValue, widget_id: u64, value: JsValue) -> Result<(), JsValue> {
|
|
match (from_value(layout_target), from_value(value)) {
|
|
(Ok(layout_target), Ok(value)) => {
|
|
let message = LayoutMessage::UpdateLayout { layout_target, widget_id, value };
|
|
self.dispatch(message);
|
|
Ok(())
|
|
}
|
|
_ => Err(Error::new("Could not update UI").into()),
|
|
}
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = loadPreferences)]
|
|
pub fn load_preferences(&self, preferences: String) {
|
|
let message = PreferencesMessage::Load { preferences };
|
|
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = selectDocument)]
|
|
pub fn select_document(&self, document_id: u64) {
|
|
let message = PortfolioMessage::SelectDocument { document_id };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = newDocumentDialog)]
|
|
pub fn new_document_dialog(&self) {
|
|
let message = DialogMessage::RequestNewDocumentDialog;
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = documentOpen)]
|
|
pub fn document_open(&self) {
|
|
let message = PortfolioMessage::OpenDocument;
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = openDocumentFile)]
|
|
pub fn open_document_file(&self, document_name: String, document_serialized_content: String) {
|
|
let message = PortfolioMessage::OpenDocumentFile {
|
|
document_name,
|
|
document_serialized_content,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = openAutoSavedDocument)]
|
|
pub fn open_auto_saved_document(&self, document_id: u64, document_name: String, document_is_saved: bool, document_serialized_content: String) {
|
|
let message = PortfolioMessage::OpenDocumentFileWithId {
|
|
document_id,
|
|
document_name,
|
|
document_is_auto_saved: true,
|
|
document_is_saved,
|
|
document_serialized_content,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = triggerAutoSave)]
|
|
pub fn trigger_auto_save(&self, document_id: u64) {
|
|
let message = PortfolioMessage::AutoSaveDocument { document_id };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = closeDocumentWithConfirmation)]
|
|
pub fn close_document_with_confirmation(&self, document_id: u64) {
|
|
let message = PortfolioMessage::CloseDocumentWithConfirmation { document_id };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
#[wasm_bindgen(js_name = requestAboutGraphiteDialogWithLocalizedCommitDate)]
|
|
pub fn request_about_graphite_dialog_with_localized_commit_date(&self, localized_commit_date: String) {
|
|
let message = DialogMessage::RequestAboutGraphiteDialogWithLocalizedCommitDate { localized_commit_date };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Send new bounds when document panel viewports get resized or moved within the editor
|
|
/// [left, top, right, bottom]...
|
|
#[wasm_bindgen(js_name = boundsOfViewports)]
|
|
pub fn bounds_of_viewports(&self, bounds_of_viewports: &[f64]) {
|
|
let chunked: Vec<_> = bounds_of_viewports.chunks(4).map(ViewportBounds::from_slice).collect();
|
|
|
|
let message = InputPreprocessorMessage::BoundsOfViewports { bounds_of_viewports: chunked };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Mouse movement within the screenspace bounds of the viewport
|
|
#[wasm_bindgen(js_name = onMouseMove)]
|
|
pub fn on_mouse_move(&self, x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
|
|
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
|
|
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
let message = InputPreprocessorMessage::PointerMove { editor_mouse_state, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Mouse scrolling within the screenspace bounds of the viewport
|
|
#[wasm_bindgen(js_name = onWheelScroll)]
|
|
pub fn on_wheel_scroll(&self, x: f64, y: f64, mouse_keys: u8, wheel_delta_x: i32, wheel_delta_y: i32, wheel_delta_z: i32, modifiers: u8) {
|
|
let mut editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
|
|
editor_mouse_state.scroll_delta = ScrollDelta::new(wheel_delta_x, wheel_delta_y, wheel_delta_z);
|
|
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
let message = InputPreprocessorMessage::WheelScroll { editor_mouse_state, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// A mouse button depressed within screenspace the bounds of the viewport
|
|
#[wasm_bindgen(js_name = onMouseDown)]
|
|
pub fn on_mouse_down(&self, x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
|
|
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
|
|
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
let message = InputPreprocessorMessage::PointerDown { editor_mouse_state, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// A mouse button released
|
|
#[wasm_bindgen(js_name = onMouseUp)]
|
|
pub fn on_mouse_up(&self, x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
|
|
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
|
|
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
let message = InputPreprocessorMessage::PointerUp { editor_mouse_state, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Mouse double clicked
|
|
#[wasm_bindgen(js_name = onDoubleClick)]
|
|
pub fn on_double_click(&self, x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
|
|
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
let message = InputPreprocessorMessage::DoubleClick { editor_mouse_state, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// A keyboard button depressed within screenspace the bounds of the viewport
|
|
#[wasm_bindgen(js_name = onKeyDown)]
|
|
pub fn on_key_down(&self, name: String, modifiers: u8) {
|
|
let key = translate_key(&name);
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
trace!("Key down {:?}, name: {}, modifiers: {:?}", key, name, modifiers);
|
|
|
|
let message = InputPreprocessorMessage::KeyDown { key, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// A keyboard button released
|
|
#[wasm_bindgen(js_name = onKeyUp)]
|
|
pub fn on_key_up(&self, name: String, modifiers: u8) {
|
|
let key = translate_key(&name);
|
|
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
|
|
|
trace!("Key up {:?}, name: {}, modifiers: {:?}", key, name, modifier_keys);
|
|
|
|
let message = InputPreprocessorMessage::KeyUp { key, modifier_keys };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// A text box was committed
|
|
#[wasm_bindgen(js_name = onChangeText)]
|
|
pub fn on_change_text(&self, new_text: String) -> Result<(), JsValue> {
|
|
let message = TextToolMessage::TextChange { new_text };
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// A font has been downloaded
|
|
#[wasm_bindgen(js_name = onFontLoad)]
|
|
pub fn on_font_load(&self, font_family: String, font_style: String, preview_url: String, data: Vec<u8>, is_default: bool) -> Result<(), JsValue> {
|
|
let message = PortfolioMessage::FontLoaded {
|
|
font_family,
|
|
font_style,
|
|
preview_url,
|
|
data,
|
|
is_default,
|
|
};
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// A text box was changed
|
|
#[wasm_bindgen(js_name = updateBounds)]
|
|
pub fn update_bounds(&self, new_text: String) -> Result<(), JsValue> {
|
|
let message = TextToolMessage::UpdateBounds { new_text };
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Begin sampling a pixel color from the document by entering eyedropper sampling mode
|
|
#[wasm_bindgen(js_name = eyedropperSampleForColorPicker)]
|
|
pub fn eyedropper_sample_for_color_picker(&self) -> Result<(), JsValue> {
|
|
let message = DialogMessage::RequestComingSoonDialog { issue: Some(832) };
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Update primary color with values on a scale from 0 to 1.
|
|
#[wasm_bindgen(js_name = updatePrimaryColor)]
|
|
pub fn update_primary_color(&self, red: f32, green: f32, blue: f32, alpha: f32) -> Result<(), JsValue> {
|
|
let primary_color = match Color::from_rgbaf32(red, green, blue, alpha) {
|
|
Some(color) => color,
|
|
None => return Err(Error::new("Invalid color").into()),
|
|
};
|
|
|
|
let message = ToolMessage::SelectPrimaryColor { color: primary_color };
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Update secondary color with values on a scale from 0 to 1.
|
|
#[wasm_bindgen(js_name = updateSecondaryColor)]
|
|
pub fn update_secondary_color(&self, red: f32, green: f32, blue: f32, alpha: f32) -> Result<(), JsValue> {
|
|
let secondary_color = match Color::from_rgbaf32(red, green, blue, alpha) {
|
|
Some(color) => color,
|
|
None => return Err(Error::new("Invalid color").into()),
|
|
};
|
|
|
|
let message = ToolMessage::SelectSecondaryColor { color: secondary_color };
|
|
self.dispatch(message);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Paste layers from a serialized json representation
|
|
#[wasm_bindgen(js_name = pasteSerializedData)]
|
|
pub fn paste_serialized_data(&self, data: String) {
|
|
let message = PortfolioMessage::PasteSerializedData { data };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Modify the layer selection based on the layer which is clicked while holding down the <kbd>Ctrl</kbd> and/or <kbd>Shift</kbd> modifier keys used for range selection behavior
|
|
#[wasm_bindgen(js_name = selectLayer)]
|
|
pub fn select_layer(&self, layer_path: Vec<LayerId>, ctrl: bool, shift: bool) {
|
|
let message = DocumentMessage::SelectLayer { layer_path, ctrl, shift };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Deselect all layers
|
|
#[wasm_bindgen(js_name = deselectAllLayers)]
|
|
pub fn deselect_all_layers(&self) {
|
|
let message = DocumentMessage::DeselectAllLayers;
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Move a layer to be next to the specified neighbor
|
|
#[wasm_bindgen(js_name = moveLayerInTree)]
|
|
pub fn move_layer_in_tree(&self, folder_path: Vec<LayerId>, insert_index: isize) {
|
|
let message = DocumentMessage::MoveSelectedLayersTo {
|
|
folder_path,
|
|
insert_index,
|
|
reverse_index: true,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Set the name for the layer
|
|
#[wasm_bindgen(js_name = setLayerName)]
|
|
pub fn set_layer_name(&self, layer_path: Vec<LayerId>, name: String) {
|
|
let message = DocumentMessage::SetLayerName { layer_path, name };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Translates document (in viewport coords)
|
|
#[wasm_bindgen(js_name = translateCanvas)]
|
|
pub fn translate_canvas(&self, delta_x: f64, delta_y: f64) {
|
|
let message = NavigationMessage::TranslateCanvas { delta: (delta_x, delta_y).into() };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Translates document (in viewport coords)
|
|
#[wasm_bindgen(js_name = translateCanvasByFraction)]
|
|
pub fn translate_canvas_by_fraction(&self, delta_x: f64, delta_y: f64) {
|
|
let message = NavigationMessage::TranslateCanvasByViewportFraction { delta: (delta_x, delta_y).into() };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Sends the blob URL generated by JS to the Image layer
|
|
#[wasm_bindgen(js_name = setImageBlobURL)]
|
|
pub fn set_image_blob_url(&self, document_id: u64, layer_path: Vec<LayerId>, blob_url: String, width: f64, height: f64) {
|
|
let resolution = (width, height);
|
|
let message = PortfolioMessage::SetImageBlobUrl {
|
|
document_id,
|
|
layer_path,
|
|
blob_url,
|
|
resolution,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Sends the blob URL generated by JS to the Imaginate layer in the respective document
|
|
#[wasm_bindgen(js_name = setImaginateImageData)]
|
|
pub fn set_imaginate_image_data(&self, document_id: u64, layer_path: Vec<LayerId>, image_data: Vec<u8>) {
|
|
let message = PortfolioMessage::ImaginateSetImageData { document_id, layer_path, image_data };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Sends the blob URL generated by JS to the Imaginate layer in the respective document
|
|
#[wasm_bindgen(js_name = setImaginateBlobURL)]
|
|
pub fn set_imaginate_blob_url(&self, document_id: u64, layer_path: Vec<LayerId>, blob_url: String, width: f64, height: f64) {
|
|
let resolution = (width, height);
|
|
let message = PortfolioMessage::ImaginateSetBlobUrl {
|
|
document_id,
|
|
layer_path,
|
|
blob_url,
|
|
resolution,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Notifies the Imaginate layer of a new percentage of completion and whether or not it's currently generating
|
|
#[wasm_bindgen(js_name = setImaginateGeneratingStatus)]
|
|
pub fn set_imaginate_generating_status(&self, document_id: u64, path: Vec<LayerId>, percent: Option<f64>, status: String) {
|
|
let status = match status.as_str() {
|
|
"Idle" => ImaginateStatus::Idle,
|
|
"Beginning" => ImaginateStatus::Beginning,
|
|
"Uploading" => ImaginateStatus::Uploading(percent.expect("Percent needs to be supplied to set ImaginateStatus::Uploading")),
|
|
"Generating" => ImaginateStatus::Generating,
|
|
"Terminating" => ImaginateStatus::Terminating,
|
|
"Terminated" => ImaginateStatus::Terminated,
|
|
_ => panic!("Invalid string from JS for ImaginateStatus, received: {}", status),
|
|
};
|
|
|
|
let percent = if matches!(status, ImaginateStatus::Uploading(_)) { None } else { percent };
|
|
|
|
let message = PortfolioMessage::ImaginateSetGeneratingStatus { document_id, path, percent, status };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Notifies the editor that the Imaginate server is available or unavailable
|
|
#[wasm_bindgen(js_name = setImaginateServerStatus)]
|
|
pub fn set_imaginate_server_status(&self, available: bool) {
|
|
let message: Message = match available {
|
|
true => PortfolioMessage::ImaginateSetServerStatus {
|
|
status: ImaginateServerStatus::Connected,
|
|
}
|
|
.into(),
|
|
false => PortfolioMessage::ImaginateSetServerStatus {
|
|
status: ImaginateServerStatus::Unavailable,
|
|
}
|
|
.into(),
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Sends the blob URL generated by JS to the Imaginate layer in the respective document
|
|
#[wasm_bindgen(js_name = processNodeGraphFrame)]
|
|
pub fn process_node_graph_frame(&self, document_id: u64, layer_path: Vec<LayerId>, image_data: Vec<u8>, width: u32, height: u32) {
|
|
let message = PortfolioMessage::ProcessNodeGraphFrame {
|
|
document_id,
|
|
layer_path,
|
|
image_data,
|
|
size: (width, height),
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Notifies the backend that the user connected a node's primary output to one of another node's inputs
|
|
#[wasm_bindgen(js_name = connectNodesByLink)]
|
|
pub fn connect_nodes_by_link(&self, output_node: u64, input_node: u64, input_node_connector_index: u32) {
|
|
let message = NodeGraphMessage::ConnectNodesByLink {
|
|
output_node,
|
|
input_node,
|
|
input_node_connector_index,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Creates a new document node in the node graph
|
|
#[wasm_bindgen(js_name = createNode)]
|
|
pub fn create_node(&self, node_type: String) {
|
|
use graph_craft::proto::{NodeIdentifier, Type};
|
|
use std::borrow::Cow;
|
|
|
|
fn generate_node_id() -> u64 {
|
|
static mut NODE_ID: u64 = 10;
|
|
unsafe {
|
|
NODE_ID += 1;
|
|
NODE_ID
|
|
}
|
|
}
|
|
|
|
let (ident, args) = match node_type.as_str() {
|
|
"Identity" => (NodeIdentifier::new("graphene_core::ops::IdNode", &[Type::Concrete(Cow::Borrowed("Any<'_>"))]), 1),
|
|
"Grayscale Color" => (NodeIdentifier::new("graphene_core::ops::IdNode", &[Type::Concrete(Cow::Borrowed("Any<'_>"))]), 1),
|
|
"Brighten Color" => (NodeIdentifier::new("graphene_core::ops::IdNode", &[Type::Concrete(Cow::Borrowed("Any<'_>"))]), 1),
|
|
"Hue Shift Color" => (NodeIdentifier::new("graphene_core::ops::IdNode", &[Type::Concrete(Cow::Borrowed("Any<'_>"))]), 1),
|
|
"Add" => (
|
|
NodeIdentifier::new("graphene_core::ops::AddNode", &[Type::Concrete(Cow::Borrowed("u32")), Type::Concrete(Cow::Borrowed("u32"))]),
|
|
2,
|
|
),
|
|
"Map Image" => (NodeIdentifier::new("graphene_std::raster::MapImageNode", &[]), 2),
|
|
_ => panic!("Invalid node type: {}", node_type),
|
|
};
|
|
|
|
let message = NodeGraphMessage::CreateNode {
|
|
node_id: generate_node_id(),
|
|
name: node_type,
|
|
identifier: ident,
|
|
num_inputs: args,
|
|
};
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Notifies the backend that the user selected a node in the node graph
|
|
#[wasm_bindgen(js_name = selectNodes)]
|
|
pub fn select_nodes(&self, nodes: Vec<u64>) {
|
|
let message = NodeGraphMessage::SelectNodes { nodes };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Pastes an image
|
|
#[wasm_bindgen(js_name = pasteImage)]
|
|
pub fn paste_image(&self, mime: String, image_data: Vec<u8>, mouse_x: Option<f64>, mouse_y: Option<f64>) {
|
|
let mouse = mouse_x.and_then(|x| mouse_y.map(|y| (x, y)));
|
|
let message = DocumentMessage::PasteImage { mime, image_data, mouse };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Toggle visibility of a layer from the layer list
|
|
#[wasm_bindgen(js_name = toggleLayerVisibility)]
|
|
pub fn toggle_layer_visibility(&self, layer_path: Vec<LayerId>) {
|
|
let message = DocumentMessage::ToggleLayerVisibility { layer_path };
|
|
self.dispatch(message);
|
|
}
|
|
|
|
/// Toggle expansions state of a layer from the layer list
|
|
#[wasm_bindgen(js_name = toggleLayerExpansion)]
|
|
pub fn toggle_layer_expansion(&self, layer_path: Vec<LayerId>) {
|
|
let message = DocumentMessage::ToggleLayerExpansion { layer_path };
|
|
self.dispatch(message);
|
|
}
|
|
}
|
|
|
|
// Needed to make JsEditorHandle functions pub to Rust.
|
|
// The reason is not fully clear but it has to do with the #[wasm_bindgen] procedural macro.
|
|
impl JsEditorHandle {
|
|
pub fn send_frontend_message_to_js_rust_proxy(&self, message: FrontendMessage) {
|
|
self.send_frontend_message_to_js(message);
|
|
}
|
|
}
|
|
|
|
impl Drop for JsEditorHandle {
|
|
fn drop(&mut self) {
|
|
// Consider removing after https://github.com/rustwasm/wasm-bindgen/pull/2984 is merged and released
|
|
EDITOR_INSTANCES.with(|instances| instances.borrow_mut().remove(&self.editor_id));
|
|
}
|
|
}
|