opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
widget.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <memory>
7#include <string_view>
8
9namespace osc { class DisplayStateChangeEvent; }
10namespace osc { class DropFileEvent; }
11namespace osc { class Event; }
12namespace osc { class KeyEvent; }
13namespace osc { class MouseEvent; }
14namespace osc { class MouseWheelEvent; }
15namespace osc { class QuitEvent; }
16namespace osc { class TextInputEvent; }
17namespace osc { class WidgetPrivate; }
18namespace osc { class WindowEvent; }
19
20#define OSC_WIDGET_DATA_GETTERS(ImplClass) \
21 const ImplClass& private_data() const { return reinterpret_cast<const ImplClass&>(base_private_data()); } \
22 ImplClass& private_data() { return reinterpret_cast<ImplClass&>(base_private_data()); }
23
24namespace osc
25{
26 class Widget {
27 public:
28 Widget(Widget* parent = nullptr);
29 virtual ~Widget() noexcept;
30
31 // Returns `impl_on_event(e)`.
32 //
33 // This method directly notifies this `Widget` with no propagation, filtering,
34 // or batching. If you want those kinds of features, you should use:
35 //
36 // - `App::post_event(Widget&, std::unique_ptr<Event>)` or
37 // - `App::notify(Widget&, Event&)`
38 bool on_event(Event& e) { return impl_on_event(e); }
39
40 // Called by the implementation before it's about to start calling `on_event`, `on_tick`, and `on_draw`
41 void on_mount() { impl_on_mount(); }
42
43 // Called by the implementation after the last time it calls `on_event`, `on_tick`, and `on_draw`
45
46 // Called by the implementation once per frame.
47 void on_tick() { impl_on_tick(); }
48
49 // Called once per frame with the expectation that the implementation will draw its contents into the current
50 // framebuffer or active ui context.
51 void on_draw() { impl_on_draw(); }
52
53 // Returns a lifetime-checked, but non-lockable, pointer to this `Widget`.
54 //
55 // Runtime lifetime is handy for checking logic/lifetime errors at runtime,
56 // but does fundamentally fix any lifetime issues in your application. If
57 // you're triggering runtime lifetime assertion exceptions, you need to fix
58 // your code.
60
61 // If it has a parent, returns a pointer to the parent of this `Widget`; otherwise,
62 // returns `nullptr`.
64
65 // If it has an ancestor of the given type, returns a pointer to that
66 // ancestor; otherwise, returns `nullptr`.
67 template<std::derived_from<Widget> T>
69 {
71 if (auto* downcasted = dynamic_cast<T*>(current)) {
72 return downcasted;
73 }
74 }
75 return nullptr;
76 }
77
78 // If it has a parent, returns a pointer to the parent of this `Widget`; otherwise,
79 // returns `nullptr`.
80 const Widget* parent() const;
81
82 // Re-parents the `Widget`
85
86 // Returns the name of this `Widget`, or an empty string if a name has not yet been
87 // set on this instance.
89
90 // Sets the name of this `Widget` instance.
91 void set_name(std::string_view);
92 protected:
93 explicit Widget(std::unique_ptr<WidgetPrivate>&&);
94
95 const WidgetPrivate& base_private_data() const { return *data_; }
96 WidgetPrivate& base_private_data() { return *data_; }
97
98 // Implementors may override this to provide custom `Event` handling.
99 //
100 // Note: the default implementation is responsible for forwarding the
101 // event to specialized event handlers (below, e.g. `impl_on_key_event`), so
102 // if you override this function it's also recommended to call the base
103 // version if your implementation also uses the specialized overrides.
104 virtual bool impl_on_event(Event&);
105
114
115 virtual void impl_on_mount();
116 virtual void impl_on_unmount();
117 virtual void impl_on_tick();
118 virtual void impl_on_draw();
119 private:
120 Widget(const Widget&) = delete;
121 Widget& operator=(const Widget&) = delete;
124
126
127 std::unique_ptr<WidgetPrivate> data_;
128 };
129}
Definition c_string_view.h:12
Definition display_state_change_event.h:14
Definition drop_file_event.h:12
Definition event.h:13
Definition key_event.h:13
Definition lifetimed_ptr.h:25
Definition mouse_event.h:13
Definition mouse_wheel_event.h:12
Definition quit_event.h:11
Definition text_input_event.h:15
Definition widget_private.h:15
Definition widget.h:26
void set_parent(Widget *)
void on_tick()
Definition widget.h:47
virtual bool impl_on_window_event(WindowEvent &)
void set_name(std::string_view)
LifetimedPtr< Widget > weak_ref()
virtual bool impl_on_text_input_event(TextInputEvent &)
Widget(Widget *parent=nullptr)
virtual void impl_on_mount()
CStringView name() const
void on_mount()
Definition widget.h:41
virtual void impl_on_tick()
virtual ~Widget() noexcept
virtual bool impl_on_display_state_change_event(DisplayStateChangeEvent &)
virtual bool impl_on_drop_file_event(DropFileEvent &)
virtual void impl_on_unmount()
virtual bool impl_on_mouse_event(MouseEvent &)
void on_unmount()
Definition widget.h:44
void set_parent(Widget &widget)
Definition widget.h:84
bool on_event(Event &e)
Definition widget.h:38
WidgetPrivate & base_private_data()
Definition widget.h:96
virtual void impl_on_draw()
void on_draw()
Definition widget.h:51
const Widget * parent() const
virtual bool impl_on_key_event(KeyEvent &)
T * first_ancestor_of_type()
Definition widget.h:68
virtual bool impl_on_mouse_wheel_event(MouseWheelEvent &)
virtual bool impl_on_event(Event &)
Widget * parent()
const WidgetPrivate & base_private_data() const
Definition widget.h:95
Widget(std::unique_ptr< WidgetPrivate > &&)
virtual bool impl_on_quit_event(QuitEvent &)
Definition window_event.h:12
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
#define OSC_WIDGET_DATA_GETTERS(ImplClass)
Definition widget.h:20