Skip to content

Commit

Permalink
fix(core): 🐛 scheduler adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
sologeek committed Jun 24, 2023
1 parent 01c8d18 commit aa08983
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
3 changes: 3 additions & 0 deletions core/src/context/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
task::{Context, RawWaker, RawWakerVTable, Waker},
};

use crate::prelude::FuturesLocalScheduler;
pub use futures::task::SpawnError;
use futures::{
executor::{block_on, LocalPool},
Expand Down Expand Up @@ -84,6 +85,8 @@ impl AppContext {
unsafe { &mut *ptr.as_mut() }
}

pub fn scheduler(&self) -> FuturesLocalScheduler { self.executor.local.borrow().spawner() }

pub(crate) fn end_frame(&mut self) {
// todo: frame cache is not a good choice? because not every text will relayout
// in every frame.
Expand Down
4 changes: 4 additions & 0 deletions core/src/context/window_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl WindowCtx {
#[inline]
pub fn app_ctx(&self) -> &AppContext { &self.app_ctx }

/// Return an local `rxRust` Scheduler
#[inline]
pub fn scheduler(&self) -> FuturesLocalScheduler { self.app_ctx.scheduler() }

/// Return an `rxRust` Scheduler, which will guarantee all task add to the
/// scheduler will finished before current frame finished.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion core/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Future for Timer {
if let Some(id) = self.as_mut().id.take() {
TIME_REACTOR.lock().unwrap().remove_timer(when, id);
}
if now > when {
if now >= when {
return Poll::Ready(());
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl Window {
}
}

pub fn wnd_ctx(&self) -> &WindowCtx { &self.context }

/// Draw an image what current render tree represent.
pub fn draw_frame(&mut self) {
if !self.need_draw() {
Expand All @@ -71,7 +73,7 @@ impl Window {
self.layout();

// wait all frame task finished.
self.run_futures();
self.frame_pool.0.run();

if !self.widget_tree.is_dirty() {
break;
Expand All @@ -97,8 +99,6 @@ impl Window {
self.context.end_frame();
}

pub fn run_futures(&mut self) { self.frame_pool.0.run_until_stalled(); }

pub fn layout(&mut self) {
self.widget_tree.layout(self.shell_wnd.inner_size());
self.context.layout_ready();
Expand Down
24 changes: 13 additions & 11 deletions ribir/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,19 @@ impl App {
}
}
}
Event::MainEventsCleared => windows.iter_mut().for_each(|(_, wnd)| {
wnd.run_futures();
if wnd.need_draw() {
let wnd = wnd
.shell_wnd()
.as_any()
.downcast_ref::<WinitShellWnd>()
.unwrap();
wnd.winit_wnd.request_redraw();
}
}),
Event::MainEventsCleared => {
app_ctx.run_until_stalled();
windows.iter_mut().for_each(|(_, wnd)| {
if wnd.need_draw() {
let wnd = wnd
.shell_wnd()
.as_any()
.downcast_ref::<WinitShellWnd>()
.unwrap();
wnd.winit_wnd.request_redraw();
}
})
}
Event::RedrawRequested(id) => {
if let Some(wnd) = windows.get_mut(&new_id(id)) {
wnd.draw_frame();
Expand Down
6 changes: 3 additions & 3 deletions ribir/tests/timer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod test_single_thread {
}
finally ctx => {
observable::of(Size::new(10., 10.))
.delay(Duration::from_millis(10), ctx.wnd_ctx().frame_scheduler())
.delay(Duration::from_millis(10), ctx.wnd_ctx().scheduler())
.subscribe(move |v| c.size = v);
}
};
Expand All @@ -32,13 +32,13 @@ mod test_single_thread {
sleep(Duration::from_millis(10));

// keep same
wnd.run_futures();
wnd.wnd_ctx().app_ctx().run_until_stalled();
wnd.draw_frame();
assert_layout_result_by_path!(wnd, {path = [0], width == 20., height == 20.,});

// trigger timeout
super::Timer::wake_timeout_futures();
wnd.run_futures();
wnd.wnd_ctx().app_ctx().run_until_stalled();
wnd.draw_frame();
assert_layout_result_by_path!(wnd, {path = [0], width == 10., height == 10.,});
}
Expand Down
2 changes: 1 addition & 1 deletion widgets/src/input/caret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Compose for Caret {
}
}
finally ctx => {
let scheduler = ctx.wnd_ctx().frame_scheduler();
let scheduler = ctx.wnd_ctx().scheduler();
let mut _guard = None;
let_watch!(this.focused)
.distinct_until_changed()
Expand Down

0 comments on commit aa08983

Please sign in to comment.