2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-04-17 19:22:07 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-02-26 20:50:04 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-12-09 14:52:38 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-04-17 19:22:07 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
2021-04-17 19:22:07 +03:00
|
|
|
#include <LibGUI/Application.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-30 05:52:27 +03:00
|
|
|
#include <LibGUI/SeparatorWidget.h>
|
2021-04-13 17:18:20 +03:00
|
|
|
#include <LibGUI/Toolbar.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
REGISTER_WIDGET(GUI, Toolbar)
|
2021-01-03 02:30:13 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
Toolbar::Toolbar(Orientation orientation, int button_size)
|
2020-12-30 05:52:27 +03:00
|
|
|
: m_orientation(orientation)
|
|
|
|
, m_button_size(button_size)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2022-07-27 15:03:33 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("collapsible", is_collapsible, set_collapsible);
|
2022-10-05 19:19:18 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("grouped", is_grouped, set_grouped);
|
2022-07-27 15:03:33 +03:00
|
|
|
|
2022-07-27 14:45:54 +03:00
|
|
|
if (m_orientation == Orientation::Horizontal)
|
|
|
|
set_fixed_height(button_size);
|
|
|
|
else
|
|
|
|
set_fixed_width(button_size);
|
|
|
|
|
2023-02-17 00:07:06 +03:00
|
|
|
set_layout<BoxLayout>(orientation, GUI::Margins { 2, 2, 2, 2 }, 0);
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
class ToolbarButton final : public Button {
|
|
|
|
C_OBJECT(ToolbarButton);
|
2020-10-30 18:47:00 +03:00
|
|
|
|
|
|
|
public:
|
2022-02-26 20:50:04 +03:00
|
|
|
virtual ~ToolbarButton() override = default;
|
2020-10-30 18:47:00 +03:00
|
|
|
|
|
|
|
private:
|
2021-04-13 17:18:20 +03:00
|
|
|
explicit ToolbarButton(Action& action)
|
2020-10-30 18:47:00 +03:00
|
|
|
{
|
|
|
|
if (action.group() && action.group()->is_exclusive())
|
|
|
|
set_exclusive(true);
|
|
|
|
set_action(action);
|
2023-08-23 21:14:42 +03:00
|
|
|
set_tooltip(tooltip(action));
|
2021-10-21 18:56:23 +03:00
|
|
|
set_focus_policy(FocusPolicy::NoFocus);
|
2020-10-30 18:47:00 +03:00
|
|
|
if (action.icon())
|
|
|
|
set_icon(action.icon());
|
|
|
|
else
|
2023-02-11 22:51:04 +03:00
|
|
|
set_text(String::from_deprecated_string(action.text()).release_value_but_fixme_should_propagate_errors());
|
2021-04-13 17:18:20 +03:00
|
|
|
set_button_style(Gfx::ButtonStyle::Coolbar);
|
2020-10-30 18:47:00 +03:00
|
|
|
}
|
2022-11-19 17:45:05 +03:00
|
|
|
|
2023-02-11 22:51:04 +03:00
|
|
|
virtual void set_text(String text) override
|
2022-11-19 17:45:05 +03:00
|
|
|
{
|
|
|
|
auto const* action = this->action();
|
|
|
|
VERIFY(action);
|
|
|
|
|
2023-08-23 21:14:42 +03:00
|
|
|
set_tooltip(tooltip(*action));
|
2022-11-19 17:45:05 +03:00
|
|
|
if (!action->icon())
|
2023-02-11 22:51:04 +03:00
|
|
|
Button::set_text(move(text));
|
2022-11-19 17:45:05 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 21:14:42 +03:00
|
|
|
String tooltip(Action const& action) const
|
2022-11-19 17:08:09 +03:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2023-03-04 20:50:28 +03:00
|
|
|
builder.append(action.tooltip());
|
2022-11-19 17:08:09 +03:00
|
|
|
if (action.shortcut().is_valid()) {
|
|
|
|
builder.append(" ("sv);
|
2022-12-06 04:12:49 +03:00
|
|
|
builder.append(action.shortcut().to_deprecated_string());
|
2022-11-19 17:08:09 +03:00
|
|
|
builder.append(')');
|
|
|
|
}
|
2023-08-23 21:14:42 +03:00
|
|
|
return MUST(builder.to_string());
|
2022-11-19 17:08:09 +03:00
|
|
|
}
|
2021-04-17 19:22:07 +03:00
|
|
|
|
|
|
|
virtual void enter_event(Core::Event& event) override
|
|
|
|
{
|
|
|
|
auto* app = Application::the();
|
|
|
|
if (app && action())
|
|
|
|
Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionEnter, *action()));
|
|
|
|
return Button::enter_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void leave_event(Core::Event& event) override
|
|
|
|
{
|
|
|
|
auto* app = Application::the();
|
|
|
|
if (app && action())
|
|
|
|
Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionLeave, *action()));
|
|
|
|
return Button::leave_event(event);
|
|
|
|
}
|
2020-10-30 18:47:00 +03:00
|
|
|
};
|
|
|
|
|
2023-08-15 11:36:28 +03:00
|
|
|
Button& Toolbar::add_action(Action& action)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2023-08-15 11:36:28 +03:00
|
|
|
auto item = make<Item>();
|
2020-02-02 17:07:41 +03:00
|
|
|
item->type = Item::Type::Action;
|
2019-07-11 16:55:54 +03:00
|
|
|
item->action = action;
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2023-08-15 11:36:28 +03:00
|
|
|
item->widget = add<ToolbarButton>(action);
|
2022-07-27 15:03:33 +03:00
|
|
|
item->widget->set_fixed_size(m_button_size, m_button_size);
|
2021-11-24 15:18:52 +03:00
|
|
|
|
2023-08-15 11:36:28 +03:00
|
|
|
m_items.append(move(item));
|
2023-03-06 19:16:25 +03:00
|
|
|
return *static_cast<Button*>(m_items.last()->widget.ptr());
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:23:59 +03:00
|
|
|
void Toolbar::add_separator()
|
|
|
|
{
|
2023-08-15 11:36:28 +03:00
|
|
|
auto item = make<Item>();
|
|
|
|
item->type = Item::Type::Separator;
|
|
|
|
item->widget = add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal);
|
|
|
|
m_items.append(move(item));
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
void Toolbar::paint_event(PaintEvent& event)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-05-10 23:58:52 +03:00
|
|
|
|
2020-09-18 10:49:51 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().button());
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2022-02-22 22:25:30 +03:00
|
|
|
Optional<UISize> Toolbar::calculated_preferred_size() const
|
|
|
|
{
|
|
|
|
if (m_orientation == Gfx::Orientation::Horizontal)
|
|
|
|
return { { SpecialDimension::Grow, SpecialDimension::Fit } };
|
|
|
|
else
|
|
|
|
return { { SpecialDimension::Fit, SpecialDimension::Grow } };
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:03:33 +03:00
|
|
|
Optional<UISize> Toolbar::calculated_min_size() const
|
|
|
|
{
|
|
|
|
if (m_collapsible) {
|
|
|
|
if (m_orientation == Gfx::Orientation::Horizontal)
|
|
|
|
return UISize(m_button_size, SpecialDimension::Shrink);
|
|
|
|
else
|
|
|
|
return UISize(SpecialDimension::Shrink, m_button_size);
|
|
|
|
}
|
|
|
|
VERIFY(layout());
|
|
|
|
return { layout()->min_size() };
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> Toolbar::create_overflow_objects()
|
|
|
|
{
|
2023-01-20 22:06:05 +03:00
|
|
|
m_overflow_action = Action::create("Overflow Menu", { Mod_Ctrl | Mod_Shift, Key_O }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/overflow-menu.png"sv)), [&](auto&) {
|
2023-05-22 01:41:18 +03:00
|
|
|
m_overflow_menu->popup(m_overflow_button->screen_relative_rect().bottom_left().moved_up(1), {}, m_overflow_button->rect());
|
2022-07-27 15:03:33 +03:00
|
|
|
});
|
2023-08-07 12:12:38 +03:00
|
|
|
m_overflow_action->set_status_tip("Show hidden toolbar actions"_string);
|
2022-07-27 15:03:33 +03:00
|
|
|
m_overflow_action->set_enabled(false);
|
|
|
|
|
2023-08-13 19:29:05 +03:00
|
|
|
add_spacer();
|
2022-07-27 15:03:33 +03:00
|
|
|
|
2023-08-15 11:36:28 +03:00
|
|
|
m_overflow_button = add_action(*m_overflow_action);
|
2022-07-27 15:03:33 +03:00
|
|
|
m_overflow_button->set_visible(false);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> Toolbar::update_overflow_menu()
|
|
|
|
{
|
|
|
|
if (!m_collapsible)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Optional<size_t> marginal_index {};
|
|
|
|
auto position { 0 };
|
|
|
|
auto is_horizontal { m_orientation == Gfx::Orientation::Horizontal };
|
2022-10-05 23:17:38 +03:00
|
|
|
auto margin { is_horizontal ? layout()->margins().horizontal_total() : layout()->margins().vertical_total() };
|
2022-10-05 23:26:29 +03:00
|
|
|
auto spacing { layout()->spacing() };
|
2022-07-27 15:03:33 +03:00
|
|
|
auto toolbar_size { is_horizontal ? width() : height() };
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_items.size() - 1; ++i) {
|
|
|
|
auto& item = m_items.at(i);
|
2023-03-06 19:16:25 +03:00
|
|
|
auto item_size = is_horizontal ? item->widget->width() : item->widget->height();
|
2022-10-05 23:17:38 +03:00
|
|
|
if (position + item_size + margin > toolbar_size) {
|
2022-07-27 15:03:33 +03:00
|
|
|
marginal_index = i;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-06 19:16:25 +03:00
|
|
|
item->widget->set_visible(true);
|
2022-10-05 23:26:29 +03:00
|
|
|
position += item_size + spacing;
|
2022-07-27 15:03:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!marginal_index.has_value()) {
|
|
|
|
if (m_overflow_action) {
|
|
|
|
m_overflow_action->set_enabled(false);
|
|
|
|
m_overflow_button->set_visible(false);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-05 23:17:38 +03:00
|
|
|
if (marginal_index.value() > 0) {
|
|
|
|
for (size_t i = marginal_index.value() - 1; i > 0; --i) {
|
|
|
|
auto& item = m_items.at(i);
|
2023-03-06 19:16:25 +03:00
|
|
|
auto item_size = is_horizontal ? item->widget->width() : item->widget->height();
|
2022-10-05 23:26:29 +03:00
|
|
|
if (position + m_button_size + spacing + margin <= toolbar_size)
|
2022-10-05 23:17:38 +03:00
|
|
|
break;
|
2023-03-06 19:16:25 +03:00
|
|
|
item->widget->set_visible(false);
|
2022-10-05 23:26:29 +03:00
|
|
|
position -= item_size + spacing;
|
2022-10-05 23:17:38 +03:00
|
|
|
marginal_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 19:19:18 +03:00
|
|
|
if (m_grouped) {
|
|
|
|
for (size_t i = marginal_index.value(); i > 0; --i) {
|
|
|
|
auto& item = m_items.at(i);
|
2023-03-06 19:16:25 +03:00
|
|
|
if (item->type == Item::Type::Separator)
|
2022-10-05 19:19:18 +03:00
|
|
|
break;
|
2023-03-06 19:16:25 +03:00
|
|
|
item->widget->set_visible(false);
|
2022-10-05 19:19:18 +03:00
|
|
|
marginal_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:03:33 +03:00
|
|
|
if (!m_overflow_action)
|
|
|
|
TRY(create_overflow_objects());
|
|
|
|
m_overflow_action->set_enabled(true);
|
|
|
|
m_overflow_button->set_visible(true);
|
|
|
|
|
2023-09-18 08:00:51 +03:00
|
|
|
m_overflow_menu = Menu::construct();
|
2022-07-27 15:03:33 +03:00
|
|
|
m_overflow_button->set_menu(m_overflow_menu);
|
|
|
|
|
|
|
|
for (size_t i = marginal_index.value(); i < m_items.size(); ++i) {
|
|
|
|
auto& item = m_items.at(i);
|
|
|
|
Item* peek_item;
|
|
|
|
if (i > 0) {
|
2023-03-06 19:16:25 +03:00
|
|
|
peek_item = m_items[i - 1];
|
2022-07-27 15:03:33 +03:00
|
|
|
if (peek_item->type == Item::Type::Separator)
|
|
|
|
peek_item->widget->set_visible(false);
|
|
|
|
}
|
|
|
|
if (i < m_items.size() - 1) {
|
2023-03-06 19:16:25 +03:00
|
|
|
item->widget->set_visible(false);
|
|
|
|
peek_item = m_items[i + 1];
|
|
|
|
if (item->action)
|
2023-08-14 11:14:27 +03:00
|
|
|
m_overflow_menu->add_action(*item->action);
|
2022-07-27 15:03:33 +03:00
|
|
|
}
|
2023-03-06 19:16:25 +03:00
|
|
|
if (item->action && peek_item->type == Item::Type::Separator)
|
2023-08-14 08:19:40 +03:00
|
|
|
m_overflow_menu->add_separator();
|
2022-07-27 15:03:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Toolbar::resize_event(GUI::ResizeEvent& event)
|
|
|
|
{
|
|
|
|
Widget::resize_event(event);
|
|
|
|
if (auto result = update_overflow_menu(); result.is_error())
|
|
|
|
warnln("Failed to update overflow menu");
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|