Compare commits
No commits in common. "fd62ea588d09e297f4abb559d63319b83b017868" and "26623f8a0dd5a279c59ecfb4a59c720dde85fa59" have entirely different histories.
fd62ea588d
...
26623f8a0d
@ -42,7 +42,5 @@ luna_app(touch.cpp touch)
|
||||
luna_app(free.cpp free)
|
||||
luna_app(gclient.cpp gclient)
|
||||
target_link_libraries(gclient PUBLIC ui)
|
||||
luna_app(about.cpp about)
|
||||
target_link_libraries(about PUBLIC ui)
|
||||
luna_app(taskbar.cpp taskbar)
|
||||
target_link_libraries(taskbar PUBLIC ui)
|
||||
|
@ -1,46 +0,0 @@
|
||||
#include <luna/String.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <ui/App.h>
|
||||
#include <ui/Button.h>
|
||||
#include <ui/Label.h>
|
||||
#include <ui/Layout.h>
|
||||
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
ui::App app;
|
||||
TRY(app.init(argc, argv));
|
||||
|
||||
auto* window = TRY(ui::Window::create(ui::Rect { 300, 300, 400, 300 }));
|
||||
app.set_main_window(window);
|
||||
|
||||
window->set_title("About");
|
||||
window->set_background(ui::CYAN);
|
||||
|
||||
utsname info;
|
||||
uname(&info);
|
||||
|
||||
ui::VerticalLayout main_layout;
|
||||
window->set_main_widget(main_layout);
|
||||
|
||||
ui::Label title("About Luna", ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center,
|
||||
ui::Font::default_bold_font());
|
||||
main_layout.add_widget(title);
|
||||
|
||||
ui::VerticalLayout version_info;
|
||||
main_layout.add_widget(version_info);
|
||||
|
||||
ui::Label license("Licensed under the BSD-2-Clause license.");
|
||||
main_layout.add_widget(license);
|
||||
|
||||
String os_release_text = TRY(String::format("OS release: %s"_sv, info.release));
|
||||
ui::Label os_release(os_release_text.view());
|
||||
version_info.add_widget(os_release);
|
||||
|
||||
String kernel_version_text = TRY(String::format("Kernel version: %s"_sv, info.version));
|
||||
ui::Label kernel_version(kernel_version_text.view());
|
||||
version_info.add_widget(kernel_version);
|
||||
|
||||
window->draw();
|
||||
|
||||
return app.run();
|
||||
}
|
@ -10,16 +10,14 @@ struct ColorWidget : public ui::Widget
|
||||
|
||||
Result<ui::EventResult> handle_mouse_move(ui::Point) override
|
||||
{
|
||||
auto old_color = m_color;
|
||||
m_color = m_second_color;
|
||||
return old_color.raw == m_second_color.raw ? ui::EventResult::DidNotHandle : ui::EventResult::DidHandle;
|
||||
return ui::EventResult::DidHandle;
|
||||
}
|
||||
|
||||
Result<ui::EventResult> handle_mouse_leave() override
|
||||
{
|
||||
auto old_color = m_color;
|
||||
m_color = m_first_color;
|
||||
return old_color.raw == m_first_color.raw ? ui::EventResult::DidNotHandle : ui::EventResult::DidHandle;
|
||||
return ui::EventResult::DidHandle;
|
||||
}
|
||||
|
||||
Result<void> draw(ui::Canvas& canvas) override
|
||||
@ -42,7 +40,7 @@ Result<int> luna_main(int argc, char** argv)
|
||||
auto* window = TRY(ui::Window::create(ui::Rect { 200, 200, 400, 300 }));
|
||||
app.set_main_window(window);
|
||||
|
||||
window->set_title("Test Window");
|
||||
window->set_title("Main Window");
|
||||
window->set_background(ui::CYAN);
|
||||
|
||||
ui::HorizontalLayout layout;
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include <os/File.h>
|
||||
#include <os/Process.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
@ -31,31 +30,18 @@ Result<int> luna_main(int argc, char** argv)
|
||||
ui::HorizontalLayout layout(ui::AdjustHeight::Yes, ui::AdjustWidth::No);
|
||||
window->set_main_widget(layout);
|
||||
|
||||
ui::Button start_button({ 0, 0, 50, 50 });
|
||||
layout.add_widget(start_button);
|
||||
ui::Button button({ 0, 0, 50, 50 });
|
||||
layout.add_widget(button);
|
||||
|
||||
ui::Container start_container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
|
||||
start_button.set_widget(start_container);
|
||||
start_button.set_action([] {
|
||||
ui::Container container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
|
||||
button.set_widget(container);
|
||||
button.set_action([] {
|
||||
StringView args[] = { "/usr/bin/gclient" };
|
||||
os::Process::spawn("/usr/bin/gclient", Slice<StringView> { args, 1 }, false);
|
||||
});
|
||||
|
||||
auto start_image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/start-icon.tga"));
|
||||
start_container.set_widget(*start_image);
|
||||
|
||||
ui::Button about_button({ 0, 0, 50, 50 });
|
||||
layout.add_widget(about_button);
|
||||
|
||||
ui::Container about_container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
|
||||
about_button.set_widget(about_container);
|
||||
about_button.set_action([] {
|
||||
StringView args[] = { "/usr/bin/about" };
|
||||
os::Process::spawn("/usr/bin/about", Slice<StringView> { args, 1 }, false);
|
||||
});
|
||||
|
||||
auto about_image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/app-about.tga"));
|
||||
about_container.set_widget(*about_image);
|
||||
auto image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/start-icon.tga"));
|
||||
container.set_widget(*image);
|
||||
|
||||
window->draw();
|
||||
|
||||
|
@ -16,7 +16,6 @@ set(SOURCES
|
||||
src/Alignment.cpp
|
||||
src/Container.cpp
|
||||
src/Button.cpp
|
||||
src/Label.cpp
|
||||
)
|
||||
|
||||
add_library(ui ${SOURCES})
|
||||
|
@ -18,10 +18,7 @@
|
||||
namespace ui
|
||||
{
|
||||
/**
|
||||
* @brief A class holding PSF font data, used for low-level direct rendering of glyphs into a canvas.
|
||||
*
|
||||
* This class does not handle special characters such as tabs or newlines. For those, you should be using a more
|
||||
* high-level component such as ui::Label instead.
|
||||
* @brief A class holding PSF font data, used for direct rendering of glyphs into a canvas.
|
||||
*/
|
||||
class Font : public Shareable
|
||||
{
|
||||
|
@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @file Label.h
|
||||
* @author apio (cloudapio.eu)
|
||||
* @brief A simple one-line text widget.
|
||||
*
|
||||
* @copyright Copyright (c) 2023, the Luna authors.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <ui/Alignment.h>
|
||||
#include <ui/Font.h>
|
||||
#include <ui/Widget.h>
|
||||
|
||||
namespace ui
|
||||
{
|
||||
/**
|
||||
* @brief Displays one line of text.
|
||||
*
|
||||
* This component does not handle newlines.
|
||||
*/
|
||||
class Label final : public Widget
|
||||
{
|
||||
public:
|
||||
Label(StringView text, VerticalAlignment valign = VerticalAlignment::Center,
|
||||
HorizontalAlignment halign = HorizontalAlignment::Center, SharedPtr<Font> font = Font::default_font());
|
||||
|
||||
void set_text(StringView text)
|
||||
{
|
||||
m_text = text;
|
||||
}
|
||||
|
||||
Result<void> draw(Canvas& canvas) override;
|
||||
|
||||
private:
|
||||
StringView m_text;
|
||||
VerticalAlignment m_valign;
|
||||
HorizontalAlignment m_halign;
|
||||
SharedPtr<Font> m_font;
|
||||
};
|
||||
}
|
@ -43,7 +43,7 @@ namespace ui
|
||||
Vector<Widget*> m_widgets;
|
||||
AdjustHeight m_adjust_height;
|
||||
AdjustWidth m_adjust_width;
|
||||
int m_used_width { 0 };
|
||||
int m_used_width;
|
||||
};
|
||||
|
||||
class VerticalLayout final : public Widget
|
||||
@ -64,6 +64,6 @@ namespace ui
|
||||
Vector<Widget*> m_widgets;
|
||||
AdjustHeight m_adjust_height;
|
||||
AdjustWidth m_adjust_width;
|
||||
int m_used_height { 0 };
|
||||
int m_used_height;
|
||||
};
|
||||
}
|
||||
|
@ -46,9 +46,9 @@ namespace ui
|
||||
void close();
|
||||
|
||||
Result<void> draw();
|
||||
Result<ui::EventResult> handle_mouse_leave();
|
||||
Result<ui::EventResult> handle_mouse_move(ui::Point position);
|
||||
Result<ui::EventResult> handle_mouse_buttons(ui::Point position, int buttons);
|
||||
Result<void> handle_mouse_leave();
|
||||
Result<void> handle_mouse_move(ui::Point position);
|
||||
Result<void> handle_mouse_buttons(ui::Point position, int buttons);
|
||||
|
||||
int id() const
|
||||
{
|
||||
|
@ -117,19 +117,17 @@ namespace ui
|
||||
MouseEventRequest request;
|
||||
READ_MESSAGE(request);
|
||||
auto* window = find_window(request.window);
|
||||
auto move_result = window->handle_mouse_move(request.position).value_or(ui::EventResult::DidNotHandle);
|
||||
auto button_result =
|
||||
window->handle_mouse_buttons(request.position, request.buttons).value_or(ui::EventResult::DidNotHandle);
|
||||
if (move_result == ui::EventResult::DidHandle || button_result == ui::EventResult::DidHandle)
|
||||
window->draw();
|
||||
window->handle_mouse_move(request.position);
|
||||
window->handle_mouse_buttons(request.position, request.buttons);
|
||||
window->draw();
|
||||
return {};
|
||||
}
|
||||
case MOUSE_LEAVE_REQUEST_ID: {
|
||||
MouseLeaveRequest request;
|
||||
READ_MESSAGE(request);
|
||||
auto* window = find_window(request.window);
|
||||
if (window->handle_mouse_leave().value_or(ui::EventResult::DidNotHandle) == ui::EventResult::DidHandle)
|
||||
window->draw();
|
||||
window->handle_mouse_leave();
|
||||
window->draw();
|
||||
return {};
|
||||
}
|
||||
default: fail("Unexpected IPC request from server!");
|
||||
|
@ -1,36 +0,0 @@
|
||||
/**
|
||||
* @file Label.cpp
|
||||
* @author apio (cloudapio.eu)
|
||||
* @brief A simple one-line text widget.
|
||||
*
|
||||
* @copyright Copyright (c) 2023, the Luna authors.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <luna/Utf8.h>
|
||||
#include <ui/Label.h>
|
||||
|
||||
namespace ui
|
||||
{
|
||||
Label::Label(StringView text, VerticalAlignment valign, HorizontalAlignment halign, SharedPtr<Font> font)
|
||||
: m_text(text), m_valign(valign), m_halign(halign), m_font(font)
|
||||
{
|
||||
}
|
||||
|
||||
Result<void> Label::draw(Canvas& canvas)
|
||||
{
|
||||
ui::Rect contained;
|
||||
contained.pos = { 0, 0 };
|
||||
contained.width = static_cast<int>(m_text.length() * m_font->width());
|
||||
contained.height = m_font->height();
|
||||
auto subcanvas =
|
||||
canvas.subcanvas(ui::align({ 0, 0, m_rect.width, m_rect.height }, contained, m_valign, m_halign));
|
||||
|
||||
Utf8StringDecoder decoder(m_text.chars());
|
||||
wchar_t buf[4096];
|
||||
TRY(decoder.decode(buf, sizeof(buf)));
|
||||
|
||||
m_font->render(buf, ui::BLACK, subcanvas);
|
||||
return {};
|
||||
}
|
||||
}
|
@ -33,15 +33,9 @@ namespace ui
|
||||
|
||||
Result<EventResult> HorizontalLayout::handle_mouse_leave()
|
||||
{
|
||||
EventResult result = ui::EventResult::DidNotHandle;
|
||||
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave());
|
||||
|
||||
for (auto widget : m_widgets)
|
||||
{
|
||||
auto rc = TRY(widget->handle_mouse_leave());
|
||||
if (rc == ui::EventResult::DidHandle) result = rc;
|
||||
}
|
||||
|
||||
return result;
|
||||
return ui::EventResult::DidNotHandle;
|
||||
}
|
||||
|
||||
Result<EventResult> HorizontalLayout::handle_mouse_up(Point position, int buttons)
|
||||
@ -128,15 +122,9 @@ namespace ui
|
||||
|
||||
Result<EventResult> VerticalLayout::handle_mouse_leave()
|
||||
{
|
||||
EventResult result = ui::EventResult::DidNotHandle;
|
||||
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave());
|
||||
|
||||
for (auto widget : m_widgets)
|
||||
{
|
||||
auto rc = TRY(widget->handle_mouse_leave());
|
||||
if (rc == ui::EventResult::DidHandle) result = rc;
|
||||
}
|
||||
|
||||
return result;
|
||||
return ui::EventResult::DidNotHandle;
|
||||
}
|
||||
|
||||
Result<EventResult> VerticalLayout::handle_mouse_up(Point position, int buttons)
|
||||
|
@ -80,38 +80,31 @@ namespace ui
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<ui::EventResult> Window::handle_mouse_leave()
|
||||
Result<void> Window::handle_mouse_leave()
|
||||
{
|
||||
if (!m_main_widget) return ui::EventResult::DidNotHandle;
|
||||
return m_main_widget->handle_mouse_leave();
|
||||
if (!m_main_widget) return {};
|
||||
TRY(m_main_widget->handle_mouse_leave());
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<ui::EventResult> Window::handle_mouse_move(ui::Point position)
|
||||
Result<void> Window::handle_mouse_move(ui::Point position)
|
||||
{
|
||||
if (!m_main_widget) return ui::EventResult::DidNotHandle;
|
||||
return m_main_widget->handle_mouse_move(position);
|
||||
if (!m_main_widget) return {};
|
||||
TRY(m_main_widget->handle_mouse_move(position));
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<ui::EventResult> Window::handle_mouse_buttons(ui::Point position, int buttons)
|
||||
Result<void> Window::handle_mouse_buttons(ui::Point position, int buttons)
|
||||
{
|
||||
if (!m_main_widget) return ui::EventResult::DidNotHandle;
|
||||
auto result = ui::EventResult::DidNotHandle;
|
||||
if (buttons)
|
||||
{
|
||||
auto rc = TRY(m_main_widget->handle_mouse_down(position, buttons));
|
||||
if (rc == ui::EventResult::DidHandle) result = rc;
|
||||
}
|
||||
if (!m_main_widget) return {};
|
||||
if (buttons) TRY(m_main_widget->handle_mouse_down(position, buttons));
|
||||
if (m_old_mouse_buttons.has_value())
|
||||
{
|
||||
int old_buttons = m_old_mouse_buttons.value();
|
||||
int diff = old_buttons & ~buttons;
|
||||
if (diff)
|
||||
{
|
||||
auto rc = TRY(m_main_widget->handle_mouse_up(position, diff));
|
||||
if (rc == ui::EventResult::DidHandle) result = rc;
|
||||
}
|
||||
if (diff) TRY(m_main_widget->handle_mouse_up(position, diff));
|
||||
}
|
||||
m_old_mouse_buttons = buttons;
|
||||
return result;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user