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

fix(core): 🐛 scheduler adjust #394

Merged
merged 1 commit into from
Jun 27, 2023
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
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
10 changes: 5 additions & 5 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 @@ -29,16 +29,16 @@ mod test_single_thread {
wnd.draw_frame();
assert_layout_result_by_path!(wnd, {path = [0], width == 20., height == 20.,});

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.,});

sleep(Duration::from_millis(10));

// 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