add syntax sugar and fix some safety issues

This commit is contained in:
hippoz 2022-04-24 02:59:47 +03:00
parent 0aebfd6465
commit 4145115697
Signed by: hippoz
GPG key ID: 7C52899193467641
6 changed files with 49 additions and 33 deletions

View file

@ -31,6 +31,8 @@ public:
bool text(Point &where, std::string &text); bool text(Point &where, std::string &text);
bool text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize); bool text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize);
bool can_paint() { if (m_cairo) return true; else return false; }
void source_rgb(RGB &source_rgb); void source_rgb(RGB &source_rgb);
void fill(); void fill();

View file

@ -1,6 +1,7 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <exception>
#include "Widget.hpp" #include "Widget.hpp"
#include "Events.hpp" #include "Events.hpp"
#include "Window.hpp" #include "Window.hpp"
@ -21,13 +22,16 @@ void Widget::set_layout(std::shared_ptr<Layout> layout) {
} }
void Widget::set_window(Window *window) { void Widget::set_window(Window *window) {
if (!window)
throw std::logic_error{"null window value for set_window"};
m_window = window; m_window = window;
m_styles = m_window->get_top_level_styles(); m_styles = m_window->get_top_level_styles();
on_init(); on_init();
} }
bool Widget::add_child(Widget *child) { bool Widget::add_child(std::shared_ptr<Widget> child) {
if (child->get_parent()) { if (child->get_parent()) {
return false; return false;
} }
@ -40,7 +44,7 @@ bool Widget::add_child(Widget *child) {
return true; return true;
} }
void Widget::remove_child(Widget *child) { void Widget::remove_child(std::shared_ptr<Widget> child) {
m_children.erase(std::remove(m_children.begin(), m_children.end(), child), m_children.end()); m_children.erase(std::remove(m_children.begin(), m_children.end(), child), m_children.end());
} }
@ -97,6 +101,10 @@ void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) {
return; return;
auto painter = m_window->get_painter(); auto painter = m_window->get_painter();
if (!painter.can_paint())
return;
auto cr = painter.get_cairo(); auto cr = painter.get_cairo();
auto event_should_do_group = event.should_do_group(); auto event_should_do_group = event.should_do_group();

View file

@ -34,7 +34,7 @@ DEF_WIDGET_STYLE_PROP(background_border_radius, double, 0.0)
private: private:
Box m_current_geometry { 0, 0, 0, 0 }; Box m_current_geometry { 0, 0, 0, 0 };
std::vector<Widget*> m_children; std::vector<std::shared_ptr<Widget>> m_children;
Widget *m_parent { nullptr }; Widget *m_parent { nullptr };
Window *m_window { nullptr }; Window *m_window { nullptr };
std::shared_ptr<TopLevelStyles> m_styles { nullptr }; std::shared_ptr<TopLevelStyles> m_styles { nullptr };
@ -59,9 +59,9 @@ public:
void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); } void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); }
void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } } void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } }
std::vector<Widget*> &get_children() { return m_children; } std::vector<std::shared_ptr<Widget>> &get_children() { return m_children; }
bool add_child(Widget *child); bool add_child(std::shared_ptr<Widget> child);
void remove_child(Widget *child); void remove_child(std::shared_ptr<Widget> child);
Widget *get_parent() { return m_parent; } Widget *get_parent() { return m_parent; }
void set_parent(Widget *parent) { m_parent = parent; } void set_parent(Widget *parent) { m_parent = parent; }
@ -97,11 +97,18 @@ public:
template<typename T, class... Args> template<typename T, class... Args>
std::shared_ptr<T> set_layout(Args&&... args) { std::shared_ptr<T> set_layout(Args&&... args) {
std::shared_ptr<T> layout = std::make_shared<T>(std::forward<T>(args)...); std::shared_ptr<T> layout = std::make_shared<T>(std::forward<Args>(args)...);
set_layout(layout); set_layout(layout);
return layout; return layout;
} }
template<typename T, class... Args>
std::shared_ptr<T> add_child(Args&&... args) {
std::shared_ptr<T> child = std::make_shared<T>(std::forward<Args>(args)...);
add_child(child);
return child;
}
virtual ~Widget() {}; virtual ~Widget() {};
protected: protected:
WidgetType m_type { WidgetType::Widget }; WidgetType m_type { WidgetType::Widget };

View file

