add "ListLayout" and remove columnlayout and rowlayout

This commit is contained in:
hippoz 2022-10-28 02:26:31 +03:00
parent cd96edba8b
commit dfa13dc46d
No known key found for this signature in database
GPG key ID: 7C52899193467641
8 changed files with 100 additions and 141 deletions

View file

@ -33,9 +33,8 @@ raven_source_files = [
'./src/ScrollContainer.cpp',
'./src/Button.cpp',
'./src/DocumentLayout.cpp',
'./src/RowLayout.cpp',
'./src/ColumnLayout.cpp',
'./src/BoxLayout.cpp',
'./src/ListLayout.cpp',
'./src/Label.cpp',
'./src/ListView.cpp',
'./src/TextInput.cpp',
@ -47,7 +46,6 @@ raven_header_files = [
'./src/Box.hpp',
'./src/BoxLayout.hpp',
'./src/Button.hpp',
'./src/ColumnLayout.hpp',
'./src/DocumentLayout.hpp',
'./src/Events.hpp',
'./src/Forward.hpp',
@ -57,8 +55,8 @@ raven_header_files = [
'./src/Painter.hpp',
'./src/Point.hpp',
'./src/RGB.hpp',
'./src/RowLayout.hpp',
'./src/ScrollContainer.hpp',
'./src/ListLayout.hpp',
'./src/Styles.hpp',
'./src/Widget.hpp',
'./src/SvgWidget.hpp',

View file

@ -1,41 +0,0 @@
#include "ColumnLayout.hpp"
#include "Point.hpp"
#include "Widget.hpp"
namespace Raven {
bool ColumnLayout::run() {
if (!m_target) {
return false;
}
Point current_point { m_margin, m_margin };
double max_width_so_far = 0;
double requested_height = 0;
auto& children = m_target->children();
for (auto child : children) {
if (child->absolute()) {
continue;
}
if (child->rect().width() > max_width_so_far) {
max_width_so_far = child->rect().width();
}
requested_height += child->rect().height() + m_margin;
child->rect().set_x(current_point.x());
child->rect().set_y(current_point.y());
current_point.add(0, child->rect().height() + m_margin);
}
m_target->rect().set_height(requested_height + m_margin);
m_target->rect().set_width(max_width_so_far + m_margin * 2);
return false;
}
}

View file

@ -1,27 +0,0 @@
#pragma once
#include "Layout.hpp"
namespace Raven {
class ColumnLayout : public Layout {
private:
double m_margin { 0.0 };
public:
ColumnLayout()
: Layout() {}
ColumnLayout(double margin)
: Layout()
, m_margin(margin) {}
bool run() override;
double margin() { return m_margin; }
void set_margin(double margin) { m_margin = margin; run(); }
virtual ~ColumnLayout() {}
};
}

57
src/ListLayout.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "ListLayout.hpp"
#include "Widget.hpp"
#include "Box.hpp"
#include "src/Events.hpp"
namespace Raven {
bool ListLayout::run() {
if (!m_target) {
return false;
}
double current_position = m_margin;
double maximum_secondary_dimension = m_margin;
if (m_direction == Direction::Horizontal) {
for (auto &child : m_target->children()) {
if (child->layout() && child->layout()->dynamically_sizes_target()) {
auto event = RelayoutSubtreeEvent();
child->dispatch_event(event);
}
child->rect().set_x(current_position);
child->rect().set_y(m_margin);
current_position += child->rect().width() + m_spacing;
if (child->rect().height() > maximum_secondary_dimension) {
maximum_secondary_dimension = child->rect().height();
}
}
m_target->rect().set_width(current_position + m_margin);
m_target->rect().set_height(maximum_secondary_dimension + m_margin * 2);
} else {
for (auto &child : m_target->children()) {
if (child->layout() && child->layout()->dynamically_sizes_target()) {
auto event = RelayoutSubtreeEvent();
child->dispatch_event(event);
}
child->rect().set_y(current_position);
child->rect().set_x(m_margin);
current_position += child->rect().height() + m_spacing;
if (child->rect().width() > maximum_secondary_dimension) {
maximum_secondary_dimension = child->rect().width();
}
}
m_target->rect().set_width(maximum_secondary_dimension + m_margin * 2);
m_target->rect().set_height(current_position + m_margin);
}
for (auto &child : m_target->children()) {
auto event = RelayoutSubtreeEvent();
child->dispatch_event(event);
}
return true;
}
}

28
src/ListLayout.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include "Layout.hpp"
#include "Box.hpp"
namespace Raven {
class ListLayout : public Layout {
public:
ListLayout(Direction direction)
: Layout()
, m_direction(direction) {}
bool run();
double margin() { return m_margin; }
void set_margin(double margin) { m_margin = margin; run(); }
double spacing() { return m_spacing; }
void set_spacing(double spacing) { m_spacing = spacing; run(); }
private:
double m_margin { 0.0 };
double m_spacing { 0.0 };
Direction m_direction;
};
}

View file

@ -1,41 +0,0 @@
#include "RowLayout.hpp"
#include "Point.hpp"
#include "Widget.hpp"
namespace Raven {
bool RowLayout::run() {
if (!m_target) {
return false;
}
Point current_point { m_margin, m_margin };
double max_height_so_far = 0;
double requested_width = 0;
auto& children = m_target->children();
for (auto child : children) {
if (child->absolute()) {
continue;
}
if (child->rect().height() > max_height_so_far) {
max_height_so_far = child->rect().height();
}
requested_width += child->rect().width() + m_margin;
child->rect().set_x(current_point.x());
child->rect().set_y(current_point.y());
current_point.add(child->rect().width() + m_margin, 0);
}
m_target->rect().set_width(requested_width + m_margin);
m_target->rect().set_height(max_height_so_far + m_margin * 2);
return false;
}
}

View file

@ -1,27 +0,0 @@
#pragma once
#include "Layout.hpp"
namespace Raven {
class RowLayout : public Layout {
private:
double m_margin { 0.0 };
public:
RowLayout()
: Layout() {}
RowLayout(double margin)
: Layout()
, m_margin(margin) {}
bool run() override;
double margin() { return m_margin; }
void set_margin(double margin) { m_margin = margin; run(); }
virtual ~RowLayout() {}
};
}

View file

@ -1,6 +1,6 @@
#include "Label.hpp"
#include "Application.hpp"
#include "ColumnLayout.hpp"
#include "ListLayout.hpp"
#include "ListView.hpp"
#include "TextInput.hpp"
#include "Window.hpp"
@ -24,8 +24,20 @@ int main() {
Raven::Application application {};
auto window = application.add_window();
auto test_window = application.add_window();
window->spawn_window();
test_window->spawn_window();
auto test_container = test_window->set_main_widget<Raven::ScrollContainer>();
auto test_widget = test_container->make_target();
auto test_layout = test_widget->set_layout<Raven::ListLayout>(Raven::Direction::Vertical);
test_layout->set_margin(69);
test_layout->set_spacing(10);
for (int i = 0; i < 1000; i++) {
test_widget->add<Raven::Button>("Hello: " + std::to_string(i));
}
auto main_widget = window->set_main_widget<Raven::Widget>();
auto main_widget_layout = main_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);