convenience function for adding layouts to a widget

This commit is contained in:
hippoz 2022-04-03 15:46:38 +03:00
parent 60a4dc51a7
commit 0aebfd6465
No known key found for this signature in database
GPG key ID: 7C52899193467641
4 changed files with 19 additions and 9 deletions

View file

@ -11,6 +11,10 @@ public:
DocumentLayout()
: Layout() {}
DocumentLayout(double margin)
: Layout()
, m_margin(margin) {}
void run();
double get_margin() { return m_margin; }

View file

@ -1,5 +1,6 @@
#include <algorithm>
#include <iostream>
#include <memory>
#include "Widget.hpp"
#include "Events.hpp"
#include "Window.hpp"
@ -14,7 +15,7 @@ void Widget::do_layout() {
}
}
void Widget::set_layout(Layout *layout) {
void Widget::set_layout(std::shared_ptr<Layout> layout) {
m_layout = layout;
m_layout->bind_to(this);
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <utility>
#include <vector>
#include <string>
#include "Box.hpp"
@ -36,8 +37,8 @@ private:
std::vector<Widget*> m_children;
Widget *m_parent { nullptr };
Window *m_window { nullptr };
std::shared_ptr<TopLevelStyles> m_styles = nullptr;
Layout *m_layout { nullptr };
std::shared_ptr<TopLevelStyles> m_styles { nullptr };
std::shared_ptr<Layout> m_layout { nullptr };
bool m_did_init { false };
bool m_is_focused { false };
bool m_is_active { false };
@ -84,8 +85,8 @@ public:
bool get_accepts_events() { return m_accepts_events; }
void set_accepts_events(bool accepts_events) { m_accepts_events = accepts_events; }
void set_layout(Layout *layout);
Layout *get_layout() { return m_layout; }
void set_layout(std::shared_ptr<Layout> layout);
std::shared_ptr<Layout> get_layout() { return m_layout; }
WidgetType get_type() { return m_type; }
ControlWidgetType get_control_type() { return m_control_type; }
@ -94,6 +95,13 @@ public:
void wants_repaint();
void wants_relayout();
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)...);
set_layout(layout);
return layout;
}
virtual ~Widget() {};
protected:
WidgetType m_type { WidgetType::Widget };

View file

@ -8,7 +8,6 @@
#include "src/DocumentLayout.hpp"
int main() {
Raven::DocumentLayout main_layout {};
Raven::Widget new_row { Raven::ControlWidgetType::NewRow };
Raven::Window window {};
@ -19,16 +18,14 @@ int main() {
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));
main_layout.set_margin(6.0);
main_widget.set_layout<Raven::DocumentLayout>(6.0);
main_widget.set_layout(&main_layout);
window.set_main_widget(&main_widget);
add_button.on_click = [&]() {