Launcher: Factor the app buttons into a LaunchButton class.

Added some LibGUI helpers while I'm at it.
This commit is contained in:
Andreas Kling 2019-02-08 00:14:37 +01:00
parent bf766fc12c
commit 6d7f000ffc
Notes: sideshowbarker 2024-07-19 15:50:00 +09:00
5 changed files with 39 additions and 36 deletions

Binary file not shown.

View file

@ -3,8 +3,11 @@
#include <LibGUI/GWidget.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GEventLoop.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
static GWindow* make_launcher_window();
@ -29,51 +32,46 @@ int main(int, char**)
return loop.exec();
}
class LauncherButton final : public GButton {
public:
LauncherButton(const String& icon_path, const String& exec_path, GWidget* parent)
: GButton(parent)
, m_executable_path(exec_path)
{
set_icon(GraphicsBitmap::load_from_file(icon_path, { 32, 32 }));
resize(50, 50);
on_click = [this] (GButton&) {
pid_t child_pid = fork();
if (!child_pid) {
int rc = execl(m_executable_path.characters(), m_executable_path.characters(), nullptr);
if (rc < 0)
perror("execl");
}
};
}
virtual ~LauncherButton() { }
private:
String m_executable_path;
};
GWindow* make_launcher_window()
{
auto* window = new GWindow;
window->set_title("Launcher");
window->set_rect({ 50, 50, 300, 60 });
window->set_rect(50, 50, 300, 60);
auto* widget = new GWidget;
window->set_main_widget(widget);
widget->set_relative_rect({ 0, 0, 300, 60 });
auto* terminal_button = new GButton(widget);
terminal_button->set_relative_rect({ 5, 5, 50, 50 });
terminal_button->set_icon(GraphicsBitmap::load_from_file("/res/icons/Terminal.rgb", { 32, 32 }));
auto* terminal_button = new LauncherButton("/res/icons/Terminal.rgb", "/bin/Terminal", widget);
terminal_button->move_to(5, 5);
terminal_button->on_click = [] (GButton&) {
pid_t child_pid = fork();
if (!child_pid) {
execve("/bin/Terminal", nullptr, nullptr);
ASSERT_NOT_REACHED();
}
};
auto* font_editor_button = new LauncherButton("/res/icons/FontEditor.rgb", "/bin/FontEditor", widget);
font_editor_button->move_to(60, 5);
auto* font_editor_button = new GButton(widget);
font_editor_button->set_relative_rect({ 60, 5, 50, 50 });
font_editor_button->set_icon(GraphicsBitmap::load_from_file("/res/icons/FontEditor.rgb", { 32, 32 }));
font_editor_button->on_click = [] (GButton&) {
pid_t child_pid = fork();
if (!child_pid) {
execve("/bin/FontEditor", nullptr, nullptr);
ASSERT_NOT_REACHED();
}
};
auto* guitest_editor_button = new GButton(widget);
guitest_editor_button->set_relative_rect({ 115, 5, 50, 50 });
guitest_editor_button->set_icon(GraphicsBitmap::load_from_file("/res/icons/generic.rgb", { 32, 32 }));
guitest_editor_button->on_click = [] (GButton&) {
pid_t child_pid = fork();
if (!child_pid) {
execve("/bin/guitest", nullptr, nullptr);
ASSERT_NOT_REACHED();
}
};
auto* guitest_editor_button = new LauncherButton("/res/icons/generic.rgb", "/bin/guitest", widget);
guitest_editor_button->move_to(115, 5);
return window;
}

View file

@ -5,7 +5,7 @@
#include <AK/Function.h>
#include <SharedGraphics/GraphicsBitmap.h>
class GButton final : public GWidget {
class GButton : public GWidget {
public:
explicit GButton(GWidget* parent);
virtual ~GButton() override;

View file

@ -56,7 +56,11 @@ public:
virtual const char* class_name() const override { return "GWidget"; }
void set_relative_rect(const Rect&);
void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
void move_to(int x, int y) { move_to({ x, y }); }
void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
void resize(int width, int height) { resize({ width, height }); }
Color background_color() const { return m_background_color; }
Color foreground_color() const { return m_foreground_color; }

View file

@ -27,6 +27,7 @@ public:
Rect rect() const;
void set_rect(const Rect&);
void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
Point position() const { return rect().location(); }