View

Base class for GUI components.

Header#include "nativeui/view.h"
Namespacenamespace nu
Typeclass (RefCounted)

View provides methods to receive and change various style properties.

View is a RefCounted type. Sub-classes of View can only be created on heap, and must be managed with scoped_refptr. Every API that accpets View* will store a reference to the pointer, so it is safe to pass a newly created raw pointer to public APIs.

scoped_refptr<nu::Container> container = new nu::Container;
container->AddChildView(new nu::Label("child"));

Class properties

const char* kClassName

The class name of this view.

Methods

const char* GetClassName() const

Return the class name.

The returned value is guarenteed to be the kClassName property of each view.

Return

const char*

Vector2dF OffsetFromView(const View* view) const

Return offset from view.

Parameters

Return

Vector2dF

Vector2dF OffsetFromWindow() const

Return offset from the window that owns the view.

Return

Vector2dF

RectF GetBounds() const

Return the position and size of the view, relative to its parent.

Return

RectF

void Layout()

Make the view re-recalculate its layout.

void SchedulePaint()

Schedule to repaint the whole view.

void SetVisible(bool visible)

Show/Hide the view.

Parameters

bool IsVisible() const

Return whether the view is visible.

Return

bool

void Focus()

Move the keyboard focus to the view.

bool HasFocus() const

Return whether the view has keyboard focus.

Return

bool

void SetFocusable(bool focusable)

Set whether the view can be focused on.

Parameters

bool IsFocusable() const

Return whether the view can be focused on.

Return

bool

void SetCapture()

Set mouse capture to the view.

void ReleaseCapture()

Release mouse capture if the view has mouse capture.

bool HasCapture() const

Return whether the view has mouse capture.

Return

bool

void SetMouseDownCanMoveWindow(bool can)

Set whether dragging mouse would move the window.

For most platforms this method only works for frameless windows, having this feature may also prevent mouse events to happen.

On macOS the Container view has this feature turned on by default. To turn this feature on for the view, the view's parent view must also has this feature turned on.

On Windows the view with this feature will be treated as titlebar, e.g. double-clicking would maximize the window, right-clicking may show the system menu.

Parameters

bool IsMouseDownCanMoveWindow() const

Return whether dragging the view would move the window.

Return

bool

void SetFont(Font* font)

Change the font used for drawing text in the view.

This methods only works for Views that display text, like Label or Entry.

Parameters

void SetColor(Color color)

Change the color used for drawing text in the view.

This methods only works for Views that display text, like Label or Entry.

Parameters

void SetBackgroundColor(Color color)

Change the background color of the view.

Parameters

void SetStyle(Args... styles)

Change the styles of the view.

Available style properties can be found at Layout System.

view->SetStyle("flex", 1, "flex-direction", "row");

Parameters

View* GetParent() const

Return parent view.

Return

View*

Window* GetWindow() const

Return the window that the view belongs to.

Return

Window*

NativeView GetNative() const

Return the native type wrapped by the view.

Return

NativeView

Events

bool on_mouse_down(View* self, const MouseEvent& event)

Emitted when pressing mouse buttons.

Parameters

Preventable

Yes.

bool on_mouse_up(View* self, const MouseEvent& event)

Emitted when releasing mouse buttons.

Parameters

Preventable

Yes.

void on_mouse_move(View* self, const MouseEvent& event)

Emitted when user moves mouse in the view.

Parameters

Preventable

No.

void on_mouse_enter(View* self, const MouseEvent& event)

Emitted when mouse enters the view.

Parameters

Preventable

No.

void on_mouse_leave(View* self, const MouseEvent& event)

Emitted when mouse leaves the view.

Parameters

Preventable

No.

bool on_key_down(View* self, const KeyEvent& event)

Emitted when pressing keyboard.

Parameters

Preventable

Yes.

bool on_key_up(View* self, const KeyEvent& event)

Emitted when releasing keyboard.

Parameters

Preventable

Yes.

void on_size_changed(View* self)

Emitted when the view's size has been changed.

Parameters

Preventable

No.

void on_capture_lost(View* self)

Emitted when the mouse capture on view has been released.

Parameters

Preventable

No.