mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Implement key handling (#65)
This commit is contained in:
committed by
Keavon Chambers
parent
599d478a5c
commit
0a112be97b
@@ -109,6 +109,19 @@ export default defineComponent({
|
||||
const { on_mouse_move } = await wasm;
|
||||
on_mouse_move(e.offsetX, e.offsetY);
|
||||
},
|
||||
async keyDown(e: KeyboardEvent) {
|
||||
const { on_key_down } = await wasm;
|
||||
on_key_down(e.key);
|
||||
},
|
||||
async keyUp(e: KeyboardEvent) {
|
||||
const { on_key_up } = await wasm;
|
||||
on_key_up(e.key);
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
let self = this;
|
||||
window.addEventListener("keyup", (evt: KeyboardEvent) => {self.keyUp(evt)})
|
||||
window.addEventListener("keydown", (evt: KeyboardEvent) => {self.keyDown(evt)})
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::shims::Error;
|
||||
use crate::wrappers::{translate_tool, Color};
|
||||
use crate::wrappers::{translate_key, translate_tool, Color};
|
||||
use crate::EDITOR_STATE;
|
||||
use crate::{shims::Error, utils};
|
||||
use editor_core::events;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
@@ -46,6 +46,24 @@ pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||
}
|
||||
|
||||
/// A keyboard button depressed within screenspace the bounds of the viewport
|
||||
#[wasm_bindgen]
|
||||
pub fn on_key_down(name: String) -> Result<(), JsValue> {
|
||||
let key = translate_key(&name);
|
||||
log::trace!("key down {:?}, name: {}", key, name);
|
||||
let ev = events::Event::KeyDown(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||
}
|
||||
|
||||
/// A keyboard button released
|
||||
#[wasm_bindgen]
|
||||
pub fn on_key_up(name: String) -> Result<(), JsValue> {
|
||||
let key = translate_key(&name);
|
||||
log::trace!("key up {:?}, name: {}", key, name);
|
||||
let ev = events::Event::KeyUp(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||
}
|
||||
|
||||
/// Update primary color
|
||||
#[wasm_bindgen]
|
||||
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn set_panic_hook() {
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn debug(msg: &str, format: &str);
|
||||
fn log(msg: &str, format: &str);
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn info(msg: &str, format: &str);
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
@@ -32,13 +32,13 @@ impl log::Log for WasmLog {
|
||||
|
||||
fn log(&self, record: &log::Record) {
|
||||
let (log, name, color): (fn(&str, &str), &str, &str) = match record.level() {
|
||||
log::Level::Trace => (debug, "trace", "color:plum"),
|
||||
log::Level::Debug => (debug, "debug", "color:plum"),
|
||||
log::Level::Warn => (warn, "warn", "color:#1b8"),
|
||||
log::Level::Info => (info, "info", "color:#fa2"),
|
||||
log::Level::Trace => (log, "trace", "color:plum"),
|
||||
log::Level::Debug => (log, "debug", "color:blue"),
|
||||
log::Level::Warn => (warn, "warn", "color:#fa2"),
|
||||
log::Level::Info => (info, "info", "color:#1b8"),
|
||||
log::Level::Error => (error, "error", "color:red"),
|
||||
};
|
||||
let msg = &format!("{}", format_args!("%c{}%c\t{}", name, record.args()));
|
||||
let msg = &format!("{}", format_args!("%c{}\t{}", name, record.args()));
|
||||
log(msg, color)
|
||||
}
|
||||
fn flush(&self) {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::shims::Error;
|
||||
use editor_core::events;
|
||||
use editor_core::tools::{SelectAppendMode, ToolType};
|
||||
use editor_core::Color as InnerColor;
|
||||
use wasm_bindgen::prelude::*;
|
||||
@@ -48,3 +49,24 @@ pub fn translate_append_mode(name: &str) -> Option<SelectAppendMode> {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn translate_key(name: &str) -> events::Key {
|
||||
use events::Key as K;
|
||||
match name {
|
||||
"e" => K::KeyE,
|
||||
"r" => K::KeyR,
|
||||
"m" => K::KeyM,
|
||||
"x" => K::KeyX,
|
||||
"0" => K::Key0,
|
||||
"1" => K::Key1,
|
||||
"2" => K::Key2,
|
||||
"3" => K::Key3,
|
||||
"4" => K::Key4,
|
||||
"5" => K::Key5,
|
||||
"6" => K::Key6,
|
||||
"7" => K::Key7,
|
||||
"8" => K::Key8,
|
||||
"9" => K::Key9,
|
||||
_ => K::UnknownKey,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user