mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 03:18:06 +08:00
Implement event/response roundtrip (#49)
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
</LayoutRow>
|
||||
<LayoutRow :class="'tools-and-viewport'">
|
||||
<LayoutCol :class="'tools'"></LayoutCol>
|
||||
<LayoutCol :class="'viewport-container'"></LayoutCol>
|
||||
<LayoutCol :class="'viewport-container'" @click="canvasClick"></LayoutCol>
|
||||
</LayoutRow>
|
||||
</LayoutCol>
|
||||
</template>
|
||||
@@ -70,12 +70,19 @@ import { defineComponent } from "vue";
|
||||
import LayoutRow from "../layout/LayoutRow.vue";
|
||||
import LayoutCol from "../layout/LayoutCol.vue";
|
||||
|
||||
const wasm = import("../../../wasm/pkg");
|
||||
|
||||
export default defineComponent({
|
||||
components: {
|
||||
LayoutRow,
|
||||
LayoutCol,
|
||||
},
|
||||
props: {
|
||||
methods: {
|
||||
async canvasClick(e: MouseEvent) {
|
||||
console.log(e);
|
||||
const { on_mouse_click } = await wasm;
|
||||
on_mouse_click(e.offsetX, e.offsetY);
|
||||
}
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
3
client/web/src/wasm-callback-processor.js
Normal file
3
client/web/src/wasm-callback-processor.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export function update_canvas() {
|
||||
console.log("update_canvas")
|
||||
}
|
||||
@@ -4,23 +4,27 @@ pub mod viewport;
|
||||
pub mod window;
|
||||
pub mod wrappers;
|
||||
|
||||
use graphite_editor_core::Editor;
|
||||
use graphite_editor_core::{events::Response, Callback, Editor};
|
||||
use std::cell::RefCell;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
||||
thread_local! {pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new())}
|
||||
thread_local! {pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response)))}
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn init() {
|
||||
utils::set_panic_hook();
|
||||
}
|
||||
|
||||
/// Send events
|
||||
#[wasm_bindgen]
|
||||
pub fn handle_event(event_name: String) {
|
||||
// TODO: add payload
|
||||
todo!()
|
||||
fn handle_response(response: Response) {
|
||||
match response {
|
||||
Response::UpdateCanvas => update_canvas(),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(module = "/../src/wasm-callback-processor.js")]
|
||||
extern "C" {
|
||||
fn update_canvas();
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::shims::Error;
|
||||
use crate::wrappers::{translate_tool, Color};
|
||||
use crate::EDITOR_STATE;
|
||||
use graphite_editor_core::events;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// Modify the currently selected tool in the document state store
|
||||
@@ -15,10 +16,22 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
||||
})
|
||||
}
|
||||
|
||||
static mut POS: (u32, u32) = (0, 0);
|
||||
|
||||
/// Mouse movement with the bounds of the canvas
|
||||
#[wasm_bindgen]
|
||||
pub fn on_mouse_move(x: u32, y: u32) {
|
||||
todo!()
|
||||
// SAFETY: This is safe because the code can only ever run in a single thread
|
||||
unsafe {
|
||||
POS = (x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/// Mouse click within the bounds of the canvas
|
||||
#[wasm_bindgen]
|
||||
pub fn on_mouse_click(x: u32, y: u32) -> Result<(), JsValue> {
|
||||
let ev = events::Event::Click(events::MouseState::from_pos(x, y));
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||
}
|
||||
|
||||
/// Update working colors
|
||||
|
||||
Reference in New Issue
Block a user