From 49d59309129f70445b60a028f2a46df8652fbf29 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 4 Aug 2023 16:08:58 +0200 Subject: [PATCH] wind: Add window titlebars using ui::Font --- wind/Mouse.cpp | 23 +++++++++++++++-------- wind/Window.cpp | 23 +++++++++++++++++++++-- wind/Window.h | 6 +++++- wind/main.cpp | 6 +++--- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/wind/Mouse.cpp b/wind/Mouse.cpp index bc1a3461..546e7765 100644 --- a/wind/Mouse.cpp +++ b/wind/Mouse.cpp @@ -33,22 +33,29 @@ void Mouse::update(const moon::MousePacket& packet) { m_dragging_window->surface.pos = ui::Point { m_position.x - m_initial_drag_position.x, m_position.y - m_initial_drag_position.y }; - m_dragging_window->surface = m_dragging_window->surface.absolute(); + m_dragging_window->surface = m_dragging_window->surface.normalized(); } else if ((packet.buttons & moon::MouseButton::Left) && !m_dragging_window) { - g_windows.for_each_reversed([this](Window* window) { - if (!this->m_dragging_window && window->surface.contains(this->m_position)) + // Iterate from the end of the list, since windows at the beginning are stacked at the bottom and windows at the + // top are at the end. + for (Window* window = g_windows.last().value_or(nullptr); window; + window = g_windows.previous(window).value_or(nullptr)) + { + if (window->surface.absolute(window->titlebar).contains(m_position)) { - this->m_dragging_window = window; - this->m_initial_drag_position = ui::Point { this->m_position.x - window->surface.pos.x, - this->m_position.y - window->surface.pos.y }; + m_dragging_window = window; + m_initial_drag_position = + ui::Point { m_position.x - window->surface.pos.x, m_position.y - window->surface.pos.y }; os::println("Started drag: window at (%d,%d,%d,%d) with offset (%d,%d)", window->surface.pos.x, window->surface.pos.y, window->surface.width, window->surface.height, - this->m_initial_drag_position.x, this->m_initial_drag_position.y); + m_initial_drag_position.x, m_initial_drag_position.y); window->focus(); } - }); + else if (window->surface.absolute(window->contents).contains(m_position)) + break; // We don't want to continue iterating, otherwise this would take into account windows whose + // titlebar is underneath another window's contents! + } } } diff --git a/wind/Window.cpp b/wind/Window.cpp index fc2918f2..66638f2b 100644 --- a/wind/Window.cpp +++ b/wind/Window.cpp @@ -1,10 +1,26 @@ #include "Window.h" +#include +#include +#include LinkedList g_windows; void Window::draw(ui::Canvas& screen) { - screen.subcanvas(surface).fill(color); + auto window = screen.subcanvas(surface); + window.subcanvas(contents).fill(color); + + wchar_t buffer[4096]; + Utf8StringDecoder decoder(name.chars()); + decoder.decode(buffer, sizeof(buffer)).release_value(); + + auto font = ui::Font::default_font(); + + auto titlebar_canvas = window.subcanvas(titlebar); + titlebar_canvas.fill(ui::GRAY); + + auto textarea = titlebar_canvas.subcanvas(ui::Rect { 10, 10, titlebar_canvas.width - 10, titlebar_canvas.height }); + font->render(buffer, ui::BLACK, textarea); } void Window::focus() @@ -14,7 +30,10 @@ void Window::focus() g_windows.append(this); } -Window::Window(ui::Rect r, ui::Color c) : surface(r), color(c) +Window::Window(ui::Rect r, ui::Color c, StringView n) : surface(r), color(c), name(n) { + auto font = ui::Font::default_font(); + titlebar = ui::Rect { 0, 0, surface.width, font->height() + 20 }; + contents = ui::Rect { 0, font->height() + 20, surface.width, surface.height - (font->height() + 20) }; g_windows.append(this); } diff --git a/wind/Window.h b/wind/Window.h index 45f1655b..7b529413 100644 --- a/wind/Window.h +++ b/wind/Window.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -7,9 +8,12 @@ struct Window : public LinkedListNode { ui::Rect surface; + ui::Rect titlebar; + ui::Rect contents; ui::Color color; + StringView name; - Window(ui::Rect, ui::Color); + Window(ui::Rect, ui::Color, StringView); void focus(); diff --git a/wind/main.cpp b/wind/main.cpp index ae8c2bcd..c095332d 100644 --- a/wind/main.cpp +++ b/wind/main.cpp @@ -72,9 +72,9 @@ Result luna_main(int argc, char** argv) ui::Color background = ui::BLACK; - TRY(make(ui::Rect { 200, 200, 600, 400 }, ui::GREEN)); - TRY(make(ui::Rect { 100, 100, 300, 200 }, ui::RED)); - TRY(make(ui::Rect { 600, 130, 350, 250 }, ui::CYAN)); + TRY(make(ui::Rect { 200, 200, 600, 400 }, ui::GREEN, "Calculator"_sv)); + TRY(make(ui::Rect { 100, 100, 300, 200 }, ui::RED, "Settings"_sv)); + TRY(make(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv)); Vector> clients;