1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

guitest2: Add a simple launcher so I can easily spawn more Terminals.

Also update GButton coding style.
This commit is contained in:
Andreas Kling 2019-01-21 00:31:11 +01:00
parent 3271c115e2
commit 6c4f1bad09
3 changed files with 45 additions and 12 deletions

View file

@ -11,7 +11,7 @@ GButton::~GButton()
{ {
} }
void GButton::setCaption(String&& caption) void GButton::set_caption(String&& caption)
{ {
if (caption == m_caption) if (caption == m_caption)
return; return;
@ -32,7 +32,7 @@ void GButton::paintEvent(GPaintEvent&)
painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black); painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black);
painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black); painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
if (m_beingPressed) { if (m_being_pressed) {
// Base // Base
painter.fill_rect({ 1, 1, width() - 2, height() - 2 }, buttonColor); painter.fill_rect({ 1, 1, width() - 2, height() - 2 }, buttonColor);
@ -58,7 +58,7 @@ void GButton::paintEvent(GPaintEvent&)
if (!caption().is_empty()) { if (!caption().is_empty()) {
auto textRect = rect(); auto textRect = rect();
if (m_beingPressed) if (m_being_pressed)
textRect.move_by(1, 1); textRect.move_by(1, 1);
painter.draw_text(textRect, caption(), Painter::TextAlignment::Center, Color::Black); painter.draw_text(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
} }
@ -68,7 +68,7 @@ void GButton::mouseDownEvent(GMouseEvent& event)
{ {
dbgprintf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); dbgprintf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
m_beingPressed = true; m_being_pressed = true;
update(); update();
GWidget::mouseDownEvent(event); GWidget::mouseDownEvent(event);
@ -78,12 +78,12 @@ void GButton::mouseUpEvent(GMouseEvent& event)
{ {
dbgprintf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); dbgprintf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
m_beingPressed = false; m_being_pressed = false;
update(); update();
GWidget::mouseUpEvent(event); GWidget::mouseUpEvent(event);
if (onClick) if (on_click)
onClick(*this); on_click(*this);
} }

View file

@ -10,9 +10,9 @@ public:
virtual ~GButton() override; virtual ~GButton() override;
String caption() const { return m_caption; } String caption() const { return m_caption; }
void setCaption(String&&); void set_caption(String&&);
Function<void(GButton&)> onClick; Function<void(GButton&)> on_click;
private: private:
virtual void paintEvent(GPaintEvent&) override; virtual void paintEvent(GPaintEvent&) override;
@ -22,6 +22,6 @@ private:
virtual const char* class_name() const override { return "GButton"; } virtual const char* class_name() const override { return "GButton"; }
String m_caption; String m_caption;
bool m_beingPressed { false }; bool m_being_pressed { false };
}; };

View file

@ -11,15 +11,20 @@
#include <LibGUI/GWindow.h> #include <LibGUI/GWindow.h>
#include <LibGUI/GWidget.h> #include <LibGUI/GWidget.h>
#include <LibGUI/GLabel.h> #include <LibGUI/GLabel.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GEventLoop.h> #include <LibGUI/GEventLoop.h>
static GWindow* make_font_test_window(); static GWindow* make_font_test_window();
static GWindow* make_launcher_window();
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
GEventLoop loop; GEventLoop loop;
auto* window = make_font_test_window(); auto* font_test_window = make_font_test_window();
window->show(); font_test_window->show();
auto* launcher_window = make_launcher_window();
launcher_window->show();
return loop.exec(); return loop.exec();
} }
@ -51,3 +56,31 @@ GWindow* make_font_test_window()
return window; return window;
} }
GWindow* make_launcher_window()
{
auto* window = new GWindow;
window->set_title("Launcher");
window->set_rect({ 100, 400, 80, 200 });
auto* widget = new GWidget;
window->set_main_widget(widget);
widget->setWindowRelativeRect({ 0, 0, 80, 200 });
auto* label = new GLabel(widget);
label->setWindowRelativeRect({ 0, 0, 80, 20 });
label->setText("Apps");
auto* button = new GButton(widget);
button->setWindowRelativeRect({ 5, 20, 70, 20 });
button->set_caption("Terminal");
button->on_click = [] (GButton&) {
if (!fork()) {
execve("/bin/Terminal", nullptr, nullptr);
ASSERT_NOT_REACHED();
}
};
return window;
}