Luna/apps/taskbar.cpp

74 lines
2.3 KiB
C++
Raw Normal View History

2023-09-11 17:15:26 +00:00
#include <os/File.h>
#include <os/IPC.h>
2023-08-15 13:33:44 +00:00
#include <os/Process.h>
#include <os/ipc/Launcher.h>
#include <signal.h>
#include <sys/wait.h>
#include <ui/App.h>
2023-08-15 13:33:44 +00:00
#include <ui/Button.h>
#include <ui/Container.h>
#include <ui/Image.h>
#include <ui/Layout.h>
static constexpr ui::Color TASKBAR_COLOR = ui::Color::from_rgb(83, 83, 83);
static OwnedPtr<os::IPC::Client> launcher_client;
void sigchld_handler(int)
{
wait(nullptr);
}
Result<void> create_widget_group_for_app(ui::HorizontalLayout& layout, StringView path, 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::Launcher::LaunchDetachedRequest request;
SET_IPC_STRING(request.command, path.chars());
launcher_client->send_async(request);
});
auto image = TRY(ui::ImageWidget::load(icon));
container->set_widget(*image);
image.leak();
return {};
}
Result<int> luna_main(int, char**)
{
ui::App app;
TRY(app.init("/tmp/wsys.sock"));
TRY(os::EventLoop::the().register_signal_handler(SIGCHLD, sigchld_handler));
launcher_client = TRY(os::IPC::Client::connect("/tmp/launch.sock", false));
ui::Rect screen = app.screen_rect();
ui::Rect bar = ui::Rect { ui::Point { 0, screen.height - 50 }, screen.width, 50 };
auto window = TRY(ui::Window::create(bar, ui::WindowType::System));
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);
window->set_main_widget(layout);
TRY(create_widget_group_for_app(layout, "/usr/bin/terminal", "/usr/share/icons/32x32/app-terminal.tga"));
TRY(create_widget_group_for_app(layout, "/usr/bin/about", "/usr/share/icons/32x32/app-about.tga"));
TRY(create_widget_group_for_app(layout, "/usr/bin/gol", "/usr/share/icons/32x32/app-gol.tga"));
TRY(create_widget_group_for_app(layout, "/usr/bin/clock", "/usr/share/icons/32x32/app-clock.tga"));
2023-10-09 20:05:30 +00:00
return app.run();
}