Compare commits
No commits in common. "55b8353d6de0b8f200f24dc08452b6a11b7b9f7a" and "a7051174a99c7fcff1e0caf961806530b670ed5c" have entirely different histories.
55b8353d6d
...
a7051174a9
7 changed files with 39 additions and 36 deletions
|
@ -13,9 +13,9 @@ void BoxLayout::run() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double total_space = m_target->rect().dimension_at(m_direction) - 2 * m_margin;
|
double total_space = m_target->rect().dimension_at(m_direction);
|
||||||
double free_space = total_space;
|
double free_space = total_space;
|
||||||
Point current_position { m_margin, m_margin };
|
Point current_position { 0.0, 0.0 };
|
||||||
int unslotted_widgets = 0;
|
int unslotted_widgets = 0;
|
||||||
std::vector<Slot> working_slots(m_slots);
|
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
|
// if the widget has a minimum or maximum size, we'll make a slot for its preferred size
|
||||||
child->rect().update();
|
child->rect().update();
|
||||||
working_slots.push_back(Slot { 0, child->rect().dimension_at(m_direction), SlotType::Pixel });
|
working_slots.push_back(Slot { 0, child->rect().dimension_at(m_direction), SlotType::Pixel });
|
||||||
free_space -= child->rect().dimension_at(m_direction) + m_spacing;
|
free_space -= child->rect().dimension_at(m_direction);
|
||||||
} else {
|
} else {
|
||||||
// we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves.
|
// we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves.
|
||||||
unslotted_widgets++;
|
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 the slot is fixed, we can already clamp it and subtract from free_space
|
||||||
if (working_slots[i].type == SlotType::Pixel) {
|
if (working_slots[i].type == SlotType::Pixel) {
|
||||||
working_slots[i].pixel = child->rect().clamp_for_dimension(m_direction, working_slots[i].pixel);
|
working_slots[i].pixel = child->rect().clamp_for_dimension(m_direction, working_slots[i].pixel);
|
||||||
free_space -= working_slots[i].pixel + m_spacing;
|
free_space -= working_slots[i].pixel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,11 +53,11 @@ void BoxLayout::run() {
|
||||||
if (slot->type == SlotType::Percent) {
|
if (slot->type == SlotType::Percent) {
|
||||||
slot->pixel = ((free_space / 100.0) * slot->percent);
|
slot->pixel = ((free_space / 100.0) * slot->percent);
|
||||||
slot->pixel = child->rect().clamp_for_dimension(m_direction, slot->pixel);
|
slot->pixel = child->rect().clamp_for_dimension(m_direction, slot->pixel);
|
||||||
free_space -= slot->pixel + m_spacing;
|
free_space -= slot->pixel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double space_per_unslotted_widget = free_space / unslotted_widgets - m_spacing;
|
double space_per_unslotted_widget = free_space / unslotted_widgets;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < m_target->children().size(); i++) {
|
for (unsigned int i = 0; i < m_target->children().size(); i++) {
|
||||||
auto child = m_target->children()[i];
|
auto child = m_target->children()[i];
|
||||||
|
@ -75,12 +75,12 @@ void BoxLayout::run() {
|
||||||
child->rect().set_y(current_position.y());
|
child->rect().set_y(current_position.y());
|
||||||
if (m_direction == Direction::Horizontal) {
|
if (m_direction == Direction::Horizontal) {
|
||||||
child->rect().set_width(slot.pixel);
|
child->rect().set_width(slot.pixel);
|
||||||
child->rect().set_height(m_target->rect().max_geometry().height() - 2 * m_margin);
|
child->rect().set_height(m_target->rect().max_geometry().height());
|
||||||
current_position.add(slot.pixel + m_spacing, 0);
|
current_position.add(slot.pixel, 0);
|
||||||
} else {
|
} else {
|
||||||
child->rect().set_width(m_target->rect().max_geometry().width() - 2 * m_margin);
|
child->rect().set_width(m_target->rect().max_geometry().width());
|
||||||
child->rect().set_height(slot.pixel);
|
child->rect().set_height(slot.pixel);
|
||||||
current_position.add(0, slot.pixel + m_spacing);
|
current_position.add(0, slot.pixel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -93,11 +93,5 @@ void BoxLayout::slot_pixel(double pixel) {
|
||||||
m_slots.push_back(Slot { 0, pixel, SlotType::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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,16 +29,7 @@ public:
|
||||||
void run();
|
void run();
|
||||||
void slot_percent(double percent);
|
void slot_percent(double percent);
|
||||||
void slot_pixel(double pixel);
|
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:
|
private:
|
||||||
double m_margin { 0.0 };
|
|
||||||
double m_spacing { 0.0 };
|
|
||||||
std::vector<Slot> m_slots;
|
std::vector<Slot> m_slots;
|
||||||
Direction m_direction { Direction::Horizontal };
|
Direction m_direction { Direction::Horizontal };
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,4 +35,10 @@ void Button::on_paint() {
|
||||||
painter.fill();
|
painter.fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Button::on_activation_update(ActivationUpdateEvent &event) {
|
||||||
|
if (event.activation_status() == false) {
|
||||||
|
on_click();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "Widget.hpp"
|
#include "Widget.hpp"
|
||||||
|
@ -7,6 +5,8 @@
|
||||||
#include "pango/pango-font.h"
|
#include "pango/pango-font.h"
|
||||||
#include "Window.hpp"
|
#include "Window.hpp"
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
namespace Raven {
|
namespace Raven {
|
||||||
|
|
||||||
class Button : public Widget {
|
class Button : public Widget {
|
||||||
|
@ -15,12 +15,18 @@ public:
|
||||||
: Widget(WidgetType::Button)
|
: Widget(WidgetType::Button)
|
||||||
, m_text(text) {}
|
, m_text(text) {}
|
||||||
|
|
||||||
|
std::function<void()> on_click { [](){} };
|
||||||
|
|
||||||
void set_text(std::string text);
|
void set_text(std::string text);
|
||||||
std::string &text() { return m_text; }
|
std::string &text() { return m_text; }
|
||||||
protected:
|
protected:
|
||||||
void on_paint();
|
void on_paint();
|
||||||
void on_init();
|
void on_init();
|
||||||
|
void on_activation_update(ActivationUpdateEvent &event);
|
||||||
private:
|
private:
|
||||||
|
void update_color();
|
||||||
|
void recompute_text_size();
|
||||||
|
|
||||||
std::string m_text;
|
std::string m_text;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -257,10 +257,6 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
|
||||||
auto activation_update_event = ActivationUpdateEvent(update_activation_to);
|
auto activation_update_event = ActivationUpdateEvent(update_activation_to);
|
||||||
on_activation_update(activation_update_event);
|
on_activation_update(activation_update_event);
|
||||||
|
|
||||||
if (update_activation_to == false) {
|
|
||||||
on_click();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_style->update_background()) {
|
if (m_style->update_background()) {
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,6 @@ public:
|
||||||
virtual ~Widget() {};
|
virtual ~Widget() {};
|
||||||
|
|
||||||
std::function<void(Event&)> on_event { [](Event&){} };
|
std::function<void(Event&)> on_event { [](Event&){} };
|
||||||
std::function<void()> on_click { [](){} };
|
|
||||||
|
|
||||||
bool fit_text(std::string &text);
|
bool fit_text(std::string &text);
|
||||||
|
|
||||||
|
|
19
src/main.cpp
19
src/main.cpp
|
@ -16,6 +16,19 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#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() {
|
int main() {
|
||||||
Raven::Window window {};
|
Raven::Window window {};
|
||||||
window.spawn_window();
|
window.spawn_window();
|
||||||
|
@ -29,15 +42,13 @@ int main() {
|
||||||
top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
|
top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
|
||||||
|
|
||||||
auto container_widget = main_widget->add<Raven::Widget>();
|
auto container_widget = main_widget->add<Raven::Widget>();
|
||||||
auto container_layout = container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
|
container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
|
||||||
container_layout->set_spacing(5);
|
|
||||||
container_layout->set_margin(18);
|
|
||||||
container_widget->resize(400, 400);
|
container_widget->resize(400, 400);
|
||||||
|
|
||||||
auto new_button = top_bar->add<Raven::Button>("add");
|
auto new_button = top_bar->add<Raven::Button>("add");
|
||||||
new_button->rect().set_max_width(50);
|
new_button->rect().set_max_width(50);
|
||||||
new_button->on_click = [container_widget]() {
|
new_button->on_click = [container_widget]() {
|
||||||
container_widget->add<Raven::Button>("hello");
|
container_widget->add<Fish>();
|
||||||
};
|
};
|
||||||
auto remove_button = top_bar->add<Raven::Button>("remove");
|
auto remove_button = top_bar->add<Raven::Button>("remove");
|
||||||
remove_button->on_click = [container_widget]() {
|
remove_button->on_click = [container_widget]() {
|
||||||
|
|
Loading…
Reference in a new issue