@ -13,7 +13,7 @@
namespace Raven { namespace Raven {
void Window::set_main_widget(Widget *main_widget) { void Window::set_main_widget(std::shared_ptr<Widget> main_widget) {
m_main_widget = main_widget; m_main_widget = main_widget;
m_main_widget->set_window(this); m_main_widget->set_window(this);
m_main_widget->set_current_geometry(m_current_geometry); m_main_widget->set_current_geometry(m_current_geometry);

View file

@ -13,7 +13,7 @@ class Window {
private: private:
Widget *m_focused_widget { nullptr }; Widget *m_focused_widget { nullptr };
Widget *m_active_widget { nullptr }; Widget *m_active_widget { nullptr };
Widget *m_main_widget { nullptr }; std::shared_ptr<Widget> m_main_widget { nullptr };
Box m_current_geometry { 0, 0, 800, 600 }; Box m_current_geometry { 0, 0, 800, 600 };
Painter m_painter {}; Painter m_painter {};
std::shared_ptr<TopLevelStyles> m_top_level_styles = std::make_shared<TopLevelStyles>(this); std::shared_ptr<TopLevelStyles> m_top_level_styles = std::make_shared<TopLevelStyles>(this);
@ -31,8 +31,8 @@ public:
Widget *get_active_widget() { return m_active_widget; } Widget *get_active_widget() { return m_active_widget; }
void set_active_widget(Widget *active_widget) { m_active_widget = active_widget; } void set_active_widget(Widget *active_widget) { m_active_widget = active_widget; }
Widget *get_main_widget() { return m_main_widget; } std::shared_ptr<Widget> get_main_widget() { return m_main_widget; }
void set_main_widget(Widget *main_widget); void set_main_widget(std::shared_ptr<Widget> main_widget);
std::shared_ptr<TopLevelStyles> get_top_level_styles() { return m_top_level_styles; } std::shared_ptr<TopLevelStyles> get_top_level_styles() { return m_top_level_styles; }
@ -48,6 +48,13 @@ public:
bool spawn_window(); bool spawn_window();
void run(bool block); void run(bool block);
template<typename T, class... Args>
std::shared_ptr<T> set_main_widget(Args&&... args) {
std::shared_ptr<T> widget = std::make_shared<T>(std::forward<Args>(args)...);
set_main_widget(widget);
return widget;
}
}; };
} }

View file

@ -8,42 +8,34 @@
#include "src/DocumentLayout.hpp" #include "src/DocumentLayout.hpp"
int main() { int main() {
Raven::Widget new_row { Raven::ControlWidgetType::NewRow };
Raven::Window window {}; Raven::Window window {};
Raven::Widget main_widget {}; auto main_widget = window.set_main_widget<Raven::Widget>();
Raven::Button add_button {"add"}; main_widget->set_layout<Raven::DocumentLayout>(6.0);
Raven::Button subtract_button {"subtract"};
Raven::Label label {"0"}; auto add_button = main_widget->add_child<Raven::Button>("add");
auto subtract_button = main_widget->add_child<Raven::Button>("subtract");
main_widget->add_child<Raven::Widget>(Raven::ControlWidgetType::NewRow);
auto label = main_widget->add_child<Raven::Label>("0");
int number = 0; int number = 0;
window.spawn_window(); window.spawn_window();
add_button.set_current_geometry(Raven::Box(0, 0, 100, 30)); add_button->set_current_geometry(Raven::Box(0, 0, 100, 30));
subtract_button.set_current_geometry(Raven::Box(0, 0, 100, 30)); subtract_button->set_current_geometry(Raven::Box(0, 0, 100, 30));
label.set_current_geometry(Raven::Box(0, 0, 100, 20)); label->set_current_geometry(Raven::Box(0, 0, 100, 20));
main_widget.set_layout<Raven::DocumentLayout>(6.0); add_button->on_click = [&]() {
window.set_main_widget(&main_widget);
add_button.on_click = [&]() {
number++; number++;
label.set_text(std::to_string(number)); label->set_text(std::to_string(number));
}; };
subtract_button.on_click = [&]() { subtract_button->on_click = [&]() {
number--; number--;
label.set_text(std::to_string(number)); label->set_text(std::to_string(number));
}; };
main_widget.add_child(&add_button);
main_widget.add_child(&subtract_button);
main_widget.add_child(&new_row);
main_widget.add_child(&label);
window.run(true); window.run(true);
return 0; return 0;
} }