mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
LibGUI: Add ability to assign a menu to a GUI::Button
When a button has a menu, it opens the menu on mousedown and the menu gains input focus immediately. While the menu is open, the button is painted in a pressed state.
This commit is contained in:
parent
9b740f218b
commit
15349a3712
2 changed files with 36 additions and 5 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -28,6 +28,7 @@
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
#include <LibGUI/ActionGroup.h>
|
#include <LibGUI/ActionGroup.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
#include <LibGUI/Menu.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/Font.h>
|
#include <LibGfx/Font.h>
|
||||||
#include <LibGfx/FontDatabase.h>
|
#include <LibGfx/FontDatabase.h>
|
||||||
|
@ -62,7 +63,9 @@ void Button::paint_event(PaintEvent& event)
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
|
|
||||||
Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled(), is_focused());
|
bool paint_pressed = is_being_pressed() || (m_menu && m_menu->is_visible());
|
||||||
|
|
||||||
|
Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, paint_pressed, is_hovered(), is_checked(), is_enabled(), is_focused());
|
||||||
|
|
||||||
if (text().is_empty() && !m_icon)
|
if (text().is_empty() && !m_icon)
|
||||||
return;
|
return;
|
||||||
|
@ -72,9 +75,9 @@ void Button::paint_event(PaintEvent& event)
|
||||||
if (m_icon && !text().is_empty())
|
if (m_icon && !text().is_empty())
|
||||||
icon_location.set_x(content_rect.x());
|
icon_location.set_x(content_rect.x());
|
||||||
|
|
||||||
if (is_being_pressed() || is_checked())
|
if (paint_pressed || is_checked()) {
|
||||||
painter.translate(1, 1);
|
painter.translate(1, 1);
|
||||||
else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::CoolBar) {
|
} else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::CoolBar) {
|
||||||
auto shadow_color = palette().button().darkened(0.7f);
|
auto shadow_color = palette().button().darkened(0.7f);
|
||||||
painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
|
painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
|
||||||
return shadow_color;
|
return shadow_color;
|
||||||
|
@ -167,4 +170,28 @@ bool Button::is_uncheckable() const
|
||||||
return m_action->group()->is_unchecking_allowed();
|
return m_action->group()->is_unchecking_allowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Button::set_menu(RefPtr<GUI::Menu> menu)
|
||||||
|
{
|
||||||
|
if (m_menu == menu)
|
||||||
|
return;
|
||||||
|
if (m_menu)
|
||||||
|
m_menu->on_visibility_change = nullptr;
|
||||||
|
m_menu = menu;
|
||||||
|
if (m_menu) {
|
||||||
|
m_menu->on_visibility_change = [&](bool) {
|
||||||
|
update();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Button::mousedown_event(MouseEvent& event)
|
||||||
|
{
|
||||||
|
if (m_menu) {
|
||||||
|
m_menu->popup(screen_relative_rect().top_left());
|
||||||
|
update();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AbstractButton::mousedown_event(event);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -63,12 +63,16 @@ public:
|
||||||
int icon_spacing() const { return m_icon_spacing; }
|
int icon_spacing() const { return m_icon_spacing; }
|
||||||
void set_icon_spacing(int spacing) { m_icon_spacing = spacing; }
|
void set_icon_spacing(int spacing) { m_icon_spacing = spacing; }
|
||||||
|
|
||||||
|
void set_menu(RefPtr<GUI::Menu>);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Button(String text = {});
|
explicit Button(String text = {});
|
||||||
|
virtual void mousedown_event(MouseEvent&) override;
|
||||||
virtual void paint_event(PaintEvent&) override;
|
virtual void paint_event(PaintEvent&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefPtr<Gfx::Bitmap> m_icon;
|
RefPtr<Gfx::Bitmap> m_icon;
|
||||||
|
RefPtr<GUI::Menu> m_menu;
|
||||||
Gfx::ButtonStyle m_button_style { Gfx::ButtonStyle::Normal };
|
Gfx::ButtonStyle m_button_style { Gfx::ButtonStyle::Normal };
|
||||||
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
||||||
WeakPtr<Action> m_action;
|
WeakPtr<Action> m_action;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue