Compare commits

...

3 commits

Author SHA1 Message Date
hippoz
d5212ea574
remove unneeded debug logs 2022-11-10 20:19:04 +02:00
hippoz
6e16ab35cd
fix scrolling affecting all widgets 2022-11-10 20:18:28 +02:00
hippoz
3fa1647fc9
add application event loop "wake-up" 2022-11-10 20:17:37 +02:00
4 changed files with 48 additions and 12 deletions

View file

@ -1,14 +1,37 @@
#include "Application.hpp"
#include <poll.h>
#include <unistd.h>
namespace Raven {
bool Application::wake() {
if (!m_wakeup_pipe_available) {
return false;
}
uint32_t buf = 0;
write(m_wakeup_pipe_write_end, &buf, 1);
return true;
}
void Application::add_window(std::shared_ptr<Window> window) {
m_windows.push_back(window);
update_fds();
}
void Application::update_fds() {
if (m_should_create_wakeup_pipe) {
m_should_create_wakeup_pipe = false;
int pipe_fds[2];
if (pipe(pipe_fds) != 0) {
// TODO: handle this?
return;
}
m_wakeup_pipe_read_end = pipe_fds[0];
m_wakeup_pipe_write_end = pipe_fds[1];
m_wakeup_pipe_available = true;
}
for (int i = 0; i < RAVEN_APPLICATION_MAX_WINDOWS; i++) {
m_fds[i].fd = -1;
m_fds[i].events = 0;
@ -18,6 +41,12 @@ void Application::update_fds() {
m_fds[i].fd = m_windows[i]->file_descriptor();
m_fds[i].events = POLL_IN;
}
if (m_wakeup_pipe_available) {
int wakeup_pipe_index = m_windows.size();
m_fds[wakeup_pipe_index].fd = m_wakeup_pipe_read_end;
m_fds[wakeup_pipe_index].events = POLL_IN;
m_fds[wakeup_pipe_index].revents = 0;
}
}
int Application::turn() {
@ -25,11 +54,14 @@ int Application::turn() {
update_fds();
m_fds_need_update = false;
}
if (poll(m_fds, m_windows.size(), -1) > 0) {
if (poll(m_fds, m_windows.size() + 1, -1) > 0) {
for (size_t i = 0; i < m_windows.size(); i++) {
if (m_fds[i].revents & POLLIN) {
m_windows[i]->run(false);
}
m_windows[i]->run(false);
}
while (!m_microtasks.empty()) {
auto callback = m_microtasks.front();
callback();
m_microtasks.pop();
}
} else {
return 1;

View file

@ -12,8 +12,12 @@ namespace Raven {
class Application {
public:
Application() {}
Application(bool should_create_wakeup_pipe)
: m_should_create_wakeup_pipe(should_create_wakeup_pipe) {}
void add_window(std::shared_ptr<Window>);
bool wake();
void queue_microtask(std::function<void()> callback) { m_microtasks.push(callback); }
int turn();
int run();
@ -29,6 +33,11 @@ private:
std::vector<std::shared_ptr<Window>> m_windows;
struct pollfd m_fds[RAVEN_APPLICATION_MAX_WINDOWS];
bool m_fds_need_update { true };
bool m_should_create_wakeup_pipe { false };
bool m_wakeup_pipe_available { false };
int m_wakeup_pipe_read_end { -1 };
int m_wakeup_pipe_write_end { -1 };
std::queue<std::function<void()>> m_microtasks;
};
}

View file

@ -94,13 +94,8 @@ bool BoxLayout::run() {
auto child = m_target->children()[i];
Slot slot = working_slots[i];
std::cout << "SLOT! " << i << std::endl;
if (slot.type == SlotType::Auto) {
std::cout << "auto - setting to " << space_per_unslotted_widget << std::endl;
slot.pixel = space_per_unslotted_widget;
} else {
std::cout << "pixel: " << slot.pixel << std::endl;
}
// make sure pixel values are aligned to the pixel grid

View file

@ -252,12 +252,12 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
update_activation_to = true;
}
if (!m_rect.contains(event.point())) {
if (m_rect.contains(event.point())) {
on_mouse_button(event);
} else {
update_activation_to = false;
}
on_mouse_button(event);
if (m_is_active != update_activation_to || (update_activation_to && m_window->active_widget() != this)) {
m_is_active = update_activation_to;
if (m_is_active && m_window)