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::Button, Karana::WebUI::Dock, Karana::WebUI::IFrame, Karana::WebUI::Layout, Karana::WebUI::Markdown, Karana::WebUI::TreeView

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 – The 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 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.

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.