Skip to content

Commit

Permalink
Implements RuntimeWindow::redraw (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 15, 2024
1 parent 4c4fba3 commit 10563dd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 4 additions & 2 deletions gui/src/rt/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ impl<'a> RuntimeContext<'a> {

/// # Panics
/// If this call has been nested.
pub(super) fn run(&mut self, f: impl FnOnce()) {
pub(super) fn run<R>(&mut self, f: impl FnOnce() -> R) -> R {
assert!(CONTEXT.get().is_null());

CONTEXT.set(unsafe { transmute(self) });
f();
let r = f();
CONTEXT.set(null_mut());

r
}
}

Expand Down
25 changes: 24 additions & 1 deletion gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ impl Runtime {

true
}

fn redraw(&mut self, el: &ActiveEventLoop, win: WindowId) {
// Get target window.
let win = match self.windows.get(&win).unwrap().upgrade() {
Some(v) => v,
None => return,
};

// Setup context.
let mut cx = RuntimeContext {
el,
windows: &mut self.windows,
on_close: &mut self.on_close,
};

// Redraw.
let e = match cx.run(move || win.redraw()) {
Ok(_) => return,
Err(e) => e,
};

todo!()
}
}

impl ApplicationHandler<Event> for Runtime {
Expand All @@ -127,7 +150,7 @@ impl ApplicationHandler<Event> for Runtime {
match event {
WindowEvent::CloseRequested => self.on_close.raise(window_id, ()),
WindowEvent::Destroyed => drop(self.windows.remove(&window_id)),
WindowEvent::RedrawRequested => todo!(),
WindowEvent::RedrawRequested => self.redraw(event_loop, window_id),
_ => {}
}
}
Expand Down
6 changes: 5 additions & 1 deletion gui/src/rt/window.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
use std::error::Error;

/// Encapsulates winit window with application-specific logic.
pub trait RuntimeWindow {}
pub trait RuntimeWindow {
fn redraw(&self) -> Result<(), Box<dyn Error>>;
}
13 changes: 12 additions & 1 deletion gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use slint::platform::{Renderer, WindowAdapter};
use slint::{PhysicalSize, PlatformError};
use std::any::Any;
use std::cell::Cell;
use std::error::Error;
use std::rc::Rc;
use winit::window::WindowId;

Expand Down Expand Up @@ -36,7 +37,17 @@ impl Window {
}
}

impl RuntimeWindow for Window {}
impl RuntimeWindow for Window {
fn redraw(&self) -> Result<(), Box<dyn Error>> {
// Wayland will show the window on the first render so we need to check visibility flag
// here.
if self.visible.get().is_some_and(|v| v) {
self.renderer.render()?;
}

Ok(())
}
}

impl WindowAdapter for Window {
fn window(&self) -> &slint::Window {
Expand Down

0 comments on commit 10563dd

Please sign in to comment.