ribir 0.2.0-alpha.6

Ribir is a framework for building modern native/wasm cross-platform user interface applications.
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::clipboard::Clipboard;
use crate::register_platform_app_events_handlers;
use crate::winit_shell_wnd::{new_id, WinitShellWnd};
use ribir_core::{prelude::*, timer::Timer, window::WindowId};
use std::rc::Rc;
use std::{convert::Infallible, sync::Once};
use winit::event::ElementState;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
use winit::{
  event::{Event, Ime, KeyEvent, StartCause, WindowEvent},
  event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy},
  keyboard::KeyCode,
};

pub struct App {
  event_loop: EventLoop<AppEvent>,
  active_wnd: Option<WindowId>,
  events_stream: MutRefItemSubject<'static, AppEvent, Infallible>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HotkeyEvent {
  pub key_code: Option<KeyCode>,
  pub modifiers: Option<ModifiersState>,
}

pub enum AppEvent {
  /// The event is sent when any future is waked to poll.
  FuturesWake,
  /// The event is sent when the application is be required to open a url. For
  /// example, it's launched from browser with a url.
  OpenUrl(String),
  /// The event is get global hotkey, it will receive the hotkey event.
  Hotkey(HotkeyEvent),
  /// The event is sent when the application window focus changed.
  WndFocusChanged(WindowId, bool),
  /// The custom event, you can send any data with this event.
  Custom(Box<dyn Any + Send>),
}

/// A sender to send event to the application event loop from which the
/// `EventSender` was created.
#[derive(Clone)]
pub struct EventSender(EventLoopProxy<AppEvent>);

#[derive(Clone)]
pub struct EventWaker(EventLoopProxy<AppEvent>);

impl App {
  /// Start an application with the `root` widget, this will use the default
  /// theme to create an application and use the `root` widget to create a
  /// window, then run the application.
  #[track_caller]
  pub fn run(root: impl WidgetBuilder) {
    Self::new_window(root, None);
    App::exec()
  }

  /// create a new window with the `root` widget and return the window id.
  #[track_caller]
  pub fn new_window(root: impl WidgetBuilder, size: Option<Size>) -> Rc<Window> {
    let app = unsafe { App::shared_mut() };
    let shell_wnd = WinitShellWnd::new(size, &app.event_loop);
    let wnd = AppCtx::new_window(Box::new(shell_wnd), root);

    if app.active_wnd.is_none() {
      app.active_wnd = Some(wnd.id());
    }
    wnd
  }

  /// Get a event sender of the application event loop, you can use this to send
  /// event.
  pub fn event_sender() -> EventSender {
    let proxy = App::shared().event_loop.create_proxy();
    EventSender(proxy)
  }

  pub fn events_stream() -> MutRefItemSubject<'static, AppEvent, Infallible> {
    App::shared().events_stream.clone()
  }

  pub fn active_window() -> Rc<Window> {
    App::shared()
      .active_wnd
      .and_then(AppCtx::get_window)
      .expect("application at least have one window before use.")
  }

  /// set the window with `id` to be the active window, and the active window.
  #[track_caller]
  pub fn set_active_window(id: WindowId) {
    let app = unsafe { App::shared_mut() };
    app.active_wnd = Some(id);
    // todo: set the window to be the top window, but we not really support
    // multi window fully, implement this later.
    if let Some(wnd) = AppCtx::get_window(id) {
      let mut shell = wnd.shell_wnd().borrow_mut();
      if shell.is_minimized() {
        shell.set_minimized(false);
      }
      shell.focus_window();
    };
  }

  /// run the application, this will start the event loop and block the current
  /// thread until the application exit.
  #[track_caller]
  pub fn exec() {
    Self::active_window().draw_frame();

    let event_loop = &mut unsafe { App::shared_mut() }.event_loop;

    let _ = event_loop.run_on_demand(move |event, loop_handle| {
      match event {
        Event::WindowEvent { event, window_id } => {
          let wnd_id = new_id(window_id);
          let Some(wnd) = AppCtx::get_window(wnd_id) else {
            return;
          };
          match event {
            WindowEvent::CloseRequested => {
              AppCtx::remove_wnd(wnd_id);
              if !AppCtx::has_wnd() {
                loop_handle.exit();
              }
            }
            WindowEvent::RedrawRequested => {
              if let Some(wnd) = AppCtx::get_window(wnd_id) {
                // if this frame is really draw, request another redraw. To make sure the draw
                // always end with a empty draw and emit an extra tick cycle message.
                if wnd.draw_frame() {
                  request_redraw(&wnd);
                }
              }
            }
            WindowEvent::Resized(_) => {
              let size = wnd.shell_wnd().borrow().inner_size();
              wnd.shell_wnd().borrow_mut().on_resize(size);
              request_redraw(&wnd)
            }
            WindowEvent::Focused(focused) => {
              let mut event = AppEvent::WndFocusChanged(wnd_id, focused);
              let app = unsafe { App::shared_mut() };
              app.events_stream.next(&mut event);
            }
            event => {
              App::dispatch_wnd_native_event(&wnd, event);
            }
          }
          wnd.emit_events();
        }
        Event::AboutToWait => {
          let run_count = AppCtx::run_until_stalled();
          if run_count > 0 {
            for wnd in AppCtx::windows().borrow().values() {
              request_redraw(wnd);
            }
          }
          if run_count > 0 {
            loop_handle.set_control_flow(ControlFlow::Poll);
          } else if let Some(t) = Timer::recently_timeout() {
            loop_handle.set_control_flow(ControlFlow::WaitUntil(t));
          } else {
            loop_handle.set_control_flow(ControlFlow::Wait);
          };
        }
        Event::NewEvents(cause) => match cause {
          StartCause::Poll | StartCause::ResumeTimeReached { start: _, requested_resume: _ } => {
            Timer::wake_timeout_futures();
          }
          _ => (),
        },
        Event::UserEvent(mut event) => {
          AppCtx::spawn_local(async move {
            let app = unsafe { App::shared_mut() };
            app.events_stream.next(&mut event);
          })
          .unwrap();
        }
        _ => (),
      }
    });
  }

  fn dispatch_wnd_native_event(wnd: &Window, event: WindowEvent) {
    static mut PRE_EDIT_HANDLE: PreEditHandle = PreEditHandle::new();
    match event {
      WindowEvent::KeyboardInput { event, .. } => {
        let KeyEvent {
          physical_key,
          logical_key,
          text,
          location,
          repeat,
          state,
          ..
        } = event;
        if unsafe { PRE_EDIT_HANDLE.is_in_pre_edit() } {
          return;
        }
        wnd.processes_keyboard_event(physical_key, logical_key, repeat, location, state);
        if state == ElementState::Pressed {
          if let Some(txt) = text {
            wnd.processes_receive_chars(txt.to_string());
          }
        }
      }
      WindowEvent::Ime(ime) => unsafe {
        PRE_EDIT_HANDLE.update(wnd, &ime);
      },
      WindowEvent::MouseInput { state, button, device_id, .. } => {
        if state == ElementState::Pressed {
          unsafe {
            PRE_EDIT_HANDLE.force_exit(wnd);
          }
        }
        wnd.process_mouse_input(device_id, state, button);
      }
      #[allow(deprecated)]
      _ => wnd.processes_native_event(event),
    }
  }

  #[track_caller]
  fn shared() -> &'static App { unsafe { Self::shared_mut() } }

  #[track_caller]
  unsafe fn shared_mut() -> &'static mut App {
    static mut INIT_ONCE: Once = Once::new();
    static mut APP: Option<App> = None;
    INIT_ONCE.call_once(|| {
      let event_loop = EventLoopBuilder::with_user_event().build().unwrap();
      let waker = EventWaker(event_loop.create_proxy());
      let clipboard = Clipboard::new().unwrap();
      unsafe {
        AppCtx::set_clipboard(Box::new(clipboard));
        AppCtx::set_runtime_waker(Box::new(waker));
      }
      register_platform_app_events_handlers();
      APP = Some(App {
        event_loop,
        events_stream: <_>::default(),
        active_wnd: None,
      })
    });
    AppCtx::thread_check();

    APP.as_mut().unwrap()
  }
}

impl EventSender {
  pub fn send(&self, e: AppEvent) {
    if let Err(err) = self.0.send_event(e) {
      log::error!("{}", err.to_string())
    }
  }
}

impl RuntimeWaker for EventWaker {
  fn clone_box(&self) -> Box<dyn RuntimeWaker + Send> { Box::new(self.clone()) }
  fn wake(&self) { let _ = self.0.send_event(AppEvent::FuturesWake); }
}

/// EventWaker only send `RibirEvent::FuturesWake`.
unsafe impl Send for EventWaker {}

pub(crate) fn request_redraw(wnd: &Window) {
  let wnd = wnd.shell_wnd().borrow();
  let shell = wnd.as_any().downcast_ref::<WinitShellWnd>().unwrap();
  shell.winit_wnd.request_redraw();
}

#[derive(Default)]
struct PreEditHandle(Option<String>);

impl PreEditHandle {
  const fn new() -> Self { Self(None) }
  fn update(&mut self, wnd: &Window, pre_edit: &Ime) {
    match pre_edit {
      Ime::Enabled => {}
      Ime::Preedit(txt, cursor) => match txt.is_empty() {
        true => self.exit(wnd),
        false => self.update_pre_edit(wnd, txt, cursor),
      },
      Ime::Commit(value) => {
        self.exit(wnd);
        wnd.processes_receive_chars(value.clone());
      }
      Ime::Disabled => self.exit(wnd),
    }
  }

