Class Widget#

Inheritance Relationships#

Derived Types#

Class Documentation#

class Widget#

Base class for UI elements.

Note: any derived class that could be instantiated MUST invoke _clearCallbacks in its destructor.

Subclassed by Karana::WebUI::TypedInput< std::string >, Karana::WebUI::Accordion, Karana::WebUI::Button, Karana::WebUI::DataTree, Karana::WebUI::Dock, Karana::WebUI::Dropdown, Karana::WebUI::IFrame, Karana::WebUI::InputGroup, Karana::WebUI::Layout, Karana::WebUI::Markdown, Karana::WebUI::QuantityInput, Karana::WebUI::Slider, Karana::WebUI::Toggle, Karana::WebUI::TreeView, Karana::WebUI::TypedInput< InputType >

Public Functions

Widget(Router &router, const ContentSource &esm = {}, const ContentSource &css = {}, const StateMap &states = {}, const StyleMap &style = {}, std::string_view init_msg = {})#

Widget constructor.

Parameters:
  • router – Provides a connection to the frontend

  • esm – Optional esm bundle that creates a frontend Widget

  • css – Optional css to attach to the document

  • statesState instances used by the widget

  • style – Style attributes for the widget’s root div, using CSSOM names (use camelCase)

  • init_msg – Passed to widget frontend’s initialize method

virtual ~Widget()#

Widget destructor.

const Karana::Core::ks_ptr<Channel> &channel() const#

Get a handle to the widget’s channel.

Returns:

The channel

std::string_view domId() const#

Get the id of the widget’s root div in the DOM.

Returns:

The id

const Karana::Core::ks_ptr<State> &state(std::string_view name) const#

Get a state instance.

Throws an error if the name isn’t known.

Parameters:

name – The name assigned to the state by this widget

Returns:

The non-null state

void setVisible(bool visible)#

Set whether the widget should be rendered.

Parameters:

visible – The visibility flag

void addDomClass(std::string_view dom_class, bool force_reflow = false)#

Add to the class list of the frontend DOM element.

Parameters:
  • dom_class – The class to add to the list

  • force_reflow – Force the frontend to do a reflow

void removeDomClass(std::string_view dom_class, bool force_reflow = false)#

Remove from the class list in the frontend DOM element.

Parameters:
  • dom_class – The class to remove from the list

  • force_reflow – Force the frontend to do a reflow

void addToDomRoot()#

Add the widget to the document’s root div.

This should be called on the widget which acts as the root container for all other widgets in an application.

void setEnabled(bool enabled)#

Set whether this widget should be enabled (e.g., whether it should render and respond on the frontend).

Parameters:

enabled

void setTooltip(std::string_view tooltip)#

Set the widget’s tooltip that appears on hover.

This will also affect any children of this widget, unless they have their own tooltip.

Parameters:

tooltip

void setSizeClass(SizeClass size_class)#

Set the widget’s tooltip that appears on hover.

This will also affect any children of this widget, unless they have their own tooltip.

Parameters:

size_class

Protected Functions

void _sendMessage(std::string_view message, int client_id)#

Send a message to the frontend.

Parameters:
  • message – The message to send.

  • client_id – The client to message.

void _broadcastMessage(std::string_view message)#

Send a message to all clients.

Parameters:

message – The message to send.

inline virtual void _onConnect(int client_id)#

Called upon a client connecting.

This can be overridden to implement custom behavior.

Parameters:

client_id – Unique id for the client.

inline virtual void _onMessage(std::string_view message, int client_id)#

Called upon a message from a client.

This can be overridden to implement custom behavior.

Parameters:
  • message – The message from the client

  • client_id – Unique id for the client

inline virtual void _onDisconnect(int client_id)#

Called upon a client disconnecting.

This can be overridden to implement custom behavior.

Parameters:

client_id – Unique id for the client.

void _clearCallbacks()#

Thread-safely clear any event callbacks.

This MUST be called in the destructor of any subclass that may be instantiated.

Protected Attributes

bool _enabled#

Whether the widget is currently enabled.

std::string _tooltip#

Tooltip to show on hover.

SizeClass _size_class = SizeClass::NONE#

Size class of this widget: used to control frontend styling.