wind: Add window titlebars using ui::Font
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
This commit is contained in:
parent
42646d3b49
commit
f2c5306f10
@ -33,22 +33,29 @@ void Mouse::update(const moon::MousePacket& packet)
|
|||||||
{
|
{
|
||||||
m_dragging_window->surface.pos =
|
m_dragging_window->surface.pos =
|
||||||
ui::Point { m_position.x - m_initial_drag_position.x, m_position.y - m_initial_drag_position.y };
|
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)
|
else if ((packet.buttons & moon::MouseButton::Left) && !m_dragging_window)
|
||||||
{
|
{
|
||||||
g_windows.for_each_reversed([this](Window* window) {
|
// Iterate from the end of the list, since windows at the beginning are stacked at the bottom and windows at the
|
||||||
if (!this->m_dragging_window && window->surface.contains(this->m_position))
|
// 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;
|
m_dragging_window = window;
|
||||||
this->m_initial_drag_position = ui::Point { this->m_position.x - window->surface.pos.x,
|
m_initial_drag_position =
|
||||||
this->m_position.y - window->surface.pos.y };
|
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,
|
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,
|
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();
|
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!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,26 @@
|
|||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
|
#include <luna/Utf8.h>
|
||||||
|
#include <os/File.h>
|
||||||
|
#include <ui/Font.h>
|
||||||
|
|
||||||
LinkedList<Window> g_windows;
|
LinkedList<Window> g_windows;
|
||||||
|
|
||||||
void Window::draw(ui::Canvas& screen)
|
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()
|
void Window::focus()
|
||||||
@ -14,7 +30,10 @@ void Window::focus()
|
|||||||
g_windows.append(this);
|
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);
|
g_windows.append(this);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/LinkedList.h>
|
#include <luna/LinkedList.h>
|
||||||
|
#include <luna/StringView.h>
|
||||||
#include <ui/Canvas.h>
|
#include <ui/Canvas.h>
|
||||||
#include <ui/Color.h>
|
#include <ui/Color.h>
|
||||||
#include <ui/Rect.h>
|
#include <ui/Rect.h>
|
||||||
@ -7,9 +8,12 @@
|
|||||||
struct Window : public LinkedListNode<Window>
|
struct Window : public LinkedListNode<Window>
|
||||||
{
|
{
|
||||||
ui::Rect surface;
|
ui::Rect surface;
|
||||||
|
ui::Rect titlebar;
|
||||||
|
ui::Rect contents;
|
||||||
ui::Color color;
|
ui::Color color;
|
||||||
|
StringView name;
|
||||||
|
|
||||||
Window(ui::Rect, ui::Color);
|
Window(ui::Rect, ui::Color, StringView);
|
||||||
|
|
||||||
void focus();
|
void focus();
|
||||||
|
|
||||||
|
@ -72,9 +72,9 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
|
|
||||||
ui::Color background = ui::BLACK;
|
ui::Color background = ui::BLACK;
|
||||||
|
|
||||||
TRY(make<Window>(ui::Rect { 200, 200, 600, 400 }, ui::GREEN));
|
TRY(make<Window>(ui::Rect { 200, 200, 600, 400 }, ui::GREEN, "Calculator"_sv));
|
||||||
TRY(make<Window>(ui::Rect { 100, 100, 300, 200 }, ui::RED));
|
TRY(make<Window>(ui::Rect { 100, 100, 300, 200 }, ui::RED, "Settings"_sv));
|
||||||
TRY(make<Window>(ui::Rect { 600, 130, 350, 250 }, ui::CYAN));
|
TRY(make<Window>(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv));
|
||||||
|
|
||||||
Vector<SharedPtr<os::LocalServer::Client>> clients;
|
Vector<SharedPtr<os::LocalServer::Client>> clients;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user