mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 07:24:56 -10:00
Compare commits
8
Commits
spike
...
async-refactor2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99f94503c3 | ||
|
|
925075337b | ||
|
|
df70222d61 | ||
|
|
1f9e0cc37b | ||
|
|
eb50be844b | ||
|
|
0f0f62c2e9 | ||
|
|
f5b1fee85f | ||
|
|
6f8c4b2adb |
No files matched your search
Generated
+5
-4
@@ -2312,9 +2312,9 @@ dependencies = [
|
||||
"graph-craft",
|
||||
"graphene-std",
|
||||
"graphite-editor",
|
||||
"graphite-wasm-wrapper",
|
||||
"image",
|
||||
"keyboard-types",
|
||||
"ron",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.18",
|
||||
@@ -2374,6 +2374,7 @@ dependencies = [
|
||||
name = "graphite-proc-macros"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"graphite-editor",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -2385,17 +2386,17 @@ dependencies = [
|
||||
name = "graphite-wasm-wrapper"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"graph-craft",
|
||||
"graphene-std",
|
||||
"graphite-editor",
|
||||
"graphite-proc-macros",
|
||||
"js-sys",
|
||||
"log",
|
||||
"node-macro",
|
||||
"ron",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"serde_bytes",
|
||||
"serde_json",
|
||||
"tsify",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
|
||||
+1
-2
@@ -14,13 +14,12 @@ use winit::event::{ButtonSource, ElementState, MouseButton, StartCause, WindowEv
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||
use winit::window::WindowId;
|
||||
|
||||
use graphite_desktop_ui::{UiCommand, UiInstance};
|
||||
|
||||
use crate::dirs;
|
||||
use crate::event::{AppEvent, AppEventScheduler};
|
||||
use crate::persist;
|
||||
use crate::preferences;
|
||||
use crate::render::{RenderError, RenderState};
|
||||
use crate::ui::{UiCommand, UiInstance};
|
||||
use crate::window::Window;
|
||||
use crate::wrapper::messages::{DesktopFrontendMessage, DesktopWrapperMessage, InputMessage, MouseKeys, MouseState, Preferences};
|
||||
use crate::wrapper::{DesktopWrapper, MmapResourceStorage, NodeGraphExecutionResult, WgpuContext, serialize_frontend_messages};
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use crate::ui::Cursor;
|
||||
use crate::wrapper::NodeGraphExecutionResult;
|
||||
use crate::wrapper::messages::DesktopWrapperMessage;
|
||||
|
||||
pub(crate) enum AppEvent {
|
||||
UiUpdate(wgpu::Texture),
|
||||
CursorChange(graphite_desktop_ui::Cursor),
|
||||
CursorChange(Cursor),
|
||||
WebCommunicationInitialized,
|
||||
DesktopWrapperMessage(DesktopWrapperMessage),
|
||||
NodeGraphExecutionResult(NodeGraphExecutionResult),
|
||||
|
||||
+3
-2
@@ -3,12 +3,13 @@ use crate::cli::Cli;
|
||||
use crate::consts::APP_LOCK_FILE_NAME;
|
||||
use crate::event::{AppEvent, CreateAppEventSchedulerEventLoopExt};
|
||||
use clap::Parser;
|
||||
use graphite_desktop_ui::{Acceleration, UiConfig, UiContext, UiEvent, UiSetupResult};
|
||||
use std::io::Write;
|
||||
use std::process::ExitCode;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use ui::{Acceleration, UiConfig, UiContext, UiEvent, UiSetupResult};
|
||||
use winit::event_loop::EventLoop;
|
||||
|
||||
pub(crate) use graphite_desktop_ui as ui;
|
||||
pub(crate) use graphite_desktop_wrapper as wrapper;
|
||||
|
||||
mod app;
|
||||
@@ -69,7 +70,7 @@ pub fn start() -> ExitCode {
|
||||
}
|
||||
};
|
||||
|
||||
dirs::clear_dir(&graphite_desktop_ui::temp_dir_root());
|
||||
dirs::clear_dir(&ui::temp_dir_root());
|
||||
|
||||
// TODO: Eventually remove this cleanup code for the old "browser" CEF directory
|
||||
dirs::delete_old_cef_browser_directory();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::consts::APP_NAME;
|
||||
use crate::event::AppEventScheduler;
|
||||
use crate::ui::Cursor;
|
||||
use crate::wrapper::messages::MenuItem;
|
||||
use crate::wrapper::{WgpuInstance, WgpuSurface};
|
||||
use graphite_desktop_ui::Cursor;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use winit::cursor::{CustomCursor, CustomCursorSource};
|
||||
|
||||
@@ -14,6 +14,7 @@ gpu = ["graphite-editor/gpu", "graphene-std/shader-nodes"]
|
||||
[dependencies]
|
||||
# Local dependencies
|
||||
graphite-editor = { workspace = true }
|
||||
graphite-wasm-wrapper = { path = "../../frontend/wrapper", default-features = false, features = ["editor"] }
|
||||
graphene-std = { workspace = true }
|
||||
graph-craft = { workspace = true }
|
||||
wgpu-executor = { workspace = true }
|
||||
@@ -22,7 +23,6 @@ wgpu = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
dirs = { workspace = true }
|
||||
ron = { workspace = true}
|
||||
vello = { workspace = true }
|
||||
image = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use graph_craft::application_io::PlatformApplicationIo;
|
||||
use graph_craft::application_io::resource::ResourceStorage;
|
||||
use graphite_editor::application::{Editor, Environment, Host, Platform};
|
||||
use graphite_editor::messages::prelude::{FrontendMessage, Message, Wake};
|
||||
use graphite_editor::messages::frontend::FrontendMessage;
|
||||
use graphite_editor::messages::prelude::Wake;
|
||||
|
||||
use message_dispatcher::DesktopWrapperMessageDispatcher;
|
||||
use messages::{DesktopFrontendMessage, DesktopWrapperMessage};
|
||||
use std::sync::Arc;
|
||||
@@ -65,21 +67,10 @@ pub enum NodeGraphExecutionResult {
|
||||
}
|
||||
|
||||
pub fn deserialize_editor_message(data: &[u8]) -> Option<DesktopWrapperMessage> {
|
||||
if let Ok(string) = std::str::from_utf8(data) {
|
||||
if let Ok(message) = ron::de::from_str::<Message>(string) {
|
||||
Some(DesktopWrapperMessage::FromWeb(message.into()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let message = graphite_wasm_wrapper::native_communication::decode_editor_command(data)?;
|
||||
Some(DesktopWrapperMessage::FromWeb(message.into()))
|
||||
}
|
||||
|
||||
pub fn serialize_frontend_messages(messages: Vec<FrontendMessage>) -> Option<Vec<u8>> {
|
||||
if let Ok(serialized) = ron::ser::to_string(&messages) {
|
||||
Some(serialized.into_bytes())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
graphite_wasm_wrapper::native_communication::encode_frontend_messages(messages)
|
||||
}
|
||||
@@ -45,7 +45,7 @@ impl<'de> Deserialize<'de> for ResourceEntry {
|
||||
}
|
||||
|
||||
let Raw { mut sources, hash, hash_timestamp } = Raw::deserialize(deserializer)?;
|
||||
sources.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||
sources.sort_by_key(|(a, _)| *a);
|
||||
sources.dedup_by(|(later_key, later_value), (kept_key, kept_value)| {
|
||||
// `dedup_by` keeps the first of each run; sorting is stable, so resolve duplicates by LWW.
|
||||
if later_key != kept_key {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::IconName;
|
||||
use super::utility_types::{MouseCursorIcon, PersistedState};
|
||||
use crate::messages::app_window::app_window_message_handler::AppWindowPlatform;
|
||||
use crate::messages::frontend::utility_types::{DocumentInfo, EyedropperPreviewImage};
|
||||
use crate::messages::frontend::utility_types::{DocumentInfo, EyedropperPreviewImage, RasterizedImage};
|
||||
use crate::messages::input_mapper::utility_types::misc::ActionShortcut;
|
||||
use crate::messages::layout::utility_types::widget_prelude::*;
|
||||
use crate::messages::portfolio::document::node_graph::utility_types::{
|
||||
@@ -14,7 +14,6 @@ use crate::messages::prelude::*;
|
||||
use crate::messages::tool::tool_messages::eyedropper_tool::PrimarySecondary;
|
||||
use graph_craft::document::NodeId;
|
||||
use graphene_std::color::SRGBA8;
|
||||
use graphene_std::raster::Image;
|
||||
use graphene_std::vector::style::FillChoiceUI;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -235,7 +234,7 @@ pub enum FrontendMessage {
|
||||
svg: String,
|
||||
},
|
||||
UpdateImageData {
|
||||
image_data: Vec<(u64, Image<SRGBA8>)>,
|
||||
image_data: Vec<RasterizedImage>,
|
||||
},
|
||||
UpdateDocumentLayerDetails {
|
||||
data: LayerPanelEntry,
|
||||
|
||||
@@ -82,3 +82,12 @@ pub struct EyedropperPreviewImage {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "wasm", derive(tsify::Tsify), tsify(large_number_types_as_bigints))]
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct RasterizedImage {
|
||||
pub id: u64,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub pixels: serde_bytes::ByteBuf,
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::messages::input_mapper::utility_types::input_keyboard::KeysGroup;
|
||||
use crate::messages::layout::utility_types::widget_prelude::*;
|
||||
use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier;
|
||||
use crate::messages::prelude::*;
|
||||
use graphene_std::color::SRGBA8;
|
||||
use graphene_std::vector::style::FillChoiceUI;
|
||||
@@ -291,7 +292,11 @@ impl LayoutMessageHandler {
|
||||
responses.add(callback_message);
|
||||
}
|
||||
WidgetValueAction::Update => {
|
||||
let callback_message = (node_type_input.on_update.callback)(&value.into());
|
||||
let Value::String(ref node_type) = value else {
|
||||
error!("NodeCatalog update was not of type: String, found {value:?}");
|
||||
return;
|
||||
};
|
||||
let callback_message = (node_type_input.on_update.callback)(&DefinitionIdentifier::from_serialized(node_type));
|
||||
responses.add(callback_message);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -23,7 +23,6 @@ use graphene_std::raster_types::{CPU, Raster};
|
||||
use graphene_std::transform::Footprint;
|
||||
use graphene_std::vector::Vector;
|
||||
use graphene_std::*;
|
||||
use serde_json::Value;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
pub struct NodePropertiesContext<'a> {
|
||||
@@ -89,12 +88,8 @@ impl DefinitionIdentifier {
|
||||
DefinitionIdentifier::Network(data) => format!("NETWORK:{}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value> for DefinitionIdentifier {
|
||||
fn from(value: Value) -> Self {
|
||||
let s = value.as_str().expect("DefinitionIdentifier value must be a string");
|
||||
|
||||
pub fn from_serialized(s: &str) -> Self {
|
||||
match s.split_once(':') {
|
||||
Some(("PROTONODE", data)) => DefinitionIdentifier::ProtoNode(ProtoNodeIdentifier::with_owned_string(data.to_string())),
|
||||
Some(("NETWORK", data)) => DefinitionIdentifier::Network(data.to_string()),
|
||||
|
||||
@@ -323,6 +323,7 @@ pub(crate) fn property_from_type(
|
||||
Type::Generic(_) => vec![TextLabel::new("Generic Type (Not Supported)").widget_instance()].into(),
|
||||
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||
Type::Ref(inner) => return property_from_type(node_id, index, inner, number_options, unit, display_decimal_places, step, context),
|
||||
};
|
||||
|
||||
extra_widgets.push(widgets);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::messages::frontend::utility_types::{ExportBounds, FileType};
|
||||
use crate::messages::frontend::utility_types::{ExportBounds, FileType, RasterizedImage};
|
||||
use crate::messages::portfolio::document::utility_types::network_interface::InputConnector;
|
||||
use crate::messages::prelude::*;
|
||||
use glam::{DAffine2, DVec2, UVec2};
|
||||
@@ -660,19 +660,15 @@ impl NodeGraphExecutor {
|
||||
|
||||
match render_output.data {
|
||||
RenderOutputType::Svg { svg, image_data } => {
|
||||
// Convert each linear-light `Image<Color>` into the JS-boundary `Image<SRGBA8>` form (gamma byte channels) before dispatching.
|
||||
// Convert each linear-light `Image<Color>` into gamma sRGB bytes (the DOM boundary form) before dispatching.
|
||||
let rgba8_bytes = |&color: &_| <[u8; 4]>::from(SRGBA8::from(color));
|
||||
let image_data = image_data
|
||||
.into_iter()
|
||||
.map(|(id, image)| {
|
||||
(
|
||||
id,
|
||||
graphene_std::raster::Image {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
data: image.data.iter().map(|&c| SRGBA8::from(c)).collect(),
|
||||
base64_string: image.base64_string,
|
||||
},
|
||||
)
|
||||
.map(|(id, image)| RasterizedImage {
|
||||
id,
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
pixels: image.data.iter().flat_map(rgba8_bytes).collect::<Vec<u8>>().into(),
|
||||
})
|
||||
.collect();
|
||||
responses.add(FrontendMessage::UpdateImageData { image_data });
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
node_modules/
|
||||
wrapper/pkg/
|
||||
wrapper/pkg-native/
|
||||
public/build/
|
||||
dist/
|
||||
@@ -29,6 +29,7 @@ export default defineConfig([
|
||||
"**/node_modules/",
|
||||
"**/dist/",
|
||||
"**/pkg/",
|
||||
"**/pkg-native/",
|
||||
"wasm/pkg/",
|
||||
// Don't ignore JS and TS dotfiles in this folder
|
||||
"!**/.*.js",
|
||||
|
||||
@@ -23,7 +23,10 @@ export default defineConfig(({ mode }) => {
|
||||
// Default for builds that exclude the `wasmSplitting` plugin, which overrides this when active
|
||||
define: { __WASM_PART_COUNT__: "1" },
|
||||
resolve: {
|
||||
alias: [{ find: /\/..\/branding\/(.*\.svg)/, replacement: path.resolve(projectRootDir, "../branding", "$1?raw") }],
|
||||
alias: [
|
||||
...(mode === "native" ? [{ find: /^\/wrapper\/pkg\//, replacement: "/wrapper/pkg-native/" }] : []),
|
||||
{ find: /\/..\/branding\/(.*\.svg)/, replacement: path.resolve(projectRootDir, "../branding", "$1?raw") },
|
||||
],
|
||||
},
|
||||
server: {
|
||||
port: 8080,
|
||||
|
||||
+15
-11
@@ -11,33 +11,37 @@ repository = "https://github.com/GraphiteEditor/Graphite"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[features]
|
||||
default = ["gpu", "shader-nodes"]
|
||||
gpu = ["editor/gpu"]
|
||||
shader-nodes = ["graphene-std/shader-nodes", "gpu"]
|
||||
native = ["node-macro/disable-registration"]
|
||||
default = ["gpu", "shader-nodes", "web"]
|
||||
web = ["editor", "editor?/wasm"]
|
||||
editor = ["dep:editor", "dep:graphene-std", "dep:graph-craft", "dep:wgpu"]
|
||||
gpu = ["editor?/gpu"]
|
||||
shader-nodes = ["graphene-std?/shader-nodes", "gpu"]
|
||||
native = []
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
# Local dependencies
|
||||
editor = { path = "../../editor", package = "graphite-editor", features = ["gpu", "wasm"] }
|
||||
graphene-std = { workspace = true }
|
||||
editor = { path = "../../editor", package = "graphite-editor", optional = true }
|
||||
graphite-proc-macros = { workspace = true }
|
||||
graphene-std = { workspace = true, optional = true }
|
||||
|
||||
# Workspace dependencies
|
||||
bytemuck = { workspace = true }
|
||||
graph-craft = { workspace = true }
|
||||
graph-craft = { workspace = true, optional = true }
|
||||
log = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
wasm-bindgen = { workspace = true }
|
||||
serde-wasm-bindgen = { workspace = true }
|
||||
js-sys = { workspace = true }
|
||||
wasm-bindgen-futures = { workspace = true }
|
||||
wgpu = { workspace = true }
|
||||
wgpu = { workspace = true, optional = true }
|
||||
web-sys = { workspace = true }
|
||||
ron = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
node-macro = { workspace = true }
|
||||
tsify = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_bytes = { workspace = true }
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = [
|
||||
|
||||
@@ -0,0 +1,690 @@
|
||||
//! Commands that JS can call on the editor. Each function maps its arguments to a `Message`. On web the generated
|
||||
//! stub dispatches it directly, on native the arguments are sent as an `EditorCommand` and the same body runs in
|
||||
//! the editor process.
|
||||
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
#[cfg(target_family = "wasm")]
|
||||
use crate::editor_wrapper::EditorWrapper;
|
||||
use graphite_proc_macros::editor_commands;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tsify::Tsify;
|
||||
#[cfg(target_family = "wasm")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[editor_commands]
|
||||
mod editor_commands {
|
||||
use crate::helpers::translate_key;
|
||||
use editor::messages::clipboard::utility_types::ClipboardContentRaw;
|
||||
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
|
||||
use editor::messages::input_mapper::utility_types::input_mouse::{EditorMouseState, ScrollDelta};
|
||||
use editor::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier;
|
||||
use editor::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
|
||||
use editor::messages::portfolio::document::utility_types::network_interface::ImportOrExport;
|
||||
use editor::messages::portfolio::utility_types::PanelGroupId;
|
||||
use editor::messages::prelude::*;
|
||||
use editor::messages::tool::tool_messages::tool_prelude::WidgetId;
|
||||
use graph_craft::document::NodeId;
|
||||
use graphene_std::raster::color::Color;
|
||||
use graphene_std::vector::style::FillChoice;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Re-sends all UI layouts to the frontend. Called during HMR re-mounts when the frontend has lost its layout state.
|
||||
fn resend_all_layouts() -> Message {
|
||||
LayoutMessage::ResendAllLayouts.into()
|
||||
}
|
||||
|
||||
/// First message of a session, sent once the frontend is ready
|
||||
fn init_portfolio() -> Message {
|
||||
PortfolioMessage::Init.into()
|
||||
}
|
||||
|
||||
/// Per-frame tick: advances the animation clock and broadcasts the animation frame event
|
||||
fn animation_frame(timestamp: u64) -> Message {
|
||||
Message::Batched {
|
||||
messages: Box::new([
|
||||
InputPreprocessorMessage::CurrentTime { timestamp }.into(),
|
||||
AnimationMessage::IncrementFrameCounter.into(),
|
||||
// Used by auto-panning, but this could possibly be refactored in the future, see:
|
||||
// <https://github.com/GraphiteEditor/Graphite/pull/2562#discussion_r2041102786>
|
||||
BroadcastMessage::TriggerEvent(EventMessage::AnimationFrame).into(),
|
||||
]),
|
||||
}
|
||||
}
|
||||
|
||||
fn auto_save_all_documents() -> Message {
|
||||
PortfolioMessage::AutoSaveAllDocuments.into()
|
||||
}
|
||||
|
||||
fn add_primary_import() -> Message {
|
||||
Message::Batched {
|
||||
messages: Box::new([DocumentMessage::AddTransaction.into(), NodeGraphMessage::AddPrimaryImport.into()]),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_secondary_import() -> Message {
|
||||
Message::Batched {
|
||||
messages: Box::new([DocumentMessage::AddTransaction.into(), NodeGraphMessage::AddSecondaryImport.into()]),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_primary_export() -> Message {
|
||||
Message::Batched {
|
||||
messages: Box::new([DocumentMessage::AddTransaction.into(), NodeGraphMessage::AddPrimaryExport.into()]),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_secondary_export() -> Message {
|
||||
Message::Batched {
|
||||
messages: Box::new([DocumentMessage::AddTransaction.into(), NodeGraphMessage::AddSecondaryExport.into()]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Start Pointer Lock
|
||||
fn app_window_pointer_lock() -> Message {
|
||||
AppWindowMessage::PointerLock.into()
|
||||
}
|
||||
|
||||
/// Minimizes the application window to the taskbar or dock
|
||||
fn app_window_minimize() -> Message {
|
||||
AppWindowMessage::Minimize.into()
|
||||
}
|
||||
|
||||
/// Toggles minimizing or restoring down the application window
|
||||
fn app_window_maximize() -> Message {
|
||||
AppWindowMessage::Maximize.into()
|
||||
}
|
||||
|
||||
fn app_window_fullscreen() -> Message {
|
||||
AppWindowMessage::Fullscreen.into()
|
||||
}
|
||||
|
||||
/// Closes the application window
|
||||
fn app_window_close() -> Message {
|
||||
AppWindowMessage::Close.into()
|
||||
}
|
||||
|
||||
/// Drag the application window
|
||||
fn app_window_drag() -> Message {
|
||||
AppWindowMessage::Drag.into()
|
||||
}
|
||||
|
||||
/// Displays a dialog with an error message
|
||||
fn error_dialog(title: String, description: String) -> Message {
|
||||
DialogMessage::DisplayDialogError { title, description }.into()
|
||||
}
|
||||
|
||||
/// Update the value of a given UI widget, but don't commit it to the history (unless `commit_layout()` is called, which handles that)
|
||||
fn widget_value_update(layout_target: LayoutTarget, widget_id: u64, value: Any, resend_widget: bool) -> Message {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
let update = LayoutMessage::WidgetValueUpdate {
|
||||
layout_target,
|
||||
widget_id,
|
||||
value: value.cast(),
|
||||
};
|
||||
if resend_widget {
|
||||
Message::Batched {
|
||||
messages: Box::new([update.into(), LayoutMessage::ResendActiveWidget { layout_target, widget_id }.into()]),
|
||||
}
|
||||
} else {
|
||||
update.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Commit the value of a given UI widget to the history
|
||||
fn widget_value_commit(layout_target: LayoutTarget, widget_id: u64, value: Any) -> Message {
|
||||
LayoutMessage::WidgetValueCommit {
|
||||
layout_target,
|
||||
widget_id: WidgetId(widget_id),
|
||||
value: value.cast(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Update the value of a given UI widget, and commit it to the history
|
||||
fn widget_value_commit_and_update(layout_target: LayoutTarget, widget_id: u64, value: Any, resend_widget: bool) -> Message {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
let mut messages: Vec<Message> = vec![
|
||||
LayoutMessage::WidgetValueCommit {
|
||||
layout_target,
|
||||
widget_id,
|
||||
value: value.cast(),
|
||||
}
|
||||
.into(),
|
||||
LayoutMessage::WidgetValueUpdate {
|
||||
layout_target,
|
||||
widget_id,
|
||||
value: value.cast(),
|
||||
}
|
||||
.into(),
|
||||
];
|
||||
if resend_widget {
|
||||
messages.push(LayoutMessage::ResendActiveWidget { layout_target, widget_id }.into());
|
||||
}
|
||||
// Close out a transaction that the widget's `on_commit` opened (if any), so a single click on widgets like the
|
||||
// NumberInput's increment buttons collapses into one history step instead of leaving the transaction in `Modified`
|
||||
messages.push(DocumentMessage::EndTransaction.into());
|
||||
Message::Batched { messages: messages.into() }
|
||||
}
|
||||
|
||||
/// Fire a widget's drag-drop action (e.g. when a draggable item is dropped on a button)
|
||||
fn widget_value_drag_drop(layout_target: LayoutTarget, widget_id: u64) -> Message {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
LayoutMessage::WidgetValueDragDrop { layout_target, widget_id }.into()
|
||||
}
|
||||
|
||||
/// Closes out the current transaction (drag-end / text-commit end), so emits during a slider drag collapse into one history step instead of N
|
||||
fn end_transaction() -> Message {
|
||||
DocumentMessage::EndTransaction.into()
|
||||
}
|
||||
|
||||
fn load_preferences(preferences: Option<String>) -> Message {
|
||||
let Some(preferences) = preferences else { return Message::NoOp };
|
||||
let Ok(preferences) = serde_json::from_str(&preferences) else {
|
||||
log::error!("Failed to deserialize preferences");
|
||||
return Message::NoOp;
|
||||
};
|
||||
PreferencesMessage::Load { preferences }.into()
|
||||
}
|
||||
|
||||
fn load_document_content(document_id: u64, document: String) -> Message {
|
||||
PersistentStateMessage::LoadDocument {
|
||||
document_id: DocumentId(document_id),
|
||||
document,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn select_document(document_id: u64) -> Message {
|
||||
PortfolioMessage::SelectDocument { document_id: DocumentId(document_id) }.into()
|
||||
}
|
||||
|
||||
/// Rename the currently active document.
|
||||
fn rename_document(new_name: String) -> Message {
|
||||
PortfolioMessage::RenameDocument { new_name }.into()
|
||||
}
|
||||
|
||||
fn new_document_dialog() -> Message {
|
||||
DialogMessage::RequestNewDocumentDialog.into()
|
||||
}
|
||||
|
||||
fn open_file(path: String, content: Vec<u8>) -> Message {
|
||||
PortfolioMessage::OpenFile { path: PathBuf::from(path), content }.into()
|
||||
}
|
||||
|
||||
fn import_file(path: String, content: Vec<u8>) -> Message {
|
||||
PortfolioMessage::ImportFile { path: PathBuf::from(path), content }.into()
|
||||
}
|
||||
|
||||
fn trigger_auto_save(document_id: u64) -> Message {
|
||||
PortfolioMessage::AutoSaveDocument { document_id: DocumentId(document_id) }.into()
|
||||
}
|
||||
|
||||
fn reorder_document(document_id: u64, new_index: usize) -> Message {
|
||||
PortfolioMessage::ReorderDocument {
|
||||
document_id: DocumentId(document_id),
|
||||
new_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn reorder_panel_group_tab(group: u64, old_index: usize, new_index: usize) -> Message {
|
||||
PortfolioMessage::ReorderPanelGroupTab {
|
||||
group: PanelGroupId(group),
|
||||
old_index,
|
||||
new_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn move_all_panel_tabs(source_group: u64, target_group: u64, insert_index: usize) -> Message {
|
||||
PortfolioMessage::MoveAllPanelTabs {
|
||||
source_group: PanelGroupId(source_group),
|
||||
target_group: PanelGroupId(target_group),
|
||||
insert_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn move_panel_tab(source_group: u64, target_group: u64, insert_index: usize) -> Message {
|
||||
PortfolioMessage::MovePanelTab {
|
||||
source_group: PanelGroupId(source_group),
|
||||
target_group: PanelGroupId(target_group),
|
||||
insert_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn set_panel_group_active_tab(group: u64, tab_index: usize) -> Message {
|
||||
PortfolioMessage::SetPanelGroupActiveTab {
|
||||
group: PanelGroupId(group),
|
||||
tab_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn split_panel_group(target_group: u64, direction: DockingSplitDirection, tabs: PanelTypes, active_tab_index: usize) -> Message {
|
||||
PortfolioMessage::SplitPanelGroup {
|
||||
target_group: PanelGroupId(target_group),
|
||||
direction,
|
||||
tabs,
|
||||
active_tab_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn set_panel_group_sizes(split_path: Vec<u32>, sizes: Vec<f64>) -> Message {
|
||||
let split_path = split_path.into_iter().map(|i| i as usize).collect();
|
||||
PortfolioMessage::SetPanelGroupSizes { split_path, sizes }.into()
|
||||
}
|
||||
|
||||
fn close_document_with_confirmation(document_id: u64) -> Message {
|
||||
PortfolioMessage::CloseDocumentWithConfirmation { document_id: DocumentId(document_id) }.into()
|
||||
}
|
||||
|
||||
fn request_about_graphite_dialog_with_localized_commit_date(localized_commit_date: String, localized_commit_year: String) -> Message {
|
||||
DialogMessage::RequestAboutGraphiteDialogWithLocalizedCommitDate {
|
||||
localized_commit_date,
|
||||
localized_commit_year,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn request_licenses_third_party_dialog_with_license_text(license_text: String) -> Message {
|
||||
DialogMessage::RequestLicensesThirdPartyDialogWithLicenseText { license_text }.into()
|
||||
}
|
||||
|
||||
/// Send new viewport info to the backend
|
||||
fn update_viewport(x: f64, y: f64, width: f64, height: f64, scale: f64) -> Message {
|
||||
ViewportMessage::Update { x, y, width, height, scale }.into()
|
||||
}
|
||||
|
||||
/// Mouse movement within the screenspace bounds of the viewport
|
||||
fn on_mouse_move(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::PointerMove { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// Mouse scrolling within the screenspace bounds of the viewport
|
||||
fn on_wheel_scroll(x: f64, y: f64, mouse_keys: u8, wheel_delta_x: f64, wheel_delta_y: f64, wheel_delta_z: f64, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::WheelScroll { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// A mouse button depressed within screenspace the bounds of the viewport
|
||||
fn on_mouse_down(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::PointerDown { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// A mouse button released
|
||||
fn on_mouse_up(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::PointerUp { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// Mouse shaken
|
||||
fn on_mouse_shake(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::PointerShake { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// Mouse double clicked
|
||||
fn on_double_click(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Message {
|
||||
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");
|
||||
InputPreprocessorMessage::DoubleClick { editor_mouse_state, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// A keyboard button depressed within screenspace the bounds of the viewport
|
||||
fn on_key_down(name: String, modifiers: u8, key_repeat: bool) -> Message {
|
||||
let key = translate_key(&name);
|
||||
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
||||
trace!("Key down {key:?}, name: {name}, modifiers: {modifiers:?}, key repeat: {key_repeat}");
|
||||
InputPreprocessorMessage::KeyDown { key, key_repeat, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// A keyboard button released
|
||||
fn on_key_up(name: String, modifiers: u8, key_repeat: bool) -> Message {
|
||||
let key = translate_key(&name);
|
||||
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
||||
trace!("Key up {key:?}, name: {name}, modifiers: {modifier_keys:?}, key repeat: {key_repeat}");
|
||||
InputPreprocessorMessage::KeyUp { key, key_repeat, modifier_keys }.into()
|
||||
}
|
||||
|
||||
/// A text box was committed
|
||||
fn on_change_text(new_text: String, is_left_or_right_click: bool) -> Message {
|
||||
TextToolMessage::TextChange { new_text, is_left_or_right_click }.into()
|
||||
}
|
||||
|
||||
/// Dialog got dismissed
|
||||
fn on_dialog_dismiss() -> Message {
|
||||
DialogMessage::Dismiss.into()
|
||||
}
|
||||
|
||||
/// A text box was changed
|
||||
fn update_bounds(new_text: String) -> Message {
|
||||
TextToolMessage::UpdateBounds { new_text }.into()
|
||||
}
|
||||
|
||||
/// Update primary color from sRGB bytes (the wire format at the JS boundary).
|
||||
fn update_primary_color(color: SRGBA8) -> Message {
|
||||
ToolMessage::SelectWorkingColor {
|
||||
color: Color::from(color),
|
||||
primary: true,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Update secondary color from sRGB bytes (the wire format at the JS boundary).
|
||||
fn update_secondary_color(color: SRGBA8) -> Message {
|
||||
ToolMessage::SelectWorkingColor {
|
||||
color: Color::from(color),
|
||||
primary: false,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Initialize the Rust color picker handler with a starting value (used when the frontend `<ColorPicker />` opens).
|
||||
fn open_color_picker(initial_value: FillChoiceUI, allow_none: bool, disabled: bool) -> Message {
|
||||
ColorPickerMessage::Open {
|
||||
initial_value: FillChoice::from(&initial_value),
|
||||
allow_none,
|
||||
disabled,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Tell the Rust color picker handler that the popover is closing.
|
||||
fn close_color_picker() -> Message {
|
||||
ColorPickerMessage::Close.into()
|
||||
}
|
||||
|
||||
/// Update the color of the currently-edited gradient stop, from sRGB bytes (the wire format at the JS boundary).
|
||||
fn update_gradient_stop_color(color: SRGBA8) -> Message {
|
||||
GradientToolMessage::UpdateStopColor { color: Color::from(color) }.into()
|
||||
}
|
||||
|
||||
/// Start a new undo transaction for gradient stop color editing
|
||||
fn start_gradient_stop_color_transaction() -> Message {
|
||||
GradientToolMessage::StartTransactionForColorStop.into()
|
||||
}
|
||||
|
||||
/// Commit the current gradient stop color transaction (called on pointer-up after each drag/click)
|
||||
fn commit_gradient_stop_color_transaction() -> Message {
|
||||
GradientToolMessage::CommitTransactionForColorStop.into()
|
||||
}
|
||||
|
||||
/// Close the gradient stop color picker and commit any pending transaction
|
||||
fn close_gradient_stop_color_picker() -> Message {
|
||||
GradientToolMessage::CloseStopColorPicker.into()
|
||||
}
|
||||
|
||||
/// Toggle clipping the alpha of a layer to the alpha of the layer below it in the layer stack
|
||||
fn clip_layer(id: u64) -> Message {
|
||||
DocumentMessage::ClipLayer { id: NodeId(id) }.into()
|
||||
}
|
||||
|
||||
/// 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
|
||||
fn select_layer(id: u64, ctrl: bool, shift: bool) -> Message {
|
||||
DocumentMessage::SelectLayer { id: NodeId(id), ctrl, shift }.into()
|
||||
}
|
||||
|
||||
/// Deselect all layers
|
||||
fn deselect_all_layers() -> Message {
|
||||
DocumentMessage::DeselectAllLayers.into()
|
||||
}
|
||||
|
||||
/// Move a layer to within a folder and placed down at the given index.
|
||||
/// If the folder is `None`, it is inserted into the document root.
|
||||
/// If the insert index is `None`, it is inserted at the start of the folder.
|
||||
fn move_layer_in_tree(insert_parent_id: Option<u64>, insert_index: Option<usize>) -> Message {
|
||||
let insert_parent_id = insert_parent_id.map(NodeId);
|
||||
let parent = insert_parent_id.map(LayerNodeIdentifier::new_unchecked).unwrap_or_default();
|
||||
|
||||
DocumentMessage::MoveSelectedLayersTo {
|
||||
parent,
|
||||
insert_index: insert_index.unwrap_or_default(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Reorder a draggable Properties panel section to the given index among its peers.
|
||||
fn reorder_properties_section(node_id: u64, insert_index: usize) -> Message {
|
||||
DocumentMessage::ReorderPropertiesSection {
|
||||
node_id: NodeId(node_id),
|
||||
insert_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Duplicate the selected layers, placing the copies within the given folder at the given index.
|
||||
/// If the folder is `None`, they are inserted into the document root.
|
||||
/// If the insert index is `None`, they are inserted at the start of the folder.
|
||||
fn duplicate_layer_in_tree(insert_parent_id: Option<u64>, insert_index: Option<usize>) -> Message {
|
||||
DocumentMessage::DuplicateSelectedLayersTo {
|
||||
parent: insert_parent_id.map(NodeId).map(LayerNodeIdentifier::new_unchecked).unwrap_or_default(),
|
||||
insert_index: insert_index.unwrap_or_default(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Set the name for the layer
|
||||
fn set_layer_name(id: u64, name: String) -> Message {
|
||||
let layer = LayerNodeIdentifier::new_unchecked(NodeId(id));
|
||||
NodeGraphMessage::SetDisplayName {
|
||||
node_id: layer.to_node(),
|
||||
network_path: Vec::new(),
|
||||
alias: name,
|
||||
skip_adding_history_step: false,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
fn pan_canvas_abort_prepare(x_not_y_axis: bool) -> Message {
|
||||
NavigationMessage::CanvasPanAbortPrepare { x_not_y_axis }.into()
|
||||
}
|
||||
|
||||
fn pan_canvas_abort(x_not_y_axis: bool) -> Message {
|
||||
NavigationMessage::CanvasPanAbort { x_not_y_axis }.into()
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
fn pan_canvas(delta_x: f64, delta_y: f64) -> Message {
|
||||
NavigationMessage::CanvasPan { delta: (delta_x, delta_y).into() }.into()
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
fn pan_canvas_by_fraction(delta_x: f64, delta_y: f64) -> Message {
|
||||
NavigationMessage::CanvasPanByViewportFraction { delta: (delta_x, delta_y).into() }.into()
|
||||
}
|
||||
|
||||
/// Merge the selected nodes into a subnetwork
|
||||
fn merge_selected_nodes() -> Message {
|
||||
NodeGraphMessage::MergeSelectedNodes.into()
|
||||
}
|
||||
|
||||
/// Toggle lock state of all selected layers
|
||||
fn toggle_selected_locked() -> Message {
|
||||
NodeGraphMessage::ToggleSelectedLocked.into()
|
||||
}
|
||||
|
||||
/// Creates a new document node in the node graph
|
||||
fn create_node(node_type: String, x: i32, y: i32) -> Message {
|
||||
let id = NodeId::new();
|
||||
NodeGraphMessage::CreateNodeFromContextMenu {
|
||||
node_id: Some(id),
|
||||
node_type: DefinitionIdentifier::from_serialized(&node_type),
|
||||
xy: Some((x / 24, y / 24)),
|
||||
add_transaction: true,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Respond to selection read
|
||||
fn read_selection(content: Option<String>, cut: bool) -> Message {
|
||||
ClipboardMessage::ReadSelection { content, cut }.into()
|
||||
}
|
||||
|
||||
/// Paste from a serialized JSON representation
|
||||
fn paste_text(data: String) -> Message {
|
||||
ClipboardMessage::ReadClipboard {
|
||||
content: ClipboardContentRaw::Text(data),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Pastes an image
|
||||
fn paste_image(
|
||||
name: Option<String>,
|
||||
image_data: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
mouse_x: Option<f64>,
|
||||
mouse_y: Option<f64>,
|
||||
insert_parent_id: Option<u64>,
|
||||
insert_index: Option<usize>,
|
||||
) -> Message {
|
||||
let mouse = mouse_x.and_then(|x| mouse_y.map(|y| (x, y)));
|
||||
let image = graphene_std::raster::Image::from_image_data(&image_data, width, height);
|
||||
|
||||
let parent_and_insert_index = if let (Some(insert_parent_id), Some(insert_index)) = (insert_parent_id, insert_index) {
|
||||
let insert_parent_id = NodeId(insert_parent_id);
|
||||
let parent = LayerNodeIdentifier::new_unchecked(insert_parent_id);
|
||||
Some((parent, insert_index))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
PortfolioMessage::InsertImage {
|
||||
name,
|
||||
image,
|
||||
mouse,
|
||||
parent_and_insert_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Pastes an SVG given its string representation
|
||||
fn paste_svg(name: Option<String>, svg: String, mouse_x: Option<f64>, mouse_y: Option<f64>, insert_parent_id: Option<u64>, insert_index: Option<usize>) -> Message {
|
||||
let mouse = mouse_x.and_then(|x| mouse_y.map(|y| (x, y)));
|
||||
|
||||
let parent_and_insert_index = if let (Some(insert_parent_id), Some(insert_index)) = (insert_parent_id, insert_index) {
|
||||
let insert_parent_id = NodeId(insert_parent_id);
|
||||
let parent = LayerNodeIdentifier::new_unchecked(insert_parent_id);
|
||||
Some((parent, insert_index))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
PortfolioMessage::InsertSvg {
|
||||
name,
|
||||
svg,
|
||||
mouse,
|
||||
parent_and_insert_index,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Toggle visibility of a layer or node given its node ID
|
||||
fn toggle_node_visibility_layer_panel(id: u64) -> Message {
|
||||
NodeGraphMessage::ToggleVisibility {
|
||||
node_id: NodeId(id),
|
||||
network_path: Vec::new(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Pin or unpin a node given its node ID
|
||||
fn set_node_pinned(id: u64, pinned: bool) -> Message {
|
||||
DocumentMessage::SetNodePinned { node_id: NodeId(id), pinned }.into()
|
||||
}
|
||||
|
||||
/// Collapse or expand a node's section in the Properties panel
|
||||
fn toggle_node_properties_section_expanded(id: u64) -> Message {
|
||||
DocumentMessage::ToggleNodePropertiesSectionExpanded { node_id: NodeId(id) }.into()
|
||||
}
|
||||
|
||||
/// Delete a layer or node given its node ID
|
||||
fn delete_node(id: u64) -> Message {
|
||||
DocumentMessage::DeleteNode { node_id: NodeId(id) }.into()
|
||||
}
|
||||
|
||||
/// Toggle lock state of a layer from the layer list
|
||||
fn toggle_layer_lock(node_id: u64) -> Message {
|
||||
NodeGraphMessage::ToggleLocked {
|
||||
node_id: NodeId(node_id),
|
||||
network_path: Vec::new(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Toggle expansions state of a layer from the layer list
|
||||
fn toggle_layer_expansion(tree_path: Vec<u64>, recursive: bool) -> Message {
|
||||
let tree_path = tree_path.into_iter().map(NodeId).collect();
|
||||
DocumentMessage::ToggleLayerExpansion { tree_path, recursive }.into()
|
||||
}
|
||||
|
||||
/// Set the active panel to the most recently clicked panel
|
||||
fn set_active_panel(panel: String) -> Message {
|
||||
DocumentMessage::SetActivePanel { active_panel: panel.into() }.into()
|
||||
}
|
||||
|
||||
/// Toggle display type for a layer
|
||||
fn set_to_node_or_layer(id: u64, is_layer: bool) -> Message {
|
||||
DocumentMessage::SetToNodeOrLayer { node_id: NodeId(id), is_layer }.into()
|
||||
}
|
||||
|
||||
/// Set the name of an import or export
|
||||
fn set_import_name(index: usize, name: String) -> Message {
|
||||
NodeGraphMessage::SetImportExportName {
|
||||
name,
|
||||
index: ImportOrExport::Import(index),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Set the name of an export
|
||||
fn set_export_name(index: usize, name: String) -> Message {
|
||||
NodeGraphMessage::SetImportExportName {
|
||||
name,
|
||||
index: ImportOrExport::Export(index),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
|
||||
#[tsify(from_wasm_abi)]
|
||||
pub struct Any(#[tsify(type = "any")] serde_json::Value);
|
||||
impl Any {
|
||||
#[cfg(feature = "editor")]
|
||||
pub(crate) fn cast<T: for<'de> Deserialize<'de>>(&self) -> T {
|
||||
serde_json::from_value(self.0.clone()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! editor_proxy_types {
|
||||
($($name:ident = $real:ty;)*) => {
|
||||
$(
|
||||
#[cfg(feature = "editor")]
|
||||
pub type $name = $real;
|
||||
#[cfg(not(feature = "editor"))]
|
||||
pub type $name = Any;
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
editor_proxy_types! {
|
||||
LayoutTarget = editor::messages::layout::utility_types::layout_widget::LayoutTarget;
|
||||
DockingSplitDirection = editor::messages::portfolio::utility_types::DockingSplitDirection;
|
||||
PanelTypes = Vec<editor::messages::portfolio::utility_types::PanelType>;
|
||||
SRGBA8 = graphene_std::color::SRGBA8;
|
||||
FillChoiceUI = graphene_std::vector::style::FillChoiceUI;
|
||||
}
|
||||
@@ -1,44 +1,33 @@
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//! JS-facing editor handle. Owns the callback that delivers `FrontendMessage`s to JS; on web `dispatch` runs
|
||||
//! messages through the in-process editor, on native `send` forwards them as `EditorCommand`s.
|
||||
|
||||
#[cfg(not(feature = "native"))]
|
||||
use crate::EDITOR;
|
||||
#[cfg(not(feature = "native"))]
|
||||
use crate::helpers::poll_node_graph_evaluation;
|
||||
use crate::helpers::{auto_save_all_documents, calculate_hash, render_image_data_to_canvases, request_animation_frame, set_timeout, translate_key, wrapper};
|
||||
use crate::{EDITOR_HAS_CRASHED, Error, FRONTEND_READY, MESSAGE_BUFFER};
|
||||
use crate::MESSAGE_BUFFER;
|
||||
#[cfg(all(feature = "native", target_family = "wasm"))]
|
||||
use crate::editor_commands::EditorCommand;
|
||||
#[cfg(not(feature = "native"))]
|
||||
use crate::helpers::poll_node_graph_evaluation;
|
||||
#[cfg(any(not(feature = "native"), target_family = "wasm"))]
|
||||
use crate::helpers::wrapper;
|
||||
#[cfg(feature = "editor")]
|
||||
use crate::helpers::{calculate_hash, render_image_data_to_canvases};
|
||||
use crate::helpers::{request_animation_frame, set_timeout};
|
||||
use crate::{EDITOR_HAS_CRASHED, FRONTEND_READY};
|
||||
#[cfg(all(not(feature = "native"), target_family = "wasm"))]
|
||||
use editor::application::{Editor, Environment, Host, Platform};
|
||||
use editor::consts::{FILE_EXTENSION, GDD_FILE_EXTENSION};
|
||||
use editor::messages::clipboard::utility_types::ClipboardContentRaw;
|
||||
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
|
||||
use editor::messages::input_mapper::utility_types::input_mouse::{EditorMouseState, ScrollDelta};
|
||||
use editor::messages::layout::utility_types::layout_widget::LayoutTarget;
|
||||
use editor::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
|
||||
use editor::messages::portfolio::document::utility_types::network_interface::ImportOrExport;
|
||||
use editor::messages::portfolio::utility_types::{DockingSplitDirection, PanelGroupId, PanelType};
|
||||
#[cfg(feature = "editor")]
|
||||
use editor::messages::prelude::*;
|
||||
use editor::messages::tool::tool_messages::tool_prelude::WidgetId;
|
||||
use graph_craft::document::NodeId;
|
||||
use graphene_std::color::SRGBA8;
|
||||
use graphene_std::graphene_hash::CacheHashWrapper;
|
||||
use graphene_std::raster::color::Color;
|
||||
use graphene_std::vector::style::{FillChoice, FillChoiceUI};
|
||||
#[cfg(feature = "editor")]
|
||||
use serde::Serialize;
|
||||
use serde_wasm_bindgen::{self, from_value};
|
||||
use std::cell::RefCell;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::Duration;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
static IMAGE_DATA_HASH: AtomicU64 = AtomicU64::new(0);
|
||||
pub(crate) static IMAGE_DATA_HASH: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// This struct is, via wasm-bindgen, used by JS to interact with the editor backend. It does this by calling functions, which are `impl`ed
|
||||
#[wasm_bindgen]
|
||||
#[derive(Clone)]
|
||||
pub struct EditorWrapper {
|
||||
@@ -46,13 +35,7 @@ pub struct EditorWrapper {
|
||||
frontend_message_handler_callback: js_sys::Function,
|
||||
}
|
||||
|
||||
// Defined separately from the `impl` block below since this `impl` block lacks the `#[wasm_bindgen]` attribute.
|
||||
// Quirks in wasm-bindgen prevent functions in `#[wasm_bindgen]` `impl` blocks from being made publicly accessible from Rust.
|
||||
impl EditorWrapper {
|
||||
pub fn send_frontend_message_to_js_rust_proxy(&self, message: FrontendMessage) {
|
||||
self.send_frontend_message_to_js(message);
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "native", target_family = "wasm"))]
|
||||
fn initialize_wrapper(frontend_message_handler_callback: js_sys::Function) -> EditorWrapper {
|
||||
use crate::{EDITOR_WRAPPER, PANIC_DIALOG_MESSAGE_CALLBACK};
|
||||
@@ -69,10 +52,6 @@ impl EditorWrapper {
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl EditorWrapper {
|
||||
// ========================
|
||||
// Editor wrapper machinery
|
||||
// ========================
|
||||
|
||||
#[cfg(all(not(feature = "native"), target_family = "wasm"))]
|
||||
pub async fn create(platform: String, uuid_random_seed: u64, frontend_message_handler_callback: js_sys::Function) -> EditorWrapper {
|
||||
use graph_craft::application_io::PlatformApplicationIo;
|
||||
@@ -115,7 +94,6 @@ impl EditorWrapper {
|
||||
#[cfg(not(feature = "native"))]
|
||||
pub(crate) fn dispatch<T: Into<Message>>(&self, message: T) {
|
||||
// Process no further messages after a crash to avoid spamming the console
|
||||
use crate::MESSAGE_BUFFER;
|
||||
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
@@ -137,24 +115,15 @@ impl EditorWrapper {
|
||||
self.send_frontend_message_to_js(message);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "native")]
|
||||
pub(crate) fn dispatch<T: Into<Message>>(&self, message: T) {
|
||||
let message: Message = message.into();
|
||||
let Ok(serialized_message) = ron::to_string(&message) else {
|
||||
log::error!("Failed to serialize message");
|
||||
return;
|
||||
};
|
||||
crate::native_communication::send_message_to_cef(serialized_message)
|
||||
}
|
||||
|
||||
// Sends a FrontendMessage to JavaScript
|
||||
#[cfg(feature = "editor")]
|
||||
pub(crate) fn send_frontend_message_to_js(&self, message: FrontendMessage) {
|
||||
if let FrontendMessage::UpdateImageData { ref image_data } = message {
|
||||
let new_hash = calculate_hash(&CacheHashWrapper(image_data));
|
||||
let new_hash = calculate_hash(image_data);
|
||||
let prev_hash = IMAGE_DATA_HASH.load(Ordering::Relaxed);
|
||||
|
||||
if new_hash != prev_hash {
|
||||
render_image_data_to_canvases(image_data.as_slice());
|
||||
render_image_data_to_canvases(image_data.iter());
|
||||
IMAGE_DATA_HASH.store(new_hash, Ordering::Relaxed);
|
||||
}
|
||||
return;
|
||||
@@ -172,16 +141,31 @@ impl EditorWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
// ================================================
|
||||
// Functions for calling the editor in Rust from JS
|
||||
// ================================================
|
||||
pub(crate) fn forward_serialized_frontend_message_to_js(&self, name: &str, data: crate::wasm_value::WasmValue) {
|
||||
let js_return_value = self.frontend_message_handler_callback.call2(&JsValue::null(), &JsValue::from(name), &data.into());
|
||||
|
||||
/// Re-sends all UI layouts to the frontend. Called during HMR re-mounts when the frontend has lost its layout state.
|
||||
#[wasm_bindgen(js_name = resendAllLayouts)]
|
||||
pub fn resend_all_layouts(&self) {
|
||||
self.dispatch(LayoutMessage::ResendAllLayouts);
|
||||
if let Err(error) = js_return_value {
|
||||
error!("While handling FrontendMessage {name:?}, JavaScript threw an error:\n{error:?}")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "native", target_family = "wasm"))]
|
||||
pub(crate) fn send(&self, command: EditorCommand) {
|
||||
// Process no further commands after a crash to avoid spamming the console
|
||||
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
|
||||
let Ok(serialized) = serde_json::to_string(&command) else {
|
||||
log::error!("Failed to serialize editor command");
|
||||
return;
|
||||
};
|
||||
crate::native_communication::send_message_to_cef(serialized)
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl EditorWrapper {
|
||||
#[wasm_bindgen(js_name = initAfterFrontendReady)]
|
||||
pub fn init_after_frontend_ready(&self) {
|
||||
// Enforce idempotency, so if this is called again during an HMR re-mount, we don't initialize the editor backend twice
|
||||
@@ -192,7 +176,8 @@ impl EditorWrapper {
|
||||
#[cfg(feature = "native")]
|
||||
crate::native_communication::initialize_native_communication();
|
||||
|
||||
self.dispatch(PortfolioMessage::Init);
|
||||
#[cfg(target_family = "wasm")]
|
||||
self.init_portfolio();
|
||||
|
||||
// Poll node graph evaluation on `requestAnimationFrame`
|
||||
{
|
||||
@@ -203,25 +188,19 @@ impl EditorWrapper {
|
||||
#[cfg(not(feature = "native"))]
|
||||
wasm_bindgen_futures::spawn_local(poll_node_graph_evaluation());
|
||||
|
||||
if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
||||
wrapper(|wrapper| {
|
||||
// Process all messages that have been queued up
|
||||
let mut messages = MESSAGE_BUFFER.take();
|
||||
messages.push(
|
||||
InputPreprocessorMessage::CurrentTime {
|
||||
timestamp: js_sys::Date::now() as u64,
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
messages.push(AnimationMessage::IncrementFrameCounter.into());
|
||||
|
||||
// Used by auto-panning, but this could possibly be refactored in the future, see:
|
||||
// <https://github.com/GraphiteEditor/Graphite/pull/2562#discussion_r2041102786>
|
||||
messages.push(BroadcastMessage::TriggerEvent(EventMessage::AnimationFrame).into());
|
||||
|
||||
wrapper.dispatch(Message::Batched { messages: messages.into() });
|
||||
});
|
||||
}
|
||||
#[cfg(any(not(feature = "native"), target_family = "wasm"))]
|
||||
wrapper(|wrapper| {
|
||||
// On web, flush the messages that queued up while the editor was locked before this frame's tick
|
||||
#[cfg(not(feature = "native"))]
|
||||
{
|
||||
let messages = MESSAGE_BUFFER.take();
|
||||
if !messages.is_empty() {
|
||||
wrapper.dispatch(Message::Batched { messages: messages.into() });
|
||||
}
|
||||
}
|
||||
#[cfg(target_family = "wasm")]
|
||||
wrapper.animation_frame(js_sys::Date::now() as u64);
|
||||
});
|
||||
|
||||
// Schedule ourself for another requestAnimationFrame callback
|
||||
request_animation_frame(f.borrow().as_ref().unwrap());
|
||||
@@ -230,94 +209,25 @@ impl EditorWrapper {
|
||||
request_animation_frame(g.borrow().as_ref().unwrap());
|
||||
}
|
||||
|
||||
const AUTO_SAVE_TIMEOUT_SECONDS: u64 = 1;
|
||||
|
||||
// Auto save all documents on `setTimeout`
|
||||
{
|
||||
let f = std::rc::Rc::new(RefCell::new(None));
|
||||
let g = f.clone();
|
||||
|
||||
*g.borrow_mut() = Some(Closure::new(move || {
|
||||
auto_save_all_documents();
|
||||
#[cfg(target_family = "wasm")]
|
||||
wrapper(|wrapper| wrapper.auto_save_all_documents());
|
||||
|
||||
// Schedule ourself for another setTimeout callback
|
||||
set_timeout(f.borrow().as_ref().unwrap(), Duration::from_secs(editor::consts::AUTO_SAVE_TIMEOUT_SECONDS));
|
||||
set_timeout(f.borrow().as_ref().unwrap(), Duration::from_secs(AUTO_SAVE_TIMEOUT_SECONDS));
|
||||
}));
|
||||
|
||||
set_timeout(g.borrow().as_ref().unwrap(), Duration::from_secs(editor::consts::AUTO_SAVE_TIMEOUT_SECONDS));
|
||||
set_timeout(g.borrow().as_ref().unwrap(), Duration::from_secs(AUTO_SAVE_TIMEOUT_SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = addPrimaryImport)]
|
||||
pub fn add_primary_import(&self) {
|
||||
self.dispatch(DocumentMessage::AddTransaction);
|
||||
self.dispatch(NodeGraphMessage::AddPrimaryImport);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = addSecondaryImport)]
|
||||
pub fn add_secondary_import(&self) {
|
||||
self.dispatch(DocumentMessage::AddTransaction);
|
||||
self.dispatch(NodeGraphMessage::AddSecondaryImport);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = addPrimaryExport)]
|
||||
pub fn add_primary_export(&self) {
|
||||
self.dispatch(DocumentMessage::AddTransaction);
|
||||
self.dispatch(NodeGraphMessage::AddPrimaryExport);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = addSecondaryExport)]
|
||||
pub fn add_secondary_export(&self) {
|
||||
self.dispatch(DocumentMessage::AddTransaction);
|
||||
self.dispatch(NodeGraphMessage::AddSecondaryExport);
|
||||
}
|
||||
|
||||
/// Start Pointer Lock
|
||||
#[wasm_bindgen(js_name = appWindowPointerLock)]
|
||||
pub fn app_window_pointer_lock(&self) {
|
||||
let message = AppWindowMessage::PointerLock;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Minimizes the application window to the taskbar or dock
|
||||
#[wasm_bindgen(js_name = appWindowMinimize)]
|
||||
pub fn app_window_minimize(&self) {
|
||||
let message = AppWindowMessage::Minimize;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Toggles minimizing or restoring down the application window
|
||||
#[wasm_bindgen(js_name = appWindowMaximize)]
|
||||
pub fn app_window_maximize(&self) {
|
||||
let message = AppWindowMessage::Maximize;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = appWindowFullscreen)]
|
||||
pub fn app_window_fullscreen(&self) {
|
||||
let message = AppWindowMessage::Fullscreen;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Closes the application window
|
||||
#[wasm_bindgen(js_name = appWindowClose)]
|
||||
pub fn app_window_close(&self) {
|
||||
let message = AppWindowMessage::Close;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Drag the application window
|
||||
#[wasm_bindgen(js_name = appWindowDrag)]
|
||||
pub fn app_window_start_drag(&self) {
|
||||
let message = AppWindowMessage::Drag;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
@@ -330,676 +240,25 @@ impl EditorWrapper {
|
||||
cfg!(debug_assertions)
|
||||
}
|
||||
|
||||
/// Get the constant `FILE_EXTENSION`
|
||||
#[wasm_bindgen(js_name = fileExtension)]
|
||||
pub fn file_extension(&self) -> String {
|
||||
FILE_EXTENSION.into()
|
||||
"graphite".into()
|
||||
}
|
||||
|
||||
/// Get the constant `GDD_FILE_EXTENSION`
|
||||
#[wasm_bindgen(js_name = gddFileExtension)]
|
||||
pub fn gdd_file_extension(&self) -> String {
|
||||
GDD_FILE_EXTENSION.into()
|
||||
}
|
||||
|
||||
/// Update the value of a given UI widget, but don't commit it to the history (unless `commit_layout()` is called, which handles that)
|
||||
#[wasm_bindgen(js_name = widgetValueUpdate)]
|
||||
pub fn widget_value_update(&self, layout_target: LayoutTarget, widget_id: u64, value: JsValue, resend_widget: bool) -> Result<(), JsValue> {
|
||||
self.widget_value_update_helper(layout_target, widget_id, value, resend_widget)
|
||||
}
|
||||
|
||||
/// Commit the value of a given UI widget to the history
|
||||
#[wasm_bindgen(js_name = widgetValueCommit)]
|
||||
pub fn widget_value_commit(&self, layout_target: LayoutTarget, widget_id: u64, value: JsValue) -> Result<(), JsValue> {
|
||||
self.widget_value_commit_helper(layout_target, widget_id, value)
|
||||
}
|
||||
|
||||
/// Update the value of a given UI widget, and commit it to the history
|
||||
#[wasm_bindgen(js_name = widgetValueCommitAndUpdate)]
|
||||
pub fn widget_value_commit_and_update(&self, layout_target: LayoutTarget, widget_id: u64, value: JsValue, resend_widget: bool) -> Result<(), JsValue> {
|
||||
self.widget_value_commit_helper(layout_target, widget_id, value.clone())?;
|
||||
self.widget_value_update_helper(layout_target, widget_id, value, resend_widget)?;
|
||||
// Close out a transaction that the widget's `on_commit` opened (if any), so a single click on widgets like the
|
||||
// NumberInput's increment buttons collapses into one history step instead of leaving the transaction in `Modified`
|
||||
self.dispatch(DocumentMessage::EndTransaction);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Fire a widget's drag-drop action (e.g. when a draggable item is dropped on a button)
|
||||
#[wasm_bindgen(js_name = widgetValueDragDrop)]
|
||||
pub fn widget_value_drag_drop(&self, layout_target: LayoutTarget, widget_id: u64) {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
self.dispatch(LayoutMessage::WidgetValueDragDrop { layout_target, widget_id });
|
||||
}
|
||||
|
||||
/// Closes out the current transaction (drag-end / text-commit end), so emits during a slider drag collapse into one history step instead of N
|
||||
#[wasm_bindgen(js_name = endTransaction)]
|
||||
pub fn end_transaction(&self) {
|
||||
self.dispatch(DocumentMessage::EndTransaction);
|
||||
}
|
||||
|
||||
pub fn widget_value_update_helper(&self, layout_target: LayoutTarget, widget_id: u64, value: JsValue, resend_widget: bool) -> Result<(), JsValue> {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
let value: serde_json::Value = from_value(value).map_err(|e| Error::new(&format!("Could not update UI: {e}")))?;
|
||||
let message = LayoutMessage::WidgetValueUpdate { layout_target, widget_id, value };
|
||||
self.dispatch(message);
|
||||
if resend_widget {
|
||||
let resend_message = LayoutMessage::ResendActiveWidget { layout_target, widget_id };
|
||||
self.dispatch(resend_message);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn widget_value_commit_helper(&self, layout_target: LayoutTarget, widget_id: u64, value: JsValue) -> Result<(), JsValue> {
|
||||
let widget_id = WidgetId(widget_id);
|
||||
let value: serde_json::Value = from_value(value).map_err(|e| Error::new(&format!("Could not commit UI: {e}")))?;
|
||||
let message = LayoutMessage::WidgetValueCommit { layout_target, widget_id, value };
|
||||
self.dispatch(message);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = loadPreferences)]
|
||||
pub fn load_preferences(&self, preferences: Option<String>) {
|
||||
if let Some(preferences) = preferences {
|
||||
let Ok(preferences) = serde_json::from_str(&preferences) else {
|
||||
log::error!("Failed to deserialize preferences");
|
||||
return;
|
||||
};
|
||||
let message = PreferencesMessage::Load { preferences };
|
||||
self.dispatch(message);
|
||||
}
|
||||
"gdd".into()
|
||||
}
|
||||
|
||||
/// Load persisted browser storage state (web only; on desktop, persistence is handled natively and this is never triggered)
|
||||
#[cfg(all(feature = "web", not(feature = "native")))]
|
||||
#[wasm_bindgen(js_name = loadPersistedState)]
|
||||
pub fn load_persisted_state(&self, state: editor::messages::frontend::utility_types::PersistedState) {
|
||||
self.dispatch(PersistentStateMessage::LoadState { state });
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = loadDocumentContent)]
|
||||
pub fn load_document_content(&self, document_id: u64, document: String) {
|
||||
let message = PersistentStateMessage::LoadDocument {
|
||||
document_id: DocumentId(document_id),
|
||||
document,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = selectDocument)]
|
||||
pub fn select_document(&self, document_id: u64) {
|
||||
let document_id = DocumentId(document_id);
|
||||
let message = PortfolioMessage::SelectDocument { document_id };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Rename the currently active document.
|
||||
#[wasm_bindgen(js_name = renameDocument)]
|
||||
pub fn rename_document(&self, new_name: String) {
|
||||
let message = PortfolioMessage::RenameDocument { new_name };
|
||||
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 = openFile)]
|
||||
pub fn open_file(&self, path: String, content: Vec<u8>) {
|
||||
let message = PortfolioMessage::OpenFile { path: PathBuf::from(path), content };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = importFile)]
|
||||
pub fn import_file(&self, path: String, content: Vec<u8>) {
|
||||
let message = PortfolioMessage::ImportFile { path: PathBuf::from(path), content };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = triggerAutoSave)]
|
||||
pub fn trigger_auto_save(&self, document_id: u64) {
|
||||
let document_id = DocumentId(document_id);
|
||||
let message = PortfolioMessage::AutoSaveDocument { document_id };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = reorderDocument)]
|
||||
pub fn reorder_document(&self, document_id: u64, new_index: usize) {
|
||||
let document_id = DocumentId(document_id);
|
||||
let message = PortfolioMessage::ReorderDocument { document_id, new_index };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = reorderPanelGroupTab)]
|
||||
pub fn reorder_panel_group_tab(&self, group: u64, old_index: usize, new_index: usize) {
|
||||
let message = PortfolioMessage::ReorderPanelGroupTab {
|
||||
group: PanelGroupId(group),
|
||||
old_index,
|
||||
new_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = moveAllPanelTabs)]
|
||||
pub fn move_all_panel_tabs(&self, source_group: u64, target_group: u64, insert_index: usize) {
|
||||
let message = PortfolioMessage::MoveAllPanelTabs {
|
||||
source_group: PanelGroupId(source_group),
|
||||
target_group: PanelGroupId(target_group),
|
||||
insert_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = movePanelTab)]
|
||||
pub fn move_panel_tab(&self, source_group: u64, target_group: u64, insert_index: usize) {
|
||||
let message = PortfolioMessage::MovePanelTab {
|
||||
source_group: PanelGroupId(source_group),
|
||||
target_group: PanelGroupId(target_group),
|
||||
insert_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = setPanelGroupActiveTab)]
|
||||
pub fn set_panel_group_active_tab(&self, group: u64, tab_index: usize) {
|
||||
let message = PortfolioMessage::SetPanelGroupActiveTab {
|
||||
group: PanelGroupId(group),
|
||||
tab_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = splitPanelGroup)]
|
||||
pub fn split_panel_group(&self, target_group: u64, direction: DockingSplitDirection, tabs: Vec<PanelType>, active_tab_index: usize) {
|
||||
let message = PortfolioMessage::SplitPanelGroup {
|
||||
target_group: PanelGroupId(target_group),
|
||||
direction,
|
||||
tabs,
|
||||
active_tab_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = setPanelGroupSizes)]
|
||||
pub fn set_panel_group_sizes(&self, split_path: Vec<u32>, sizes: Vec<f64>) {
|
||||
let split_path = split_path.into_iter().map(|i| i as usize).collect();
|
||||
let message = PortfolioMessage::SetPanelGroupSizes { split_path, sizes };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = closeDocumentWithConfirmation)]
|
||||
pub fn close_document_with_confirmation(&self, document_id: u64) {
|
||||
let document_id = DocumentId(document_id);
|
||||
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, localized_commit_year: String) {
|
||||
let message = DialogMessage::RequestAboutGraphiteDialogWithLocalizedCommitDate {
|
||||
localized_commit_date,
|
||||
localized_commit_year,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = requestLicensesThirdPartyDialogWithLicenseText)]
|
||||
pub fn request_licenses_third_party_dialog_with_license_text(&self, license_text: String) {
|
||||
let message = DialogMessage::RequestLicensesThirdPartyDialogWithLicenseText { license_text };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Send new viewport info to the backend
|
||||
#[wasm_bindgen(js_name = updateViewport)]
|
||||
pub fn update_viewport(&self, x: f64, y: f64, width: f64, height: f64, scale: f64) {
|
||||
let message = ViewportMessage::Update { x, y, width, height, scale };
|
||||
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: f64, wheel_delta_y: f64, wheel_delta_z: f64, 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 shaken
|
||||
#[wasm_bindgen(js_name = onMouseShake)]
|
||||
pub fn on_mouse_shake(&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::PointerShake { 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, key_repeat: bool) {
|
||||
let key = translate_key(&name);
|
||||
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
||||
|
||||
trace!("Key down {key:?}, name: {name}, modifiers: {modifiers:?}, key repeat: {key_repeat}");
|
||||
|
||||
let message = InputPreprocessorMessage::KeyDown { key, key_repeat, modifier_keys };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// A keyboard button released
|
||||
#[wasm_bindgen(js_name = onKeyUp)]
|
||||
pub fn on_key_up(&self, name: String, modifiers: u8, key_repeat: bool) {
|
||||
let key = translate_key(&name);
|
||||
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("Invalid modifier keys");
|
||||
|
||||
trace!("Key up {key:?}, name: {name}, modifiers: {modifier_keys:?}, key repeat: {key_repeat}");
|
||||
|
||||
let message = InputPreprocessorMessage::KeyUp { key, key_repeat, modifier_keys };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// A text box was committed
|
||||
#[wasm_bindgen(js_name = onChangeText)]
|
||||
pub fn on_change_text(&self, new_text: String, is_left_or_right_click: bool) -> Result<(), JsValue> {
|
||||
let message = TextToolMessage::TextChange { new_text, is_left_or_right_click };
|
||||
self.dispatch(message);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Dialog got dismissed
|
||||
#[wasm_bindgen(js_name = onDialogDismiss)]
|
||||
pub fn on_dialog_dismiss(&self) {
|
||||
let message = DialogMessage::Dismiss;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// 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(())
|
||||
}
|
||||
|
||||
/// Update primary color from sRGB bytes (the wire format at the JS boundary).
|
||||
#[wasm_bindgen(js_name = updatePrimaryColor)]
|
||||
pub fn update_primary_color(&self, color: SRGBA8) {
|
||||
self.dispatch(ToolMessage::SelectWorkingColor {
|
||||
color: Color::from(color),
|
||||
primary: true,
|
||||
});
|
||||
}
|
||||
|
||||
/// Update secondary color from sRGB bytes (the wire format at the JS boundary).
|
||||
#[wasm_bindgen(js_name = updateSecondaryColor)]
|
||||
pub fn update_secondary_color(&self, color: SRGBA8) {
|
||||
self.dispatch(ToolMessage::SelectWorkingColor {
|
||||
color: Color::from(color),
|
||||
primary: false,
|
||||
});
|
||||
}
|
||||
|
||||
/// Initialize the Rust color picker handler with a starting value (used when the frontend `<ColorPicker />` opens).
|
||||
#[wasm_bindgen(js_name = openColorPicker)]
|
||||
pub fn open_color_picker(&self, initial_value: FillChoiceUI, allow_none: bool, disabled: bool) {
|
||||
let initial_value = FillChoice::from(&initial_value);
|
||||
self.dispatch(ColorPickerMessage::Open { initial_value, allow_none, disabled });
|
||||
}
|
||||
|
||||
/// Tell the Rust color picker handler that the popover is closing.
|
||||
#[wasm_bindgen(js_name = closeColorPicker)]
|
||||
pub fn close_color_picker(&self) {
|
||||
self.dispatch(ColorPickerMessage::Close);
|
||||
}
|
||||
|
||||
/// Update the color of the currently-edited gradient stop, from sRGB bytes (the wire format at the JS boundary).
|
||||
#[wasm_bindgen(js_name = updateGradientStopColor)]
|
||||
pub fn update_gradient_stop_color(&self, color: SRGBA8) {
|
||||
self.dispatch(GradientToolMessage::UpdateStopColor { color: Color::from(color) });
|
||||
}
|
||||
|
||||
/// Start a new undo transaction for gradient stop color editing
|
||||
#[wasm_bindgen(js_name = startGradientStopColorTransaction)]
|
||||
pub fn start_gradient_stop_color_transaction(&self) {
|
||||
self.dispatch(GradientToolMessage::StartTransactionForColorStop);
|
||||
}
|
||||
|
||||
/// Commit the current gradient stop color transaction (called on pointer-up after each drag/click)
|
||||
#[wasm_bindgen(js_name = commitGradientStopColorTransaction)]
|
||||
pub fn commit_gradient_stop_color_transaction(&self) {
|
||||
self.dispatch(GradientToolMessage::CommitTransactionForColorStop);
|
||||
}
|
||||
|
||||
/// Close the gradient stop color picker and commit any pending transaction
|
||||
#[wasm_bindgen(js_name = closeGradientStopColorPicker)]
|
||||
pub fn close_gradient_stop_color_picker(&self) {
|
||||
self.dispatch(GradientToolMessage::CloseStopColorPicker);
|
||||
}
|
||||
|
||||
/// Toggle clipping the alpha of a layer to the alpha of the layer below it in the layer stack
|
||||
#[wasm_bindgen(js_name = clipLayer)]
|
||||
pub fn clip_layer(&self, id: u64) {
|
||||
let id = NodeId(id);
|
||||
let message = DocumentMessage::ClipLayer { id };
|
||||
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, id: u64, ctrl: bool, shift: bool) {
|
||||
let id = NodeId(id);
|
||||
let message = DocumentMessage::SelectLayer { id, 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 within a folder and placed down at the given index.
|
||||
/// If the folder is `None`, it is inserted into the document root.
|
||||
/// If the insert index is `None`, it is inserted at the start of the folder.
|
||||
#[wasm_bindgen(js_name = moveLayerInTree)]
|
||||
pub fn move_layer_in_tree(&self, insert_parent_id: Option<u64>, insert_index: Option<usize>) {
|
||||
let insert_parent_id = insert_parent_id.map(NodeId);
|
||||
let parent = insert_parent_id.map(LayerNodeIdentifier::new_unchecked).unwrap_or_default();
|
||||
|
||||
let message = DocumentMessage::MoveSelectedLayersTo {
|
||||
parent,
|
||||
insert_index: insert_index.unwrap_or_default(),
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Reorder a draggable Properties panel section to the given index among its peers.
|
||||
#[wasm_bindgen(js_name = reorderPropertiesSection)]
|
||||
pub fn reorder_properties_section(&self, node_id: u64, insert_index: usize) {
|
||||
self.dispatch(DocumentMessage::ReorderPropertiesSection {
|
||||
node_id: NodeId(node_id),
|
||||
insert_index,
|
||||
});
|
||||
}
|
||||
|
||||
/// Duplicate the selected layers, placing the copies within the given folder at the given index.
|
||||
/// If the folder is `None`, they are inserted into the document root.
|
||||
/// If the insert index is `None`, they are inserted at the start of the folder.
|
||||
#[wasm_bindgen(js_name = duplicateLayerInTree)]
|
||||
pub fn duplicate_layer_in_tree(&self, insert_parent_id: Option<u64>, insert_index: Option<usize>) {
|
||||
let message = DocumentMessage::DuplicateSelectedLayersTo {
|
||||
parent: insert_parent_id.map(NodeId).map(LayerNodeIdentifier::new_unchecked).unwrap_or_default(),
|
||||
insert_index: insert_index.unwrap_or_default(),
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Set the name for the layer
|
||||
#[wasm_bindgen(js_name = setLayerName)]
|
||||
pub fn set_layer_name(&self, id: u64, name: String) {
|
||||
let layer = LayerNodeIdentifier::new_unchecked(NodeId(id));
|
||||
let message = NodeGraphMessage::SetDisplayName {
|
||||
node_id: layer.to_node(),
|
||||
network_path: Vec::new(),
|
||||
alias: name,
|
||||
skip_adding_history_step: false,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
#[wasm_bindgen(js_name = panCanvasAbortPrepare)]
|
||||
pub fn pan_canvas_abort_prepare(&self, x_not_y_axis: bool) {
|
||||
let message = NavigationMessage::CanvasPanAbortPrepare { x_not_y_axis };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = panCanvasAbort)]
|
||||
pub fn pan_canvas_abort(&self, x_not_y_axis: bool) {
|
||||
let message = NavigationMessage::CanvasPanAbort { x_not_y_axis };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
#[wasm_bindgen(js_name = panCanvas)]
|
||||
pub fn pan_canvas(&self, delta_x: f64, delta_y: f64) {
|
||||
let message = NavigationMessage::CanvasPan { delta: (delta_x, delta_y).into() };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Translates document (in viewport coords)
|
||||
#[wasm_bindgen(js_name = panCanvasByFraction)]
|
||||
pub fn pan_canvas_by_fraction(&self, delta_x: f64, delta_y: f64) {
|
||||
let message = NavigationMessage::CanvasPanByViewportFraction { delta: (delta_x, delta_y).into() };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Merge the selected nodes into a subnetwork
|
||||
#[wasm_bindgen(js_name = mergeSelectedNodes)]
|
||||
pub fn merge_nodes(&self) {
|
||||
let message = NodeGraphMessage::MergeSelectedNodes;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Toggle lock state of all selected layers
|
||||
#[wasm_bindgen(js_name = toggleSelectedLocked)]
|
||||
pub fn toggle_selected_locked(&self) {
|
||||
let message = NodeGraphMessage::ToggleSelectedLocked;
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Creates a new document node in the node graph
|
||||
#[wasm_bindgen(js_name = createNode)]
|
||||
pub fn create_node(&self, node_type: JsValue, x: i32, y: i32) {
|
||||
let value: serde_json::Value = serde_wasm_bindgen::from_value(node_type).unwrap();
|
||||
|
||||
let id = NodeId::new();
|
||||
let message = NodeGraphMessage::CreateNodeFromContextMenu {
|
||||
node_id: Some(id),
|
||||
node_type: value.into(),
|
||||
xy: Some((x / 24, y / 24)),
|
||||
add_transaction: true,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Respond to selection read
|
||||
#[wasm_bindgen(js_name = readSelection)]
|
||||
pub fn read_selection(&self, content: Option<String>, cut: bool) {
|
||||
let message = ClipboardMessage::ReadSelection { content, cut };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Paste from a serialized JSON representation
|
||||
#[wasm_bindgen(js_name = pasteText)]
|
||||
pub fn paste_text(&self, data: String) {
|
||||
let message = ClipboardMessage::ReadClipboard {
|
||||
content: ClipboardContentRaw::Text(data),
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Pastes an image
|
||||
#[wasm_bindgen(js_name = pasteImage)]
|
||||
pub fn paste_image(
|
||||
&self,
|
||||
name: Option<String>,
|
||||
image_data: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
mouse_x: Option<f64>,
|
||||
mouse_y: Option<f64>,
|
||||
insert_parent_id: Option<u64>,
|
||||
insert_index: Option<usize>,
|
||||
) {
|
||||
let mouse = mouse_x.and_then(|x| mouse_y.map(|y| (x, y)));
|
||||
let image = graphene_std::raster::Image::from_image_data(&image_data, width, height);
|
||||
|
||||
let parent_and_insert_index = if let (Some(insert_parent_id), Some(insert_index)) = (insert_parent_id, insert_index) {
|
||||
let insert_parent_id = NodeId(insert_parent_id);
|
||||
let parent = LayerNodeIdentifier::new_unchecked(insert_parent_id);
|
||||
Some((parent, insert_index))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let message = PortfolioMessage::InsertImage {
|
||||
name,
|
||||
image,
|
||||
mouse,
|
||||
parent_and_insert_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Pastes an SVG given its string representation
|
||||
#[wasm_bindgen(js_name = pasteSvg)]
|
||||
pub fn paste_svg(&self, name: Option<String>, svg: String, mouse_x: Option<f64>, mouse_y: Option<f64>, insert_parent_id: Option<u64>, insert_index: Option<usize>) {
|
||||
let mouse = mouse_x.and_then(|x| mouse_y.map(|y| (x, y)));
|
||||
|
||||
let parent_and_insert_index = if let (Some(insert_parent_id), Some(insert_index)) = (insert_parent_id, insert_index) {
|
||||
let insert_parent_id = NodeId(insert_parent_id);
|
||||
let parent = LayerNodeIdentifier::new_unchecked(insert_parent_id);
|
||||
Some((parent, insert_index))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let message = PortfolioMessage::InsertSvg {
|
||||
name,
|
||||
svg,
|
||||
mouse,
|
||||
parent_and_insert_index,
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Toggle visibility of a layer or node given its node ID
|
||||
#[wasm_bindgen(js_name = toggleNodeVisibilityLayerPanel)]
|
||||
pub fn toggle_node_visibility_layer(&self, id: u64) {
|
||||
let node_id = NodeId(id);
|
||||
let message = NodeGraphMessage::ToggleVisibility { node_id, network_path: Vec::new() };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Pin or unpin a node given its node ID
|
||||
#[wasm_bindgen(js_name = setNodePinned)]
|
||||
pub fn set_node_pinned(&self, id: u64, pinned: bool) {
|
||||
self.dispatch(DocumentMessage::SetNodePinned { node_id: NodeId(id), pinned });
|
||||
}
|
||||
|
||||
/// Collapse or expand a node's section in the Properties panel
|
||||
#[wasm_bindgen(js_name = toggleNodePropertiesSectionExpanded)]
|
||||
pub fn toggle_node_properties_section_expanded(&self, id: u64) {
|
||||
self.dispatch(DocumentMessage::ToggleNodePropertiesSectionExpanded { node_id: NodeId(id) });
|
||||
}
|
||||
|
||||
/// Delete a layer or node given its node ID
|
||||
#[wasm_bindgen(js_name = deleteNode)]
|
||||
pub fn delete_node(&self, id: u64) {
|
||||
self.dispatch(DocumentMessage::DeleteNode { node_id: NodeId(id) });
|
||||
}
|
||||
|
||||
/// Toggle lock state of a layer from the layer list
|
||||
#[wasm_bindgen(js_name = toggleLayerLock)]
|
||||
pub fn toggle_layer_lock(&self, node_id: u64) {
|
||||
let message = NodeGraphMessage::ToggleLocked {
|
||||
node_id: NodeId(node_id),
|
||||
network_path: Vec::new(),
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Toggle expansions state of a layer from the layer list
|
||||
#[wasm_bindgen(js_name = toggleLayerExpansion)]
|
||||
pub fn toggle_layer_expansion(&self, tree_path: &[u64], recursive: bool) {
|
||||
let tree_path = tree_path.iter().map(|&id| NodeId(id)).collect();
|
||||
let message = DocumentMessage::ToggleLayerExpansion { tree_path, recursive };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Set the active panel to the most recently clicked panel
|
||||
#[wasm_bindgen(js_name = setActivePanel)]
|
||||
pub fn set_active_panel(&self, panel: String) {
|
||||
let message = DocumentMessage::SetActivePanel { active_panel: panel.into() };
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Toggle display type for a layer
|
||||
#[wasm_bindgen(js_name = setToNodeOrLayer)]
|
||||
pub fn set_to_node_or_layer(&self, id: u64, is_layer: bool) {
|
||||
self.dispatch(DocumentMessage::SetToNodeOrLayer { node_id: NodeId(id), is_layer });
|
||||
}
|
||||
|
||||
/// Set the name of an import or export
|
||||
#[wasm_bindgen(js_name = setImportName)]
|
||||
pub fn set_import_name(&self, index: usize, name: String) {
|
||||
let message = NodeGraphMessage::SetImportExportName {
|
||||
name,
|
||||
index: ImportOrExport::Import(index),
|
||||
};
|
||||
self.dispatch(message);
|
||||
}
|
||||
|
||||
/// Set the name of an export
|
||||
#[wasm_bindgen(js_name = setExportName)]
|
||||
pub fn set_export_name(&self, index: usize, name: String) {
|
||||
let message = NodeGraphMessage::SetImportExportName {
|
||||
name,
|
||||
index: ImportOrExport::Export(index),
|
||||
};
|
||||
self.dispatch(message);
|
||||
#[cfg(feature = "native")]
|
||||
#[wasm_bindgen(js_name = loadPersistedState)]
|
||||
pub fn load_persisted_state(&self, _state: JsValue) {
|
||||
log::error!("loadPersistedState is unavailable on desktop; persistence is handled natively");
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
#[cfg(not(feature = "native"))]
|
||||
use crate::EDITOR;
|
||||
#[cfg(not(feature = "native"))]
|
||||
use crate::EDITOR_HAS_CRASHED;
|
||||
use crate::EDITOR_WRAPPER;
|
||||
use crate::editor_wrapper::EditorWrapper;
|
||||
use crate::{EDITOR_HAS_CRASHED, EDITOR_WRAPPER};
|
||||
use crate::native_communication::RasterizedImage;
|
||||
#[cfg(not(feature = "native"))]
|
||||
use editor::application::Editor;
|
||||
#[cfg(feature = "editor")]
|
||||
use editor::messages::input_mapper::utility_types::input_keyboard::Key;
|
||||
#[cfg(not(feature = "native"))]
|
||||
use editor::messages::prelude::*;
|
||||
use graphene_std::color::SRGBA8;
|
||||
use graphene_std::raster::Image;
|
||||
use js_sys::{Object, Reflect};
|
||||
#[cfg(not(feature = "native"))]
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
use wasm_bindgen::JsCast;
|
||||
@@ -119,18 +123,8 @@ pub(crate) fn async_wake_callback() -> Wake {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn auto_save_all_documents() {
|
||||
// Process no further messages after a crash to avoid spamming the console
|
||||
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
|
||||
wrapper(|wrapper| {
|
||||
wrapper.dispatch(PortfolioMessage::AutoSaveAllDocuments);
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn render_image_data_to_canvases(image_data: &[(u64, Image<SRGBA8>)]) {
|
||||
/// Blits each rasterized image to a canvas registered on `window.imageCanvases`
|
||||
pub(crate) fn render_image_data_to_canvases<'a>(image_data: impl IntoIterator<Item = &'a RasterizedImage>) {
|
||||
let window = match window() {
|
||||
Some(window) => window,
|
||||
None => {
|
||||
@@ -155,11 +149,13 @@ pub(crate) fn render_image_data_to_canvases(image_data: &[(u64, Image<SRGBA8>)])
|
||||
};
|
||||
let canvases_obj = Object::from(canvases_obj);
|
||||
|
||||
for (placeholder_id, image) in image_data.iter() {
|
||||
for image in image_data {
|
||||
let (placeholder_id, width, height) = (image.id, image.width, image.height);
|
||||
let pixels = image.pixels.as_slice();
|
||||
let canvas_name = placeholder_id.to_string();
|
||||
let js_key = JsValue::from_str(&canvas_name);
|
||||
|
||||
if Reflect::has(&canvases_obj, &js_key).unwrap_or(false) || image.width == 0 || image.height == 0 {
|
||||
if Reflect::has(&canvases_obj, &js_key).unwrap_or(false) || width == 0 || height == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -169,8 +165,8 @@ pub(crate) fn render_image_data_to_canvases(image_data: &[(u64, Image<SRGBA8>)])
|
||||
.dyn_into::<HtmlCanvasElement>()
|
||||
.expect("Failed to cast element to HtmlCanvasElement");
|
||||
|
||||
canvas.set_width(image.width);
|
||||
canvas.set_height(image.height);
|
||||
canvas.set_width(width);
|
||||
canvas.set_height(height);
|
||||
|
||||
let context: CanvasRenderingContext2d = canvas
|
||||
.get_context("2d")
|
||||
@@ -178,10 +174,8 @@ pub(crate) fn render_image_data_to_canvases(image_data: &[(u64, Image<SRGBA8>)])
|
||||
.expect("2d context was not found")
|
||||
.dyn_into::<CanvasRenderingContext2d>()
|
||||
.expect("Failed to cast context to CanvasRenderingContext2d");
|
||||
// `SRGBA8` is `#[repr(C)]` of four `u8`s, so the data buffer is already the byte format the canvas expects
|
||||
let u8_data: &[u8] = bytemuck::cast_slice(&image.data);
|
||||
let clamped_u8_data = wasm_bindgen::Clamped(u8_data);
|
||||
match ImageData::new_with_u8_clamped_array_and_sh(clamped_u8_data, image.width, image.height) {
|
||||
let clamped_pixels = wasm_bindgen::Clamped(pixels);
|
||||
match ImageData::new_with_u8_clamped_array_and_sh(clamped_pixels, width, height) {
|
||||
Ok(image_data_obj) => {
|
||||
if context.put_image_data(&image_data_obj, 0, 0).is_err() {
|
||||
error!("Failed to put image data on canvas for id: {placeholder_id}");
|
||||
@@ -209,6 +203,7 @@ pub(crate) fn calculate_hash<T: std::hash::Hash>(t: &T) -> u64 {
|
||||
}
|
||||
|
||||
/// Translate a keyboard key from its JS name to its Rust `Key` enum
|
||||
#[cfg(feature = "editor")]
|
||||
pub(crate) fn translate_key(name: &str) -> Key {
|
||||
use Key::*;
|
||||
|
||||
|
||||
+69
-39
@@ -4,11 +4,18 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod editor_commands;
|
||||
pub mod editor_wrapper;
|
||||
pub mod helpers;
|
||||
pub mod native_communication;
|
||||
mod wasm_value;
|
||||
|
||||
#[cfg(any(feature = "native", not(target_family = "wasm")))]
|
||||
pub use editor_commands::EditorCommand;
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
use crate::helpers::wrapper;
|
||||
#[cfg(feature = "editor")]
|
||||
use editor::messages::prelude::*;
|
||||
use std::panic;
|
||||
use std::sync::Mutex;
|
||||
@@ -24,6 +31,7 @@ pub static LOGGER: WasmLog = WasmLog;
|
||||
thread_local! {
|
||||
#[cfg(not(feature = "native"))]
|
||||
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
|
||||
#[cfg(not(feature = "native"))]
|
||||
pub static MESSAGE_BUFFER: std::cell::RefCell<Vec<Message>> = const { std::cell::RefCell::new(Vec::new()) };
|
||||
pub static EDITOR_WRAPPER: Mutex<Option<editor_wrapper::EditorWrapper>> = const { Mutex::new(None) };
|
||||
pub static PANIC_DIALOG_MESSAGE_CALLBACK: std::cell::RefCell<Option<js_sys::Function>> = const { std::cell::RefCell::new(None) };
|
||||
@@ -43,36 +51,41 @@ pub fn init_graphite() {
|
||||
/// When a panic occurs, notify the user and log the error to the JS console before the backend dies
|
||||
pub fn panic_hook(info: &panic::PanicHookInfo) {
|
||||
let info = info.to_string();
|
||||
let backtrace = Error::new("stack").stack().to_string();
|
||||
if backtrace.contains("DynAnyNode") {
|
||||
log::error!("Node graph evaluation panicked {info}");
|
||||
|
||||
// When the graph panics, the node runtime lock may not be released properly
|
||||
if editor::node_graph_executor::NODE_RUNTIME.try_lock().is_none() {
|
||||
unsafe { editor::node_graph_executor::NODE_RUNTIME.force_unlock() };
|
||||
// Node graph panics can only originate here when the node graph runs inside this wasm module
|
||||
#[cfg(feature = "editor")]
|
||||
{
|
||||
let backtrace = Error::new("stack").stack().to_string();
|
||||
if backtrace.contains("DynAnyNode") {
|
||||
log::error!("Node graph evaluation panicked {info}");
|
||||
|
||||
// When the graph panics, the node runtime lock may not be released properly
|
||||
if editor::node_graph_executor::NODE_RUNTIME.try_lock().is_none() {
|
||||
unsafe { editor::node_graph_executor::NODE_RUNTIME.force_unlock() };
|
||||
}
|
||||
|
||||
if !NODE_GRAPH_ERROR_DISPLAYED.load(Ordering::SeqCst) {
|
||||
NODE_GRAPH_ERROR_DISPLAYED.store(true, Ordering::SeqCst);
|
||||
wrapper(|wrapper| {
|
||||
let error = r#"
|
||||
<rect x="50%" y="50%" width="600" height="100" transform="translate(-300 -50)" rx="4" fill="var(--color-error-red)" />
|
||||
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="18" fill="var(--color-2-mildblack)">
|
||||
<tspan x="50%" dy="-24" font-weight="bold">The document crashed while being rendered in its current state.</tspan>
|
||||
<tspan x="50%" dy="24">The editor is now unstable! Undo your last action to restore the artwork,</tspan>
|
||||
<tspan x="50%" dy="24">then save your document and restart the editor before continuing work.</tspan>
|
||||
/text>"#
|
||||
// It's a mystery why the `/text>` tag above needs to be missing its `<`, but when it exists it prints the `<` character in the text. However this works with it removed.
|
||||
.to_string();
|
||||
wrapper.send_frontend_message_to_js(FrontendMessage::UpdateDocumentArtwork { svg: error });
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if !NODE_GRAPH_ERROR_DISPLAYED.load(Ordering::SeqCst) {
|
||||
NODE_GRAPH_ERROR_DISPLAYED.store(true, Ordering::SeqCst);
|
||||
wrapper(|wrapper| {
|
||||
let error = r#"
|
||||
<rect x="50%" y="50%" width="600" height="100" transform="translate(-300 -50)" rx="4" fill="var(--color-error-red)" />
|
||||
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="18" fill="var(--color-2-mildblack)">
|
||||
<tspan x="50%" dy="-24" font-weight="bold">The document crashed while being rendered in its current state.</tspan>
|
||||
<tspan x="50%" dy="24">The editor is now unstable! Undo your last action to restore the artwork,</tspan>
|
||||
<tspan x="50%" dy="24">then save your document and restart the editor before continuing work.</tspan>
|
||||
/text>"#
|
||||
// It's a mystery why the `/text>` tag above needs to be missing its `<`, but when it exists it prints the `<` character in the text. However this works with it removed.
|
||||
.to_string();
|
||||
wrapper.send_frontend_message_to_js_rust_proxy(FrontendMessage::UpdateDocumentArtwork { svg: error });
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
EDITOR_HAS_CRASHED.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
EDITOR_HAS_CRASHED.store(true, Ordering::SeqCst);
|
||||
|
||||
log::error!("{info}");
|
||||
|
||||
// Prefer using the raw JS callback to avoid mutex lock contention inside the panic hook.
|
||||
@@ -82,30 +95,20 @@ pub fn panic_hook(info: &panic::PanicHookInfo) {
|
||||
}
|
||||
|
||||
fn send_panic_dialog_via_callback(panic_info: String) -> Result<(), String> {
|
||||
let message = FrontendMessage::DisplayDialogPanic { panic_info };
|
||||
let message_type = message.to_discriminant().local_name();
|
||||
let message = PanicDialogMessage::DisplayDialogPanic { panic_info: panic_info.clone() };
|
||||
let Ok(message_data) = serde_wasm_bindgen::to_value(&message) else {
|
||||
log::error!("Failed to serialize crash dialog panic message");
|
||||
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
|
||||
unreachable!("Message variant changed unexpectedly")
|
||||
};
|
||||
return Err(panic_info);
|
||||
};
|
||||
|
||||
PANIC_DIALOG_MESSAGE_CALLBACK.with(|callback| {
|
||||
let callback_ref = callback.borrow();
|
||||
let Some(callback) = callback_ref.as_ref() else {
|
||||
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
|
||||
unreachable!("Message variant changed unexpectedly")
|
||||
};
|
||||
return Err(panic_info);
|
||||
};
|
||||
|
||||
if let Err(error) = callback.call2(&JsValue::null(), &JsValue::from(message_type), &message_data) {
|
||||
log::error!("Failed to send crash dialog panic message to JS: {:?}", error);
|
||||
let FrontendMessage::DisplayDialogPanic { panic_info } = message else {
|
||||
unreachable!("Message variant changed unexpectedly")
|
||||
};
|
||||
if let Err(error) = callback.call2(&JsValue::null(), &JsValue::from("DisplayDialogPanic"), &message_data) {
|
||||
log::error!("Failed to send crash dialog panic message to JS: {error:?}");
|
||||
return Err(panic_info);
|
||||
}
|
||||
|
||||
@@ -201,3 +204,30 @@ impl log::Log for WasmLog {
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
use editor::messages::prelude::FrontendMessage as PanicDialogMessage;
|
||||
|
||||
#[cfg(not(feature = "editor"))]
|
||||
use PanicDialogMessageCopy as PanicDialogMessage;
|
||||
|
||||
#[cfg(any(not(feature = "editor"), test))]
|
||||
#[derive(serde::Serialize)]
|
||||
enum PanicDialogMessageCopy {
|
||||
DisplayDialogPanic {
|
||||
#[serde(rename = "panicInfo")]
|
||||
panic_info: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "editor"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn panic_dialog_copy_matches_editor_shape() {
|
||||
let copy = serde_json::to_value(PanicDialogMessageCopy::DisplayDialogPanic { panic_info: "info".into() }).unwrap();
|
||||
let real = serde_json::to_value(FrontendMessage::DisplayDialogPanic { panic_info: "info".into() }).unwrap();
|
||||
assert_eq!(copy, real);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,77 @@
|
||||
use crate::editor_wrapper::EditorWrapper;
|
||||
use crate::helpers::wrapper;
|
||||
use editor::messages::prelude::FrontendMessage;
|
||||
//! Messaging with the native process. Serialized `FrontendMessage`s come in and are forwarded to JS,
|
||||
//! `EditorCommand`s go back out. Encoding and decoding both live here so the wire format stays in one place.
|
||||
|
||||
use crate::editor_wrapper::{EditorWrapper, IMAGE_DATA_HASH};
|
||||
use crate::helpers::{calculate_hash, render_image_data_to_canvases, wrapper};
|
||||
use crate::wasm_value::WasmValue;
|
||||
use js_sys::{ArrayBuffer, Uint8Array};
|
||||
use std::sync::atomic::Ordering;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen(js_name = "receiveNativeMessage")]
|
||||
pub fn receive_native_message(buffer: ArrayBuffer) {
|
||||
let buffer = Uint8Array::new(buffer.as_ref()).to_vec();
|
||||
match ron::from_str::<Vec<FrontendMessage>>(str::from_utf8(buffer.as_slice()).unwrap()) {
|
||||
match serde_json::from_slice::<Vec<(String, String)>>(&buffer) {
|
||||
Ok(messages) => {
|
||||
let callback = move |wrapper: &mut EditorWrapper| {
|
||||
for message in messages {
|
||||
wrapper.send_frontend_message_to_js_rust_proxy(message);
|
||||
wrapper(move |wrapper: &mut EditorWrapper| {
|
||||
for (name, data) in messages {
|
||||
match name.as_str() {
|
||||
"UpdateImageData" => {
|
||||
let new_hash = calculate_hash(&data);
|
||||
if IMAGE_DATA_HASH.swap(new_hash, Ordering::Relaxed) == new_hash {
|
||||
continue;
|
||||
}
|
||||
|
||||
match serde_json::from_str::<Vec<RasterizedImage>>(&data) {
|
||||
Ok(image_data) => render_image_data_to_canvases(image_data.iter()),
|
||||
Err(e) => error!("Failed to deserialize image data: {e:?}"),
|
||||
}
|
||||
}
|
||||
_ => match serde_json::from_str::<WasmValue>(&data) {
|
||||
Ok(value) => wrapper.forward_serialized_frontend_message_to_js(&name, value),
|
||||
Err(e) => log::error!("Failed to deserialize frontend message {name}: {e:?}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
wrapper(callback);
|
||||
});
|
||||
}
|
||||
Err(e) => log::error!("Failed to deserialize frontend messages: {e:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
pub fn encode_frontend_messages(messages: Vec<editor::messages::prelude::FrontendMessage>) -> Option<Vec<u8>> {
|
||||
use editor::messages::prelude::FrontendMessage;
|
||||
use editor::utility_traits::{AsMessage, ToDiscriminant};
|
||||
|
||||
let messages: Vec<(String, String)> = messages
|
||||
.into_iter()
|
||||
.map(|message| {
|
||||
let name = message.to_discriminant().local_name();
|
||||
let data = match message {
|
||||
FrontendMessage::UpdateImageData { image_data } => serde_json::to_string(&image_data).inspect_err(|e| log::error!("Failed to serialize {name} payload: {e}")).ok()?,
|
||||
message => crate::wasm_value::encode(&message)
|
||||
.inspect_err(|e| log::error!("Failed to serialize FrontendMessage {name}: {e}"))
|
||||
.ok()?,
|
||||
};
|
||||
Some((name, data))
|
||||
})
|
||||
.collect::<Option<_>>()?;
|
||||
|
||||
serde_json::to_vec(&messages).ok()
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "editor", any(feature = "native", not(target_family = "wasm"))))]
|
||||
pub fn decode_editor_command(data: &[u8]) -> Option<editor::messages::prelude::Message> {
|
||||
match serde_json::from_slice::<crate::EditorCommand>(data) {
|
||||
Ok(command) => Some(command.into()),
|
||||
Err(e) => {
|
||||
log::error!("Failed to deserialize editor command: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn initialize_native_communication() {
|
||||
let global = js_sys::global();
|
||||
|
||||
@@ -44,3 +96,38 @@ pub fn send_message_to_cef(message: String) {
|
||||
// Call it with argument
|
||||
func.call1(&JsValue::NULL, &JsValue::from(buffer)).expect("Function call failed");
|
||||
}
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
pub(crate) use editor::messages::frontend::utility_types::RasterizedImage;
|
||||
|
||||
#[cfg(not(feature = "editor"))]
|
||||
pub(crate) use RasterizedImageCopy as RasterizedImage;
|
||||
|
||||
#[cfg(any(not(feature = "editor"), test))]
|
||||
#[derive(serde::Deserialize)]
|
||||
pub(crate) struct RasterizedImageCopy {
|
||||
pub(crate) id: u64,
|
||||
pub(crate) width: u32,
|
||||
pub(crate) height: u32,
|
||||
pub(crate) pixels: Vec<u8>,
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "editor"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rasterized_image_copy_matches_editor_shape() {
|
||||
let editor_image = RasterizedImage {
|
||||
id: 7,
|
||||
width: 2,
|
||||
height: 1,
|
||||
pixels: serde_bytes::ByteBuf::from(vec![1, 2, 3, 4, 5, 6, 7, 8]),
|
||||
};
|
||||
let json = serde_json::to_string(&vec![editor_image]).unwrap();
|
||||
let decoded: Vec<RasterizedImageCopy> = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(decoded[0].id, 7);
|
||||
assert_eq!((decoded[0].width, decoded[0].height), (2, 1));
|
||||
assert_eq!(decoded[0].pixels, [1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,448 @@
|
||||
//! Serializable stand-in for `JsValue`, used to send `FrontendMessage`s from the native editor process to JS.
|
||||
//! Keeps the types JSON can't represent (`BigInt`, `Uint8Array`, `Map`, `undefined`, `NaN`) so the wasm side
|
||||
//! can reconstruct the same `JsValue`s that `serde_wasm_bindgen` produces on web.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "editor")]
|
||||
use serde_json::Error;
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub(crate) enum WasmValue {
|
||||
Undefined,
|
||||
Bool(bool),
|
||||
Number(f64),
|
||||
NonFinite(NonFinite),
|
||||
Int(i64),
|
||||
UInt(u64),
|
||||
String(String),
|
||||
Bytes(Vec<u8>),
|
||||
Seq(Vec<WasmValue>),
|
||||
Object(Vec<(String, WasmValue)>),
|
||||
Map(Vec<(WasmValue, WasmValue)>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub(crate) enum NonFinite {
|
||||
Nan,
|
||||
PositiveInfinity,
|
||||
NegativeInfinity,
|
||||
}
|
||||
|
||||
impl From<NonFinite> for f64 {
|
||||
fn from(value: NonFinite) -> Self {
|
||||
match value {
|
||||
NonFinite::Nan => f64::NAN,
|
||||
NonFinite::PositiveInfinity => f64::INFINITY,
|
||||
NonFinite::NegativeInfinity => f64::NEG_INFINITY,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WasmValue> for JsValue {
|
||||
fn from(value: WasmValue) -> Self {
|
||||
match value {
|
||||
WasmValue::Undefined => JsValue::UNDEFINED,
|
||||
WasmValue::Bool(value) => JsValue::from_bool(value),
|
||||
WasmValue::Number(value) => JsValue::from_f64(value),
|
||||
WasmValue::NonFinite(value) => JsValue::from_f64(value.into()),
|
||||
WasmValue::Int(value) => JsValue::from(value),
|
||||
WasmValue::UInt(value) => JsValue::from(value),
|
||||
WasmValue::String(value) => JsValue::from_str(&value),
|
||||
WasmValue::Bytes(bytes) => js_sys::Uint8Array::from(bytes.as_slice()).into(),
|
||||
WasmValue::Seq(values) => {
|
||||
let array = js_sys::Array::new();
|
||||
for value in values {
|
||||
array.push(&value.into());
|
||||
}
|
||||
array.into()
|
||||
}
|
||||
WasmValue::Object(fields) => {
|
||||
let object = js_sys::Object::new();
|
||||
for (key, value) in fields {
|
||||
let _ = js_sys::Reflect::set(&object, &JsValue::from_str(&key), &value.into());
|
||||
}
|
||||
object.into()
|
||||
}
|
||||
WasmValue::Map(entries) => {
|
||||
let map = js_sys::Map::new();
|
||||
for (key, value) in entries {
|
||||
map.set(&key.into(), &value.into());
|
||||
}
|
||||
map.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
pub(crate) fn encode<T: Serialize + ?Sized>(value: &T) -> Result<String, serde_json::Error> {
|
||||
serde_json::to_string(&ser::to_value(value)?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "editor")]
|
||||
mod ser {
|
||||
use super::*;
|
||||
use serde::ser::{self, Error as _};
|
||||
|
||||
pub(super) fn to_value<T: Serialize + ?Sized>(value: &T) -> Result<WasmValue, Error> {
|
||||
value.serialize(ValueSerializer)
|
||||
}
|
||||
|
||||
struct ValueSerializer;
|
||||
|
||||
impl ser::Serializer for ValueSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
type SerializeSeq = SeqSerializer;
|
||||
type SerializeTuple = SeqSerializer;
|
||||
type SerializeTupleStruct = SeqSerializer;
|
||||
type SerializeTupleVariant = VariantSeqSerializer;
|
||||
type SerializeMap = MapSerializer;
|
||||
type SerializeStruct = ObjectSerializer;
|
||||
type SerializeStructVariant = VariantObjectSerializer;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Bool(v))
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Int(v))
|
||||
}
|
||||
|
||||
fn serialize_i128(self, v: i128) -> Result<WasmValue, Error> {
|
||||
if let Ok(v) = i64::try_from(v) {
|
||||
Ok(WasmValue::Int(v))
|
||||
} else {
|
||||
u64::try_from(v)
|
||||
.map(WasmValue::UInt)
|
||||
.map_err(|_| Error::custom(format!("i128 value {v} exceeds the wire BigInt range")))
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Number(v as f64))
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::UInt(v))
|
||||
}
|
||||
|
||||
fn serialize_u128(self, v: u128) -> Result<WasmValue, Error> {
|
||||
u64::try_from(v)
|
||||
.map(WasmValue::UInt)
|
||||
.map_err(|_| Error::custom(format!("u128 value {v} exceeds the wire BigInt range")))
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<WasmValue, Error> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<WasmValue, Error> {
|
||||
Ok(if v.is_finite() {
|
||||
WasmValue::Number(v)
|
||||
} else if v.is_nan() {
|
||||
WasmValue::NonFinite(NonFinite::Nan)
|
||||
} else if v > 0. {
|
||||
WasmValue::NonFinite(NonFinite::PositiveInfinity)
|
||||
} else {
|
||||
WasmValue::NonFinite(NonFinite::NegativeInfinity)
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::String(v.to_string()))
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::String(v.to_string()))
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Bytes(v.to_vec()))
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Undefined)
|
||||
}
|
||||
|
||||
fn serialize_some<T: Serialize + ?Sized>(self, value: &T) -> Result<WasmValue, Error> {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Undefined)
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Undefined)
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::String(variant.to_string()))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: Serialize + ?Sized>(self, _name: &'static str, value: &T) -> Result<WasmValue, Error> {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: Serialize + ?Sized>(self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Object(vec![(variant.to_string(), value.serialize(ValueSerializer)?)]))
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<SeqSerializer, Error> {
|
||||
Ok(SeqSerializer(Vec::with_capacity(len.unwrap_or_default())))
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<SeqSerializer, Error> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, _name: &'static str, len: usize) -> Result<SeqSerializer, Error> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize) -> Result<VariantSeqSerializer, Error> {
|
||||
Ok(VariantSeqSerializer {
|
||||
variant,
|
||||
elements: Vec::with_capacity(len),
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<MapSerializer, Error> {
|
||||
Ok(MapSerializer {
|
||||
entries: Vec::with_capacity(len.unwrap_or_default()),
|
||||
pending_key: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn serialize_struct(self, _name: &'static str, len: usize) -> Result<ObjectSerializer, Error> {
|
||||
Ok(ObjectSerializer(Vec::with_capacity(len)))
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize) -> Result<VariantObjectSerializer, Error> {
|
||||
Ok(VariantObjectSerializer {
|
||||
variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqSerializer(Vec<WasmValue>);
|
||||
|
||||
impl ser::SerializeSeq for SeqSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
|
||||
self.0.push(value.serialize(ValueSerializer)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Seq(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::SerializeTuple for SeqSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
|
||||
ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::SerializeTupleStruct for SeqSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
|
||||
ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct VariantSeqSerializer {
|
||||
variant: &'static str,
|
||||
elements: Vec<WasmValue>,
|
||||
}
|
||||
|
||||
impl ser::SerializeTupleVariant for VariantSeqSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
|
||||
self.elements.push(value.serialize(ValueSerializer)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Object(vec![(self.variant.to_string(), WasmValue::Seq(self.elements))]))
|
||||
}
|
||||
}
|
||||
|
||||
struct MapSerializer {
|
||||
entries: Vec<(WasmValue, WasmValue)>,
|
||||
pending_key: Option<WasmValue>,
|
||||
}
|
||||
|
||||
impl ser::SerializeMap for MapSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T: Serialize + ?Sized>(&mut self, key: &T) -> Result<(), Error> {
|
||||
self.pending_key = Some(key.serialize(ValueSerializer)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_value<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error> {
|
||||
let key = self.pending_key.take().ok_or_else(|| Error::custom("Map value serialized without a key"))?;
|
||||
self.entries.push((key, value.serialize(ValueSerializer)?));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Map(self.entries))
|
||||
}
|
||||
}
|
||||
|
||||
struct ObjectSerializer(Vec<(String, WasmValue)>);
|
||||
|
||||
impl ser::SerializeStruct for ObjectSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: Serialize + ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error> {
|
||||
self.0.push((key.to_string(), value.serialize(ValueSerializer)?));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Object(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
struct VariantObjectSerializer {
|
||||
variant: &'static str,
|
||||
fields: Vec<(String, WasmValue)>,
|
||||
}
|
||||
|
||||
impl ser::SerializeStructVariant for VariantObjectSerializer {
|
||||
type Ok = WasmValue;
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: Serialize + ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error> {
|
||||
self.fields.push((key.to_string(), value.serialize(ValueSerializer)?));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn end(self) -> Result<WasmValue, Error> {
|
||||
Ok(WasmValue::Object(vec![(self.variant.to_string(), WasmValue::Object(self.fields))]))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::wasm_value::encode;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum TestMessage {
|
||||
UnitVariant,
|
||||
StructVariant {
|
||||
#[serde(rename = "nodeId")]
|
||||
node_id: u64,
|
||||
width: u32,
|
||||
scale: f64,
|
||||
min: f64,
|
||||
max: f64,
|
||||
label: Option<String>,
|
||||
widths: std::collections::BTreeMap<u64, u32>,
|
||||
flags: Vec<bool>,
|
||||
bytes: serde_bytes::ByteBuf,
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirrors_serde_wasm_bindgen_representations() {
|
||||
assert_eq!(to_value(&TestMessage::UnitVariant).unwrap(), WasmValue::String("unitVariant".into()));
|
||||
|
||||
let message = TestMessage::StructVariant {
|
||||
node_id: u64::MAX,
|
||||
width: 8,
|
||||
scale: f64::NAN,
|
||||
min: f64::NEG_INFINITY,
|
||||
max: f64::INFINITY,
|
||||
label: None,
|
||||
widths: [(42, 7)].into(),
|
||||
flags: vec![true, false],
|
||||
bytes: serde_bytes::ByteBuf::from(vec![0, 255]),
|
||||
};
|
||||
let expected = WasmValue::Object(vec![(
|
||||
"structVariant".into(),
|
||||
WasmValue::Object(vec![
|
||||
("nodeId".into(), WasmValue::UInt(u64::MAX)),
|
||||
("width".into(), WasmValue::Number(8.)),
|
||||
("scale".into(), WasmValue::NonFinite(NonFinite::Nan)),
|
||||
("min".into(), WasmValue::NonFinite(NonFinite::NegativeInfinity)),
|
||||
("max".into(), WasmValue::NonFinite(NonFinite::PositiveInfinity)),
|
||||
("label".into(), WasmValue::Undefined),
|
||||
("widths".into(), WasmValue::Map(vec![(WasmValue::UInt(42), WasmValue::Number(7.))])),
|
||||
("flags".into(), WasmValue::Seq(vec![WasmValue::Bool(true), WasmValue::Bool(false)])),
|
||||
("bytes".into(), WasmValue::Bytes(vec![0, 255])),
|
||||
]),
|
||||
)]);
|
||||
assert_eq!(to_value(&message).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_decode_roundtrip() {
|
||||
let message = TestMessage::StructVariant {
|
||||
node_id: 1,
|
||||
width: 2,
|
||||
scale: f64::NAN,
|
||||
min: f64::NEG_INFINITY,
|
||||
max: 1.5,
|
||||
label: Some("hi".into()),
|
||||
widths: [(3, 4)].into(),
|
||||
flags: vec![true],
|
||||
bytes: serde_bytes::ByteBuf::from(vec![9]),
|
||||
};
|
||||
|
||||
let json: &str = &encode(&message).unwrap();
|
||||
let decoded: WasmValue = serde_json::from_str(json).unwrap();
|
||||
|
||||
assert_eq!(decoded, to_value(&message).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -309,6 +309,7 @@ macro_rules! tagged_value {
|
||||
pub fn from_type(input: &Type) -> Option<Self> {
|
||||
match input {
|
||||
Type::Generic(_) => None,
|
||||
Type::Ref(_) => None,
|
||||
Type::Concrete(concrete_type) => {
|
||||
let name = concrete_type.name.as_ref();
|
||||
// TODO: Add default implementations for types such as TaggedValue::Subpaths, and use the defaults here and in document_node_types
|
||||
@@ -569,6 +570,7 @@ impl TaggedValue {
|
||||
|
||||
match ty {
|
||||
Type::Generic(_) => None,
|
||||
Type::Ref(_) => None,
|
||||
Type::Concrete(concrete_type) => {
|
||||
let ty = concrete_type.id?;
|
||||
use std::any::TypeId;
|
||||
|
||||
@@ -262,7 +262,7 @@ impl BorrowTree {
|
||||
/// This ensures that no borrowed data can escape the node graph.
|
||||
pub async fn eval_tagged_value<I>(&self, id: NodeId, input: I) -> Result<TaggedValue, String>
|
||||
where
|
||||
I: StaticType + 'static + Send + Sync,
|
||||
I: StaticType + 'static + Send + Sync + std::panic::UnwindSafe,
|
||||
{
|
||||
let (node, _path) = self.nodes.get(&id).cloned().ok_or("Output node not found in executor")?;
|
||||
let output = node.eval(Box::new(input));
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
use std::cell::UnsafeCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
|
||||
pub struct Arena {
|
||||
generation: AtomicU64,
|
||||
offset: AtomicUsize,
|
||||
buf: Box<[UnsafeCell<MaybeUninit<u8>>]>,
|
||||
drops: Mutex<Vec<DropEntry>>,
|
||||
}
|
||||
|
||||
struct DropEntry {
|
||||
offset: usize,
|
||||
drop_fn: unsafe fn(*mut u8),
|
||||
}
|
||||
|
||||
// SAFETY: disjoint regions are handed out by an atomic bump; a region is written
|
||||
// only by its allocating caller before publication, and cross-thread hand-off is
|
||||
// ordered by the Release/Acquire pair on the published handle word.
|
||||
unsafe impl Sync for Arena {}
|
||||
unsafe impl Send for Arena {}
|
||||
|
||||
impl Arena {
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let buf = (0..capacity).map(|_| UnsafeCell::new(MaybeUninit::uninit())).collect();
|
||||
Self {
|
||||
// Starts at 1 so the null handle word (0) can never upgrade.
|
||||
generation: AtomicU64::new(1),
|
||||
offset: AtomicUsize::new(0),
|
||||
buf,
|
||||
drops: Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generation(&self) -> u64 {
|
||||
self.generation.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
fn base(&self) -> *mut u8 {
|
||||
self.buf.as_ptr() as *mut u8
|
||||
}
|
||||
|
||||
fn reserve(&self, size: usize, align: usize) -> Option<usize> {
|
||||
debug_assert!(align.is_power_of_two());
|
||||
let base = self.base() as usize;
|
||||
let mut start = 0;
|
||||
self.offset
|
||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
|
||||
// Alignment is computed on the absolute address; the backbone
|
||||
// allocation itself has no alignment guarantee.
|
||||
let addr = (base.checked_add(current)?.checked_add(align - 1)?) & !(align - 1);
|
||||
start = addr - base;
|
||||
let end = start.checked_add(size)?;
|
||||
(end <= self.buf.len()).then_some(end)
|
||||
})
|
||||
.ok()?;
|
||||
Some(start)
|
||||
}
|
||||
|
||||
pub fn alloc<T>(&self, value: T) -> Option<(&T, ArenaWeak<T>)> {
|
||||
let offset = self.reserve(size_of::<T>(), align_of::<T>())?;
|
||||
let ptr = unsafe { self.base().add(offset) }.cast::<T>();
|
||||
// SAFETY: freshly reserved, aligned, in-bounds, unaliased.
|
||||
unsafe { ptr.write(value) };
|
||||
if std::mem::needs_drop::<T>() {
|
||||
unsafe fn glue<T>(p: *mut u8) {
|
||||
unsafe { p.cast::<T>().drop_in_place() }
|
||||
}
|
||||
self.drops.lock().unwrap().push(DropEntry { offset, drop_fn: glue::<T> });
|
||||
}
|
||||
// SAFETY: initialized above; insert-only, so no `&mut` to it can exist.
|
||||
Some((unsafe { &*ptr }, ArenaWeak::new(self.generation(), offset)))
|
||||
}
|
||||
|
||||
pub fn alloc_slice_copy<T: Copy>(&self, src: &[T]) -> Option<&[T]> {
|
||||
let buf = self.alloc_scratch::<T>(src.len())?;
|
||||
for (slot, &value) in buf.iter_mut().zip(src) {
|
||||
slot.write(value);
|
||||
}
|
||||
// SAFETY: every lane written above from `src`.
|
||||
Some(unsafe { std::slice::from_raw_parts(buf.as_ptr().cast::<T>(), src.len()) })
|
||||
}
|
||||
|
||||
// Not drop-tracked: callers must either consume every written lane or
|
||||
// restrict themselves to `Copy` payloads (leak, not UB, otherwise).
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn alloc_scratch<T>(&self, len: usize) -> Option<&mut [MaybeUninit<T>]> {
|
||||
let size = size_of::<T>().checked_mul(len)?;
|
||||
let offset = self.reserve(size, align_of::<T>())?;
|
||||
let ptr = unsafe { self.base().add(offset) }.cast::<MaybeUninit<T>>();
|
||||
// SAFETY: exclusive region; lifetime tied to `&self`, and `reset` takes
|
||||
// `&mut self`, so the slice cannot outlive the generation.
|
||||
Some(unsafe { std::slice::from_raw_parts_mut(ptr, len) })
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
let base = self.base();
|
||||
for entry in self.drops.get_mut().unwrap().drain(..) {
|
||||
// SAFETY: registered at alloc time; insert-only means the region was
|
||||
// never overwritten within this generation.
|
||||
unsafe { (entry.drop_fn)(base.add(entry.offset)) }
|
||||
}
|
||||
*self.offset.get_mut() = 0;
|
||||
self.generation.fetch_add(1, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Arena {
|
||||
fn drop(&mut self) {
|
||||
self.reset();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArenaWeak<T> {
|
||||
word: u64,
|
||||
_marker: PhantomData<*const T>,
|
||||
}
|
||||
|
||||
impl<T> Clone for ArenaWeak<T> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
impl<T> Copy for ArenaWeak<T> {}
|
||||
|
||||
impl<T> ArenaWeak<T> {
|
||||
pub const NULL: Self = ArenaWeak { word: 0, _marker: PhantomData };
|
||||
|
||||
fn new(generation: u64, offset: usize) -> Self {
|
||||
debug_assert!(offset < u32::MAX as usize);
|
||||
Self {
|
||||
word: ((generation & 0xFFFF_FFFF) << 32) | offset as u64,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn upgrade(self, arena: &Arena) -> Option<&T> {
|
||||
let generation = self.word >> 32;
|
||||
if generation != arena.generation() & 0xFFFF_FFFF {
|
||||
return None;
|
||||
}
|
||||
let offset = (self.word & 0xFFFF_FFFF) as usize;
|
||||
// SAFETY: same generation means the entry was fully written before its
|
||||
// word was published (Release) and cannot move or be overwritten within
|
||||
// a generation (insert-only); the Acquire load that produced this word
|
||||
// ordered the payload writes.
|
||||
Some(unsafe { &*arena.base().add(offset).cast::<T>() })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArenaCell<T> {
|
||||
word: AtomicU64,
|
||||
_marker: PhantomData<fn() -> T>,
|
||||
}
|
||||
|
||||
impl<T> Default for ArenaCell<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
word: AtomicU64::new(0),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ArenaCell<T> {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn load<'e>(&self, arena: &'e Arena) -> Option<&'e T> {
|
||||
let weak = ArenaWeak::<T> {
|
||||
word: self.word.load(Ordering::Acquire),
|
||||
_marker: PhantomData,
|
||||
};
|
||||
weak.upgrade(arena)
|
||||
}
|
||||
|
||||
pub fn store(&self, weak: ArenaWeak<T>) {
|
||||
self.word.store(weak.word, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
#[test]
|
||||
fn alloc_upgrade_reset_miss() {
|
||||
let mut arena = Arena::new(1024);
|
||||
let cell = ArenaCell::new();
|
||||
let (value, weak) = arena.alloc(41u32).unwrap();
|
||||
assert_eq!(*value, 41);
|
||||
cell.store(weak);
|
||||
assert_eq!(cell.load(&arena), Some(&41));
|
||||
arena.reset();
|
||||
assert_eq!(cell.load(&arena), None, "stale handle must miss");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capacity_survives_reset() {
|
||||
let mut arena = Arena::new(64 + align_of::<u32>() - 1);
|
||||
for _ in 0..10 {
|
||||
for _ in 0..16 {
|
||||
assert!(arena.alloc(0u32).is_some());
|
||||
}
|
||||
assert!(arena.alloc(0u32).is_none(), "exhausted within generation");
|
||||
arena.reset();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_glue_runs_on_reset() {
|
||||
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||
struct Probe(#[allow(dead_code)] String);
|
||||
impl Drop for Probe {
|
||||
fn drop(&mut self) {
|
||||
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
let mut arena = Arena::new(1024);
|
||||
arena.alloc(Probe("owns heap".into())).unwrap();
|
||||
arena.alloc(Probe("me too".into())).unwrap();
|
||||
assert_eq!(DROPS.load(Ordering::Relaxed), 0);
|
||||
arena.reset();
|
||||
assert_eq!(DROPS.load(Ordering::Relaxed), 2);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::arena::Arena;
|
||||
use crate::transform::Footprint;
|
||||
use glam::DVec2;
|
||||
pub use no_std_types::context::{ArcCtx, Ctx};
|
||||
use std::any::Any;
|
||||
use std::borrow::Borrow;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::panic::Location;
|
||||
use std::sync::Arc;
|
||||
@@ -62,7 +62,9 @@ pub trait InjectRealTime {}
|
||||
pub trait InjectAnimationTime {}
|
||||
pub trait InjectPointerPosition {}
|
||||
pub trait InjectPosition {}
|
||||
pub trait InjectIndex {}
|
||||
pub trait InjectIndex {
|
||||
fn set_index(&mut self, index: u64);
|
||||
}
|
||||
pub trait InjectVarArgs {}
|
||||
|
||||
// ================
|
||||
@@ -101,7 +103,6 @@ impl<T: Ctx> InjectRealTime for T {}
|
||||
impl<T: Ctx> InjectAnimationTime for T {}
|
||||
impl<T: Ctx> InjectPointerPosition for T {}
|
||||
impl<T: Ctx> InjectPosition for T {}
|
||||
impl<T: Ctx> InjectIndex for T {}
|
||||
impl<T: Ctx> InjectVarArgs for T {}
|
||||
|
||||
// =============
|
||||
@@ -383,48 +384,6 @@ impl ExtractFootprint for () {
|
||||
}
|
||||
}
|
||||
|
||||
// =====================================
|
||||
// EXTRACT TRAIT IMPLS FOR `ContextImpl`
|
||||
// =====================================
|
||||
|
||||
impl Ctx for ContextImpl<'_> {}
|
||||
|
||||
impl ExtractFootprint for ContextImpl<'_> {
|
||||
fn try_footprint(&self) -> Option<&Footprint> {
|
||||
self.footprint
|
||||
}
|
||||
}
|
||||
impl ExtractRealTime for ContextImpl<'_> {
|
||||
fn try_real_time(&self) -> Option<f64> {
|
||||
self.real_time
|
||||
}
|
||||
}
|
||||
impl ExtractPosition for ContextImpl<'_> {
|
||||
fn try_position(&self) -> Option<impl Iterator<Item = DVec2>> {
|
||||
self.position.clone().map(|x| x.into_iter())
|
||||
}
|
||||
}
|
||||
impl ExtractIndex for ContextImpl<'_> {
|
||||
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
|
||||
self.index.clone().map(|x| x.into_iter())
|
||||
}
|
||||
}
|
||||
impl ExtractVarArgs for ContextImpl<'_> {
|
||||
fn vararg(&self, index: usize) -> Result<DynRef<'_>, VarArgsResult> {
|
||||
let Some(inner) = self.varargs else { return Err(VarArgsResult::NoVarArgs) };
|
||||
inner.get(index).ok_or(VarArgsResult::IndexOutOfBounds).copied()
|
||||
}
|
||||
|
||||
fn varargs_len(&self) -> Result<usize, VarArgsResult> {
|
||||
let Some(inner) = self.varargs else { return Err(VarArgsResult::NoVarArgs) };
|
||||
Ok(inner.len())
|
||||
}
|
||||
|
||||
fn hash_varargs(&self, _hasher: &mut dyn Hasher) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// EXTRACT TRAIT IMPLS FOR `OwnedContextImpl`
|
||||
// ==========================================
|
||||
@@ -680,32 +639,447 @@ impl OwnedContextImpl {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, dyn_any::DynAny)]
|
||||
pub struct ContextImpl<'a> {
|
||||
pub(crate) footprint: Option<&'a Footprint>,
|
||||
pub type SourceId = u64;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct IndexLink<'a> {
|
||||
pub index: u64,
|
||||
pub outer: Option<&'a IndexLink<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PositionLink<'a> {
|
||||
pub position: DVec2,
|
||||
pub outer: Option<&'a PositionLink<'a>>,
|
||||
}
|
||||
|
||||
pub type DynSlot<'a> = &'a (dyn AnyHash + Send + Sync);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct VarArgLink<'a> {
|
||||
pub args: &'a [DynSlot<'a>],
|
||||
pub outer: Option<&'a VarArgLink<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct EvalScope<'a> {
|
||||
real_time: Option<f64>,
|
||||
position: Option<Vec<DVec2>>, // This could be converted into a single enum to save extra bytes
|
||||
index: Option<Vec<usize>>, // This could be converted into a single enum to save extra bytes
|
||||
varargs: Option<&'a [DynRef<'a>]>,
|
||||
animation_time: Option<f64>,
|
||||
pointer_position: Option<DVec2>,
|
||||
generations: &'a [(SourceId, u64)],
|
||||
arena: &'a Arena,
|
||||
hash: u64,
|
||||
}
|
||||
|
||||
impl<'a> EvalScope<'a> {
|
||||
pub fn new(real_time: Option<f64>, animation_time: Option<f64>, pointer_position: Option<DVec2>, generations: &'a [(SourceId, u64)], arena: &'a Arena) -> Self {
|
||||
let mut scope = Self {
|
||||
real_time,
|
||||
animation_time,
|
||||
pointer_position,
|
||||
generations,
|
||||
arena,
|
||||
hash: 0,
|
||||
};
|
||||
scope.hash = scope.compute_hash(None);
|
||||
scope
|
||||
}
|
||||
|
||||
pub fn retained(&self, retain: &[SourceId]) -> EvalScope<'a> {
|
||||
EvalScope {
|
||||
hash: self.compute_hash(Some(retain)),
|
||||
..*self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_real_time(&self, real_time: Option<f64>) -> EvalScope<'a> {
|
||||
let mut scope = EvalScope { real_time, ..*self };
|
||||
scope.hash = scope.compute_hash(None);
|
||||
scope
|
||||
}
|
||||
|
||||
pub fn with_animation_time(&self, animation_time: Option<f64>) -> EvalScope<'a> {
|
||||
let mut scope = EvalScope { animation_time, ..*self };
|
||||
scope.hash = scope.compute_hash(None);
|
||||
scope
|
||||
}
|
||||
|
||||
fn compute_hash(&self, retain: Option<&[SourceId]>) -> u64 {
|
||||
let mut hasher = std::hash::DefaultHasher::new();
|
||||
self.real_time.map(f64::to_bits).hash(&mut hasher);
|
||||
self.animation_time.map(f64::to_bits).hash(&mut hasher);
|
||||
self.pointer_position.map(|position| (position.x.to_bits(), position.y.to_bits())).hash(&mut hasher);
|
||||
for (source, generation) in self.generations {
|
||||
if retain.is_none_or(|retain| retain.contains(source)) {
|
||||
(source, generation).hash(&mut hasher);
|
||||
}
|
||||
}
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
pub fn generation(&self, source: SourceId) -> Option<u64> {
|
||||
self.generations.iter().find(|(candidate, _)| *candidate == source).map(|(_, generation)| *generation)
|
||||
}
|
||||
|
||||
pub fn generations(&self) -> &'a [(SourceId, u64)] {
|
||||
self.generations
|
||||
}
|
||||
|
||||
pub fn arena(&self) -> &'a Arena {
|
||||
self.arena
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ExtractArena {
|
||||
type ArenaRef;
|
||||
fn arena(&self) -> Self::ArenaRef;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ContextImpl<'a> {
|
||||
index: IndexLink<'a>,
|
||||
position: Option<&'a PositionLink<'a>>,
|
||||
varargs: Option<&'a VarArgLink<'a>>,
|
||||
footprint: Option<&'a Footprint>,
|
||||
scope: &'a EvalScope<'a>,
|
||||
}
|
||||
|
||||
impl<'a> ContextImpl<'a> {
|
||||
pub fn with_footprint<'f>(&self, new_footprint: &'f Footprint, varargs: Option<&'f impl Borrow<[DynRef<'f>]>>) -> ContextImpl<'f>
|
||||
pub fn root(scope: &'a EvalScope<'a>) -> Self {
|
||||
Self {
|
||||
index: IndexLink { index: 0, outer: None },
|
||||
position: None,
|
||||
varargs: None,
|
||||
footprint: None,
|
||||
scope,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scope(&self) -> &'a EvalScope<'a> {
|
||||
self.scope
|
||||
}
|
||||
|
||||
pub fn index_head(&self) -> IndexLink<'a> {
|
||||
self.index
|
||||
}
|
||||
|
||||
pub fn with_footprint<'s>(&self, footprint: &'s Footprint) -> ContextImpl<'s>
|
||||
where
|
||||
'a: 'f,
|
||||
'a: 's,
|
||||
{
|
||||
ContextImpl { footprint: Some(footprint), ..*self }
|
||||
}
|
||||
|
||||
pub fn with_scope<'s>(&self, scope: &'s EvalScope<'s>) -> ContextImpl<'s>
|
||||
where
|
||||
'a: 's,
|
||||
{
|
||||
ContextImpl { scope, ..*self }
|
||||
}
|
||||
|
||||
pub fn with_varargs<'s>(&self, varargs: &'s VarArgLink<'s>) -> ContextImpl<'s>
|
||||
where
|
||||
'a: 's,
|
||||
{
|
||||
ContextImpl { varargs: Some(varargs), ..*self }
|
||||
}
|
||||
|
||||
pub fn with_position<'s>(&self, position: &'s PositionLink<'s>) -> ContextImpl<'s>
|
||||
where
|
||||
'a: 's,
|
||||
{
|
||||
ContextImpl { position: Some(position), ..*self }
|
||||
}
|
||||
|
||||
pub fn promoted<'s>(&self, spilled_head: &'s IndexLink<'s>, inner_index: u64) -> ContextImpl<'s>
|
||||
where
|
||||
'a: 's,
|
||||
{
|
||||
ContextImpl {
|
||||
footprint: Some(new_footprint),
|
||||
position: self.position.clone(),
|
||||
index: self.index.clone(),
|
||||
varargs: varargs.map(|x| x.borrow()),
|
||||
index: IndexLink {
|
||||
index: inner_index,
|
||||
outer: Some(spilled_head),
|
||||
},
|
||||
..*self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ctx for ContextImpl<'_> {}
|
||||
|
||||
impl InjectIndex for ContextImpl<'_> {
|
||||
fn set_index(&mut self, index: u64) {
|
||||
self.index.index = index;
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtractFootprint for ContextImpl<'_> {
|
||||
fn try_footprint(&self) -> Option<&Footprint> {
|
||||
self.footprint
|
||||
}
|
||||
}
|
||||
impl ExtractRealTime for ContextImpl<'_> {
|
||||
fn try_real_time(&self) -> Option<f64> {
|
||||
self.scope.real_time
|
||||
}
|
||||
}
|
||||
impl ExtractAnimationTime for ContextImpl<'_> {
|
||||
fn try_animation_time(&self) -> Option<f64> {
|
||||
self.scope.animation_time
|
||||
}
|
||||
}
|
||||
impl ExtractPointerPosition for ContextImpl<'_> {
|
||||
fn try_pointer_position(&self) -> Option<DVec2> {
|
||||
self.scope.pointer_position
|
||||
}
|
||||
}
|
||||
impl ExtractIndex for ContextImpl<'_> {
|
||||
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
|
||||
Some(std::iter::successors(Some(&self.index), |link| link.outer).map(|link| link.index as usize))
|
||||
}
|
||||
}
|
||||
impl ExtractPosition for ContextImpl<'_> {
|
||||
fn try_position(&self) -> Option<impl Iterator<Item = DVec2>> {
|
||||
self.position.map(|head| std::iter::successors(Some(head), |link| link.outer).map(|link| link.position))
|
||||
}
|
||||
}
|
||||
impl ExtractVarArgs for ContextImpl<'_> {
|
||||
fn vararg(&self, index: usize) -> Result<DynRef<'_>, VarArgsResult> {
|
||||
let mut link = self.varargs.ok_or(VarArgsResult::NoVarArgs)?;
|
||||
let mut remaining = index;
|
||||
loop {
|
||||
match link.args.get(remaining) {
|
||||
Some(arg) => return Ok(*arg as DynRef<'_>),
|
||||
None => {
|
||||
remaining -= link.args.len();
|
||||
link = link.outer.ok_or(VarArgsResult::IndexOutOfBounds)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn varargs_len(&self) -> Result<usize, VarArgsResult> {
|
||||
let head = self.varargs.ok_or(VarArgsResult::NoVarArgs)?;
|
||||
Ok(std::iter::successors(Some(head), |link| link.outer).map(|link| link.args.len()).sum())
|
||||
}
|
||||
|
||||
fn hash_varargs(&self, hasher: &mut dyn Hasher) {
|
||||
let mut count = 0u64;
|
||||
let mut link = self.varargs;
|
||||
while let Some(current) = link {
|
||||
for arg in current.args {
|
||||
arg.dyn_hash(&mut *hasher);
|
||||
count += 1;
|
||||
}
|
||||
link = current.outer;
|
||||
}
|
||||
count.hash(&mut &mut *hasher);
|
||||
}
|
||||
}
|
||||
impl<'a> ExtractArena for ContextImpl<'a> {
|
||||
type ArenaRef = &'a Arena;
|
||||
fn arena(&self) -> &'a Arena {
|
||||
self.scope.arena
|
||||
}
|
||||
}
|
||||
|
||||
impl graphene_hash::CacheHash for ContextImpl<'_> {
|
||||
fn cache_hash<H: Hasher>(&self, state: &mut H) {
|
||||
match self.footprint {
|
||||
Some(footprint) => {
|
||||
1u8.hash(state);
|
||||
footprint.cache_hash(state);
|
||||
}
|
||||
None => 0u8.hash(state),
|
||||
}
|
||||
let mut count = 0u64;
|
||||
for link in std::iter::successors(Some(&self.index), |link| link.outer) {
|
||||
link.index.hash(state);
|
||||
count += 1;
|
||||
}
|
||||
count.hash(state);
|
||||
count = 0;
|
||||
let mut position = self.position;
|
||||
while let Some(link) = position {
|
||||
link.position.x.to_bits().hash(state);
|
||||
link.position.y.to_bits().hash(state);
|
||||
count += 1;
|
||||
position = link.outer;
|
||||
}
|
||||
count.hash(state);
|
||||
self.hash_varargs(state);
|
||||
self.scope.hash.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VarArgsResult {
|
||||
IndexOutOfBounds,
|
||||
NoVarArgs,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod context_impl_tests {
|
||||
use super::*;
|
||||
use crate::graphene_hash::CacheHash;
|
||||
|
||||
fn hash_of(ctx: &ContextImpl) -> u64 {
|
||||
let mut hasher = std::hash::DefaultHasher::new();
|
||||
ctx.cache_hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn scope_fixture<'a>(generations: &'a [(SourceId, u64)], arena: &'a Arena) -> EvalScope<'a> {
|
||||
EvalScope::new(Some(0.5), Some(1.5), None, generations, arena)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_contexts_hash_equal() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [(0, 1), (1, 3)];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let a = ContextImpl::root(&scope);
|
||||
let b = ContextImpl::root(&scope);
|
||||
assert_eq!(hash_of(&a), hash_of(&b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_axis_changes_the_hash() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [(0, 1)];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let root = ContextImpl::root(&scope);
|
||||
let footprint = Footprint::DEFAULT;
|
||||
|
||||
let mut indexed = root;
|
||||
indexed.set_index(4);
|
||||
let position_link = PositionLink {
|
||||
position: DVec2::new(1.0, 2.0),
|
||||
outer: None,
|
||||
};
|
||||
let time_scope = scope.with_real_time(Some(9.75));
|
||||
let variants = [root.with_footprint(&footprint), indexed, root.with_position(&position_link), root.with_scope(&time_scope)];
|
||||
let root_hash = hash_of(&root);
|
||||
let mut hashes: Vec<u64> = variants.iter().map(hash_of).collect();
|
||||
hashes.push(root_hash);
|
||||
hashes.sort();
|
||||
hashes.dedup();
|
||||
assert_eq!(hashes.len(), variants.len() + 1, "every axis must contribute to the hash");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index_level_order_matters() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let root = ContextImpl::root(&scope);
|
||||
|
||||
let outer_one = IndexLink { index: 1, outer: None };
|
||||
let mut one_two = root.promoted(&outer_one, 2);
|
||||
let outer_two = IndexLink { index: 2, outer: None };
|
||||
let mut two_one = root.promoted(&outer_two, 1);
|
||||
assert_ne!(hash_of(&one_two), hash_of(&two_one));
|
||||
|
||||
one_two.set_index(7);
|
||||
two_one.set_index(7);
|
||||
assert_ne!(hash_of(&one_two), hash_of(&two_one), "outer levels must stay hashed after set_index");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn axis_boundaries_are_unambiguous() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let root = ContextImpl::root(&scope);
|
||||
|
||||
let position = PositionLink {
|
||||
position: DVec2::new(5.0, 5.0),
|
||||
outer: None,
|
||||
};
|
||||
let spilled = root.index_head();
|
||||
let mut deep_index = root.promoted(&spilled, 0);
|
||||
deep_index.set_index(5);
|
||||
let shallow_with_position = root.with_position(&position);
|
||||
assert_ne!(hash_of(&deep_index), hash_of(&shallow_with_position), "index levels must not blur into position levels");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retain_scopes_generation_invalidation() {
|
||||
let arena = Arena::new(64);
|
||||
let initial = [(0, 1), (1, 3)];
|
||||
let bumped_unretained = [(0, 2), (1, 3)];
|
||||
let bumped_retained = [(0, 1), (1, 4)];
|
||||
|
||||
let hash_with = |generations: &[(SourceId, u64)]| {
|
||||
let scope = scope_fixture(generations, &arena).retained(&[1]);
|
||||
let retained_scope_context = ContextImpl::root(&scope);
|
||||
hash_of(&retained_scope_context)
|
||||
};
|
||||
assert_eq!(hash_with(&initial), hash_with(&bumped_unretained), "unretained source bumps must not invalidate");
|
||||
assert_ne!(hash_with(&initial), hash_with(&bumped_retained), "retained source bumps must invalidate");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unretained_scope_sees_every_bump() {
|
||||
let arena = Arena::new(64);
|
||||
let initial = [(0, 1)];
|
||||
let bumped = [(0, 2)];
|
||||
let hash_with = |generations: &[(SourceId, u64)]| {
|
||||
let scope = scope_fixture(generations, &arena);
|
||||
hash_of(&ContextImpl::root(&scope))
|
||||
};
|
||||
assert_ne!(hash_with(&initial), hash_with(&bumped));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_index_is_visible_and_keeps_outer_levels() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let root = ContextImpl::root(&scope);
|
||||
let spilled = root.index_head();
|
||||
let mut ctx = root.promoted(&spilled, 0);
|
||||
ctx.set_index(11);
|
||||
let levels: Vec<usize> = ctx.try_index().unwrap().collect();
|
||||
assert_eq!(levels, vec![11, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vararg_chain_concatenates_innermost_first() {
|
||||
let arena = Arena::new(64);
|
||||
let generations = [];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let root = ContextImpl::root(&scope);
|
||||
|
||||
let outer_value = 7u32;
|
||||
let outer_args: [DynSlot; 1] = [&outer_value];
|
||||
let outer_link = VarArgLink { args: &outer_args, outer: None };
|
||||
let outer_ctx = root.with_varargs(&outer_link);
|
||||
|
||||
let inner_value = String::from("inner");
|
||||
let inner_args: [DynSlot; 1] = [&inner_value];
|
||||
let inner_link = VarArgLink {
|
||||
args: &inner_args,
|
||||
outer: Some(&outer_link),
|
||||
};
|
||||
let inner_ctx = root.with_varargs(&inner_link);
|
||||
|
||||
assert_eq!(root.varargs_len(), Err(VarArgsResult::NoVarArgs));
|
||||
assert_eq!(outer_ctx.varargs_len(), Ok(1));
|
||||
assert_eq!(inner_ctx.varargs_len(), Ok(2));
|
||||
assert!(inner_ctx.vararg(0).unwrap().downcast_ref::<String>().is_some());
|
||||
assert!(inner_ctx.vararg(1).unwrap().downcast_ref::<u32>().is_some());
|
||||
assert!(matches!(inner_ctx.vararg(2), Err(VarArgsResult::IndexOutOfBounds)));
|
||||
assert_ne!(hash_of(&outer_ctx), hash_of(&inner_ctx));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scope_arena_reaches_kernels_through_extract_arena() {
|
||||
let arena = Arena::new(1024);
|
||||
let generations = [];
|
||||
let scope = scope_fixture(&generations, &arena);
|
||||
let ctx = ContextImpl::root(&scope);
|
||||
let (value, _) = ExtractArena::arena(&ctx).alloc(41u32).unwrap();
|
||||
assert_eq!(*value, 41);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
use crate::gpoll::Finality;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
|
||||
|
||||
const SLOT_EMPTY: u8 = 0;
|
||||
const SLOT_FINAL: u8 = 1;
|
||||
const SLOT_PARTIAL: u8 = 2;
|
||||
|
||||
pub struct FrameTable<T, const CAP: usize> {
|
||||
slots: [FrameSlot<T>; CAP],
|
||||
}
|
||||
|
||||
struct FrameSlot<T> {
|
||||
key: AtomicU64,
|
||||
state: AtomicU8,
|
||||
value: UnsafeCell<MaybeUninit<T>>,
|
||||
}
|
||||
|
||||
pub enum Lookup<'t, T> {
|
||||
Hit(Finality, &'t T),
|
||||
Vacant(VacantSlot<'t, T>),
|
||||
Full,
|
||||
}
|
||||
|
||||
pub struct VacantSlot<'t, T> {
|
||||
slot: &'t FrameSlot<T>,
|
||||
}
|
||||
|
||||
impl<T, const CAP: usize> Default for FrameTable<T, CAP> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const CAP: usize> FrameTable<T, CAP> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
slots: std::array::from_fn(|_| FrameSlot {
|
||||
key: AtomicU64::new(0),
|
||||
state: AtomicU8::new(SLOT_EMPTY),
|
||||
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lookup(&self, hash: u64) -> Lookup<'_, T> {
|
||||
// Keys are forced odd so the empty sentinel (0) can never collide.
|
||||
let key = hash | 1;
|
||||
for probe in 0..CAP {
|
||||
let slot = &self.slots[(key as usize).wrapping_add(probe) % CAP];
|
||||
let stored = slot.key.load(Ordering::Acquire);
|
||||
if stored == key {
|
||||
return match slot.state.load(Ordering::Acquire) {
|
||||
// SAFETY: a published state was stored with Release after the
|
||||
// value write; the Acquire load above ordered that write.
|
||||
SLOT_FINAL => Lookup::Hit(Finality::AllFinal, unsafe { (*slot.value.get()).assume_init_ref() }),
|
||||
SLOT_PARTIAL => Lookup::Hit(Finality::Partial, unsafe { (*slot.value.get()).assume_init_ref() }),
|
||||
_ => Lookup::Full,
|
||||
};
|
||||
}
|
||||
if stored == 0 && slot.key.compare_exchange(0, key, Ordering::AcqRel, Ordering::Acquire).is_ok() {
|
||||
return Lookup::Vacant(VacantSlot { slot });
|
||||
}
|
||||
}
|
||||
Lookup::Full
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const CAP: usize> Drop for FrameTable<T, CAP> {
|
||||
fn drop(&mut self) {
|
||||
for slot in &self.slots {
|
||||
if slot.state.load(Ordering::Acquire) != SLOT_EMPTY {
|
||||
// SAFETY: a non-empty state is only ever stored after the value
|
||||
// write in `publish`.
|
||||
unsafe { (*slot.value.get()).assume_init_drop() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t, T> VacantSlot<'t, T> {
|
||||
pub fn publish(self, value: T, finality: Finality) -> &'t T {
|
||||
// SAFETY: the CAS in `lookup` reserved this slot exclusively for us and
|
||||
// its state is still SLOT_EMPTY, so nobody reads the value yet.
|
||||
let lent = unsafe { &*(*self.slot.value.get()).write(value) };
|
||||
let state = match finality {
|
||||
Finality::AllFinal => SLOT_FINAL,
|
||||
Finality::Partial => SLOT_PARTIAL,
|
||||
};
|
||||
self.slot.state.store(state, Ordering::Release);
|
||||
lent
|
||||
}
|
||||
|
||||
pub fn release(self) {
|
||||
self.slot.key.store(0, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
#[test]
|
||||
fn publish_then_hit_with_finality() {
|
||||
let table = FrameTable::<u32, 8>::new();
|
||||
let Lookup::Vacant(slot) = table.lookup(7) else {
|
||||
panic!("fresh table must be vacant");
|
||||
};
|
||||
assert_eq!(*slot.publish(41, Finality::Partial), 41);
|
||||
let Lookup::Hit(finality, value) = table.lookup(7) else {
|
||||
panic!("published key must hit");
|
||||
};
|
||||
assert_eq!((finality, *value), (Finality::Partial, 41));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn released_slot_is_vacant_again() {
|
||||
let table = FrameTable::<u32, 8>::new();
|
||||
let Lookup::Vacant(slot) = table.lookup(7) else { unreachable!() };
|
||||
slot.release();
|
||||
assert!(matches!(table.lookup(7), Lookup::Vacant(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distinct_keys_probe_past_collisions_until_full() {
|
||||
let table = FrameTable::<u32, 4>::new();
|
||||
for hash in [2, 4, 6, 8] {
|
||||
let Lookup::Vacant(slot) = table.lookup(hash) else {
|
||||
panic!("hash {hash} should find a vacant slot");
|
||||
};
|
||||
slot.publish(hash as u32, Finality::AllFinal);
|
||||
}
|
||||
assert!(matches!(table.lookup(100), Lookup::Full));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_runs_glue_for_published_values_only() {
|
||||
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||
struct Probe;
|
||||
impl Drop for Probe {
|
||||
fn drop(&mut self) {
|
||||
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
let table = FrameTable::<Probe, 8>::new();
|
||||
let Lookup::Vacant(slot) = table.lookup(1) else { unreachable!() };
|
||||
slot.publish(Probe, Finality::AllFinal);
|
||||
let Lookup::Vacant(reserved_but_unpublished) = table.lookup(2) else { unreachable!() };
|
||||
reserved_but_unpublished.release();
|
||||
drop(table);
|
||||
assert_eq!(DROPS.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
use crate::context::InjectIndex;
|
||||
use crate::gpoll::{Extent, Finality, GPoll, GraphError};
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BatchStatus<'a, T> {
|
||||
Lent(&'a [T], Finality),
|
||||
Filled(&'a mut [T], Finality),
|
||||
Pending,
|
||||
Error(GraphError),
|
||||
NeedBuffer,
|
||||
InvalidRange,
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// The first `len` elements of `scratch` must be initialized, and `len` must not exceed `scratch.len()`.
|
||||
pub unsafe fn assume_init_prefix_mut<T>(scratch: &mut [MaybeUninit<T>], len: usize) -> &mut [T] {
|
||||
debug_assert!(len <= scratch.len());
|
||||
unsafe { std::slice::from_raw_parts_mut(scratch.as_mut_ptr().cast::<T>(), len) }
|
||||
}
|
||||
|
||||
pub trait GNode<Input> {
|
||||
type Output;
|
||||
|
||||
fn eval(&self, input: &Input) -> GPoll<Self::Output>;
|
||||
|
||||
fn extent(&self, _input: &Input) -> GPoll<Extent> {
|
||||
GPoll::Final(Extent::Free)
|
||||
}
|
||||
|
||||
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||
where
|
||||
Input: InjectIndex + Copy,
|
||||
{
|
||||
let Some(scratch) = scratch else {
|
||||
return BatchStatus::NeedBuffer;
|
||||
};
|
||||
let Some(len) = range.end.checked_sub(range.start).map(|len| len as usize) else {
|
||||
return BatchStatus::InvalidRange;
|
||||
};
|
||||
if scratch.len() < len {
|
||||
return BatchStatus::InvalidRange;
|
||||
}
|
||||
let mut local = *input;
|
||||
let mut finality = Finality::AllFinal;
|
||||
for offset in 0..len {
|
||||
local.set_index(range.start + offset as u64);
|
||||
let abort = match self.eval(&local) {
|
||||
GPoll::Final(value) => {
|
||||
scratch[offset].write(value);
|
||||
None
|
||||
}
|
||||
GPoll::Partial(value) => {
|
||||
scratch[offset].write(value);
|
||||
finality = Finality::Partial;
|
||||
None
|
||||
}
|
||||
GPoll::Pending => Some(BatchStatus::Pending),
|
||||
GPoll::Fallback(boxed) => Some(BatchStatus::Error(boxed.1)),
|
||||
GPoll::Error(e) => Some(BatchStatus::Error(*e)),
|
||||
};
|
||||
if let Some(status) = abort {
|
||||
for written in scratch[..offset].iter_mut() {
|
||||
// SAFETY: every lane before `offset` was written by this loop.
|
||||
unsafe { written.assume_init_drop() };
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
// SAFETY: all `len` lanes were written by the loop above.
|
||||
BatchStatus::Filled(unsafe { assume_init_prefix_mut(scratch, len) }, finality)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Input, N> GNode<Input> for &N
|
||||
where
|
||||
N: GNode<Input> + ?Sized,
|
||||
{
|
||||
type Output = N::Output;
|
||||
|
||||
fn eval(&self, input: &Input) -> GPoll<Self::Output> {
|
||||
(**self).eval(input)
|
||||
}
|
||||
|
||||
fn extent(&self, input: &Input) -> GPoll<Extent> {
|
||||
(**self).extent(input)
|
||||
}
|
||||
|
||||
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||
where
|
||||
Input: InjectIndex + Copy,
|
||||
{
|
||||
(**self).eval_batch(input, range, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Input, N> GNode<Input> for Box<N>
|
||||
where
|
||||
N: GNode<Input> + ?Sized,
|
||||
{
|
||||
type Output = N::Output;
|
||||
|
||||
fn eval(&self, input: &Input) -> GPoll<Self::Output> {
|
||||
(**self).eval(input)
|
||||
}
|
||||
|
||||
fn extent(&self, input: &Input) -> GPoll<Extent> {
|
||||
(**self).extent(input)
|
||||
}
|
||||
|
||||
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||
where
|
||||
Input: InjectIndex + Copy,
|
||||
{
|
||||
(**self).eval_batch(input, range, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct TestInput {
|
||||
index: u64,
|
||||
}
|
||||
|
||||
impl InjectIndex for TestInput {
|
||||
fn set_index(&mut self, index: u64) {
|
||||
self.index = index;
|
||||
}
|
||||
}
|
||||
|
||||
struct Double;
|
||||
|
||||
impl GNode<TestInput> for Double {
|
||||
type Output = u64;
|
||||
|
||||
fn eval(&self, input: &TestInput) -> GPoll<u64> {
|
||||
GPoll::Final(input.index * 2)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spec_loop_fills_scratch_per_lane() {
|
||||
let input = TestInput { index: 0 };
|
||||
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||
let status = Double.eval_batch(&input, 2..6, Some(&mut scratch));
|
||||
let BatchStatus::Filled(lanes, finality) = status else {
|
||||
panic!("expected filled, got {status:?}");
|
||||
};
|
||||
assert_eq!(lanes, &[4, 6, 8, 10]);
|
||||
assert_eq!(finality, Finality::AllFinal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probe_without_scratch_requests_a_buffer() {
|
||||
let input = TestInput { index: 0 };
|
||||
assert!(matches!(Double.eval_batch(&input, 0..4, None), BatchStatus::NeedBuffer));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undersized_scratch_is_an_invalid_range() {
|
||||
let input = TestInput { index: 0 };
|
||||
let mut scratch = [const { MaybeUninit::uninit() }; 2];
|
||||
assert!(matches!(Double.eval_batch(&input, 0..4, Some(&mut scratch)), BatchStatus::InvalidRange));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_lane_downgrades_batch_finality() {
|
||||
struct PartialAtThree;
|
||||
impl GNode<TestInput> for PartialAtThree {
|
||||
type Output = u64;
|
||||
fn eval(&self, input: &TestInput) -> GPoll<u64> {
|
||||
match input.index {
|
||||
3 => GPoll::Partial(input.index),
|
||||
index => GPoll::Final(index),
|
||||
}
|
||||
}
|
||||
}
|
||||
let input = TestInput { index: 0 };
|
||||
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||
let status = PartialAtThree.eval_batch(&input, 0..4, Some(&mut scratch));
|
||||
let BatchStatus::Filled(lanes, finality) = status else {
|
||||
panic!("expected filled, got {status:?}");
|
||||
};
|
||||
assert_eq!(lanes, &[0, 1, 2, 3]);
|
||||
assert_eq!(finality, Finality::Partial);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abort_drops_already_written_lanes() {
|
||||
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||
struct Probe;
|
||||
impl Drop for Probe {
|
||||
fn drop(&mut self) {
|
||||
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
struct PendingAtTwo;
|
||||
impl GNode<TestInput> for PendingAtTwo {
|
||||
type Output = Probe;
|
||||
fn eval(&self, input: &TestInput) -> GPoll<Probe> {
|
||||
match input.index {
|
||||
2 => GPoll::Pending,
|
||||
_ => GPoll::Final(Probe),
|
||||
}
|
||||
}
|
||||
}
|
||||
let input = TestInput { index: 0 };
|
||||
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||
let status = PendingAtTwo.eval_batch(&input, 0..4, Some(&mut scratch));
|
||||
assert!(matches!(status, BatchStatus::Pending));
|
||||
assert_eq!(DROPS.load(Ordering::Relaxed), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_is_object_safe_across_erased_edges() {
|
||||
let erased: Box<dyn GNode<TestInput, Output = u64>> = Box::new(Double);
|
||||
let input = TestInput { index: 21 };
|
||||
assert_eq!(erased.eval(&input), GPoll::Final(42));
|
||||
let mut scratch = [const { MaybeUninit::uninit() }; 2];
|
||||
let status = erased.eval_batch(&input, 0..2, Some(&mut scratch));
|
||||
assert!(matches!(status, BatchStatus::Filled(_, Finality::AllFinal)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ErrorKind {
|
||||
Node(&'static str),
|
||||
ArenaExhausted,
|
||||
}
|
||||
|
||||
impl PartialEq<&str> for ErrorKind {
|
||||
fn eq(&self, other: &&str) -> bool {
|
||||
matches!(self, ErrorKind::Node(kind) if kind == other)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct GraphError {
|
||||
pub kind: ErrorKind,
|
||||
pub trace: Vec<usize>,
|
||||
}
|
||||
|
||||
impl GraphError {
|
||||
pub fn new(kind: &'static str) -> Self {
|
||||
Self {
|
||||
kind: ErrorKind::Node(kind),
|
||||
trace: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GPoll<T> {
|
||||
Pending,
|
||||
Final(T),
|
||||
Partial(T),
|
||||
Fallback(Box<(T, GraphError)>),
|
||||
Error(Box<GraphError>),
|
||||
}
|
||||
|
||||
impl<T> GPoll<T> {
|
||||
#[inline(always)]
|
||||
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> GPoll<U> {
|
||||
match self {
|
||||
GPoll::Pending => GPoll::Pending,
|
||||
GPoll::Final(value) => GPoll::Final(f(value)),
|
||||
GPoll::Partial(value) => GPoll::Partial(f(value)),
|
||||
GPoll::Fallback(boxed) => {
|
||||
let (value, e) = *boxed;
|
||||
GPoll::Fallback(Box::new((f(value), e)))
|
||||
}
|
||||
GPoll::Error(e) => GPoll::Error(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn and_then<U>(self, f: impl FnOnce(T) -> GPoll<U>) -> GPoll<U> {
|
||||
match self {
|
||||
GPoll::Pending => GPoll::Pending,
|
||||
GPoll::Final(value) => f(value),
|
||||
GPoll::Partial(value) => match f(value) {
|
||||
GPoll::Final(result) => GPoll::Partial(result),
|
||||
other => other,
|
||||
},
|
||||
GPoll::Fallback(boxed) => {
|
||||
let (value, e) = *boxed;
|
||||
match f(value) {
|
||||
GPoll::Pending => GPoll::Pending,
|
||||
GPoll::Final(result) | GPoll::Partial(result) => GPoll::Fallback(Box::new((result, e))),
|
||||
GPoll::Fallback(inner) => {
|
||||
let (result, _) = *inner;
|
||||
GPoll::Fallback(Box::new((result, e)))
|
||||
}
|
||||
GPoll::Error(inner) => GPoll::Error(inner),
|
||||
}
|
||||
}
|
||||
GPoll::Error(e) => GPoll::Error(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn zip<U>(self, other: GPoll<U>) -> GPoll<(T, U)> {
|
||||
match (self, other) {
|
||||
(GPoll::Error(e), _) | (_, GPoll::Error(e)) => GPoll::Error(e),
|
||||
(GPoll::Pending, _) | (_, GPoll::Pending) => GPoll::Pending,
|
||||
(GPoll::Final(a), GPoll::Final(b)) => GPoll::Final((a, b)),
|
||||
(GPoll::Fallback(boxed), GPoll::Final(b) | GPoll::Partial(b)) => {
|
||||
let (a, e) = *boxed;
|
||||
GPoll::Fallback(Box::new(((a, b), e)))
|
||||
}
|
||||
(GPoll::Final(a) | GPoll::Partial(a), GPoll::Fallback(boxed)) => {
|
||||
let (b, e) = *boxed;
|
||||
GPoll::Fallback(Box::new(((a, b), e)))
|
||||
}
|
||||
(GPoll::Fallback(first), GPoll::Fallback(second)) => {
|
||||
let (a, e) = *first;
|
||||
let (b, _) = *second;
|
||||
GPoll::Fallback(Box::new(((a, b), e)))
|
||||
}
|
||||
(GPoll::Partial(a), GPoll::Final(b) | GPoll::Partial(b)) | (GPoll::Final(a), GPoll::Partial(b)) => GPoll::Partial((a, b)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn trace(self, input: usize) -> Self {
|
||||
match self {
|
||||
GPoll::Fallback(mut boxed) => {
|
||||
boxed.1.trace.push(input);
|
||||
GPoll::Fallback(boxed)
|
||||
}
|
||||
GPoll::Error(mut e) => {
|
||||
e.trace.push(input);
|
||||
GPoll::Error(e)
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fallback(value: T, kind: &'static str) -> Self {
|
||||
GPoll::Fallback(Box::new((value, GraphError::new(kind))))
|
||||
}
|
||||
|
||||
pub fn error(kind: &'static str) -> Self {
|
||||
GPoll::Error(Box::new(GraphError::new(kind)))
|
||||
}
|
||||
|
||||
pub fn arena_exhausted() -> Self {
|
||||
GPoll::Error(Box::new(GraphError {
|
||||
kind: ErrorKind::ArenaExhausted,
|
||||
trace: Vec::new(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Interrupt {
|
||||
Pending,
|
||||
Error(Box<GraphError>),
|
||||
}
|
||||
|
||||
impl From<GraphError> for Interrupt {
|
||||
fn from(error: GraphError) -> Self {
|
||||
Interrupt::Error(Box::new(error))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Interrupt> for GPoll<T> {
|
||||
fn from(interrupt: Interrupt) -> Self {
|
||||
match interrupt {
|
||||
Interrupt::Pending => GPoll::Pending,
|
||||
Interrupt::Error(e) => GPoll::Error(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Extent {
|
||||
Free,
|
||||
Exactly(usize),
|
||||
}
|
||||
|
||||
impl Extent {
|
||||
pub fn meet(a: GPoll<Extent>, b: GPoll<Extent>) -> GPoll<Extent> {
|
||||
a.zip(b).and_then(|(a, b)| match (a, b) {
|
||||
(Extent::Free, other) | (other, Extent::Free) => GPoll::Final(other),
|
||||
(Extent::Exactly(n), Extent::Exactly(m)) if n == m => GPoll::Final(Extent::Exactly(n)),
|
||||
(Extent::Exactly(n), Extent::Exactly(m)) => GPoll::fallback(Extent::Exactly(n.min(m)), "extent mismatch"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Finality {
|
||||
AllFinal,
|
||||
Partial,
|
||||
}
|
||||
|
||||
impl Finality {
|
||||
pub fn meet(self, other: Finality) -> Finality {
|
||||
match (self, other) {
|
||||
(Finality::AllFinal, Finality::AllFinal) => Finality::AllFinal,
|
||||
_ => Finality::Partial,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn free_is_the_meet_identity() {
|
||||
let meet = Extent::meet(GPoll::Final(Extent::Free), GPoll::Final(Extent::Exactly(4)));
|
||||
assert_eq!(meet, GPoll::Final(Extent::Exactly(4)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extent_mismatch_truncates_and_reports() {
|
||||
let meet = Extent::meet(GPoll::Final(Extent::Exactly(3)), GPoll::Final(Extent::Exactly(5)));
|
||||
let GPoll::Fallback(boxed) = meet else {
|
||||
panic!("expected fallback, got {meet:?}");
|
||||
};
|
||||
assert_eq!(boxed.0, Extent::Exactly(3));
|
||||
assert!(boxed.1.kind == "extent mismatch");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_dominates_pending_in_zip() {
|
||||
let zipped = GPoll::<u32>::error("boom").zip(GPoll::<u32>::Pending);
|
||||
assert!(matches!(zipped, GPoll::Error(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trace_builds_root_to_source_path() {
|
||||
let poll = GPoll::<u32>::error("boom").trace(2).trace(0);
|
||||
let GPoll::Error(e) = poll else { unreachable!() };
|
||||
assert_eq!(e.trace, vec![2, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interrupt_round_trips_to_gpoll() {
|
||||
assert_eq!(GPoll::<u32>::from(Interrupt::Pending), GPoll::Pending);
|
||||
let interrupt = Interrupt::from(GraphError::new("boom"));
|
||||
assert!(matches!(GPoll::<u32>::from(interrupt), GPoll::Error(e) if e.kind == "boom"));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
extern crate log;
|
||||
|
||||
pub mod arena;
|
||||
pub mod bounds;
|
||||
pub mod consts;
|
||||
pub mod context;
|
||||
pub mod frame_table;
|
||||
pub mod generic;
|
||||
pub mod gnode;
|
||||
pub mod gpoll;
|
||||
pub mod list;
|
||||
pub mod math;
|
||||
pub mod memo;
|
||||
|
||||
@@ -235,6 +235,7 @@ pub enum Type {
|
||||
Fn(Box<Type>, Box<Type>),
|
||||
/// Represents a future which promises to return the inner type.
|
||||
Future(Box<Type>),
|
||||
Ref(Box<Type>),
|
||||
}
|
||||
|
||||
impl Default for Type {
|
||||
@@ -308,6 +309,7 @@ impl Type {
|
||||
Self::Concrete(ty) => Some(ty.size),
|
||||
Self::Fn(_, _) => None,
|
||||
Self::Future(_) => None,
|
||||
Self::Ref(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,6 +319,7 @@ impl Type {
|
||||
Self::Concrete(ty) => Some(ty.align),
|
||||
Self::Fn(_, _) => None,
|
||||
Self::Future(_) => None,
|
||||
Self::Ref(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,6 +329,7 @@ impl Type {
|
||||
Self::Concrete(_) => self,
|
||||
Self::Fn(_, output) => output.nested_type(),
|
||||
Self::Future(output) => output.nested_type(),
|
||||
Self::Ref(inner) => inner.nested_type(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,6 +342,7 @@ impl Type {
|
||||
Self::Concrete(_) => None,
|
||||
Self::Fn(_, output) => output.replace_nested(f),
|
||||
Self::Future(output) => output.replace_nested(f),
|
||||
Self::Ref(inner) => inner.replace_nested(f),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,6 +352,7 @@ impl Type {
|
||||
Type::Concrete(ty) => simplify_identifier_name(&ty.name),
|
||||
Type::Fn(call_arg, return_value) => format!("{} called with {}", return_value.identifier_name(), call_arg.identifier_name()),
|
||||
Type::Future(ty) => ty.identifier_name(),
|
||||
Type::Ref(ty) => ty.identifier_name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -441,6 +447,7 @@ impl std::fmt::Display for Type {
|
||||
Type::Concrete(ty) => write!(f, "{ty}"),
|
||||
Type::Fn(_, return_value) => write!(f, "{return_value}"),
|
||||
Type::Future(ty) => write!(f, "{ty}"),
|
||||
Type::Ref(ty) => write!(f, "{ty}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,6 @@ license = "Apache-2.0"
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[features]
|
||||
disable-registration = []
|
||||
|
||||
[dependencies]
|
||||
# Workspace dependencies
|
||||
syn = { workspace = true }
|
||||
|
||||
@@ -720,24 +720,18 @@ fn generate_register_node_impl(parsed: &ParsedNodeFn, field_names: &[&Ident], st
|
||||
)
|
||||
));
|
||||
}
|
||||
let native = quote! {
|
||||
#[cfg_attr(not(target_family = "wasm"), ctor)]
|
||||
fn register_node() {
|
||||
let mut registry = NODE_REGISTRY.lock().unwrap();
|
||||
registry.insert(
|
||||
#identifier(),
|
||||
vec![
|
||||
#(#constructors,)*
|
||||
]
|
||||
);
|
||||
}
|
||||
};
|
||||
if cfg!(feature = "disable-registration") {
|
||||
return Ok(native);
|
||||
}
|
||||
|
||||
Ok(quote! {
|
||||
#native
|
||||
#[cfg_attr(not(target_family = "wasm"), ctor)]
|
||||
fn register_node() {
|
||||
let mut registry = NODE_REGISTRY.lock().unwrap();
|
||||
registry.insert(
|
||||
#identifier(),
|
||||
vec![
|
||||
#(#constructors,)*
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#wasm_shim
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ serde-discriminant = []
|
||||
proc-macro2 = { workspace = true }
|
||||
syn = { workspace = true }
|
||||
quote = { workspace = true }
|
||||
convert_case = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# Local dependencies
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
use convert_case::{Case, Casing};
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{Error, FnArg, Ident, Item, ItemFn, ItemMod, ItemUse, Pat, Visibility};
|
||||
|
||||
pub fn editor_commands_impl(attr: TokenStream, module: ItemMod) -> syn::Result<TokenStream> {
|
||||
if !attr.is_empty() {
|
||||
return Err(Error::new(attr.span(), "#[editor_commands] takes no arguments"));
|
||||
}
|
||||
for attr in &module.attrs {
|
||||
if !attr.path().is_ident("doc") {
|
||||
return Err(Error::new(attr.span(), "the #[editor_commands] module may not have other attributes"));
|
||||
}
|
||||
}
|
||||
let Some((_, items)) = module.content else {
|
||||
return Err(Error::new(module.mod_token.span, "#[editor_commands] requires a module with an inline body"));
|
||||
};
|
||||
|
||||
let mut imports: Vec<ItemUse> = Vec::new();
|
||||
let mut functions: Vec<ItemFn> = Vec::new();
|
||||
for item in items {
|
||||
match item {
|
||||
Item::Use(import) => imports.push(import),
|
||||
Item::Fn(function) => functions.push(function),
|
||||
other => return Err(Error::new(other.span(), "only `use` imports and command functions may appear in an #[editor_commands] module")),
|
||||
}
|
||||
}
|
||||
|
||||
let mut variants = TokenStream::new();
|
||||
let mut stubs = TokenStream::new();
|
||||
let mut arms = TokenStream::new();
|
||||
|
||||
for function in &functions {
|
||||
for attr in &function.attrs {
|
||||
if !attr.path().is_ident("doc") {
|
||||
return Err(Error::new(
|
||||
attr.span(),
|
||||
"command functions may not have attributes; anything that doesn't fit the `fn name(args…) -> Message` contract belongs in a plain impl block",
|
||||
));
|
||||
}
|
||||
}
|
||||
if !matches!(function.vis, Visibility::Inherited) {
|
||||
return Err(Error::new(
|
||||
function.span(),
|
||||
"command functions have no visibility modifier; the macro generates the public JS-facing stub",
|
||||
));
|
||||
}
|
||||
|
||||
let signature = &function.sig;
|
||||
if let Some(receiver) = signature.receiver() {
|
||||
return Err(Error::new(receiver.span(), "command functions take no `self`; they are pure `args… -> Message` translations"));
|
||||
}
|
||||
if !signature.generics.params.is_empty() || signature.asyncness.is_some() || signature.unsafety.is_some() {
|
||||
return Err(Error::new(signature.span(), "command functions must be plain non-generic, non-async, safe functions"));
|
||||
}
|
||||
|
||||
let docs = &function.attrs;
|
||||
let fn_name = &signature.ident;
|
||||
let variant = Ident::new(&fn_name.to_string().to_case(Case::Pascal), fn_name.span());
|
||||
let js_name = Ident::new(&fn_name.to_string().to_case(Case::Camel), fn_name.span());
|
||||
|
||||
let mut param_names = Vec::new();
|
||||
let mut param_types = Vec::new();
|
||||
for parameter in &signature.inputs {
|
||||
let FnArg::Typed(pat_type) = parameter else { unreachable!("receiver is rejected above") };
|
||||
let Pat::Ident(pat_ident) = &*pat_type.pat else {
|
||||
return Err(Error::new(pat_type.span(), "command parameters must be plain identifiers"));
|
||||
};
|
||||
param_names.push(&pat_ident.ident);
|
||||
param_types.push(&*pat_type.ty);
|
||||
}
|
||||
|
||||
let return_type = &signature.output;
|
||||
let body = &function.block;
|
||||
|
||||
let span = fn_name.span();
|
||||
variants.extend(quote_spanned! {span=>
|
||||
#(#docs)*
|
||||
#variant { #(#param_names: #param_types,)* },
|
||||
});
|
||||
stubs.extend(quote_spanned! {span=>
|
||||
#(#docs)*
|
||||
#[cfg(not(feature = "native"))]
|
||||
#[wasm_bindgen(js_name = #js_name)]
|
||||
pub fn #fn_name(&self, #(#param_names: #param_types,)*) {
|
||||
self.dispatch((move || #return_type #body)())
|
||||
}
|
||||
#(#docs)*
|
||||
#[cfg(feature = "native")]
|
||||
#[wasm_bindgen(js_name = #js_name)]
|
||||
pub fn #fn_name(&self, #(#param_names: #param_types,)*) {
|
||||
self.send(EditorCommand::#variant { #(#param_names,)* })
|
||||
}
|
||||
});
|
||||
arms.extend(quote_spanned! {span=>
|
||||
EditorCommand::#variant { #(#param_names,)* } => #body,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(quote! {
|
||||
#(
|
||||
#[cfg(feature = "editor")]
|
||||
#imports
|
||||
)*
|
||||
|
||||
#[cfg(any(feature = "native", not(target_family = "wasm")))]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub enum EditorCommand {
|
||||
#variants
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "editor", any(feature = "native", not(target_family = "wasm"))))]
|
||||
impl From<EditorCommand> for Message {
|
||||
fn from(command: EditorCommand) -> Self {
|
||||
match command {
|
||||
#arms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_family = "wasm")]
|
||||
#[wasm_bindgen]
|
||||
impl EditorWrapper {
|
||||
#stubs
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
mod as_message;
|
||||
mod combined_message_attrs;
|
||||
mod discriminant;
|
||||
mod editor_commands;
|
||||
mod extract_fields;
|
||||
mod helper_structs;
|
||||
mod helpers;
|
||||
@@ -15,6 +16,7 @@ mod widget_builder;
|
||||
use crate::as_message::derive_as_message_impl;
|
||||
use crate::combined_message_attrs::combined_message_attrs_impl;
|
||||
use crate::discriminant::derive_discriminant_impl;
|
||||
use crate::editor_commands::editor_commands_impl;
|
||||
use crate::extract_fields::derive_extract_field_impl;
|
||||
use crate::helper_structs::AttrInnerSingleString;
|
||||
use crate::hierarchical_tree::generate_hierarchical_tree;
|
||||
@@ -302,6 +304,12 @@ pub fn message_handler_data(attr: TokenStream, input_item: TokenStream) -> Token
|
||||
TokenStream::from(message_handler_data_attr_impl(attr.into(), input_item.into()).unwrap_or_else(|err| err.to_compile_error()))
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn editor_commands(attr: TokenStream, input_item: TokenStream) -> TokenStream {
|
||||
let module = syn::parse_macro_input!(input_item as syn::ItemMod);
|
||||
TokenStream::from(editor_commands_impl(attr.into(), module).unwrap_or_else(|err| err.to_compile_error()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -8,7 +8,15 @@ use std::path::Path;
|
||||
use std::process::ExitCode;
|
||||
use std::time::Duration;
|
||||
|
||||
const EXCLUDED_DIRECTORIES: &[&str] = &["target", ".git", "frontend/node_modules", "frontend/dist", "frontend/wrapper/pkg", "tools"];
|
||||
const EXCLUDED_DIRECTORIES: &[&str] = &[
|
||||
"target",
|
||||
".git",
|
||||
"frontend/node_modules",
|
||||
"frontend/dist",
|
||||
"frontend/wrapper/pkg",
|
||||
"frontend/wrapper/pkg-native",
|
||||
"tools",
|
||||
];
|
||||
const INCLUDED_EXTENSIONS: &[&str] = &["rs"];
|
||||
|
||||
const DEBOUNCE: Duration = Duration::from_millis(500);
|
||||
|
||||
@@ -10,8 +10,12 @@ pub fn frontend_dir() -> PathBuf {
|
||||
workspace_dir().join("frontend")
|
||||
}
|
||||
|
||||
fn wasm_glue_path() -> PathBuf {
|
||||
frontend_dir().join("wrapper").join("pkg").join(format!("{OUT_NAME}.js"))
|
||||
fn pkg_dir(native: bool) -> PathBuf {
|
||||
frontend_dir().join("wrapper").join(if native { "pkg-native" } else { "pkg" })
|
||||
}
|
||||
|
||||
fn wasm_glue_path(native: bool) -> PathBuf {
|
||||
pkg_dir(native).join(format!("{OUT_NAME}.js"))
|
||||
}
|
||||
|
||||
pub fn setup() -> Result<(), Error> {
|
||||
@@ -50,15 +54,15 @@ pub fn build_wasm(release: bool, native: bool) -> Result<(), Error> {
|
||||
|
||||
pub fn build_wasm_steps(release: bool, native: bool) -> Vec<Expression> {
|
||||
let wasm_artifact = target_dir().join(WASM_TARGET).join(if release { "release" } else { "debug" }).join(format!("{OUT_NAME}.wasm"));
|
||||
let pkg_dir = frontend_dir().join("wrapper").join("pkg");
|
||||
let pkg_dir = pkg_dir(native);
|
||||
|
||||
let mut steps = vec![
|
||||
cmd!("cargo", "build", "--lib", "--package", WRAPPER_CRATE, "--target", WASM_TARGET)
|
||||
.arg_if(release, "--release")
|
||||
.args_if(native, ["--no-default-features", "--features", "native"])
|
||||
.dir(workspace_dir())
|
||||
.before_spawn(|_| {
|
||||
if is_build_corrupted() {
|
||||
.before_spawn(move |_| {
|
||||
if is_build_corrupted(wasm_glue_path(native)) {
|
||||
clean_wasm();
|
||||
}
|
||||
Ok(())
|
||||
@@ -90,8 +94,8 @@ pub fn clean_wasm() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn is_build_corrupted() -> bool {
|
||||
let Ok(js) = std::fs::read_to_string(wasm_glue_path()) else {
|
||||
pub fn is_build_corrupted(path: PathBuf) -> bool {
|
||||
let Ok(js) = std::fs::read_to_string(&path) else {
|
||||
return false;
|
||||
};
|
||||
js.contains("from \"env\"") || js.contains("from 'env'")
|
||||
|
||||
Reference in new issue
Block a user