Compare commits

..

No commits in common. "b3dc027ba06024c1581167250b8907edea031d3a" and "9a9c7e577a5abbccccdfb3e3b2994c94f4687d0f" have entirely different histories.

8 changed files with 46 additions and 143 deletions

View File

@ -15,26 +15,6 @@ void sigchld_handler(int)
wait(nullptr);
}
Result<void> create_widget_group_for_app(ui::HorizontalLayout& layout, Slice<StringView> args, StringView icon)
{
auto* button = new (std::nothrow) ui::Button({ 0, 0, 50, 50 });
if (!button) return err(ENOMEM);
layout.add_widget(*button);
auto* container = new (std::nothrow)
ui::Container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
if (!container) return err(ENOMEM);
button->set_widget(*container);
button->set_action([=] { os::Process::spawn(args[0], args, false); });
auto image = TRY(ui::ImageWidget::load(icon));
container->set_widget(*image);
image.leak();
return {};
}
Result<int> luna_main(int argc, char** argv)
{
ui::App app;
@ -50,17 +30,34 @@ Result<int> luna_main(int argc, char** argv)
app.set_main_window(window);
window->set_background(TASKBAR_COLOR);
ui::HorizontalLayout layout(ui::Margins { 0, 0, 0, 0 }, ui::AdjustHeight::Yes, ui::AdjustWidth::No);
ui::HorizontalLayout layout(ui::AdjustHeight::Yes, ui::AdjustWidth::No);
window->set_main_widget(layout);
StringView terminal_command[] = { "/usr/bin/terminal" };
TRY(create_widget_group_for_app(layout, { terminal_command, 1 }, "/usr/share/icons/32x32/app-terminal.tga"));
ui::Button term_button({ 0, 0, 50, 50 });
layout.add_widget(term_button);
StringView about_command[] = { "/usr/bin/about" };
TRY(create_widget_group_for_app(layout, { about_command, 1 }, "/usr/share/icons/32x32/app-about.tga"));
ui::Container term_container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
term_button.set_widget(term_container);
term_button.set_action([] {
StringView args[] = { "/usr/bin/terminal" };
os::Process::spawn("/usr/bin/terminal", Slice<StringView> { args, 1 }, false);
});
StringView gol_command[] = { "/usr/bin/gol" };
TRY(create_widget_group_for_app(layout, { gol_command, 1 }, "/usr/share/icons/32x32/app-gol.tga"));
auto term_image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/app-terminal.tga"));
term_container.set_widget(*term_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);
window->draw();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -49,13 +49,6 @@ template <typename T> class OwnedPtr
return m_ptr;
}
T* leak()
{
T* ptr = m_ptr;
m_ptr = nullptr;
return ptr;
}
T* operator->() const
{
return m_ptr;

View File

@ -1,68 +0,0 @@
#pragma once
#include <luna/Heap.h>
namespace ui
{
class Action
{
public:
Action()
{
m_action = nullptr;
}
Action(Action&& other) : m_action(other.m_action)
{
other.m_action = nullptr;
}
template <typename T> Action(T&& function)
{
m_action = new (std::nothrow) ActionImpl<T>(move(function));
}
template <typename T> Action& operator=(T&& function)
{
if (m_action) delete m_action;
m_action = new (std::nothrow) ActionImpl<T>(move(function));
return *this;
}
void operator()()
{
check(m_action);
return m_action->call();
}
~Action()
{
if (m_action) delete m_action;
}
private:
class ActionBase
{
public:
virtual void call() = 0;
virtual ~ActionBase() = default;
};
template <typename T> class ActionImpl final : public ActionBase
{
public:
ActionImpl(T&& function) : m_function(move(function))
{
}
void call()
{
return m_function();
}
private:
T m_function;
};
ActionBase* m_action;
};
}

View File

@ -8,7 +8,6 @@
*/
#pragma once
#include <ui/Action.h>
#include <ui/Widget.h>
namespace ui
@ -19,7 +18,7 @@ namespace ui
Button(Rect rect);
void set_widget(Widget& widget);
void set_action(Action&& action);
void set_action(void (*action)(void));
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -32,6 +31,6 @@ namespace ui
bool m_hovered { false };
bool m_clicked { false };
Widget* m_child;
Action m_action;
void (*m_action)(void);
};
}

View File

