add syntax sugar and fix some safety issues
This commit is contained in:
parent
0aebfd6465
commit
4145115697
6 changed files with 49 additions and 33 deletions
|
@ -31,6 +31,8 @@ public:
|
|||
bool text(Point &where, std::string &text);
|
||||
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 fill();
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <exception>
|
||||
#include "Widget.hpp"
|
||||
#include "Events.hpp"
|
||||
#include "Window.hpp"
|
||||
|
@ -21,13 +22,16 @@ void Widget::set_layout(std::shared_ptr<Layout> layout) {
|
|||
}
|
||||
|
||||
void Widget::set_window(Window *window) {
|
||||
if (!window)
|
||||
throw std::logic_error{"null window value for set_window"};
|
||||
|
||||
m_window = window;
|
||||
m_styles = m_window->get_top_level_styles();
|
||||
|
||||
on_init();
|
||||
}
|
||||
|
||||
bool Widget::add_child(Widget *child) {
|
||||
bool Widget::add_child(std::shared_ptr<Widget> child) {
|
||||
if (child->get_parent()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -40,7 +44,7 @@ bool Widget::add_child(Widget *child) {
|
|||
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());
|
||||
}
|
||||
|
||||
|
@ -97,6 +101,10 @@ void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) {
|
|||
return;
|
||||
|
||||
auto painter = m_window->get_painter();
|
||||
|
||||
if (!painter.can_paint())
|
||||
return;
|
||||
|
||||
auto cr = painter.get_cairo();
|
||||
auto event_should_do_group = event.should_do_group();
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ DEF_WIDGET_STYLE_PROP(background_border_radius, double, 0.0)
|
|||
|
||||
private:
|
||||
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 };
|
||||
Window *m_window { 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, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } }
|
||||
|
||||
std::vector<Widget*> &get_children() { return m_children; }
|
||||
bool add_child(Widget *child);
|
||||
void remove_child(Widget *child);
|
||||
std::vector<std::shared_ptr<Widget>> &get_children() { return m_children; }
|
||||
bool add_child(std::shared_ptr<Widget> child);
|
||||
void remove_child(std::shared_ptr<Widget> child);
|
||||
|
||||
Widget *get_parent() { return m_parent; }
|
||||
void set_parent(Widget *parent) { m_parent = parent; }
|
||||
|
@ -97,11 +97,18 @@ public:
|
|||
|
||||
template<typename T, class... 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);
|
||||
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() {};
|
||||
protected:
|
||||
WidgetType m_type { WidgetType::Widget };
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
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->set_window(this);
|
||||
m_main_widget->set_current_geometry(m_current_geometry);
|
||||
|
|
|
@ -13,7 +13,7 @@ class Window {
|
|||
private:
|
||||
Widget *m_focused_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 };
|
||||
Painter m_painter {};
|
||||
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; }
|
||||
void set_active_widget(Widget *active_widget) { m_active_widget = active_widget; }
|
||||
|
||||
Widget *get_main_widget() { return m_main_widget; }
|
||||
void set_main_widget(Widget *main_widget);
|
||||
std::shared_ptr<Widget> get_main_widget() { return m_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; }
|
||||
|
||||
|
@ -48,6 +48,13 @@ public:
|
|||
|
||||
bool spawn_window();
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
36
src/main.cpp
36
src/main.cpp
|
@ -8,42 +8,34 @@
|
|||
#include "src/DocumentLayout.hpp"
|
||||
|
||||
int main() {
|
||||
Raven::Widget new_row { Raven::ControlWidgetType::NewRow };
|
||||
Raven::Window window {};
|
||||
|
||||
Raven::Widget main_widget {};
|
||||
Raven::Button add_button {"add"};
|
||||
Raven::Button subtract_button {"subtract"};
|
||||
Raven::Label label {"0"};
|
||||
auto main_widget = window.set_main_widget<Raven::Widget>();
|
||||
main_widget->set_layout<Raven::DocumentLayout>(6.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;
|
||||
|
||||
window.spawn_window();
|
||||
|
||||
add_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));
|
||||
add_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));
|
||||
|
||||
main_widget.set_layout<Raven::DocumentLayout>(6.0);
|
||||
|
||||
window.set_main_widget(&main_widget);
|
||||
|
||||
add_button.on_click = [&]() {
|
||||
add_button->on_click = [&]() {
|
||||
number++;
|
||||
label.set_text(std::to_string(number));
|
||||
label->set_text(std::to_string(number));
|
||||
};
|
||||
|
||||
subtract_button.on_click = [&]() {
|
||||
subtract_button->on_click = [&]() {
|
||||
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);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue