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

Implements RuntimeWindow::redraw #1184

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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