@ -25,19 +25,10 @@ namespace ui
Yes
};
struct Margins
{
int left;
int right;
int top;
int bottom;
};
class HorizontalLayout final : public Widget
{
public:
HorizontalLayout(Margins margins = Margins { 0, 0, 0, 0 }, AdjustHeight adjust_height = AdjustHeight::Yes,
AdjustWidth adjust_width = AdjustWidth::Yes);
HorizontalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -51,7 +42,6 @@ namespace ui
private:
Vector<Widget*> m_widgets;
Margins m_margins;
AdjustHeight m_adjust_height;
AdjustWidth m_adjust_width;
int m_used_width { 0 };
@ -60,8 +50,7 @@ namespace ui
class VerticalLayout final : public Widget
{
public:
VerticalLayout(Margins margins = Margins { 0, 0, 0, 0 }, AdjustHeight adjust_height = AdjustHeight::Yes,
AdjustWidth adjust_width = AdjustWidth::Yes);
VerticalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -75,7 +64,6 @@ namespace ui
private:
Vector<Widget*> m_widgets;
Margins m_margins;
AdjustHeight m_adjust_height;
AdjustWidth m_adjust_width;
int m_used_height { 0 };

View File

@ -24,9 +24,9 @@ namespace ui
widget.set_parent(this);
}
void Button::set_action(Action&& action)
void Button::set_action(void (*action)(void))
{
m_action = move(action);
m_action = action;
}
Result<EventResult> Button::handle_mouse_move(Point position)

View File

@ -12,8 +12,8 @@
namespace ui
{
HorizontalLayout::HorizontalLayout(Margins margins, AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_margins(margins), m_adjust_height(adjust_height), m_adjust_width(adjust_width)
HorizontalLayout::HorizontalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
{
}
@ -95,8 +95,8 @@ namespace ui
if (m_adjust_width == AdjustWidth::No)
{
widget.rect().pos.x = m_rect.pos.x + m_used_width + m_margins.left;
m_used_width += m_margins.left + widget.rect().width + m_margins.right;
widget.rect().pos.x = m_rect.pos.x + m_used_width;
m_used_width += widget.rect().width;
}
else
{
@ -104,27 +104,24 @@ namespace ui
div_t result = div(m_rect.width, (int)m_widgets.size());
for (auto w : m_widgets)
{
w->rect().pos.x = m_rect.pos.x + used_width + m_margins.left;
w->rect().width = result.quot - (m_margins.left + m_margins.right);
w->rect().pos.x = m_rect.pos.x + used_width;
w->rect().width = result.quot;
used_width += result.quot;
}
m_widgets[m_widgets.size() - 1]->rect().width += result.rem;
}
widget.rect().pos.y = m_rect.pos.y + m_margins.top;
widget.rect().pos.y = m_rect.pos.y;
if (m_adjust_height == AdjustHeight::Yes)
{
widget.rect().height = m_rect.height - (m_margins.top + m_margins.bottom);
}
if (m_adjust_height == AdjustHeight::Yes) { widget.rect().height = m_rect.height; }
widget.set_parent(this);
return {};
}
VerticalLayout::VerticalLayout(Margins margins, AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_margins(margins), m_adjust_height(adjust_height), m_adjust_width(adjust_width)
VerticalLayout::VerticalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
{
}
@ -206,8 +203,8 @@ namespace ui
if (m_adjust_height == AdjustHeight::No)
{
widget.rect().pos.y = m_rect.pos.y + m_used_height + m_margins.top;
m_used_height += m_margins.top + widget.rect().height + m_margins.bottom;
widget.rect().pos.y = m_rect.pos.y + m_used_height;
m_used_height += widget.rect().height;
}
else
{
@ -215,19 +212,16 @@ namespace ui
div_t result = div(m_rect.height, (int)m_widgets.size());
for (auto w : m_widgets)
{
w->rect().pos.y = m_rect.pos.y + used_height + m_margins.top;
w->rect().height = result.quot - (m_margins.top + m_margins.bottom);
w->rect().pos.y = m_rect.pos.y + used_height;
w->rect().height = result.quot;
used_height += result.quot;
}
m_widgets[m_widgets.size() - 1]->rect().height += result.rem;
}
widget.rect().pos.x = m_rect.pos.x + m_margins.left;
widget.rect().pos.x = m_rect.pos.x;
if (m_adjust_width == AdjustWidth::Yes)
{
widget.rect().width = m_rect.width - (m_margins.left + m_margins.right);
}
if (m_adjust_width == AdjustWidth::Yes) { widget.rect().width = m_rect.width; }
widget.set_parent(this);