Compare commits

...

2 commits

Author SHA1 Message Date
hippoz
55b8353d6d
remove on_click from button and add it to the Widget class 2022-07-28 18:54:29 +03:00
hippoz
909f911260
add margin and spacing to boxlayout 2022-07-28 18:54:06 +03:00
7 changed files with 36 additions and 39 deletions

View file

@ -13,9 +13,9 @@ void BoxLayout::run() {
return;
}
double total_space = m_target->rect().dimension_at(m_direction);
double total_space = m_target->rect().dimension_at(m_direction) - 2 * m_margin;
double free_space = total_space;
Point current_position { 0.0, 0.0 };
Point current_position { m_margin, m_margin };
int unslotted_widgets = 0;
std::vector<Slot> working_slots(m_slots);
@ -28,7 +28,7 @@ void BoxLayout::run() {
// if the widget has a minimum or maximum size, we'll make a slot for its preferred size
child->rect().update();
working_slots.push_back(Slot { 0, child->rect().dimension_at(m_direction), SlotType::Pixel });
free_space -= child->rect().dimension_at(m_direction);
free_space -= child->rect().dimension_at(m_direction) + m_spacing;
} else {
// we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves.
unslotted_widgets++;
@ -40,7 +40,7 @@ void BoxLayout::run() {
// if the slot is fixed, we can already clamp it and subtract from free_space
if (working_slots[i].type == SlotType::Pixel) {
working_slots[i].pixel = child->rect().clamp_for_dimension(m_direction, working_slots[i].pixel);
free_space -= working_slots[i].pixel;
free_space -= working_slots[i].pixel + m_spacing;
}
}
}
@ -53,11 +53,11 @@ void BoxLayout::run() {
if (slot->type == SlotType::Percent) {
slot->pixel = ((free_space / 100.0) * slot->percent);
slot->pixel = child->rect().clamp_for_dimension(m_direction, slot->pixel);
free_space -= slot->pixel;
free_space -= slot->pixel + m_spacing;
}
}
double space_per_unslotted_widget = free_space / unslotted_widgets;
double space_per_unslotted_widget = free_space / unslotted_widgets - m_spacing;
for (unsigned int i = 0; i < m_target->children().size(); i++) {
auto child = m_target->children()[i];
@ -75,12 +75,12 @@ void BoxLayout::run() {
child->rect().set_y(current_position.y());
if (m_direction == Direction::Horizontal) {
child->rect().set_width(slot.pixel);
child->rect().set_height(m_target->rect().max_geometry().height());
current_position.add(slot.pixel, 0);
child->rect().set_height(m_target->rect().max_geometry().height() - 2 * m_margin);
current_position.add(slot.pixel + m_spacing, 0);
} else {
child->rect().set_width(m_target->rect().max_geometry().width());
child->rect().set_width(m_target->rect().max_geometry().width() - 2 * m_margin);
child->rect().set_height(slot.pixel);
current_position.add(0, slot.pixel);
current_position.add(0, slot.pixel + m_spacing);
}
}
};
@ -93,5 +93,11 @@ void BoxLayout::slot_pixel(double pixel) {
m_slots.push_back(Slot { 0, pixel, SlotType::Pixel });
}
void BoxLayout::slot_pixel(double pixel, double times) {
for (int i = 0; i < times; i++) {
slot_pixel(pixel);
}
}
}

View file

@ -29,7 +29,16 @@ public:
void run();
void slot_percent(double percent);
void slot_pixel(double pixel);
void slot_pixel(double pixel, double times);
void set_margin(double margin) { m_margin = margin; }
double &margin() { return m_margin; }
void set_spacing(double spacing) { m_spacing = spacing; }
double &spacing() { return m_spacing; }
private:
double m_margin { 0.0 };
double m_spacing { 0.0 };
std::vector<Slot> m_slots;
Direction m_direction { Direction::Horizontal };
};

View file

@ -35,10 +35,4 @@ void Button::on_paint() {
painter.fill();
}
void Button::on_activation_update(ActivationUpdateEvent &event) {
if (event.activation_status() == false) {
on_click();
}
}
}

View file

@ -1,3 +1,5 @@
#pragma once
#include <string>
#include <functional>
#include "Widget.hpp"
@ -5,8 +7,6 @@
#include "pango/pango-font.h"
#include "Window.hpp"
#pragma once
namespace Raven {
class Button : public Widget {
@ -15,18 +15,12 @@ public:
: Widget(WidgetType::Button)
, m_text(text) {}
std::function<void()> on_click { [](){} };
void set_text(std::string text);
std::string &text() { return m_text; }
protected:
void on_paint();
void on_init();
void on_activation_update(ActivationUpdateEvent &event);
private:
void update_color();
void recompute_text_size();
std::string m_text;
};

View file

@ -257,6 +257,10 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
auto activation_update_event = ActivationUpdateEvent(update_activation_to);
on_activation_update(activation_update_event);
if (update_activation_to == false) {
on_click();
}
if (m_style->update_background()) {
repaint();
}

View file

@ -44,6 +44,7 @@ public:
virtual ~Widget() {};
std::function<void(Event&)> on_event { [](Event&){} };
std::function<void()> on_click { [](){} };
bool fit_text(std::string &text);

View file

@ -16,19 +16,6 @@
#include <memory>
#include <string>
class Fish : public Raven::Widget {
public:
Fish()
: Raven::Widget() {}
protected:
void on_paint() {
if (!painter()) return;
//painter()->png("tuna.png");
painter()->cairo()->paint();
}
};
int main() {
Raven::Window window {};
window.spawn_window();
@ -42,13 +29,15 @@ int main() {
top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
auto container_widget = main_widget->add<Raven::Widget>();
container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
auto container_layout = container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
container_layout->set_spacing(5);
container_layout->set_margin(18);
container_widget->resize(400, 400);
auto new_button = top_bar->add<Raven::Button>("add");
new_button->rect().set_max_width(50);
new_button->on_click = [container_widget]() {
container_widget->add<Fish>();
container_widget->add<Raven::Button>("hello");
};
auto remove_button = top_bar->add<Raven::Button>("remove");
remove_button->on_click = [container_widget]() {