HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Data Fields
tt::widget Class Reference

#include <ttauri/widgets/widget.hpp>

Inheritance diagram for tt::widget:
std::enable_shared_from_this< widget > tt::abstract_button_widget< bool > tt::abstract_button_widget< value_type > tt::scroll_bar_widget< false > tt::scroll_bar_widget< true > tt::abstract_button_widget< T > tt::abstract_container_widget tt::label_widget tt::scroll_bar_widget< IsVertical > tt::system_menu_widget tt::text_field_widget< T > tt::window_traffic_lights_widget

Public Member Functions

 widget (gui_window &window, std::shared_ptr< abstract_container_widget > parent) noexcept
 
 widget (const widget &)=delete
 
widgetoperator= (const widget &)=delete
 
 widget (widget &&)=delete
 
widgetoperator= (widget &&)=delete
 
virtual void init () noexcept
 Should be called right after allocating and constructing a widget.
 
float margin () const noexcept
 Get the margin around the Widget.
 
float draw_layer () const noexcept
 The first drawing layer of the widget.
 
int logical_layer () const noexcept
 The logical layer of the widget.
 
int semantic_layer () const noexcept
 The semantic layer of the widget.
 
ranged_int< 3 > width_resistance () const noexcept
 Get the resistance in width.
 
ranged_int< 3 > height_resistance () const noexcept
 Get the resistance in height.
 
interval_vec2 preferred_size () const noexcept
 Get the size-range of the widget.
 
relative_base_line preferred_base_line () const noexcept
 Get the relative_base_line of the widget.
 
void set_layout_parameters (aarect const &window_rectangle, aarect const &window_clipping_rectangle, float window_base_line=std::numeric_limits< float >::infinity()) noexcept
 Set the location and size of the widget inside the window.
 
aarect window_rectangle () const noexcept
 Get the rectangle in window coordinates.
 
virtual aarect window_clipping_rectangle () const noexcept
 Get the clipping-rectangle in window coordinates.
 
float window_base_line () const noexcept
 Get the base-line distance from the bottom of the window.
 
aarect rectangle () const noexcept
 Get the rectangle in local coordinates.
 
float base_line () const noexcept
 Get the base-line in local coordinates.
 
gui_devicedevice () const noexcept
 
virtual hit_box hitbox_test (f32x4 window_position) const noexcept
 Find the widget that is under the mouse cursor.
 
virtual bool accepts_keyboard_focus (keyboard_focus_group group) const noexcept
 Check if the widget will accept keyboard focus.
 
