Temp commit before rewriting the window- and desktop manager

This commit is contained in:
Rasmus Rasmussen 2025-12-18 16:14:27 +01:00
parent f71b54e5f2
commit 4f7aba8aa3
11 changed files with 63 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include "tft_handler.h" #include "tft_handler.h"
#include "event_manager.h" #include "event_manager.h"
#include "GLOBALS.h" #include "GLOBALS.h"
#include "global_queue.h"
#include <vector> #include <vector>
WindowManager* wm; WindowManager* wm;
@ -41,6 +42,8 @@ void setup() {
// Initialize event manager (creates dispatcher task) // Initialize event manager (creates dispatcher task)
EventManager::getInstance().init(); EventManager::getInstance().init();
initGlobalQueue();
// Initialize components (they subscribe to events) // Initialize components (they subscribe to events)
im->init(_display_state, th); im->init(_display_state, th);
@ -83,6 +86,7 @@ void setup() {
.background_color = 0xbdf7, .background_color = 0xbdf7,
.foreground_color = COL_DARK_BLUE, .foreground_color = COL_DARK_BLUE,
.focused = false, .focused = false,
.minimized = false,
.title = "Hello World!", .title = "Hello World!",
.window_decorations = wdec, .window_decorations = wdec,
}; };
@ -97,6 +101,7 @@ void setup() {
.background_color = 0xbdf7, .background_color = 0xbdf7,
.foreground_color = COL_DARK_BLUE, .foreground_color = COL_DARK_BLUE,
.focused = true, .focused = true,
.minimized = false,
.title = "Second Window", .title = "Second Window",
.window_decorations = wdec, .window_decorations = wdec,
}; };
@ -114,5 +119,5 @@ void loop() {
_display_state->update_display.store(false); _display_state->update_display.store(false);
} }
delay(5); delay(0);
} }

View File

@ -16,6 +16,18 @@ struct CLICK_EVENT {
CLICK_EVENTS event; CLICK_EVENTS event;
}; };
enum class ApplicationEventState {
NONE = 0,
CREATED = 1,
CLOSED = 2,
};
struct APPLICATION_EVENT {
int id;
ApplicationEventState state;
char title[64];
};
struct DISPLAY_STATE { struct DISPLAY_STATE {
std::atomic<bool> update_display; std::atomic<bool> update_display;
}; };

View File

