2023-08-03 15:38:49 +00:00
|
|
|
#include "Window.h"
|
2023-08-04 14:08:58 +00:00
|
|
|
#include <luna/Utf8.h>
|
|
|
|
#include <os/File.h>
|
2023-08-14 18:08:05 +00:00
|
|
|
#include <sys/mman.h>
|
2023-08-04 14:08:58 +00:00
|
|
|
#include <ui/Font.h>
|
2023-08-04 19:17:50 +00:00
|
|
|
#include <ui/Image.h>
|
2023-08-03 15:38:49 +00:00
|
|
|
|
|
|
|
LinkedList<Window> g_windows;
|
|
|
|
|
2023-09-20 17:45:01 +00:00
|
|
|
static constexpr ui::Color TITLEBAR_COLOR = ui::Color::from_rgb(53, 53, 53);
|
|
|
|
|
2023-08-03 15:38:49 +00:00
|
|
|
void Window::draw(ui::Canvas& screen)
|
|
|
|
{
|
2023-08-14 18:08:05 +00:00
|
|
|
dirty = false;
|
|
|
|
|
2023-08-04 14:08:58 +00:00
|
|
|
auto window = screen.subcanvas(surface);
|
2023-08-14 18:08:05 +00:00
|
|
|
window.subcanvas(contents).fill(pixels, contents.width);
|
2023-08-04 14:08:58 +00:00
|
|
|
|
|
|
|
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);
|
2023-09-20 17:45:01 +00:00
|
|
|
titlebar_canvas.fill(TITLEBAR_COLOR);
|
2023-08-04 14:08:58 +00:00
|
|
|
|
|
|
|
auto textarea = titlebar_canvas.subcanvas(ui::Rect { 10, 10, titlebar_canvas.width - 10, titlebar_canvas.height });
|
2023-09-20 17:45:01 +00:00
|
|
|
font->render(buffer, ui::WHITE, textarea);
|
2023-08-04 19:17:50 +00:00
|
|
|
|
2023-08-07 17:10:27 +00:00
|
|
|
static SharedPtr<ui::Image> g_close_icon;
|
|
|
|
|
|
|
|
if (!g_close_icon) g_close_icon = ui::Image::load("/usr/share/icons/16x16/app-close.tga").release_value();
|
|
|
|
|
|
|
|
auto close_area = window.subcanvas(close_button);
|
|
|
|
close_area.fill(g_close_icon->pixels(), g_close_icon->width());
|
2023-08-03 15:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::focus()
|
|
|
|
{
|
|
|
|
// Bring the window to the front of the list.
|
|
|
|
g_windows.remove(this);
|
|
|
|
g_windows.append(this);
|
|
|
|
}
|
|
|
|
|
2023-09-27 16:14:32 +00:00
|
|
|
Window::Window(ui::Rect r, String&& n, ui::WindowType t) : surface(r), name(move(n)), type(t)
|
2023-08-03 15:38:49 +00:00
|
|
|
{
|
2023-08-04 14:08:58 +00:00
|
|
|
auto font = ui::Font::default_font();
|
2023-09-27 16:14:32 +00:00
|
|
|
bool decorated { type == ui::WindowType::Normal };
|
2023-08-15 10:56:55 +00:00
|
|
|
if (decorated && surface.width < 36) surface.width = 36;
|
|
|
|
if (decorated && surface.height < (font->height() + 20)) surface.height = font->height() + 20;
|
|
|
|
titlebar = decorated ? ui::Rect { 0, 0, surface.width, font->height() + 20 } : ui::Rect { 0, 0, 0, 0 };
|
|
|
|
close_button = decorated ? ui::Rect { surface.width - 26, 10, 16, 16 } : ui::Rect { 0, 0, 0, 0 };
|
|
|
|
contents = decorated ? ui::Rect { 0, font->height() + 20, surface.width, surface.height - (font->height() + 20) }
|
|
|
|
: ui::Rect { 0, 0, surface.width, surface.height };
|
2023-08-03 15:38:49 +00:00
|
|
|
g_windows.append(this);
|
|
|
|
}
|
2023-08-14 16:15:29 +00:00
|
|
|
|
|
|
|
int Window::titlebar_height()
|
|
|
|
{
|
|
|
|
auto font = ui::Font::default_font();
|
|
|
|
return font->height() + 20;
|
|
|
|
}
|
2023-08-14 18:08:05 +00:00
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
|
|
|
usize size = contents.width * contents.height * 4;
|
|
|
|
|
|
|
|
munmap(pixels, size);
|
|
|
|
}
|