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); 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) Result<int> luna_main(int argc, char** argv)
{ {
ui::App app; ui::App app;
@ -50,17 +30,34 @@ Result<int> luna_main(int argc, char** argv)
app.set_main_window(window); app.set_main_window(window);
window->set_background(TASKBAR_COLOR); 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); window->set_main_widget(layout);
StringView terminal_command[] = { "/usr/bin/terminal" }; ui::Button term_button({ 0, 0, 50, 50 });
TRY(create_widget_group_for_app(layout, { terminal_command, 1 }, "/usr/share/icons/32x32/app-terminal.tga")); layout.add_widget(term_button);
StringView about_command[] = { "/usr/bin/about" }; ui::Container term_container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
TRY(create_widget_group_for_app(layout, { about_command, 1 }, "/usr/share/icons/32x32/app-about.tga")); 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" }; auto term_image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/app-terminal.tga"));
TRY(create_widget_group_for_app(layout, { gol_command, 1 }, "/usr/share/icons/32x32/app-gol.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(); 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; return m_ptr;
} }
T* leak()
{
T* ptr = m_ptr;
m_ptr = nullptr;
return ptr;
}
T* operator->() const T* operator->() const
{ {
return m_ptr; 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 #pragma once
#include <ui/Action.h>
#include <ui/Widget.h> #include <ui/Widget.h>
namespace ui namespace ui
@ -19,7 +18,7 @@ namespace ui
Button(Rect rect); Button(Rect rect);
void set_widget(Widget& widget); 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_move(Point position) override;
Result<EventResult> handle_mouse_leave() override; Result<EventResult> handle_mouse_leave() override;
@ -32,6 +31,6 @@ namespace ui
bool m_hovered { false }; bool m_hovered { false };
bool m_clicked { false }; bool m_clicked { false };
Widget* m_child; Widget* m_child;
Action m_action; void (*m_action)(void);
}; };
} }

View File

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

View File

@ -24,9 +24,9 @@ namespace ui
widget.set_parent(this); 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) Result<EventResult> Button::handle_mouse_move(Point position)

View File

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