@ -1,5 +1,15 @@
#pragma once #pragma once
#include "WString.h" #include <string>
struct TaskBarItem {
int id;
int place_x;
int place_y;
int icon_size_x;
int icon_size_y;
bool focused;
std::string name;
};
struct Desktop_Item { struct Desktop_Item {
int id; int id;

View File

@ -1,11 +1,12 @@
#include "desktop_hander.h" #include "desktop_hander.h"
#include "global_queue.h"
#include "tft_handler.h" #include "tft_handler.h"
void Desktop_Hander::init(TFT_Handler* th, DISPLAY_STATE* ds) { void Desktop_Hander::init(TFT_Handler* th, DISPLAY_STATE* ds) {
tft = th; tft = th;
display_state = ds; display_state = ds;
// Subscribe to events // Subscribe to click events
EventManager::getInstance().subscribe(this); EventManager::getInstance().subscribe(this);
// Create desktop items // Create desktop items
@ -52,6 +53,10 @@ void Desktop_Hander::draw_task_bar(int color) {
// Button // Button
tft->draw_box(6, 298, 50, 18, color); tft->draw_box(6, 298, 50, 18, color);
for (TaskBarItem item : task_bar_items) {
}
} }
void Desktop_Hander::draw_desktop_Item(Desktop_Item desktop_item) { void Desktop_Hander::draw_desktop_Item(Desktop_Item desktop_item) {

View File

@ -9,6 +9,7 @@ class Desktop_Hander : public IEventListener {
private: private:
TFT_Handler* tft; TFT_Handler* tft;
DISPLAY_STATE* display_state; DISPLAY_STATE* display_state;
std::vector<TaskBarItem> task_bar_items;
std::vector<Desktop_Item> items; std::vector<Desktop_Item> items;
int handle_desktop_click(CLICK_EVENT event); int handle_desktop_click(CLICK_EVENT event);

View File

@ -1,3 +1,4 @@
#include "GLOBALS.h"
#include "event_manager.h" #include "event_manager.h"
void EventManager::init() { void EventManager::init() {
@ -5,7 +6,7 @@ void EventManager::init() {
} }
void EventManager::publish(const CLICK_EVENT& event) { void EventManager::publish(const CLICK_EVENT& event) {
xQueueSend(_event_queue, &event, 0); xQueueSend(_click_event_queue, &event, 0);
} }
void EventManager::subscribe(IEventListener* listener) { void EventManager::subscribe(IEventListener* listener) {
@ -24,7 +25,7 @@ void EventManager::dispatch_task(void* pvParameters) {
CLICK_EVENT event; CLICK_EVENT event;
for (;;) { for (;;) {
if (xQueueReceive(self->_event_queue, &event, portMAX_DELAY)) { if (xQueueReceive(self->_click_event_queue, &event, portMAX_DELAY)) {
// Dispatch to all listeners // Dispatch to all listeners
for (auto* listener : self->_listeners) { for (auto* listener : self->_listeners) {
listener->on_click_event(event); listener->on_click_event(event);

View File

@ -17,12 +17,12 @@ public:
class EventManager { class EventManager {
private: private:
QueueHandle_t _event_queue; QueueHandle_t _click_event_queue;
std::vector<IEventListener*> _listeners; std::vector<IEventListener*> _listeners;
TaskHandle_t _dispatcher_task; TaskHandle_t _dispatcher_task;
EventManager() { EventManager() {
_event_queue = xQueueCreate(10, sizeof(CLICK_EVENT)); _click_event_queue = xQueueCreate(10, sizeof(CLICK_EVENT));
} }
EventManager(const EventManager&) = delete; EventManager(const EventManager&) = delete;

View File

@ -51,22 +51,18 @@ void InputManager::handle_button_press(int buttonIndex) {
mi.x = (mi.x + 5 > 476) ? 476 : mi.x + 5; mi.x = (mi.x + 5 > 476) ? 476 : mi.x + 5;
needs_redraw = true; needs_redraw = true;
break; break;
case 1: case 1:
mi.y = (mi.y + 5 > 316) ? 316 : mi.y + 5; mi.y = (mi.y + 5 > 316) ? 316 : mi.y + 5;
needs_redraw = true; needs_redraw = true;
break; break;
case 2: case 2:
mi.y = (mi.y - 5 < 0) ? 0 : mi.y - 5; mi.y = (mi.y - 5 < 0) ? 0 : mi.y - 5;
needs_redraw = true; needs_redraw = true;
break; break;
case 3: case 3:
mi.x = (mi.x - 5 < 0) ? 0 : mi.x - 5; mi.x = (mi.x - 5 < 0) ? 0 : mi.x - 5;
needs_redraw = true; needs_redraw = true;
break; break;
case 4: // Click case 4: // Click
{ {
CLICK_EVENT event = { CLICK_EVENT event = {
@ -77,7 +73,6 @@ void InputManager::handle_button_press(int buttonIndex) {
EventManager::getInstance().publish(event); EventManager::getInstance().publish(event);
} }
break; break;
case 5: // Right click case 5: // Right click
{ {
CLICK_EVENT event = { CLICK_EVENT event = {

View File

@ -27,6 +27,7 @@ struct Window {
int background_color; int background_color;
int foreground_color; int foreground_color;
bool focused; bool focused;
bool minimized;
std::string title; std::string title;
std::vector<WindowDecoration> window_decorations; std::vector<WindowDecoration> window_decorations;
}; };

View File

@ -1,13 +1,14 @@
#include "window.h" #include "window.h"
#include "GLOBALS.h" #include "GLOBALS.h"
#include "window_manager.h" #include "window_manager.h"
#include "global_queue.h"
#include <algorithm> #include <algorithm>
void WindowManager::init(TFT_Handler* th, DISPLAY_STATE* ds) { void WindowManager::init(TFT_Handler* th, DISPLAY_STATE* ds) {
tft = th; tft = th;
display_state = ds; display_state = ds;
// Subscribe to events // Subscribe to click events
EventManager::getInstance().subscribe(this); EventManager::getInstance().subscribe(this);
} }
@ -33,6 +34,16 @@ void WindowManager::close_window(int window_id) {
} }
} }
void WindowManager::minimize_window(int window_id) {
for (auto it = windows.begin(); it != windows.end(); ++it) {
if (it->id == window_id) {
it->minimized = true;
display_state->update_display.store(true);
break;
}
}
}
void WindowManager::draw_all() { void WindowManager::draw_all() {
tft->start_draw(); tft->start_draw();
for (const auto& win : windows) { for (const auto& win : windows) {
@ -42,6 +53,8 @@ void WindowManager::draw_all() {
} }
void WindowManager::draw_window(Window window) { void WindowManager::draw_window(Window window) {
if (window.minimized) return;
// Outer shadow // Outer shadow
tft->draw_rect(window.x, window.y, window.width + 2, window.height + 2, 1, 0x0000); tft->draw_rect(window.x, window.y, window.width + 2, window.height + 2, 1, 0x0000);
@ -97,11 +110,16 @@ int WindowManager::handle_window_click(CLICK_EVENT event) {
for (auto it = window.window_decorations.begin(); it != window.window_decorations.end(); ++it) { for (auto it = window.window_decorations.begin(); it != window.window_decorations.end(); ++it) {
int x = window.x + it->x_offset; int x = window.x + it->x_offset;
int y = window.y + it->y_offset; int y = window.y + it->y_offset;
Serial.printf("x: %i y: %i\n", x, y);
if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::CLOSE) { if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::CLOSE) {
close_window(window.id); close_window(window.id);
return window.id; return window.id;
} }
if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::MINIMIZE) {
minimize_window(window.id);
return window.id;
}
} }
if (window.focused) return -1; if (window.focused) return -1;

View File

@ -25,6 +25,7 @@ public:
void init(TFT_Handler* th, DISPLAY_STATE* ds); void init(TFT_Handler* th, DISPLAY_STATE* ds);
void create_window(Window window); void create_window(Window window);
void close_window(int window_id); void close_window(int window_id);
void minimize_window(int window_id);
void draw_all(); void draw_all();
void draw_window(Window window); void draw_window(Window window);