  fn is_in_pre_edit(&self) -> bool { self.0.is_some() }

  fn force_exit(&mut self, wnd: &Window) {
    if self.is_in_pre_edit() {
      wnd.set_ime_allowed(false);
      wnd.processes_ime_pre_edit(ImePreEdit::End);
      if let Some(s) = self.0.take() {
        wnd.processes_receive_chars(s);
      }
      wnd.set_ime_allowed(true);
    }
  }

  fn exit(&mut self, wnd: &Window) {
    if self.is_in_pre_edit() {
      wnd.processes_ime_pre_edit(ImePreEdit::End);
      self.0.take();
    }
  }

  fn update_pre_edit(&mut self, wnd: &Window, txt: &str, cursor: &Option<(usize, usize)>) {
    if !self.is_in_pre_edit() {
      wnd.processes_ime_pre_edit(ImePreEdit::Begin);
    }

    wnd.processes_ime_pre_edit(ImePreEdit::PreEdit {
      value: txt.to_owned(),
      cursor: *cursor,
    });
    self.0 = Some(txt.to_owned());
  }
}

#[cfg(test)]
mod tests {
  use ribir_core::{
    prelude::*,
    test_helper::{MockBox, TestWindow},
  };
  use std::{cell::RefCell, rc::Rc};
  use winit::event::{Ime, WindowEvent};

  use super::App;
  #[derive(Debug, Default)]
  struct LogImeEvent {
    log: Rc<RefCell<Vec<String>>>,
  }
  impl Compose for LogImeEvent {
    fn compose(this: impl StateWriter<Value = Self>) -> impl WidgetBuilder {
      fn_widget! {
        @MockBox {
          size: INFINITY_SIZE,
          auto_focus: true,
          on_ime_pre_edit: move |e| {
            match &e.pre_edit {
              ImePreEdit::Begin => $this.log.borrow_mut().push("on_ime_pre_edit_begin".to_string()),
              ImePreEdit::PreEdit { value, .. } => $this.log.borrow_mut().push(format!("on_ime_pre_edit_update {value}")),
              ImePreEdit::End => $this.log.borrow_mut().push("on_ime_pre_edit_end".to_string()),
            }
          },
          on_chars: move|e| $this.log.borrow_mut().push(format!("on_chars {}", e.chars)),
          on_tap: move |_| $this.log.borrow_mut().push("on_tap".to_string()),
        }
      }
    }
  }
  #[test]
  fn ime_pre_edit() {
    let w = Stateful::new(LogImeEvent::default());
    let log = w.read().log.clone();

    let w = fn_widget! { @ {w} };
    let mut wnd = TestWindow::new_with_size(w, Size::new(200., 200.));

    wnd.draw_frame();

    App::dispatch_wnd_native_event(&wnd, WindowEvent::Ime(Ime::Enabled));
    App::dispatch_wnd_native_event(
      &wnd,
      WindowEvent::Ime(Ime::Preedit("hello".to_string(), None)),
    );
    App::dispatch_wnd_native_event(&wnd, WindowEvent::Ime(Ime::Disabled));
    wnd.draw_frame();
    assert_eq!(
      &*log.borrow(),
      &[
        "on_ime_pre_edit_begin",
        "on_ime_pre_edit_update hello",
        "on_ime_pre_edit_end"
      ]
    );

    log.borrow_mut().clear();
    App::dispatch_wnd_native_event(
      &wnd,
      WindowEvent::Ime(Ime::Preedit("hello".to_string(), None)),
    );
    App::dispatch_wnd_native_event(&wnd, WindowEvent::Ime(Ime::Commit("hello".to_string())));
    wnd.draw_frame();
    assert_eq!(
      &*log.borrow(),
      &[
        "on_ime_pre_edit_begin",
        "on_ime_pre_edit_update hello",
        "on_ime_pre_edit_end",
        "on_chars hello",
      ]
    );

    log.borrow_mut().clear();
    App::dispatch_wnd_native_event(
      &wnd,
      WindowEvent::Ime(Ime::Preedit("hello".to_string(), None)),
    );
    let device_id = unsafe { winit::event::DeviceId::dummy() };
    App::dispatch_wnd_native_event(
      &wnd,
      WindowEvent::MouseInput {
        state: winit::event::ElementState::Pressed,
        button: winit::event::MouseButton::Left,
        device_id,
      },
    );
    App::dispatch_wnd_native_event(
      &wnd,
      WindowEvent::MouseInput {
        state: winit::event::ElementState::Released,
        button: winit::event::MouseButton::Left,
        device_id,
      },
    );
    wnd.draw_frame();
    assert_eq!(
      &*log.borrow(),
      &[
        "on_ime_pre_edit_begin",
        "on_ime_pre_edit_update hello",
        "on_ime_pre_edit_end",
        "on_chars hello",
        "on_tap",
      ]
    );
  }
}