add microtask system and temporary logging system

This commit is contained in:
hippoz 2022-07-27 02:32:33 +03:00
parent 9f52cad7e9
commit cc1d49c435
No known key found for this signature in database
GPG key ID: 7C52899193467641
4 changed files with 36 additions and 1 deletions

View file

@ -5,6 +5,8 @@ project(
) )
project_description = 'The Raven user interface library' project_description = 'The Raven user interface library'
add_project_arguments('-g3', language : 'cpp')
cairomm_dep = dependency('cairomm-1.0') cairomm_dep = dependency('cairomm-1.0')
pangocairo_dep = dependency('pangocairo') pangocairo_dep = dependency('pangocairo')
xlib_dep = dependency('x11') xlib_dep = dependency('x11')

5
src/Logging.hpp Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#define ERROR std::clog << "[ERROR] (" << __FILE__ << ":" << __LINE__ << "): "
#define INFO std::clog << "[INFO] (" << __FILE__ << ":" << __LINE__ << "): "
#define WARN std::clog << "[WARN] (" << __FILE__ << ":" << __LINE__ << "): "

View file

@ -10,6 +10,7 @@
#include "Window.hpp" #include "Window.hpp"
#include "Events.hpp" #include "Events.hpp"
#include "Point.hpp" #include "Point.hpp"
#include "Logging.hpp"
namespace Raven { namespace Raven {
@ -65,7 +66,10 @@ bool Window::dispatch_to_main_widget(Event &event) {
if (!m_main_widget) if (!m_main_widget)
return false; return false;
//std::cout << "Dispatched " << event.name() << " to main widget" << std::endl; // MouseMove events tend to spam the output
if (event.type() != EventType::MouseMove) {
INFO << "Dispatched event '" << event.name() << "' to main widget" << std::endl;
}
m_main_widget->dispatch_event(event); m_main_widget->dispatch_event(event);
return true; return true;
@ -112,6 +116,7 @@ void Window::reflow() {
} }
void Window::start_batch() { void Window::start_batch() {
//INFO << "New batch started. Current count: " << m_batches << std::endl;
m_batches++; m_batches++;
} }
@ -122,6 +127,7 @@ void Window::end_batch() {
// we are only interested in performing batch operations for the last batch // we are only interested in performing batch operations for the last batch
if (m_batches > 1) { if (m_batches > 1) {
m_batches--; m_batches--;
//INFO << "Consumed batch. Current count: " << m_batches << std::endl;
return; return;
} }
@ -135,6 +141,7 @@ void Window::end_batch() {
m_did_relayout_during_batch = false; m_did_relayout_during_batch = false;
m_did_repaint_during_batch = false; m_did_repaint_during_batch = false;
INFO << "Performed all batch actions." << std::endl;
} }
void Window::run(bool block) { void Window::run(bool block) {
@ -148,10 +155,12 @@ void Window::run(bool block) {
switch (e.type) { switch (e.type) {
case MapNotify: { case MapNotify: {
INFO << "[X11 EVENT] MapNotify" << std::endl;
end_batch(); end_batch();
break; break;
} }
case ConfigureNotify: { case ConfigureNotify: {
INFO << "[X11 EVENT] ConfigureNotify" << std::endl;
if (e.xconfigure.width != m_rect.width() || e.xconfigure.height != m_rect.height()) { if (e.xconfigure.width != m_rect.width() || e.xconfigure.height != m_rect.height()) {
m_xlib_surface->set_size(e.xconfigure.width, e.xconfigure.height); m_xlib_surface->set_size(e.xconfigure.width, e.xconfigure.height);
m_rect.set_width(e.xconfigure.width); m_rect.set_width(e.xconfigure.width);
@ -169,24 +178,29 @@ void Window::run(bool block) {
break; break;
} }
case Expose: { case Expose: {
INFO << "[X11 EVENT] Expose" << std::endl;
if (e.xexpose.count == 0) { if (e.xexpose.count == 0) {
reflow(); reflow();
} }
break; break;
} }
case ButtonRelease: { case ButtonRelease: {
INFO << "[X11 EVENT] ButtonRelease" << std::endl;
auto point = Point(e.xbutton.x, e.xbutton.y); auto point = Point(e.xbutton.x, e.xbutton.y);
auto event = MouseButtonEvent(false, false, false, false, point); auto event = MouseButtonEvent(false, false, false, false, point);
dispatch_to_main_widget(event); dispatch_to_main_widget(event);
break; break;
} }
case ButtonPress: { case ButtonPress: {
INFO << "[X11 EVENT] ButtonPress" << std::endl;
auto point = Point(e.xbutton.x, e.xbutton.y); auto point = Point(e.xbutton.x, e.xbutton.y);
auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, e.xbutton.button == Button4, e.xbutton.button == Button5, point); auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, e.xbutton.button == Button4, e.xbutton.button == Button5, point);
dispatch_to_main_widget(event); dispatch_to_main_widget(event);
break; break;
} }
case MotionNotify: { case MotionNotify: {
// spammy
//INFO << "[X11 EVENT] MotionNotify" << std::endl;
auto point = Point(e.xmotion.x, e.xmotion.y); auto point = Point(e.xmotion.x, e.xmotion.y);
auto event = MouseMoveEvent(point); auto event = MouseMoveEvent(point);
dispatch_to_main_widget(event); dispatch_to_main_widget(event);
@ -195,6 +209,16 @@ void Window::run(bool block) {
break; break;
} }
} }
if (XPending(m_x_display) == 0 && !m_microtasks.empty()) {
start_batch();
while (!m_microtasks.empty()) {
auto callback = m_microtasks.front();
callback();
m_microtasks.pop();
}
end_batch();
}
} }
} }

View file

@ -8,6 +8,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <cairomm/xlib_surface.h> #include <cairomm/xlib_surface.h>
#include <functional> #include <functional>
#include <queue>
namespace Raven { namespace Raven {
@ -41,6 +42,8 @@ public:
void relayout(); void relayout();
void reflow(); void reflow();
void queue_microtask(std::function<void()> callback) { m_microtasks.push(callback); }
Box &rect() { return m_rect; } Box &rect() { return m_rect; }
template<typename T, class... Args> template<typename T, class... Args>
@ -59,6 +62,7 @@ private:
int m_batches { 1 }; int m_batches { 1 };
bool m_did_repaint_during_batch { false }; bool m_did_repaint_during_batch { false };
bool m_did_relayout_during_batch { false }; bool m_did_relayout_during_batch { false };
std::queue<std::function<void()>> m_microtasks;
Display *m_x_display { nullptr }; Display *m_x_display { nullptr };