View
Base class for GUI components.
| Header | #include "nativeui/view.h" |
| Namespace | namespace nu |
| Type | class (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 accepets 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
const View*view
Return
Vector2dFVector2dF OffsetFromWindow() const
Return offset from the window that owns the view.
Return
Vector2dFRectF GetBounds() const
Return the position and size of the view, relative to its parent.
Return
RectFvoid Layout()
Make the view re-recalculate its layout.
void SchedulePaint()
Schedule to repaint the whole view.
void SchedulePaintRect(const RectF& rect)
Schedule to repaint the rect area in view.
Parameters
const RectF&rect
void SetVisible(bool visible)
Show/Hide the view.
Parameters
boolvisible
bool IsVisible() const
Return whether the view is visible.
Return
boolvoid SetEnabled(bool enable)
Set whether the view is enabled.
The enabled state of each view is not affected by its parent, disabling a container-like view does not have any effect.
Parameters
boolenable
bool IsEnabled() const
Return whether the view is enabled.
Return
boolvoid Focus()
Move the keyboard focus to the view.
bool HasFocus() const
Return whether the view has keyboard focus.
Return
boolvoid SetFocusable(bool focusable)
Set whether the view can be focused on.
Parameters
boolfocusable
bool IsFocusable() const
Return whether the view can be focused on.
Return
boolvoid 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
boolvoid 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
boolcan
bool IsMouseDownCanMoveWindow() const
Return whether dragging the view would move the window.
Return
boolint DoDrag(std::vector<Clipboard::Data> data, int operations)
Like DoDragWithOptions but do not set drag image.
Parameters
std::vector<Clipboard::Data>dataintoperations
Return
intint DoDragWithOptions(std::vector<Clipboard::Data> data, int operations, const DragOptions& options)
Start a drag session.
The return value is a DragOperation indicating the result of
dragging.
This method should only be called in the on_mouse_down event, when user
starts to drag the cursor.
This method is blocking that it does not return until the drag session is finished or cancelled. During the call a nested UI message loop will run and other events will still be emitted.
Note that on macOS certain views may have IsMouseDownCanMoveWindow
defaulting to true, which will prevent drag session to start. Make sure
to call SetMouseDownCanMoveWindow(false) for drag sources.
Parameters
std::vector<Clipboard::Data>data - An array ofClipboard::Datathat will be passed to drop target.intoperations - Must be one or more ofDragOperationmasks, indicates which drag operations are supported.const DragOptions&options
Return
intvoid CancelDrag()
Cancel current drag session if the view is being used as drag source.
bool IsDragging() const
Return whether the view is being used as drag source.
Return
boolvoid RegisterDraggedTypes(std::set<Clipboard::Data::Type> types)
Make the view a drag destination that accepets types.
Parameters
std::set<Clipboard::Data::Type>types - An array ofClipboard::Data::Type.
void SetCursor(scoped_refptr<Cursor> cursor)
Set the cursor to show when hovering the view.
On Linux, setting cursor would force the view to own its own GDK window.
For certain views like Label, this may have remove the view's background
color.
Parameters
scoped_refptr<Cursor>cursor
void SetFont(scoped_refptr<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
scoped_refptr<Font>font
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
Colorcolor
void SetBackgroundColor(Color color)
Change the background color of the view.
Parameters
Colorcolor
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
Args...styles - Variadic parameters that are pairs of keys and values.
std::string GetComputedLayout() const
Return string representation of the view's layout.
Return
std::stringSizeF GetMinimumSize() const
Return the minimum size needed to show the view.
Return
SizeFView* 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
NativeViewEvents
bool on_mouse_down(View* self, const MouseEvent& event)
Emitted when pressing mouse buttons.
Parameters
View*selfconst MouseEvent&event
Preventable
Yes.bool on_mouse_up(View* self, const MouseEvent& event)
Emitted when releasing mouse buttons.
Parameters
View*selfconst MouseEvent&event
Preventable
Yes.void on_mouse_move(View* self, const MouseEvent& event)
Emitted when user moves mouse in the view.
Parameters
View*selfconst MouseEvent&event
Preventable
No.void on_mouse_enter(View* self, const MouseEvent& event)
Emitted when mouse enters the view.
Parameters
View*selfconst MouseEvent&event
Preventable
No.void on_mouse_leave(View* self, const MouseEvent& event)
Emitted when mouse leaves the view.
Parameters
View*selfconst MouseEvent&event
Preventable
No.bool on_key_down(View* self, const KeyEvent& event)
Emitted when pressing keyboard.
Parameters
View*selfconst KeyEvent&event
Preventable
Yes.bool on_key_up(View* self, const KeyEvent& event)
Emitted when releasing keyboard.
Parameters
View*selfconst KeyEvent&event
Preventable
Yes.void on_drag_leave(View* self, DraggingInfo* info)>
Emitted when cursor leaves the view while dragging.
This event will also be emitted before the handle_drop event when user
drops the data on the view.
Parameters
View*selfDraggingInfo*info
Preventable
No.void on_size_changed(View* self)
Emitted when the view's size has been changed.
Parameters
View*self
Preventable
No.void on_capture_lost(View* self)
Emitted when the mouse capture on view has been released.
Parameters
View*self
Preventable
No.Delegates
int handle_drag_enter(View* self, DraggingInfo* info, const PointF& point)
Called when user drags the cursor over the view for the first time.
A DragOperation should be returned, indicating which dragging
operation the destination will perform when cursor is released.
This delegate will not be called if the view has not registered dragged types, or if the dragged data does not belong to the registered type.
On Linux the dragged data is not yet available when this is called, you
should usually only read data in the handle_drop delegate.
Parameters
View*selfDraggingInfo*infoconst PointF&point
Return
intint handle_drag_update(View* self, DraggingInfo* info, const PointF& point)
Called when user moves the cursor over the view while dragging.
A DragOperation should be returned, indicating which dragging
operation the destination will perform when cursor is released.
If this delegate is not implemented, the return value of previous
handle_drag_enter call will be returned.
This delegate is usually used when implementing a custom view with
multiple dropping areas, you only need to implement handle_drag_enter
for simple tasks.
On Linux the dragged data is not yet available when this is called, you
should usually only read data in the handle_drop delegate.
Parameters
View*selfDraggingInfo*infoconst PointF&point
Return
intbool handle_drop(View* self, DraggingInfo* info, const PointF& point)
Called when user releases the dragged data on the view.
Returning true will inform the drag source that the data has been
accepted with the drag operation returned by previous handle_drag_enter
or handle_drag_update call.
If the drag operation is Move, the drag source may also take actions
to "remove" the data on its side.
Returning false will inform the drag source that the drag has been
cancelled, and operating system may display some visual effects.
Parameters
View*selfDraggingInfo*infoconst PointF&point
Return
bool