denox_wsi 0.3.0

Window system integration for Denox
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright 2023 Jo Bates. All rights reserved. MIT license.

use crate::{
  create_window_options::CreateWindowOptions,
  device_ids::DeviceIds,
  event::WsiEvent,
  request::{handle_requests, Request},
};
use deno_core::anyhow;
use deno_webgpu::wgpu_core::id::SurfaceId;
use std::{
  cell::Cell, collections::HashMap, rc::Rc, sync::mpsc as std_mpsc, thread,
};
use tokio::sync::mpsc as tokio_mpsc;
use winit::{
  dpi::{PhysicalPosition, PhysicalSize},
  error::{NotSupportedError, OsError},
  event_loop::{EventLoop, EventLoopProxy},
  window::{Fullscreen, WindowId},
};

// Spawn a proxy thread and hijack the calling thread for the real event loop.
// On some platforms (e.g. macOS), this needs to be called from the main thread.
pub fn hijack_main_and_spawn_proxy<F>(f: F) -> !
where
  F: FnOnce(Rc<WsiEventLoopProxy>) + Send + 'static,
{
  // Initialize.
  let event_loop = EventLoop::new();
  let event_loop_proxy = event_loop.create_proxy();
  let (event_tx, event_rx) = tokio_mpsc::channel(1);
  let (request_tx, mut request_rx) = std_mpsc::sync_channel(1);

  // Spawn the proxy thread.
  thread::spawn(move || {
    let wsi_event_loop_proxy = Rc::new(WsiEventLoopProxy {
      event_loop_proxy,
      waiting_for_event: Cell::new(false),
      event_rx: Cell::new(Some(event_rx)),
      request_tx,
    });
    let _retain = wsi_event_loop_proxy.clone();
    f(wsi_event_loop_proxy);
  });

  // Handle requests until the proxy thread is ready for the first event.
  let mut windows = HashMap::new();
  handle_requests(&event_loop, &mut request_rx, &mut windows);

  // Run the real event loop.
  let mut device_ids = DeviceIds::new();
  event_loop.run(move |event, window_target, control_flow| {
    let event = WsiEvent::from(event, &mut device_ids);
    event_tx.blocking_send(event).unwrap();
    handle_requests(window_target, &mut request_rx, &mut windows);
    control_flow.set_wait();
  });
}

// Event loop proxy.
pub struct WsiEventLoopProxy {
  event_loop_proxy: EventLoopProxy<()>,
  waiting_for_event: Cell<bool>,
  event_rx: Cell<Option<tokio_mpsc::Receiver<WsiEvent>>>,
  request_tx: std_mpsc::SyncSender<Request>,
}

impl WsiEventLoopProxy {
  // Get the next event from the real event loop.
  // Don't call this multiple times concurrently.
  pub(crate) async fn next_event(&self) -> Result<WsiEvent, anyhow::Error> {
    // Take the receiver for exclusive use.
    let Some(mut event_rx) = self.event_rx.take() else {
      return Err(anyhow::Error::msg("Receiver already in use"));
    };

    // Send the request.
    self.request_tx.send(Request::NextEvent).unwrap();

    // Async wait for the event.
    self.waiting_for_event.set(true);
    let event = event_rx.recv().await.unwrap();
    self.waiting_for_event.set(false);

    // Save the receiver for re-use.
    self.event_rx.set(Some(event_rx));

    // Return the event.
    Ok(event)
  }

  // Send a request from the proxy thread to the real event loop.
  fn send_request(&self, request: Request) {
    self.request_tx.send(request).unwrap();

    // Send an event to the real event loop if the proxy thread is currently
    // waiting to receive an event. The real event loop might be waiting on an
    // event too and won't process this request until it receives one.
    if self.waiting_for_event.get() {
      self.event_loop_proxy.send_event(()).unwrap();

      // We don't need to do this again until we request the next event.
      self.waiting_for_event.set(false);
    }
  }

  pub(crate) fn create_window(
    &self,
    options: Option<Box<CreateWindowOptions>>,
  ) -> Result<WindowId, OsError> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::CreateWindow { options, result_tx });
    result_rx.recv().unwrap()
  }

  pub(crate) fn destroy_window(&self, window_id: WindowId) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::DestroyWindow {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn create_webgpu_surface(
    &self,
    window_id: WindowId,
    webgpu_instance: Box<deno_webgpu::Instance>,
  ) -> (Box<deno_webgpu::Instance>, SurfaceId) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::CreateWebGpuSurface {
      window_id,
      webgpu_instance,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_scale_factor(&self, window_id: WindowId) -> f64 {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowScaleFactor {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_request_redraw(&self, window_id: WindowId) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowRedraw {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_inner_position(
    &self,
    window_id: WindowId,
  ) -> Result<PhysicalPosition<i32>, NotSupportedError> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowInnerPosition {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_outer_position(
    &self,
    window_id: WindowId,
  ) -> Result<PhysicalPosition<i32>, NotSupportedError> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowOuterPosition {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_outer_position(
    &self,
    window_id: WindowId,
    position: PhysicalPosition<i32>,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetOuterPosition {
      window_id,
      position,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_inner_size(
    &self,
    window_id: WindowId,
  ) -> PhysicalSize<u32> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowInnerSize {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_inner_size(
    &self,
    window_id: WindowId,
    size: PhysicalSize<u32>,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetInnerSize {
      window_id,
      size,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_outer_size(
    &self,
    window_id: WindowId,
  ) -> PhysicalSize<u32> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowOuterSize {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_min_inner_size(
    &self,
    window_id: WindowId,
    size: Option<PhysicalSize<u32>>,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetMinInnerSize {
      window_id,
      size,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_max_inner_size(
    &self,
    window_id: WindowId,
    size: Option<PhysicalSize<u32>>,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetMaxInnerSize {
      window_id,
      size,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_title(&self, window_id: WindowId, title: String) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetTitle {
      window_id,
      title,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_visible(&self, window_id: WindowId, visible: bool) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetVisible {
      window_id,
      visible,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_is_visible(&self, window_id: WindowId) -> Option<bool> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowIsVisible {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_resizable(
    &self,
    window_id: WindowId,
    resizable: bool,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetResizable {
      window_id,
      resizable,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_is_resizable(&self, window_id: WindowId) -> bool {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowIsResizable {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_minimized(
    &self,
    window_id: WindowId,
    minimized: bool,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetMinimized {
      window_id,
      minimized,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_maximized(
    &self,
    window_id: WindowId,
    maximized: bool,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetMaximized {
      window_id,
      maximized,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_is_maximized(&self, window_id: WindowId) -> bool {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowIsMaximized {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_fullscreen(
    &self,
    window_id: WindowId,
    fullscreen: Option<Fullscreen>,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetFullscreen {
      window_id,
      fullscreen,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_fullscreen(
    &self,
    window_id: WindowId,
  ) -> Option<Fullscreen> {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowFullscreen {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_decorations(
    &self,
    window_id: WindowId,
    decorations: bool,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetDecorations {
      window_id,
      decorations,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_is_decorated(&self, window_id: WindowId) -> bool {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowIsDecorated {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn window_set_always_on_top(
    &self,
    window_id: WindowId,
    always_on_top: bool,
  ) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::WindowSetAlwaysOnTop {
      window_id,
      always_on_top,
      result_tx,
    });
    result_rx.recv().unwrap()
  }

  pub(crate) fn focus_window(&self, window_id: WindowId) {
    let (result_tx, result_rx) = std_mpsc::sync_channel(0);
    self.send_request(Request::FocusWindow {
      window_id,
      result_tx,
    });
    result_rx.recv().unwrap()
  }
}