virtual bool update_constraints (hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
 Update the constraints of the widget.
 
virtual void update_layout (hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
 Update the internal layout of the widget.
 
virtual draw_context make_draw_context (draw_context context) const noexcept
 Make a draw context for this widget.
 
virtual void draw (draw_context context, hires_utc_clock::time_point display_time_point) noexcept
 Draw the widget.
 
virtual bool handle_event (command command) noexcept
 Handle command.
 
virtual bool handle_event (std::vector< command > const &commands) noexcept
 
virtual bool handle_command_recursive (command command, std::vector< std::shared_ptr< widget > > const &reject_list) noexcept
 Handle command recursive.
 
virtual bool handle_event (mouse_event const &event) noexcept
 
virtual bool handle_event (keyboard_event const &event) noexcept
 Handle keyboard event.
 
virtual std::shared_ptr< widgetfind_next_widget (std::shared_ptr< widget > const &current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept
 Find the next widget that handles keyboard focus.
 
std::shared_ptr< abstract_container_widget const > shared_parent () const noexcept
 Get a shared_ptr to the parent.
 
std::shared_ptr< abstract_container_widgetshared_parent () noexcept
 Get a shared_ptr to the parent.
 
abstract_container_widget const & parent () const noexcept
 Get a reference to the parent.
 
abstract_container_widgetparent () noexcept
 Get a reference to the parent.
 
bool is_first (keyboard_focus_group group) const noexcept
 Is this widget the first widget in the parent container.
 
bool is_last (keyboard_focus_group group) const noexcept
 Is this widget the last widget in the parent container.
 
- Public Member Functions inherited from std::enable_shared_from_this< widget >
enable_shared_from_this (T... args)
 
operator= (T... args)
 
shared_from_this (T... args)
 
~enable_shared_from_this (T... args)
 

Static Public Member Functions

static std::vector< std::shared_ptr< widget > > parent_chain (std::shared_ptr< tt::widget > const &child_widget) noexcept
 Get a list of parents of a given widget.
 

Data Fields

gui_windowwindow
 Convenient reference to the Window.
 
observable< bool > enabled = true
 The widget is enabled.
 

Detailed Description

View of a widget. A view contains the dynamic data for a Widget. It is often accompanied with a Backing which contains that static data of an Widget and the drawing code. Backings are shared between Views.

All methods should make sure the mutex is locked by the current thread.

Rendering is done in three distinct phases:

  1. Updating Constraints
  2. Updating Layout
  3. Drawing (optional phase)

Each of these phases is executed to completion for all widget belonging to a window before the next phases is executed.

Updating Constraints

In this phases the widget will update the constraints to determine the position and size of the widget inside the window.

The updateConstraints() function will be called on each widget recursively. You should minimize the cost of this function as much as possible.

Since this function is called on each frame, the widget should first check if constraint changes are needed.

A widget should return true if any of the constraints has changed.

Updating Layout

A widget may update its internal (expensive) layout calculations from the updateLayout() function.

Since this function is called on each frame, the widget should first check if layout calculations are needed. If a constraint has changed (the window size is also a constraint) then the forceLayout flag is set.

A widget should return true if the window needs to be redrawn.

Drawing (optional)

A widget can draw itself when the draw() function is called. This phase is only entered when one of the widget's layout was changed. But if this phase is entered then all the widgets' draw() functions are called.

Constructor & Destructor Documentation

◆ widget()

tt::widget::widget ( gui_window & window,
std::shared_ptr< abstract_container_widget > parent )
noexcept

Constructor for creating sub views.

Member Function Documentation

◆ accepts_keyboard_focus()

virtual bool tt::widget::accepts_keyboard_focus ( keyboard_focus_group group) const
inlinevirtualnoexcept

◆ base_line()

float tt::widget::base_line ( ) const
inlinenoexcept

Get the base-line in local coordinates.

Precondition
mutex must be locked by current thread.

◆ draw()

virtual void tt::widget::draw ( draw_context context,
hires_utc_clock::time_point display_time_point )
inlinevirtualnoexcept

Draw the widget.

This function is called by the window (optionally) on every frame. It should recursively call this function on every visible child. This function is only called when updateLayout() has returned true.

The overriding function should call the base class's draw(), the place where the call this function will determine the order of the vertices into each buffer. This is important when needing to do the painters algorithm for alpha-compositing. However the pipelines are always drawn in the same order.

Precondition
mutex must be locked by current thread.
Parameters
contextThe context to where the widget will draw.
display_time_pointThe time point when the widget will be shown on the screen.

Reimplemented in tt::abstract_container_widget, tt::toolbar_widget, tt::button_widget< T >, tt::checkbox_widget< T >, tt::checkbox_widget< bool >, tt::label_widget, tt::menu_item_widget< T >, tt::menu_item_widget< value_type >, tt::overlay_view_widget, tt::radio_button_widget< T >, tt::scroll_bar_widget< IsVertical >, tt::scroll_bar_widget< false >, tt::scroll_bar_widget< true >, tt::selection_widget< T >, tt::system_menu_widget, tt::tab_view_widget< T >, tt::text_field_widget< T >, tt::toggle_widget, tt::toolbar_tab_button_widget< T >, and tt::window_traffic_lights_widget.

◆ draw_layer()

float tt::widget::draw_layer ( ) const
inlinenoexcept

The first drawing layer of the widget.

Drawing layers start at 0.0 and go up to 100.0.

Each child widget that has drawing to do increases the layer by 1.0.

The widget should draw within 0.0 and 1.0 of its drawing layer. The toWindowTransfer and the DrawingContext will already include the draw_layer.

An overlay widget such as popups will increase the layer by 25.0, to make sure the overlay will draw above other widgets in the window.

Precondition
mutex must be locked by current thread.
Returns
The draw layer of this widget.

◆ find_next_widget()

virtual std::shared_ptr< widget > tt::widget::find_next_widget ( std::shared_ptr< widget > const & current_keyboard_widget,
keyboard_focus_group group,
keyboard_focus_direction direction ) const
virtualnoexcept

Find the next widget that handles keyboard focus.

This recursively looks for the current keyboard widget, then returns the next (or previous) widget that returns true from accepts_keyboard_focus().

Parameters
current_keyboard_widgetThe widget that currently has focus; or empty to get the first widget that accepts focus.
groupThe group to which the widget must belong.
directionThe direction in which to walk the widget tree.
Returns
A pointer to the next widget.
Return values
currentKeyboardWidgetwhen currentKeyboardWidget was found but no next widget was found.
emptywhen currentKeyboardWidget is not found in this Widget.

Reimplemented in tt::abstract_container_widget, tt::selection_widget< T >, and tt::tab_view_widget< T >.

◆ handle_command_recursive()

virtual bool tt::widget::handle_command_recursive ( command command,
std::vector< std::shared_ptr< widget > > const & reject_list )
virtualnoexcept

Handle command recursive.

Handle a command and pass it to each child.

Parameters
commandThe command to handle by this widget.
reject_listThe widgets that should ignore this command
Returns
True when the command was handled by this widget or recursed child.

Reimplemented in tt::abstract_container_widget.

◆ handle_event() [1/3]

virtual bool tt::widget::handle_event ( command command)
virtualnoexcept

◆ handle_event() [2/3]

virtual bool tt::widget::handle_event ( keyboard_event const & event)
virtualnoexcept

Handle keyboard event.

Called by the operating system when editing text, or entering special keys

Parameters
eventThe keyboard event.
Returns
If this widget has handled the keyboard event.

Reimplemented in tt::text_field_widget< T >.

◆ handle_event() [3/3]

virtual bool tt::widget::handle_event ( mouse_event const & event)
virtualnoexcept

Handle mouse event. Called by the operating system to show the position and button state of the mouse. This is called very often so it must be made efficient. This function is also used to determine the mouse cursor.

In most cased overriding methods should call the super's handle_event() at the start of the function, to detect hover.

When this method does not handle the event the window will call handle_event() on the widget's parent.

Parameters
eventThe mouse event, positions are in window coordinates.
Returns
If this widget has handled the mouse event.

Reimplemented in tt::scroll_bar_widget< IsVertical >, tt::scroll_bar_widget< false >, tt::scroll_bar_widget< true >, tt::abstract_button_widget< T >, tt::abstract_button_widget< bool >, tt::abstract_button_widget< value_type >, tt::scroll_view_widget< CanScrollHorizontally, CanScrollVertically, ControlsWindow >, tt::selection_widget< T >, tt::text_field_widget< T >, and tt::window_traffic_lights_widget.

◆ height_resistance()

ranged_int< 3 > tt::widget::height_resistance ( ) const
inlinenoexcept

Get the resistance in height.

The amount of resistance the widget has for resizing to larger than the minimum size.

Precondition
mutex must be locked by current thread.
Returns
The amount of resistance to vertical resize.
Return values
0Greedy: Widget will try to become the largest.
1Normal: Widget will share the space with others.
2Resist: Widget will try to maintain minimum size.

◆ hitbox_test()

virtual hit_box tt::widget::hitbox_test ( f32x4 window_position) const
inlinevirtualnoexcept

Find the widget that is under the mouse cursor.

This function will recursively test with visual child widgets, when widgets overlap on the screen the hitbox object with the highest elevation is returned.

Parameters
window_positionThe coordinate of the mouse on the window. Use fromWindowTransform to convert to widget-local coordinates.
Returns
A hit_box object with the cursor-type and a reference to the widget.

Reimplemented in tt::abstract_container_widget, tt::toolbar_widget, tt::abstract_button_widget< T >, tt::abstract_button_widget< bool >, tt::abstract_button_widget< value_type >, tt::scroll_bar_widget< IsVertical >, tt::scroll_bar_widget< false >, tt::scroll_bar_widget< true >, tt::scroll_view_widget< CanScrollHorizontally, CanScrollVertically, ControlsWindow >, tt::selection_widget< T >, tt::system_menu_widget, tt::tab_view_widget< T >, tt::text_field_widget< T >, tt::window_traffic_lights_widget, and tt::window_widget.

◆ init()

virtual void tt::widget::init ( )
inlinevirtualnoexcept

◆ is_first()

bool tt::widget::is_first ( keyboard_focus_group group) const
noexcept

Is this widget the first widget in the parent container.

◆ is_last()

bool tt::widget::is_last ( keyboard_focus_group group) const
noexcept

Is this widget the last widget in the parent container.

◆ logical_layer()

int tt::widget::logical_layer ( ) const
inlinenoexcept

The logical layer of the widget.

The logical layer can be used to determine how for away from the window-widget (root) the current widget is.

Logical layers start at 0 for the window-widget. Each child widget increases the logical layer by 1.

Precondition
mutex must be locked by current thread.
Returns
The draw layer of this widget.

◆ make_draw_context()

virtual draw_context tt::widget::make_draw_context ( draw_context context) const
virtualnoexcept

Make a draw context for this widget.

This function will make a draw context with the correct transformation and default color values.

Precondition
mutex must be locked by current thread.
Parameters
contextA template drawing context. This template may be taken from the parent's draw call.
Returns
A new draw context for drawing the current widget in the local coordinate system.

Reimplemented in tt::abstract_button_widget< T >, tt::abstract_button_widget< bool >, and tt::abstract_button_widget< value_type >.

◆ margin()

float tt::widget::margin ( ) const
inlinenoexcept

Get the margin around the Widget.

A container widget should layout the children in such a way that the maximum margin of neighbouring widgets is maintained.

Precondition
mutex must be locked by current thread.
Returns
The margin for this widget.

◆ parent() [1/2]

abstract_container_widget const & tt::widget::parent ( ) const
noexcept

Get a reference to the parent.

It is undefined behavior to call this function when the widget does not have a parent.

◆ parent() [2/2]

abstract_container_widget & tt::widget::parent ( )
noexcept

Get a reference to the parent.

It is undefined behavior to call this function when the widget does not have a parent.

◆ parent_chain()

static std::vector< std::shared_ptr< widget > > tt::widget::parent_chain ( std::shared_ptr< tt::widget > const & child_widget)
staticnoexcept

Get a list of parents of a given widget.

The chain includes the given widget.

◆ preferred_base_line()

relative_base_line tt::widget::preferred_base_line ( ) const
inlinenoexcept

Get the relative_base_line of the widget.

This value is used by a container widget to align the text of its children on the same base-line.

By taking std::max() of the preferred_base_line() of all children, you will get the highest priority relative-base-line. The container can then use the relative_base_line::position() method to get the base-line position that should be used for all widgets in a row.

Precondition
mutex must be locked by current thread.
updateConstraint() must be called first.
Returns
A relative base-line position preferred by this widget.

◆ preferred_size()

interval_vec2 tt::widget::preferred_size ( ) const
inlinenoexcept

Get the size-range of the widget.

This size-range is used to determine the size of container widgets and eventually the size of the window.

Precondition
mutex must be locked by current thread.
updateConstraint() must be called first.
Returns
The minimum and maximum size as an interval of a 2D vector.

◆ rectangle()

aarect tt::widget::rectangle ( ) const
inlinenoexcept

Get the rectangle in local coordinates.

Precondition
mutex must be locked by current thread.

◆ semantic_layer()

int tt::widget::semantic_layer ( ) const
inlinenoexcept

The semantic layer of the widget.

The semantic layer is used mostly by the draw() function for selecting colors from the theme, to denote nesting widgets inside other widgets.

Semantic layers start at 0 for the window-widget and for any pop-up widgets.

The semantic layer is increased by one, whenever a user of the user-interface would understand the next layer to begin.

In most cases it would mean that a container widget that does not draw itself will not increase the semantic_layer number.

Precondition
mutex must be locked by current thread.
Returns
The draw layer of this widget.

◆ set_layout_parameters()

void tt::widget::set_layout_parameters ( aarect const & window_rectangle,
aarect const & window_clipping_rectangle,
float window_base_line = std::numeric_limits<float>::infinity() )
inlinenoexcept

Set the location and size of the widget inside the window.

The parent should call this set_layout_paramters() before this updateLayout().

If the parent's layout did not change, it does not need to call this set_layout_parameters(). This way the parent does not need to keep a cache, recalculate or query the client for these layout parameters for each frame.

Precondition
mutex must be locked by current thread.
Parameters
window_rectangleThe location and size of the widget inside the window.
window_clipping_rectangleThe location and size of the clipping rectangle, beyond which not to draw or accept mouse events.
window_base_lineThe position of the text base-line from the bottom of the window. When not given the middle of the _window_rectangle is used together with the preferred relative base-line.

◆ shared_parent() [1/2]

std::shared_ptr< abstract_container_widget const > tt::widget::shared_parent ( ) const
noexcept

Get a shared_ptr to the parent.

◆ shared_parent() [2/2]

std::shared_ptr< abstract_container_widget > tt::widget::shared_parent ( )
noexcept

Get a shared_ptr to the parent.

◆ update_constraints()

virtual bool tt::widget::update_constraints ( hires_utc_clock::time_point display_time_point,
bool need_reconstrain )
virtualnoexcept

Update the constraints of the widget.

This function is called on each vertical sync, even if no drawing is to be done. It should recursively call updateConstraints() on each of the visible children, so they get a chance to update.

This function may be used for expensive calculations, such as text-shaping, which should only be done when the data changes. Because this function is called on every vertical sync it should cache these calculations.

Subclasses should call updateConstraints() on its base-class to check if the constraints where changed. Widget::update_constraints() will check if requestConstraints was set. Container::update_constraints() will check if any of the children changed constraints.

If the container, due to a change in constraints, wants the window to resize to the minimum size it should set window.requestResize to true.

This function will change what is returned by preferred_size() and preferred_base_line().

Precondition
mutex must be locked by current thread.
Parameters
display_time_pointThe time point when the widget will be shown on the screen.
need_reconstrainForce the widget to re-constrain.
Returns
True if its or any children's constraints has changed.

Reimplemented in tt::abstract_container_widget, tt::row_column_layout_widget< Arrangement >, tt::toolbar_widget, tt::button_widget< T >, tt::checkbox_widget< T >, tt::checkbox_widget< bool >, tt::grid_layout_widget, tt::label_widget, tt::menu_item_widget< T >, tt::menu_item_widget< value_type >, tt::overlay_view_widget, tt::radio_button_widget< T >, tt::scroll_bar_widget< IsVertical >, tt::scroll_bar_widget< false >, tt::scroll_bar_widget< true >, tt::scroll_view_widget< CanScrollHorizontally, CanScrollVertically, ControlsWindow >, tt::selection_widget< T >, tt::system_menu_widget, tt::tab_view_widget< T >, tt::text_field_widget< T >, tt::toggle_widget, tt::toolbar_tab_button_widget< T >, tt::window_traffic_lights_widget, and tt::window_widget.

◆ update_layout()

virtual void tt::widget::update_layout ( hires_utc_clock::time_point display_time_point,
bool need_layout )
virtualnoexcept

Update the internal layout of the widget.

This function is called on each vertical sync, even if no drawing is to be done. It should recursively call updateLayout() on each of the visible children, so they get a chance to update.

This function may be used for expensive calculations, such as geometry calculations, which should only be done when the data or sizes change. Because this function is called on every vertical sync it should cache these calculations.

This function will likely call set_window_rectangle() and set_window_base_line() on its children, before calling updateLayout() on that child.

Subclasses should call updateLayout() on its children, call updateLayout() on its base class with forceLayout argument to the result of layoutRequest.exchange(false).

Precondition
mutex must be locked by current thread.
Parameters
display_time_pointThe time point when the widget will be shown on the screen.
need_layoutForce the widget to layout

Reimplemented in tt::abstract_container_widget, tt::menu_item_widget< T >, tt::menu_item_widget< value_type >, tt::row_column_layout_widget< Arrangement >, tt::toolbar_widget, tt::window_widget, tt::grid_layout_widget, tt::overlay_view_widget, tt::radio_button_widget< T >, tt::scroll_bar_widget< IsVertical >, tt::scroll_bar_widget< false >, tt::scroll_bar_widget< true >, tt::scroll_view_widget< CanScrollHorizontally, CanScrollVertically, ControlsWindow >, tt::selection_widget< T >, tt::system_menu_widget, tt::tab_view_widget< T >, tt::text_field_widget< T >, tt::toggle_widget, tt::toolbar_tab_button_widget< T >, tt::window_traffic_lights_widget, tt::button_widget< T >, tt::checkbox_widget< T >, tt::checkbox_widget< bool >, and tt::label_widget.

◆ width_resistance()

ranged_int< 3 > tt::widget::width_resistance ( ) const
inlinenoexcept

Get the resistance in width.

The amount of resistance the widget has for resizing to larger than the minimum size.

Precondition
mutex must be locked by current thread.
Returns
The amount of resistance to horizontal resize.
Return values
0Greedy: Widget will try to become the largest.
1Normal: Widget will share the space with others.
2Resist: Widget will try to maintain minimum size.

◆ window_base_line()

float tt::widget::window_base_line ( ) const
inlinenoexcept

Get the base-line distance from the bottom of the window.

Precondition
mutex must be locked by current thread.

◆ window_clipping_rectangle()

virtual aarect tt::widget::window_clipping_rectangle ( ) const
inlinevirtualnoexcept

Get the clipping-rectangle in window coordinates.

Precondition
mutex must be locked by current thread.

Reimplemented in tt::toolbar_tab_button_widget< T >.

◆ window_rectangle()

aarect tt::widget::window_rectangle ( ) const
inlinenoexcept

Get the rectangle in window coordinates.

Precondition
mutex must be locked by current thread.

Field Documentation

◆ enabled

observable<bool> tt::widget::enabled = true

The widget is enabled.

◆ window

gui_window& tt::widget::window

Convenient reference to the Window.


The documentation for this class was generated from the following file: