Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support newtype pattern in bindings #4

Open
ctaggart opened this issue May 25, 2020 · 0 comments
Open

support newtype pattern in bindings #4

ctaggart opened this issue May 25, 2020 · 0 comments
Labels
upstream Something is need from upstream

Comments

@ctaggart
Copy link
Contributor

ctaggart commented May 25, 2020

It would be more type-safe if we could wrap primitives using the newtype pattern. The API would then become:

pub struct KeyCode(pub i32);

extern {
  # [ wasm_bindgen ( method , getter , js_name = keyCode ) ] 
  pub fn key_code(this: &IKeyboardEvent) -> KeyCode; 
}

Currently, KeyCode is defined as an alias and the enum values becomes constants in a separate type.

# [ wasm_bindgen ( method , getter , js_name = keyCode ) ]
pub fn key_code(this: &IKeyboardEvent) -> KeyCode;

pub type KeyCode = i32;
pub type KeyCodeRef = i32;
#[doc = "Virtual Key Codes, the value does not hold any inherent meaning."]
#[doc = "Inspired somewhat from https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx"]
#[doc = "But these are \"more general\", as they should work across browsers & OS`s."]
#[derive(Debug, PartialEq, Eq)]
pub struct KeyCodeConst(pub i32);
impl KeyCodeConst {
#[doc = "Placed first to cover the 0 value of the enum."]
pub const UNKNOWN: KeyCodeConst = KeyCodeConst(0);
pub const BACKSPACE: KeyCodeConst = KeyCodeConst(1);
pub const TAB: KeyCodeConst = KeyCodeConst(2);
pub const ENTER: KeyCodeConst = KeyCodeConst(3);
pub const SHIFT: KeyCodeConst = KeyCodeConst(4);
pub const CTRL: KeyCodeConst = KeyCodeConst(5);
pub const ALT: KeyCodeConst = KeyCodeConst(6);
pub const PAUSE_BREAK: KeyCodeConst = KeyCodeConst(7);
pub const CAPS_LOCK: KeyCodeConst = KeyCodeConst(8);
pub const ESCAPE: KeyCodeConst = KeyCodeConst(9);
pub const SPACE: KeyCodeConst = KeyCodeConst(10);
pub const PAGE_UP: KeyCodeConst = KeyCodeConst(11);
pub const PAGE_DOWN: KeyCodeConst = KeyCodeConst(12);
pub const END: KeyCodeConst = KeyCodeConst(13);
pub const HOME: KeyCodeConst = KeyCodeConst(14);
pub const LEFT_ARROW: KeyCodeConst = KeyCodeConst(15);
pub const UP_ARROW: KeyCodeConst = KeyCodeConst(16);
pub const RIGHT_ARROW: KeyCodeConst = KeyCodeConst(17);
pub const DOWN_ARROW: KeyCodeConst = KeyCodeConst(18);
pub const INSERT: KeyCodeConst = KeyCodeConst(19);
pub const DELETE: KeyCodeConst = KeyCodeConst(20);
pub const KEY_0: KeyCodeConst = KeyCodeConst(21);
pub const KEY_1: KeyCodeConst = KeyCodeConst(22);
pub const KEY_2: KeyCodeConst = KeyCodeConst(23);
pub const KEY_3: KeyCodeConst = KeyCodeConst(24);
pub const KEY_4: KeyCodeConst = KeyCodeConst(25);
pub const KEY_5: KeyCodeConst = KeyCodeConst(26);
pub const KEY_6: KeyCodeConst = KeyCodeConst(27);
pub const KEY_7: KeyCodeConst = KeyCodeConst(28);
pub const KEY_8: KeyCodeConst = KeyCodeConst(29);
pub const KEY_9: KeyCodeConst = KeyCodeConst(30);
pub const KEY_A: KeyCodeConst = KeyCodeConst(31);
pub const KEY_B: KeyCodeConst = KeyCodeConst(32);
pub const KEY_C: KeyCodeConst = KeyCodeConst(33);
pub const KEY_D: KeyCodeConst = KeyCodeConst(34);
pub const KEY_E: KeyCodeConst = KeyCodeConst(35);
pub const KEY_F: KeyCodeConst = KeyCodeConst(36);
pub const KEY_G: KeyCodeConst = KeyCodeConst(37);
pub const KEY_H: KeyCodeConst = KeyCodeConst(38);
pub const KEY_I: KeyCodeConst = KeyCodeConst(39);
pub const KEY_J: KeyCodeConst = KeyCodeConst(40);
pub const KEY_K: KeyCodeConst = KeyCodeConst(41);
pub const KEY_L: KeyCodeConst = KeyCodeConst(42);
pub const KEY_M: KeyCodeConst = KeyCodeConst(43);
pub const KEY_N: KeyCodeConst = KeyCodeConst(44);
pub const KEY_O: KeyCodeConst = KeyCodeConst(45);
pub const KEY_P: KeyCodeConst = KeyCodeConst(46);
pub const KEY_Q: KeyCodeConst = KeyCodeConst(47);
pub const KEY_R: KeyCodeConst = KeyCodeConst(48);
pub const KEY_S: KeyCodeConst = KeyCodeConst(49);
pub const KEY_T: KeyCodeConst = KeyCodeConst(50);
pub const KEY_U: KeyCodeConst = KeyCodeConst(51);
pub const KEY_V: KeyCodeConst = KeyCodeConst(52);
pub const KEY_W: KeyCodeConst = KeyCodeConst(53);
pub const KEY_X: KeyCodeConst = KeyCodeConst(54);
pub const KEY_Y: KeyCodeConst = KeyCodeConst(55);
pub const KEY_Z: KeyCodeConst = KeyCodeConst(56);
pub const META: KeyCodeConst = KeyCodeConst(57);
pub const CONTEXT_MENU: KeyCodeConst = KeyCodeConst(58);
pub const F1: KeyCodeConst = KeyCodeConst(59);
pub const F2: KeyCodeConst = KeyCodeConst(60);
pub const F3: KeyCodeConst = KeyCodeConst(61);
pub const F4: KeyCodeConst = KeyCodeConst(62);
pub const F5: KeyCodeConst = KeyCodeConst(63);
pub const F6: KeyCodeConst = KeyCodeConst(64);
pub const F7: KeyCodeConst = KeyCodeConst(65);
pub const F8: KeyCodeConst = KeyCodeConst(66);
pub const F9: KeyCodeConst = KeyCodeConst(67);
pub const F10: KeyCodeConst = KeyCodeConst(68);
pub const F11: KeyCodeConst = KeyCodeConst(69);
pub const F12: KeyCodeConst = KeyCodeConst(70);
pub const F13: KeyCodeConst = KeyCodeConst(71);
pub const F14: KeyCodeConst = KeyCodeConst(72);
pub const F15: KeyCodeConst = KeyCodeConst(73);
pub const F16: KeyCodeConst = KeyCodeConst(74);
pub const F17: KeyCodeConst = KeyCodeConst(75);
pub const F18: KeyCodeConst = KeyCodeConst(76);
pub const F19: KeyCodeConst = KeyCodeConst(77);
pub const NUM_LOCK: KeyCodeConst = KeyCodeConst(78);
pub const SCROLL_LOCK: KeyCodeConst = KeyCodeConst(79);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the ';:' key"]
pub const US_SEMICOLON: KeyCodeConst = KeyCodeConst(80);
#[doc = "For any country/region, the '+' key"]
#[doc = "For the US standard keyboard, the '=+' key"]
pub const US_EQUAL: KeyCodeConst = KeyCodeConst(81);
#[doc = "For any country/region, the ',' key"]
#[doc = "For the US standard keyboard, the ',<' key"]
pub const US_COMMA: KeyCodeConst = KeyCodeConst(82);
#[doc = "For any country/region, the '-' key"]
#[doc = "For the US standard keyboard, the '-_' key"]
pub const US_MINUS: KeyCodeConst = KeyCodeConst(83);
#[doc = "For any country/region, the '.' key"]
#[doc = "For the US standard keyboard, the '.>' key"]
pub const US_DOT: KeyCodeConst = KeyCodeConst(84);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the '/?' key"]
pub const US_SLASH: KeyCodeConst = KeyCodeConst(85);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the '`~' key"]
pub const US_BACKTICK: KeyCodeConst = KeyCodeConst(86);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the '[{' key"]
pub const US_OPEN_SQUARE_BRACKET: KeyCodeConst = KeyCodeConst(87);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the '\\|' key"]
pub const US_BACKSLASH: KeyCodeConst = KeyCodeConst(88);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the ']}' key"]
pub const US_CLOSE_SQUARE_BRACKET: KeyCodeConst = KeyCodeConst(89);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
#[doc = "For the US standard keyboard, the ''\"' key"]
pub const US_QUOTE: KeyCodeConst = KeyCodeConst(90);
#[doc = "Used for miscellaneous characters; it can vary by keyboard."]
pub const OEM_8: KeyCodeConst = KeyCodeConst(91);
#[doc = "Either the angle bracket key or the backslash key on the RT 102-key keyboard."]
pub const OEM_102: KeyCodeConst = KeyCodeConst(92);
pub const NUMPAD_0: KeyCodeConst = KeyCodeConst(93);
pub const NUMPAD_1: KeyCodeConst = KeyCodeConst(94);
pub const NUMPAD_2: KeyCodeConst = KeyCodeConst(95);
pub const NUMPAD_3: KeyCodeConst = KeyCodeConst(96);
pub const NUMPAD_4: KeyCodeConst = KeyCodeConst(97);
pub const NUMPAD_5: KeyCodeConst = KeyCodeConst(98);
pub const NUMPAD_6: KeyCodeConst = KeyCodeConst(99);
pub const NUMPAD_7: KeyCodeConst = KeyCodeConst(100);
pub const NUMPAD_8: KeyCodeConst = KeyCodeConst(101);
pub const NUMPAD_9: KeyCodeConst = KeyCodeConst(102);
pub const NUMPAD_MULTIPLY: KeyCodeConst = KeyCodeConst(103);
pub const NUMPAD_ADD: KeyCodeConst = KeyCodeConst(104);
pub const NUMPAD_SEPARATOR: KeyCodeConst = KeyCodeConst(105);
pub const NUMPAD_SUBTRACT: KeyCodeConst = KeyCodeConst(106);
pub const NUMPAD_DECIMAL: KeyCodeConst = KeyCodeConst(107);
pub const NUMPAD_DIVIDE: KeyCodeConst = KeyCodeConst(108);
#[doc = "Cover all key codes when IME is processing input."]
pub const KEY_IN_COMPOSITION: KeyCodeConst = KeyCodeConst(109);
pub const ABNT_C1: KeyCodeConst = KeyCodeConst(110);
pub const ABNT_C2: KeyCodeConst = KeyCodeConst(111);
#[doc = "Placed last to cover the length of the enum."]
#[doc = "Please do not depend on this value!"]
pub const MAX_VALUE: KeyCodeConst = KeyCodeConst(112);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream Something is need from upstream
Projects
None yet
Development

No branches or pull requests

1 participant