libui+taskbar: Make Buttons use Actions and clean up taskbar code

This commit is contained in:
apio 2023-09-27 18:52:17 +02:00
parent 3d46e56386
commit 041d15a547
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 30 additions and 29 deletions

View File

@ -15,6 +15,26 @@ 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;
@ -30,34 +50,14 @@ 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::AdjustHeight::Yes, ui::AdjustWidth::No); ui::HorizontalLayout layout(ui::Margins { 0, 0, 0, 0 }, ui::AdjustHeight::Yes, ui::AdjustWidth::No);
window->set_main_widget(layout); window->set_main_widget(layout);
ui::Button term_button({ 0, 0, 50, 50 }); StringView terminal_command[] = { "/usr/bin/terminal" };
layout.add_widget(term_button); TRY(create_widget_group_for_app(layout, { terminal_command, 1 }, "/usr/share/icons/32x32/app-terminal.tga"));
ui::Container term_container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center); StringView about_command[] = { "/usr/bin/about" };
term_button.set_widget(term_container); TRY(create_widget_group_for_app(layout, { about_command, 1 }, "/usr/share/icons/32x32/app-about.tga"));
term_button.set_action([] {
StringView args[] = { "/usr/bin/terminal" };
os::Process::spawn("/usr/bin/terminal", Slice<StringView> { args, 1 }, false);
});
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(); window->draw();

View File

@ -8,6 +8,7 @@
*/ */
#pragma once #pragma once
#include <ui/Action.h>
#include <ui/Widget.h> #include <ui/Widget.h>
namespace ui namespace ui
@ -18,7 +19,7 @@ namespace ui
Button(Rect rect); Button(Rect rect);
void set_widget(Widget& widget); void set_widget(Widget& widget);
void set_action(void (*action)(void)); void set_action(Action&& action);
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;
@ -31,6 +32,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;
void (*m_action)(void); Action m_action;
}; };
} }

View File

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