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
No known key found for this signature in database
GPG key ID: 7C52899193467641
6 changed files with 30 additions and 16 deletions

2
.gitignore vendored
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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