add an property to widgets to make them omit layout...

and add on_event function pointer that users can set on any widgets to
act upon various events manually
This commit is contained in:
hippoz 2022-05-14 15:44:05 +03:00
parent fbece08638
commit a01a3c1927
Signed by: hippoz
GPG key ID: 7C52899193467641
6 changed files with 30 additions and 16 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
builddir/
buildclang/
releaseclang/
.cache/
compile_commands.json

View file

@ -14,6 +14,9 @@ void DocumentLayout::run() {
auto& children = m_target->children();
for (auto& child : children) {
if (child->absolute())
continue;
if (child->current_geometry().height() > largest_height_so_far) {
largest_height_so_far = child->current_geometry().height();
}

View file

@ -221,6 +221,8 @@ void Widget::dispatch_event(Event &event) {
if (!m_accepts_events)
return;
on_event(event);
switch (event.type()) {
case EventType::MouseMove: {
handle_mouse_move_event(reinterpret_cast<MouseMoveEvent&>(event));

View file

@ -4,6 +4,7 @@
#include <utility>
#include <vector>
#include <string>
#include <functional>
#include "Box.hpp"
#include "Events.hpp"
#include "Forward.hpp"
@ -44,6 +45,7 @@ private:
bool m_is_active { false };
bool m_consumes_hits { false };
bool m_accepts_events { true };
bool m_absolute { false };
ControlWidgetType m_control_type { ControlWidgetType::Widget };
public:
Widget() {}
@ -58,6 +60,8 @@ public:
m_accepts_events = false;
}
std::function<void(Event&)> on_event { [](Event&){} };
Box &current_geometry() { return m_current_geometry; }
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(); } }
@ -94,6 +98,9 @@ public:
bool accepts_events() { return m_accepts_events; }
void set_accepts_events(bool accepts_events) { m_accepts_events = accepts_events; }
bool absolute() { return m_absolute; }
void set_absolute(bool absolute) { m_absolute = absolute; }
void set_layout(std::shared_ptr<Layout> layout);
std::shared_ptr<Layout> layout() { return m_layout; }

View file

@ -6,6 +6,7 @@
#include "Painter.hpp"
#include <X11/Xlib.h>
#include <cairomm/xlib_surface.h>
#include <functional>
namespace Raven {

View file

@ -6,6 +6,7 @@
#include "Label.hpp"
#include "Layout.hpp"
#include "src/DocumentLayout.hpp"
#include "src/Events.hpp"
#include <iostream>
#include <memory>
#include <string>
@ -17,25 +18,23 @@ int main() {
auto main_widget = window.set_main_widget<Raven::Widget>();
main_widget->set_layout<Raven::DocumentLayout>(6.0);
int number = 0;
// int number = 0;
bool is_dragging = false;
for (int i = 0; i < 500; i++) {
auto button = main_widget->add<Raven::Label>("text ");
/*
auto button = main_widget->add<Raven::Button>("hello");
button->set_absolute(true);
button->on_click = [&]() {
number++;
window.start_batch();
for (auto& c : main_widget->children()) {
if (c->type() == Raven::WidgetType::Button) {
auto button_child = std::static_pointer_cast<Raven::Button>(c);
button_child->set_text(std::to_string(number));
}
}
window.end_batch();
is_dragging = !is_dragging;
};
*/
main_widget->on_event = [&](Raven::Event &event) {
if (event.type() == Raven::EventType::MouseMove) {
auto mouse_move = reinterpret_cast<Raven::MouseMoveEvent&>(event);
if (is_dragging) {
button->move_to(mouse_move.point().x(), mouse_move.point().y());
}
}
};
window.run(true);
return 0;