Skip to content

Commit 18bba6e

Browse files
committed
Add sixtyfps::Window::request_redraw()
This allows scheduling a redraw of a Window explicitly. Unfortunately it comes with a winit caveat :(
1 parent 8959eac commit 18bba6e

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ as well as the [Rust migration guide for the `sixtyfps` crate](api/sixtyfps-rs/m
2929

3030
- `TextEdit::font-size` and `LineEdit::font-size` have been added to control the size of these widgets.
3131
- Added `sixtyfps::Window::set_rendering_notifier` to get a callback before and after a new frame is being rendered.
32+
- Added `sixtyfps::Window::request_redraw()` to schedule redrawing of the window contents.
3233

3334
### Fixed
3435

api/cpp/cbindgen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ fn gen_corelib(
268268
"sixtyfps_windowrc_set_component",
269269
"sixtyfps_windowrc_show_popup",
270270
"sixtyfps_windowrc_set_rendering_notifier",
271+
"sixtyfps_windowrc_request_redraw",
271272
"sixtyfps_new_path_elements",
272273
"sixtyfps_new_path_events",
273274
"sixtyfps_color_brighter",

api/cpp/include/sixtyfps.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ class WindowRc
158158
}
159159
}
160160

161+
void request_redraw() const { cbindgen_private::sixtyfps_windowrc_request_redraw(&inner); }
162+
161163
private:
162164
cbindgen_private::WindowRcOpaque inner;
163165
};
@@ -177,7 +179,8 @@ constexpr inline ItemTreeNode make_dyn_node(std::uintptr_t offset, std::uint32_t
177179
parent_index } };
178180
}
179181

180-
inline ItemRef get_item_ref(ComponentRef component, cbindgen_private::Slice<ItemTreeNode> item_tree, int index)
182+
inline ItemRef get_item_ref(ComponentRef component, cbindgen_private::Slice<ItemTreeNode> item_tree,
183+
int index)
181184
{
182185
const auto &item = item_tree.ptr[index].item.item;
183186
return ItemRef { item.vtable, reinterpret_cast<char *>(component.instance) + item.offset };
@@ -341,6 +344,9 @@ class Window
341344
return inner.set_rendering_notifier(std::forward<F>(callback));
342345
}
343346

347+
/// This function issues a request to the windowing system to redraw the contents of the window.
348+
void request_redraw() const { inner.request_redraw(); }
349+
344350
/// \private
345351
private_api::WindowRc &window_handle() { return inner; }
346352
/// \private
@@ -429,7 +435,8 @@ inline SharedVector<float> solve_box_layout(const cbindgen_private::BoxLayoutDat
429435
cbindgen_private::Slice<int> repeater_indexes)
430436
{
431437
SharedVector<float> result;
432-
cbindgen_private::Slice<uint32_t> ri { reinterpret_cast<uint32_t *>(repeater_indexes.ptr), repeater_indexes.len };
438+
cbindgen_private::Slice<uint32_t> ri { reinterpret_cast<uint32_t *>(repeater_indexes.ptr),
439+
repeater_indexes.len };
433440
cbindgen_private::sixtyfps_solve_box_layout(&data, ri, &result);
434441
return result;
435442
}
@@ -467,7 +474,8 @@ inline SharedVector<float> solve_path_layout(const cbindgen_private::PathLayoutD
467474
cbindgen_private::Slice<int> repeater_indexes)
468475
{
469476
SharedVector<float> result;
470-
cbindgen_private::Slice<uint32_t> ri { reinterpret_cast<uint32_t *>(repeater_indexes.ptr), repeater_indexes.len };
477+
cbindgen_private::Slice<uint32_t> ri { reinterpret_cast<uint32_t *>(repeater_indexes.ptr),
478+
repeater_indexes.len };
471479
cbindgen_private::sixtyfps_solve_path_layout(&data, ri, &result);
472480
return result;
473481
}
@@ -490,7 +498,8 @@ struct AbstractRepeaterView
490498
using ModelPeer = std::weak_ptr<AbstractRepeaterView>;
491499

492500
template<typename M>
493-
auto access_array_index(const M &model, int index) {
501+
auto access_array_index(const M &model, int index)
502+
{
494503
model->track_row_data_changes(index);
495504
if (const auto v = model->row_data(index)) {
496505
return *v;

internal/core/api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ impl Window {
119119
) -> std::result::Result<(), SetRenderingNotifierError> {
120120
self.0.set_rendering_notifier(Box::new(callback))
121121
}
122+
123+
/// This function issues a request to the windowing system to redraw the contents of the window.
124+
pub fn request_redraw(&self) {
125+
self.0.request_redraw();
126+
127+
// When this function is called by the user, we want it to translate to a requestAnimationFrame()
128+
// on the web. If called through the rendering notifier (so from within the event loop processing),
129+
// unfortunately winit will only do that if set the control flow to Poll. This hack achieves that.
130+
#[cfg(target_arch = "wasm32")]
131+
crate::animations::CURRENT_ANIMATION_DRIVER
132+
.with(|driver| driver.set_has_active_animations());
133+
}
122134
}
123135

124136
impl crate::window::WindowHandleAccess for Window {

internal/core/window.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,4 +729,11 @@ pub mod ffi {
729729
}
730730
}
731731
}
732+
733+
/// This function issues a request to the windowing system to redraw the contents of the window.
734+
#[no_mangle]
735+
pub unsafe extern "C" fn sixtyfps_windowrc_request_redraw(handle: *const WindowRcOpaque) {
736+
let window = &*(handle as *const WindowRc);
737+
window.request_redraw();
738+
}
732739
}

0 commit comments

Comments
 (0)