Skip to content

Commit

Permalink
Introduce error handling in the FemtoVG and Skia renderers
Browse files Browse the repository at this point in the history
Avoid unwrap() and expect() and instead propagate errors all the way
down to run_event_loop(), show(), and hide() in the Slint AIP.
  • Loading branch information
tronical committed Mar 23, 2023
1 parent ef07da4 commit 03382e7
Show file tree
Hide file tree
Showing 27 changed files with 601 additions and 343 deletions.
17 changes: 9 additions & 8 deletions api/cpp/platform.rs
Expand Up @@ -53,8 +53,9 @@ impl WindowAdapterSealed for CppWindowAdapter {
unsafe { (self.show)(self.user_data) };
Ok(())
}
fn hide(&self) {
fn hide(&self) -> Result<(), PlatformError> {
unsafe { (self.hide)(self.user_data) }
Ok(())
}

fn request_redraw(&self) {
Expand Down Expand Up @@ -230,7 +231,7 @@ pub unsafe extern "C" fn slint_skia_renderer_show_win32(
RawWindowHandle::Win32(init_raw!(raw_window_handle::Win32WindowHandle { hwnd, hinstance })),
RawDisplayHandle::Windows(raw_window_handle::WindowsDisplayHandle::empty()),
);
r.show(handle, PhysicalSize { width: size.width, height: size.height })
r.show(handle, PhysicalSize { width: size.width, height: size.height }).unwrap()
}

#[no_mangle]
Expand All @@ -248,7 +249,7 @@ pub unsafe extern "C" fn slint_skia_renderer_show_x11(
RawWindowHandle::Xcb(init_raw!(XcbWindowHandle { window, visual_id })),
RawDisplayHandle::Xcb(init_raw!(XcbDisplayHandle { connection, screen })),
);
r.show(handle, PhysicalSize { width: size.width, height: size.height })
r.show(handle, PhysicalSize { width: size.width, height: size.height }).unwrap();
}

#[no_mangle]
Expand All @@ -265,7 +266,7 @@ pub unsafe extern "C" fn slint_skia_renderer_show_wayland(
RawWindowHandle::Wayland(init_raw!(WaylandWindowHandle { surface })),
RawDisplayHandle::Wayland(init_raw!(WaylandDisplayHandle { display })),
);
r.show(handle, PhysicalSize { width: size.width, height: size.height })
r.show(handle, PhysicalSize { width: size.width, height: size.height }).unwrap();
}

#[no_mangle]
Expand All @@ -282,25 +283,25 @@ pub unsafe extern "C" fn slint_skia_renderer_show_appkit(
RawWindowHandle::AppKit(init_raw!(AppKitWindowHandle { ns_view, ns_window })),
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()),
);
r.show(handle, PhysicalSize { width: size.width, height: size.height })
r.show(handle, PhysicalSize { width: size.width, height: size.height }).unwrap();
}

#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_hide(r: SkiaRendererOpaque) {
let r = &*(r as *const SkiaRenderer);
r.hide()
r.hide().unwrap()
}

#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_resize(r: SkiaRendererOpaque, size: IntSize) {
let r = &*(r as *const SkiaRenderer);
r.resize_event(PhysicalSize { width: size.width, height: size.height });
r.resize_event(PhysicalSize { width: size.width, height: size.height }).unwrap();
}

#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_render(r: SkiaRendererOpaque, size: IntSize) {
let r = &*(r as *const SkiaRenderer);
r.render(PhysicalSize { width: size.width, height: size.height });
r.render(PhysicalSize { width: size.width, height: size.height }).unwrap();
}

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion api/node/native/lib.rs
Expand Up @@ -498,7 +498,7 @@ declare_types! {
let this = cx.this();
let window = cx.borrow(&this, |x| x.0.as_ref().cloned());
let window_adapter = window.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
window_adapter.hide();
window_adapter.hide().unwrap();
Ok(JsUndefined::new().as_value(&mut cx))
}

Expand Down
2 changes: 1 addition & 1 deletion api/rs/slint/docs.rs
Expand Up @@ -110,7 +110,7 @@ pub mod generated_code {

/// Marks the window of this component to be hidden on the screen. This de-registers
/// the window from the windowing system and it will not receive any further events.
fn hide(&self) {
fn hide(&self) -> Result<(), crate::PlatformError> {
unimplemented!();
}

Expand Down
4 changes: 2 additions & 2 deletions api/wasm-interpreter/src/lib.rs
Expand Up @@ -191,8 +191,8 @@ impl WrappedInstance {
}
/// Hides this instance and prevents further updates of the canvas element.
#[wasm_bindgen]
pub fn hide(&self) {
self.0.hide();
pub fn hide(&self) -> Result<(), JsValue> {
self.0.hide().map_err(|e| -> JsValue { format!("{e}").into() })
}

/// THIS FUNCTION IS NOT PART THE PUBLIC API!
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/rust/main.rs
Expand Up @@ -48,7 +48,7 @@ pub fn main() {
let weak_window = main_window.as_weak();
main_window.on_popup_confirmed(move || {
let window = weak_window.unwrap();
window.hide();
window.hide().unwrap();
});

{
Expand Down
6 changes: 4 additions & 2 deletions internal/backends/qt/qt_window.rs
Expand Up @@ -1498,7 +1498,7 @@ impl WindowAdapterSealed for QtWindow {
Ok(())
}

fn hide(&self) {
fn hide(&self) -> Result<(), i_slint_core::platform::PlatformError> {
self.rendering_metrics_collector.take();
let widget_ptr = self.widget_ptr();
cpp! {unsafe [widget_ptr as "QWidget*"] {
Expand All @@ -1507,6 +1507,7 @@ impl WindowAdapterSealed for QtWindow {
// visible windows, and ends the application if needed
QEventLoopLocker();
}};
Ok(())
}

fn request_redraw(&self) {
Expand Down Expand Up @@ -1930,9 +1931,10 @@ impl Renderer for QtWindow {
&self,
component: ComponentRef,
_items: &mut dyn Iterator<Item = Pin<i_slint_core::items::ItemRef<'_>>>,
) {
) -> Result<(), i_slint_core::platform::PlatformError> {
// Invalidate caches:
self.cache.component_destroyed(component);
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/backends/testing/lib.rs
Expand Up @@ -60,8 +60,9 @@ impl WindowAdapterSealed for TestingWindow {
Ok(())
}

fn hide(&self) {
fn hide(&self) -> Result<(), i_slint_core::platform::PlatformError> {
self.shown.set(false);
Ok(())
}

fn renderer(&self) -> &dyn Renderer {
Expand Down

0 comments on commit 03382e7

Please sign in to comment.