Skip to content

Commit

Permalink
C++: add assert_main_thread to public functions in the Window
Browse files Browse the repository at this point in the history
Some have it some don't. Just add it everywhere.

See #5762
  • Loading branch information
ogoffart authored Aug 6, 2024
1 parent 916d6cd commit 0f8246d
Showing 1 changed file with 68 additions and 11 deletions.
79 changes: 68 additions & 11 deletions api/cpp/include/slint_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,27 @@ class Window
///
/// Call hide() to make the window invisible again, and drop the additional
/// strong reference.
void show() { inner.show(); }
void show()
{
private_api::assert_main_thread();
inner.show();
}
/// Hides the window, so that it is not visible anymore. The additional strong
/// reference on the associated component, that was created when show() was called, is
/// dropped.
void hide() { inner.hide(); }
void hide()
{
private_api::assert_main_thread();
inner.hide();
}

/// Returns the visibility state of the window. This function can return false even if you
/// previously called show() on it, for example if the user minimized the window.
bool is_visible() const { return inner.is_visible(); }
bool is_visible() const
{
private_api::assert_main_thread();
return inner.is_visible();
}

/// This function allows registering a callback that's invoked during the different phases of
/// rendering. This allows custom rendering on top or below of the scene.
Expand All @@ -283,6 +295,7 @@ class Window
template<std::invocable<RenderingState, GraphicsAPI> F>
std::optional<SetRenderingNotifierError> set_rendering_notifier(F &&callback) const
{
private_api::assert_main_thread();
return inner.set_rendering_notifier(std::forward<F>(callback));
}

Expand All @@ -295,70 +308,109 @@ class Window
void on_close_requested(F &&callback) const
// clang-format on
{
private_api::assert_main_thread();
return inner.on_close_requested(std::forward<F>(callback));
}

/// This function issues a request to the windowing system to redraw the contents of the window.
void request_redraw() const { inner.request_redraw(); }
void request_redraw() const
{
private_api::assert_main_thread();
inner.request_redraw();
}

/// Returns the position of the window on the screen, in physical screen coordinates and
/// including a window frame (if present).
slint::PhysicalPosition position() const { return inner.position(); }
slint::PhysicalPosition position() const
{
private_api::assert_main_thread();
return inner.position();
}

/// Sets the position of the window on the screen, in physical screen coordinates and including
/// a window frame (if present).
/// Note that on some windowing systems, such as Wayland, this functionality is not available.
void set_position(const slint::LogicalPosition &pos) { inner.set_logical_position(pos); }
void set_position(const slint::LogicalPosition &pos)
{
private_api::assert_main_thread();
inner.set_logical_position(pos);
}
/// Sets the position of the window on the screen, in physical screen coordinates and including
/// a window frame (if present).
/// Note that on some windowing systems, such as Wayland, this functionality is not available.
void set_position(const slint::PhysicalPosition &pos) { inner.set_physical_position(pos); }
void set_position(const slint::PhysicalPosition &pos)
{
private_api::assert_main_thread();
inner.set_physical_position(pos);
}

/// Returns the size of the window on the screen, in physical screen coordinates and excluding
/// a window frame (if present).
slint::PhysicalSize size() const { return inner.size(); }
slint::PhysicalSize size() const
{
private_api::assert_main_thread();
return inner.size();
}

/// Resizes the window to the specified size on the screen, in logical pixels and excluding
/// a window frame (if present).
void set_size(const slint::LogicalSize &size) { inner.set_logical_size(size); }
void set_size(const slint::LogicalSize &size)
{
private_api::assert_main_thread();
inner.set_logical_size(size);
}
/// Resizes the window to the specified size on the screen, in physical pixels and excluding
/// a window frame (if present).
void set_size(const slint::PhysicalSize &size) { inner.set_physical_size(size); }
void set_size(const slint::PhysicalSize &size)
{
private_api::assert_main_thread();
inner.set_physical_size(size);
}

/// This function returns the scale factor that allows converting between logical and
/// physical pixels.
float scale_factor() const { return inner.scale_factor(); }
float scale_factor() const
{
private_api::assert_main_thread();
return inner.scale_factor();
}

/// Returns if the window is currently fullscreen
bool is_fullscreen() const
{
private_api::assert_main_thread();
return cbindgen_private::slint_windowrc_is_fullscreen(&inner.handle());
}
/// Set or unset the window to display fullscreen.
void set_fullscreen(bool fullscreen)
{
private_api::assert_main_thread();
cbindgen_private::slint_windowrc_set_fullscreen(&inner.handle(), fullscreen);
}

/// Returns if the window is currently maximized
bool is_maximized() const
{
private_api::assert_main_thread();
return cbindgen_private::slint_windowrc_is_maximized(&inner.handle());
}
/// Maximize or unmaximize the window.
void set_maximized(bool maximized)
{
private_api::assert_main_thread();
cbindgen_private::slint_windowrc_set_maximized(&inner.handle(), maximized);
}

/// Returns if the window is currently minimized
bool is_minimized() const
{
private_api::assert_main_thread();
return cbindgen_private::slint_windowrc_is_minimized(&inner.handle());
}
/// Minimize or unminimze the window.
void set_minimized(bool minimized)
{
private_api::assert_main_thread();
cbindgen_private::slint_windowrc_set_minimized(&inner.handle(), minimized);
}

Expand Down Expand Up @@ -407,6 +459,7 @@ class Window
/// \a button is the button that was pressed.
void dispatch_pointer_press_event(LogicalPosition pos, PointerEventButton button)
{
private_api::assert_main_thread();
using slint::cbindgen_private::MouseEvent;
MouseEvent event { .tag = MouseEvent::Tag::Pressed,
.pressed = MouseEvent::Pressed_Body { .position = { pos.x, pos.y },
Expand All @@ -423,6 +476,7 @@ class Window
/// \a button is the button that was released.
void dispatch_pointer_release_event(LogicalPosition pos, PointerEventButton button)
{
private_api::assert_main_thread();
using slint::cbindgen_private::MouseEvent;
MouseEvent event { .tag = MouseEvent::Tag::Released,
.released = MouseEvent::Released_Body { .position = { pos.x, pos.y },
Expand All @@ -438,6 +492,7 @@ class Window
/// This event is triggered when the pointer exits the window.
void dispatch_pointer_exit_event()
{
private_api::assert_main_thread();
using slint::cbindgen_private::MouseEvent;
MouseEvent event { .tag = MouseEvent::Tag::Exit, .moved = {} };
inner.dispatch_pointer_event(event);
Expand All @@ -451,6 +506,7 @@ class Window
/// \a pos represents the logical position of the pointer relative to the window.
void dispatch_pointer_move_event(LogicalPosition pos)
{
private_api::assert_main_thread();
using slint::cbindgen_private::MouseEvent;
MouseEvent event { .tag = MouseEvent::Tag::Moved,
.moved = MouseEvent::Moved_Body { .position = { pos.x, pos.y } } };
Expand All @@ -467,6 +523,7 @@ class Window
/// directions in logical pixels.
void dispatch_pointer_scroll_event(LogicalPosition pos, float delta_x, float delta_y)
{
private_api::assert_main_thread();
using slint::cbindgen_private::MouseEvent;
MouseEvent event { .tag = MouseEvent::Tag::Wheel,
.wheel = MouseEvent::Wheel_Body { .position = { pos.x, pos.y },
Expand Down

0 comments on commit 0f8246d

Please sign in to comment.