1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:57:34 +00:00

LibGUI+WindowServer: Add support for GWidget tooltips.

Any GWidget can have a tooltip and it will automatically pop up below the
center of the widget when hovered. GActions added to GToolBars will use
the action text() as their tooltip automagically. :^)
This commit is contained in:
Andreas Kling 2019-04-08 18:58:44 +02:00
parent 3e175c9a96
commit 7f2eeb0b35
15 changed files with 136 additions and 27 deletions

View file

@ -2,6 +2,10 @@
#include <LibGUI/GEventLoop.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GPainter.h>
#include <WindowServer/WSAPITypes.h>
static GApplication* s_the;
@ -65,3 +69,46 @@ GAction* GApplication::action_for_key_event(const GKeyEvent& event)
return nullptr;
return (*it).value;
}
class GApplication::TooltipWindow final : public GWindow {
public:
TooltipWindow()
{
set_title("Tooltip");
set_window_type(GWindowType::Tooltip);
m_label = new GLabel;
m_label->set_background_color(Color::from_rgb(0xdac7b5));
m_label->set_fill_with_background_color(true);
m_label->set_frame_thickness(1);
m_label->set_frame_shape(GFrame::Shape::Container);
m_label->set_frame_shadow(GFrame::Shadow::Plain);
set_main_widget(m_label);
}
void set_tooltip(const String& tooltip)
{
// FIXME: Add some kind of GLabel auto-sizing feature.
int text_width = m_label->font().width(tooltip);
set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8);
m_label->set_text(tooltip);
}
GLabel* m_label { nullptr };
};
void GApplication::show_tooltip(const String& tooltip, const Point& screen_location)
{
if (!m_tooltip_window) {
m_tooltip_window = new TooltipWindow;
m_tooltip_window->set_double_buffering_enabled(false);
}
m_tooltip_window->set_tooltip(tooltip);
m_tooltip_window->move_to(screen_location);
m_tooltip_window->show();
}
void GApplication::hide_tooltip()
{
if (m_tooltip_window)
m_tooltip_window->hide();
}

View file

@ -9,6 +9,7 @@ class GAction;
class GKeyEvent;
class GEventLoop;
class GMenuBar;
class Point;
class GApplication {
public:
@ -25,8 +26,13 @@ public:
void register_shortcut_action(Badge<GAction>, GAction&);
void unregister_shortcut_action(Badge<GAction>, GAction&);
void show_tooltip(const String&, const Point& screen_location);
void hide_tooltip();
private:
OwnPtr<GEventLoop> m_event_loop;
OwnPtr<GMenuBar> m_menubar;
HashMap<GShortcut, GAction*> m_shortcut_actions;
class TooltipWindow;
TooltipWindow* m_tooltip_window { nullptr };
};

View file

@ -7,8 +7,8 @@ class GraphicsBitmap;
class GLabel final : public GFrame {
public:
explicit GLabel(GWidget* parent);
GLabel(const String& text, GWidget* parent);
explicit GLabel(GWidget* parent = nullptr);
GLabel(const String& text, GWidget* parent = nullptr);
virtual ~GLabel() override;
String text() const { return m_text; }

View file

@ -26,6 +26,7 @@ void GToolBar::add_action(Retained<GAction>&& action)
item->action = move(action);
auto* button = new GButton(this);
button->set_tooltip(item->action->text());
if (item->action->icon())
button->set_icon(item->action->icon());
else

View file

@ -6,6 +6,7 @@
#include <AK/Assertions.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GApplication.h>
#include <unistd.h>
@ -79,9 +80,9 @@ void GWidget::event(GEvent& event)
case GEvent::MouseUp:
return handle_mouseup_event(static_cast<GMouseEvent&>(event));
case GEvent::Enter:
return enter_event(event);
return handle_enter_event(event);
case GEvent::Leave:
return leave_event(event);
return handle_leave_event(event);
default:
return GObject::event(event);
}
@ -177,6 +178,19 @@ void GWidget::handle_mousedown_event(GMouseEvent& event)
mousedown_event(event);
}
void GWidget::handle_enter_event(GEvent& event)
{
if (has_tooltip())
GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
enter_event(event);
}
void GWidget::handle_leave_event(GEvent& event)
{
GApplication::the().hide_tooltip();
leave_event(event);
}
void GWidget::click_event(GMouseEvent&)
{
}
@ -261,6 +275,11 @@ Rect GWidget::window_relative_rect() const
return rect;
}
Rect GWidget::screen_relative_rect() const
{
return window_relative_rect().translated(window()->position());
}
GWidget::HitTestResult GWidget::hit_test(int x, int y)
{
// FIXME: Care about z-order.

View file

@ -34,6 +34,10 @@ public:
Size preferred_size() const { return m_preferred_size; }
void set_preferred_size(const Size&);
bool has_tooltip() const { return !m_tooltip.is_empty(); }
String tooltip() const { return m_tooltip; }
void set_tooltip(const String& tooltip) { m_tooltip = tooltip; }
virtual void event(GEvent&) override;
virtual void paint_event(GPaintEvent&);
virtual void resize_event(GResizeEvent&);
@ -56,6 +60,7 @@ public:
Point relative_position() const { return m_relative_rect.location(); }
Rect window_relative_rect() const;
Rect screen_relative_rect() const;
int x() const { return m_relative_rect.x(); }
int y() const { return m_relative_rect.y(); }
@ -149,6 +154,8 @@ private:
void handle_resize_event(GResizeEvent&);
void handle_mousedown_event(GMouseEvent&);
void handle_mouseup_event(GMouseEvent&);
void handle_enter_event(GEvent&);
void handle_leave_event(GEvent&);
void do_layout();
GWindow* m_window { nullptr };
@ -158,6 +165,7 @@ private:
Color m_background_color;
Color m_foreground_color;
RetainPtr<Font> m_font;
String m_tooltip;
SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
SizePolicy m_vertical_size_policy { SizePolicy::Fill };

View file

@ -83,7 +83,11 @@ void GWindow::hide()
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::DestroyWindow;
request.window_id = m_window_id;
GEventLoop::current().post_message_to_server(request);
GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyWindow);
m_window_id = 0;
m_pending_paint_event_rects.clear();
m_back_bitmap = nullptr;
m_front_bitmap = nullptr;
}
void GWindow::set_title(const String& title)
@ -139,6 +143,8 @@ void GWindow::set_rect(const Rect& a_rect)
request.window_id = m_window_id;
request.window.rect = a_rect;
GEventLoop::current().post_message_to_server(request);
if (m_main_widget)
m_main_widget->resize(a_rect.size());
}
void GWindow::set_window_type(GWindowType window_type)
@ -193,6 +199,8 @@ void GWindow::event(GEvent& event)
}
if (event.is_paint_event()) {
if (!m_window_id)
return;
m_pending_paint_event_rects.clear();
if (!m_main_widget)
return;

View file

@ -6,4 +6,5 @@ enum class GWindowType {
Menu,
WindowSwitcher,
Taskbar,
Tooltip,
};