mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
Launcher: Factor the app buttons into a LaunchButton class.
Added some LibGUI helpers while I'm at it.
This commit is contained in:
parent
bf766fc12c
commit
6d7f000ffc
Notes:
sideshowbarker
2024-07-19 15:50:00 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6d7f000ffc0
5 changed files with 39 additions and 36 deletions
Binary file not shown.
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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(); }
|
||||
|
||||
|
|
Loading…
Reference in a new issue