Skip to content

Commit

Permalink
Add WSI.setDeviceEventFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
jbatez committed Feb 12, 2023
1 parent b98b308 commit 97d7109
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
7 changes: 7 additions & 0 deletions cli/tsc/dts/lib.deno.wsi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare namespace Deno {
export const wsi: WSI;
export class WSI {
nextEvent(): Promise<WSIEvent>;
setDeviceEventFilter(filter: WSIDeviceEventFilter): void;
createWindow(options?: WSICreateWindowOptions): WSIWindow;
}

Expand Down Expand Up @@ -83,6 +84,12 @@ declare namespace Deno {
| "col-resize"
| "row-resize";

// https://docs.rs/winit/0.28.1/winit/event_loop/enum.DeviceEventFilter.html
export type WSIDeviceEventFilter =
| "always"
| "unfocused"
| "never";

// https://docs.rs/winit/0.28.1/winit/event/enum.Event.html
export type WSIEvent =
| {
Expand Down
13 changes: 13 additions & 0 deletions ext/wsi/01_wsi.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@
return event;
}

setDeviceEventFilter(filter) {
webidl.assertBranded(this, WSIPrototype);
const prefix = "Failed to execute 'setDeviceEventFilter' on 'WSI'";

webidl.requiredArguments(arguments.length, 1, { prefix });
filter = webidl.converters["WSIDeviceEventFilter"](filter, {
prefix,
context: "Argument 1",
});

return ops.op_wsi_set_device_event_filter(filter);
}

createWindow(options) {
webidl.assertBranded(this, WSIPrototype);
const prefix = "Failed to execute 'createWindow' on 'WSI'";
Expand Down
10 changes: 10 additions & 0 deletions ext/wsi/02_idl_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
],
);

// ENUM: WSIDeviceEventFilter
webidl.converters["WSIDeviceEventFilter"] = webidl.createEnumConverter(
"WSIDeviceEventFilter",
[
"always",
"unfocused",
"never",
],
);

// ENUM: WSIIMEPurpose
webidl.converters["WSIIMEPurpose"] = webidl.createEnumConverter(
"WSIIMEPurpose",
Expand Down
29 changes: 25 additions & 4 deletions ext/wsi/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::{Serialize, Serializer};
use winit::event::{
ElementState, Force, MouseButton, MouseScrollDelta, TouchPhase,
VirtualKeyCode,
use serde::{Deserialize, Serialize, Serializer};
use winit::{
event::{
ElementState, Force, MouseButton, MouseScrollDelta, TouchPhase,
VirtualKeyCode,
},
event_loop::DeviceEventFilter,
};

#[derive(Debug, Serialize)]
Expand All @@ -20,6 +23,24 @@ impl From<ElementState> for WsiButtonState {
}
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum WsiDeviceEventFilter {
Always,
Unfocused,
Never,
}

impl From<WsiDeviceEventFilter> for DeviceEventFilter {
fn from(filter: WsiDeviceEventFilter) -> Self {
match filter {
WsiDeviceEventFilter::Always => Self::Always,
WsiDeviceEventFilter::Unfocused => Self::Unfocused,
WsiDeviceEventFilter::Never => Self::Never,
}
}
}

#[derive(Debug, Serialize)]
pub struct WsiKeyCode(#[serde(with = "WsiKeyCodeDef")] pub VirtualKeyCode);

Expand Down
12 changes: 12 additions & 0 deletions ext/wsi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
cursor::{WsiCursorGrabMode, WsiCursorIcon},
event::WsiEvent,
event_loop::WsiEventLoopProxy,
input::WsiDeviceEventFilter,
window::{
WsiCreateWindowOptions, WsiImePurpose, WsiResizeDirection,
WsiUserAttentionType, WsiWindowLevel, WsiWindowTheme,
Expand All @@ -36,6 +37,7 @@ pub fn init(event_loop_proxy: Option<Rc<WsiEventLoopProxy>>) -> Extension {
))
.ops(vec![
op_wsi_next_event::decl(),
op_wsi_set_device_event_filter::decl(),
op_wsi_create_window::decl(),
op_wsi_window_set_content_protected::decl(),
op_wsi_window_set_cursor_grab_mode::decl(),
Expand Down Expand Up @@ -121,6 +123,16 @@ async fn op_wsi_next_event(
}
}

#[op]
fn op_wsi_set_device_event_filter(
state: &mut OpState,
filter: WsiDeviceEventFilter,
) {
try_borrow_event_loop_proxy(state, "Deno.wsi.setDeviceEventFilter").execute(
|window_target, _| window_target.set_device_event_filter(filter.into()),
)
}

#[op]
fn op_wsi_create_window(
state: &mut OpState,
Expand Down

0 comments on commit 97d7109

Please sign in to comment.