2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2023-02-20 21:03:44 +03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2021-10-07 10:52:04 +03:00
|
|
|
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
|
2022-05-17 13:47:35 +03:00
|
|
|
* Copyright (c) 2022, Cameron Youell <cameronyouell@gmail.com>
|
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-07-04 06:39:24 +03:00
|
|
|
#include <AK/GenericShorthands.h>
|
2020-09-14 17:38:07 +03:00
|
|
|
#include <AK/JsonValue.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
2022-08-08 03:08:16 +03:00
|
|
|
#include <LibGUI/Desktop.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/TabWidget.h>
|
2020-10-30 18:25:52 +03:00
|
|
|
#include <LibGUI/Window.h>
|
2020-04-24 23:27:46 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2022-04-09 10:28:38 +03:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2019-05-05 02:31:02 +03:00
|
|
|
|
2021-01-03 02:30:13 +03:00
|
|
|
REGISTER_WIDGET(GUI, TabWidget)
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
TabWidget::TabWidget()
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2020-10-30 18:30:33 +03:00
|
|
|
set_focus_policy(FocusPolicy::NoFocus);
|
2020-10-30 14:21:52 +03:00
|
|
|
|
2021-06-15 15:28:39 +03:00
|
|
|
REGISTER_MARGINS_PROPERTY("container_margins", container_margins, set_container_margins);
|
2022-05-05 16:08:33 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("show_close_buttons", close_button_enabled, set_close_button_enabled);
|
|
|
|
REGISTER_BOOL_PROPERTY("show_tab_bar", is_bar_visible, set_bar_visible);
|
|
|
|
REGISTER_BOOL_PROPERTY("reorder_allowed", reorder_allowed, set_reorder_allowed);
|
2020-09-15 22:33:37 +03:00
|
|
|
REGISTER_BOOL_PROPERTY("uniform_tabs", uniform_tabs, set_uniform_tabs);
|
2022-05-17 13:47:35 +03:00
|
|
|
REGISTER_ENUM_PROPERTY("tab_position", this->tab_position, set_tab_position, TabPosition,
|
|
|
|
{ TabPosition::Top, "Top" },
|
|
|
|
{ TabPosition::Bottom, "Bottom" },
|
|
|
|
{ TabPosition::Left, "Left" },
|
|
|
|
{ TabPosition::Right, "Right" }, );
|
2023-11-06 03:29:02 +03:00
|
|
|
REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2022-03-19 00:57:05 +03:00
|
|
|
ErrorOr<void> TabWidget::try_add_widget(Widget& widget)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2024-03-28 19:17:07 +03:00
|
|
|
TRY(m_tabs.try_append({ widget.title(), nullptr, nullptr, &widget, false }));
|
2022-12-13 00:43:27 +03:00
|
|
|
TRY(try_add_child(widget));
|
2020-10-30 18:30:33 +03:00
|
|
|
update_focus_policy();
|
2021-04-09 23:23:14 +03:00
|
|
|
if (on_tab_count_change)
|
|
|
|
on_tab_count_change(m_tabs.size());
|
2022-12-05 16:00:35 +03:00
|
|
|
layout_relevant_change_occurred();
|
2021-11-24 15:13:30 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:57:05 +03:00
|
|
|
void TabWidget::add_widget(Widget& widget)
|
2021-11-24 15:13:30 +03:00
|
|
|
{
|
2022-03-19 00:57:05 +03:00
|
|
|
MUST(try_add_widget(widget));
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:02:24 +03:00
|
|
|
void TabWidget::remove_widget(Widget& widget)
|
|
|
|
{
|
2021-04-09 23:23:14 +03:00
|
|
|
VERIFY(widget.parent() == this);
|
2021-10-18 20:21:42 +03:00
|
|
|
auto tab_index = m_tabs.find_if([&widget](auto& entry) { return &widget == entry.widget; }).index();
|
|
|
|
|
|
|
|
auto is_active = active_widget() == &widget;
|
|
|
|
m_tabs.remove(tab_index);
|
2020-04-06 01:02:24 +03:00
|
|
|
remove_child(widget);
|
2021-10-18 20:21:42 +03:00
|
|
|
|
|
|
|
if (is_active && m_tabs.size() > 0) {
|
|
|
|
auto next_tab_index = tab_index >= m_tabs.size() ? m_tabs.size() - 1 : tab_index;
|
|
|
|
set_tab_index(next_tab_index);
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:30:33 +03:00
|
|
|
update_focus_policy();
|
2021-04-09 23:23:14 +03:00
|
|
|
if (on_tab_count_change)
|
|
|
|
on_tab_count_change(m_tabs.size());
|
2022-07-04 06:41:06 +03:00
|
|
|
|
2022-12-05 16:00:35 +03:00
|
|
|
layout_relevant_change_occurred();
|
2020-04-06 01:02:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 05:01:32 +03:00
|
|
|
void TabWidget::remove_all_tabs_except(Widget& widget)
|
|
|
|
{
|
|
|
|
VERIFY(widget.parent() == this);
|
|
|
|
set_active_widget(&widget);
|
|
|
|
m_tabs.remove_all_matching([this, &widget](auto& entry) {
|
|
|
|
bool is_other = &widget != entry.widget;
|
|
|
|
if (is_other)
|
|
|
|
remove_child(*entry.widget);
|
|
|
|
return is_other;
|
|
|
|
});
|
|
|
|
VERIFY(m_tabs.size() == 1);
|
|
|
|
update_focus_policy();
|
|
|
|
if (on_tab_count_change)
|
|
|
|
on_tab_count_change(1);
|
2022-07-04 06:41:06 +03:00
|
|
|
|
2022-12-05 16:00:35 +03:00
|
|
|
layout_relevant_change_occurred();
|
2021-08-06 05:01:32 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 18:30:33 +03:00
|
|
|
void TabWidget::update_focus_policy()
|
|
|
|
{
|
|
|
|
FocusPolicy policy;
|
|
|
|
if (is_bar_visible() && !m_tabs.is_empty())
|
|
|
|
policy = FocusPolicy::TabFocus;
|
|
|
|
else
|
|
|
|
policy = FocusPolicy::NoFocus;
|
|
|
|
set_focus_policy(policy);
|
|
|
|
}
|
2020-10-31 01:38:42 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::set_active_widget(Widget* widget)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
|
|
|
if (widget == m_active_widget)
|
|
|
|
return;
|
|
|
|
|
2020-10-31 01:38:42 +03:00
|
|
|
bool active_widget_had_focus = m_active_widget && m_active_widget->has_focus_within();
|
2020-10-30 13:34:19 +03:00
|
|
|
|
2019-05-05 02:31:02 +03:00
|
|
|
if (m_active_widget)
|
|
|
|
m_active_widget->set_visible(false);
|
|
|
|
m_active_widget = widget;
|
|
|
|
if (m_active_widget) {
|
|
|
|
m_active_widget->set_relative_rect(child_rect_for_size(size()));
|
2020-10-30 14:21:52 +03:00
|
|
|
if (active_widget_had_focus)
|
2020-10-30 13:34:19 +03:00
|
|
|
m_active_widget->set_focus(true);
|
2019-05-05 02:31:02 +03:00
|
|
|
m_active_widget->set_visible(true);
|
2021-08-30 21:12:48 +03:00
|
|
|
deferred_invoke([this] {
|
2020-04-06 01:02:24 +03:00
|
|
|
if (on_change)
|
|
|
|
on_change(*m_active_widget);
|
|
|
|
});
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2022-12-05 16:00:35 +03:00
|
|
|
layout_relevant_change_occurred();
|
2022-07-04 06:41:06 +03:00
|
|
|
|
2019-05-07 15:01:20 +03:00
|
|
|
update_bar();
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 03:01:23 +03:00
|
|
|
void TabWidget::set_tab_index(int index)
|
|
|
|
{
|
|
|
|
if (m_tabs.at(index).widget == m_active_widget)
|
|
|
|
return;
|
|
|
|
set_active_widget(m_tabs.at(index).widget);
|
|
|
|
|
|
|
|
update_bar();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::resize_event(ResizeEvent& event)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
|
|
|
if (!m_active_widget)
|
|
|
|
return;
|
|
|
|
m_active_widget->set_relative_rect(child_rect_for_size(event.size()));
|
|
|
|
}
|
|
|
|
|
2022-12-07 00:35:32 +03:00
|
|
|
Gfx::IntRect TabWidget::child_rect_for_size(Gfx::IntSize size) const
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect rect;
|
2019-07-28 18:39:04 +03:00
|
|
|
switch (m_tab_position) {
|
|
|
|
case TabPosition::Top:
|
2021-06-15 15:28:39 +03:00
|
|
|
rect = { { m_container_margins.left(), bar_height() + m_container_margins.top() }, { size.width() - m_container_margins.left() - m_container_margins.right(), size.height() - bar_height() - m_container_margins.top() - m_container_margins.bottom() } };
|
2019-10-23 20:51:09 +03:00
|
|
|
break;
|
2019-07-28 18:39:04 +03:00
|
|
|
case TabPosition::Bottom:
|
2021-06-15 15:28:39 +03:00
|
|
|
rect = { { m_container_margins.left(), m_container_margins.top() }, { size.width() - m_container_margins.left() - m_container_margins.right(), size.height() - bar_height() - m_container_margins.top() - m_container_margins.bottom() } };
|
2019-10-23 20:51:09 +03:00
|
|
|
break;
|
2022-05-17 13:47:35 +03:00
|
|
|
case TabPosition::Left:
|
|
|
|
rect = { { get_max_tab_width() + m_container_margins.left(), m_container_margins.top() }, { size.width() - get_max_tab_width() - m_container_margins.left() - m_container_margins.right(), size.height() - m_container_margins.top() - m_container_margins.bottom() } };
|
|
|
|
break;
|
|
|
|
case TabPosition::Right:
|
|
|
|
rect = { { m_container_margins.left(), m_container_margins.top() }, { size.width() - get_max_tab_width() - m_container_margins.left() - m_container_margins.right(), size.height() - m_container_margins.top() - m_container_margins.bottom() } };
|
|
|
|
break;
|
2019-07-28 18:39:04 +03:00
|
|
|
}
|
2019-10-23 20:51:09 +03:00
|
|
|
if (rect.is_empty())
|
|
|
|
return {};
|
|
|
|
return rect;
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::child_event(Core::ChildEvent& event)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2020-07-26 18:16:35 +03:00
|
|
|
if (!event.child() || !is<Widget>(*event.child()))
|
2020-02-02 17:07:41 +03:00
|
|
|
return Widget::child_event(event);
|
2021-06-24 20:53:42 +03:00
|
|
|
auto& child = verify_cast<Widget>(*event.child());
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.type() == Event::ChildAdded) {
|
2019-05-05 02:31:02 +03:00
|
|
|
if (!m_active_widget)
|
|
|
|
set_active_widget(&child);
|
|
|
|
else if (m_active_widget != &child)
|
|
|
|
child.set_visible(false);
|
2020-02-02 17:07:41 +03:00
|
|
|
} else if (event.type() == Event::ChildRemoved) {
|
2019-05-05 02:31:02 +03:00
|
|
|
if (m_active_widget == &child) {
|
2020-02-02 17:07:41 +03:00
|
|
|
Widget* new_active_widget = nullptr;
|
2019-06-07 12:46:02 +03:00
|
|
|
for_each_child_widget([&](auto& new_child) {
|
2019-05-27 04:52:33 +03:00
|
|
|
new_active_widget = &new_child;
|
2019-06-07 18:13:23 +03:00
|
|
|
return IterationDecision::Break;
|
2019-05-27 04:52:33 +03:00
|
|
|
});
|
2019-05-05 02:31:02 +03:00
|
|
|
set_active_widget(new_active_widget);
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
Widget::child_event(event);
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect TabWidget::bar_rect() const
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2019-07-28 18:39:04 +03:00
|
|
|
switch (m_tab_position) {
|
|
|
|
case TabPosition::Top:
|
|
|
|
return { 0, 0, width(), bar_height() };
|
|
|
|
case TabPosition::Bottom:
|
|
|
|
return { 0, height() - bar_height(), width(), bar_height() };
|
2022-05-17 13:47:35 +03:00
|
|
|
case TabPosition::Left:
|
|
|
|
return { 0, 0, get_max_tab_width(), height() };
|
|
|
|
case TabPosition::Right:
|
|
|
|
return { width() - get_max_tab_width(), 0, get_max_tab_width(), height() };
|
2019-07-28 18:39:04 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-07-28 18:39:04 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect TabWidget::container_rect() const
|
2019-07-28 18:39:04 +03:00
|
|
|
{
|
|
|
|
switch (m_tab_position) {
|
|
|
|
case TabPosition::Top:
|
|
|
|
return { 0, bar_height(), width(), height() - bar_height() };
|
|
|
|
case TabPosition::Bottom:
|
|
|
|
return { 0, 0, width(), height() - bar_height() };
|
2022-05-17 13:47:35 +03:00
|
|
|
case TabPosition::Left:
|
|
|
|
return { get_max_tab_width(), 0, width() - get_max_tab_width(), height() };
|
|
|
|
case TabPosition::Right:
|
|
|
|
return { 0, 0, width() - get_max_tab_width(), height() };
|
2019-07-28 18:39:04 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::paint_event(PaintEvent& event)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2020-05-19 00:44:08 +03:00
|
|
|
if (!m_bar_visible)
|
|
|
|
return;
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-05-05 02:31:02 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2021-06-15 15:28:39 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().button());
|
2019-05-05 16:03:34 +03:00
|
|
|
|
2021-06-15 15:28:39 +03:00
|
|
|
if (!m_container_margins.is_null()) {
|
2023-04-29 23:47:52 +03:00
|
|
|
Gfx::StylePainter::paint_frame(painter, container_rect(), palette(), Gfx::FrameStyle::RaisedContainer);
|
2021-06-15 15:28:39 +03:00
|
|
|
}
|
2019-05-05 16:03:34 +03:00
|
|
|
|
2024-03-28 19:17:07 +03:00
|
|
|
auto make_icon_rect = [](auto const& button_rect) {
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect icon_rect { button_rect.x(), button_rect.y(), 16, 16 };
|
2022-05-17 13:47:35 +03:00
|
|
|
icon_rect.translate_by(4, (button_rect.height() / 2) - (icon_rect.height() / 2));
|
|
|
|
|
2024-03-28 19:17:07 +03:00
|
|
|
return icon_rect;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto paint_tab_icon_if_needed = [&](auto& icon, auto& button_rect, auto& icon_rect, auto& text_rect) {
|
|
|
|
if (!icon)
|
|
|
|
return;
|
|
|
|
|
2020-04-24 23:27:46 +03:00
|
|
|
painter.draw_scaled_bitmap(icon_rect, *icon, icon->rect());
|
2024-03-28 19:17:07 +03:00
|
|
|
|
2023-05-22 01:41:18 +03:00
|
|
|
text_rect.set_x(icon_rect.right() + 4);
|
2020-04-24 23:27:46 +03:00
|
|
|
text_rect.intersect(button_rect);
|
2024-03-28 19:17:07 +03:00
|
|
|
|
|
|
|
icon_rect.set_x(text_rect.x());
|
2020-04-24 23:27:46 +03:00
|
|
|
};
|
|
|
|
|
2022-08-08 03:08:16 +03:00
|
|
|
bool accented = Desktop::the().system_effects().tab_accents();
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
2019-05-05 15:39:37 +03:00
|
|
|
if (m_tabs[i].widget == m_active_widget)
|
|
|
|
continue;
|
2021-08-27 18:30:23 +03:00
|
|
|
bool hovered = i == m_hovered_tab_index;
|
2019-05-05 15:39:37 +03:00
|
|
|
auto button_rect = this->button_rect(i);
|
2022-08-08 03:08:16 +03:00
|
|
|
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled(), m_tab_position, window()->is_active(), accented);
|
2021-06-19 13:10:43 +03:00
|
|
|
|
2022-07-14 16:41:47 +03:00
|
|
|
auto tab_button_content_rect = button_rect.shrunken(8, 0);
|
2024-03-28 19:17:07 +03:00
|
|
|
auto icon_rect = make_icon_rect(button_rect);
|
2020-10-30 14:21:52 +03:00
|
|
|
|
2024-03-28 19:17:07 +03:00
|
|
|
paint_tab_icon_if_needed(m_tabs[i].action_icon, button_rect, icon_rect, tab_button_content_rect);
|
|
|
|
paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, icon_rect, tab_button_content_rect);
|
2021-07-28 01:51:59 +03:00
|
|
|
tab_button_content_rect.set_width(tab_button_content_rect.width() - (m_close_button_enabled ? 16 : 0));
|
2020-10-30 14:21:52 +03:00
|
|
|
|
2021-07-28 01:51:59 +03:00
|
|
|
painter.draw_text(tab_button_content_rect, m_tabs[i].title, m_text_alignment, palette().button_text(), Gfx::TextElision::Right);
|
2019-05-05 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 10:52:04 +03:00
|
|
|
if (m_close_button_enabled) {
|
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
|
|
|
if (m_tabs[i].widget == m_active_widget)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool hovered_close_button = i == m_hovered_close_button_index;
|
|
|
|
bool pressed_close_button = i == m_pressed_close_button_index;
|
|
|
|
auto close_button_rect = this->close_button_rect(i);
|
|
|
|
|
|
|
|
if (hovered_close_button)
|
2023-04-29 23:47:52 +03:00
|
|
|
Gfx::StylePainter::paint_frame(painter, close_button_rect, palette(), pressed_close_button ? Gfx::FrameStyle::SunkenPanel : Gfx::FrameStyle::RaisedPanel);
|
2021-10-07 10:52:04 +03:00
|
|
|
|
|
|
|
Gfx::IntRect icon_rect { close_button_rect.x() + 3, close_button_rect.y() + 3, 6, 6 };
|
2022-12-11 13:29:51 +03:00
|
|
|
if (!m_tabs[i].modified) {
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(icon_rect.top_left(), icon_rect.bottom_right().translated(-1), palette().button_text());
|
|
|
|
painter.draw_line(icon_rect.top_right().moved_left(1), icon_rect.bottom_left().moved_up(1), palette().button_text());
|
2022-12-11 13:29:51 +03:00
|
|
|
} else {
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(icon_rect.top_left().moved_right(1), icon_rect.bottom_right().translated(-2), palette().button_text());
|
|
|
|
painter.draw_line(icon_rect.top_right().moved_left(2), icon_rect.bottom_left().translated(1, -2), palette().button_text());
|
|
|
|
painter.draw_line(icon_rect.bottom_left(), icon_rect.bottom_right().moved_left(1), palette().button_text(), 1, Painter::LineStyle::Dotted);
|
2022-12-11 13:29:51 +03:00
|
|
|
}
|
2021-10-07 10:52:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
2019-05-05 15:39:37 +03:00
|
|
|
if (m_tabs[i].widget != m_active_widget)
|
|
|
|
continue;
|
2021-10-07 10:52:04 +03:00
|
|
|
|
2021-08-27 18:30:23 +03:00
|
|
|
bool hovered = i == m_hovered_tab_index;
|
2019-05-05 02:31:02 +03:00
|
|
|
auto button_rect = this->button_rect(i);
|
2021-10-07 10:52:04 +03:00
|
|
|
|
2022-05-17 13:47:35 +03:00
|
|
|
if (m_dragging_active_tab) {
|
|
|
|
if (this->has_vertical_tabs())
|
|
|
|
button_rect.set_y(m_mouse_pos - m_grab_offset);
|
|
|
|
else
|
|
|
|
button_rect.set_x(m_mouse_pos - m_grab_offset);
|
|
|
|
}
|
2020-10-30 14:21:52 +03:00
|
|
|
|
2022-07-14 16:41:47 +03:00
|
|
|
auto tab_button_content_rect = button_rect.shrunken(8, 0);
|
2024-03-28 19:17:07 +03:00
|
|
|
auto icon_rect = make_icon_rect(button_rect);
|
|
|
|
|
2022-08-08 03:08:16 +03:00
|
|
|
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled(), m_tab_position, window()->is_active(), accented);
|
2021-07-28 01:51:59 +03:00
|
|
|
|
2024-03-28 19:17:07 +03:00
|
|
|
paint_tab_icon_if_needed(m_tabs[i].action_icon, button_rect, icon_rect, tab_button_content_rect);
|
|
|
|
paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, icon_rect, tab_button_content_rect);
|
2021-07-28 01:51:59 +03:00
|
|
|
tab_button_content_rect.set_width(tab_button_content_rect.width() - (m_close_button_enabled ? 16 : 0));
|
2020-10-30 14:21:52 +03:00
|
|
|
|
2021-07-28 01:51:59 +03:00
|
|
|
painter.draw_text(tab_button_content_rect, m_tabs[i].title, m_text_alignment, palette().button_text(), Gfx::TextElision::Right);
|
2020-10-30 14:21:52 +03:00
|
|
|
|
|
|
|
if (is_focused()) {
|
2023-03-03 21:32:19 +03:00
|
|
|
Gfx::IntRect focus_rect { 0, 0, min(tab_button_content_rect.width(), font().width(m_tabs[i].title)), font().pixel_size_rounded_up() };
|
2021-07-28 01:51:59 +03:00
|
|
|
focus_rect.align_within(tab_button_content_rect, m_text_alignment);
|
|
|
|
focus_rect.inflate(6, 4);
|
|
|
|
|
|
|
|
painter.draw_focus_rect(focus_rect, palette().focus_outline());
|
2020-10-30 14:21:52 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 00:39:27 +03:00
|
|
|
if (m_tab_position == TabPosition::Top) {
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(button_rect.bottom_left().moved_right(1), button_rect.bottom_right().translated(-2, 0), palette().button());
|
2020-08-24 00:39:27 +03:00
|
|
|
} else if (m_tab_position == TabPosition::Bottom) {
|
|
|
|
painter.set_pixel(button_rect.top_left().translated(0, -1), palette().threed_highlight());
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.set_pixel(button_rect.top_right().translated(-2, -1), palette().threed_shadow1());
|
|
|
|
painter.draw_line(button_rect.top_left().translated(1, -1), button_rect.top_right().translated(-3, -1), palette().button());
|
|
|
|
painter.draw_line(button_rect.top_left().translated(1, -2), button_rect.top_right().translated(-3, -2), palette().button());
|
2020-08-24 00:39:27 +03:00
|
|
|
}
|
2019-05-05 15:39:37 +03:00
|
|
|
break;
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
2021-06-19 13:10:43 +03:00
|
|
|
|
|
|
|
if (!m_close_button_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
2021-10-07 10:52:04 +03:00
|
|
|
if (m_tabs[i].widget != m_active_widget)
|
|
|
|
continue;
|
|
|
|
|
2021-08-27 18:30:23 +03:00
|
|
|
bool hovered_close_button = i == m_hovered_close_button_index;
|
|
|
|
bool pressed_close_button = i == m_pressed_close_button_index;
|
2021-06-19 13:10:43 +03:00
|
|
|
auto close_button_rect = this->close_button_rect(i);
|
|
|
|
|
2022-05-17 13:47:35 +03:00
|
|
|
if (m_dragging_active_tab) {
|
|
|
|
if (this->has_vertical_tabs())
|
|
|
|
close_button_rect.set_y((m_mouse_pos - m_grab_offset) + (close_button_rect.y() - button_rect(i).y()));
|
|
|
|
else
|
|
|
|
close_button_rect.set_x((m_mouse_pos - m_grab_offset) + (close_button_rect.x() - button_rect(i).x()));
|
|
|
|
}
|
2021-10-07 10:52:04 +03:00
|
|
|
|
2021-06-19 13:10:43 +03:00
|
|
|
if (hovered_close_button)
|
2023-04-29 23:47:52 +03:00
|
|
|
Gfx::StylePainter::paint_frame(painter, close_button_rect, palette(), pressed_close_button ? Gfx::FrameStyle::SunkenPanel : Gfx::FrameStyle::RaisedPanel);
|
2021-06-19 13:10:43 +03:00
|
|
|
|
|
|
|
Gfx::IntRect icon_rect { close_button_rect.x() + 3, close_button_rect.y() + 3, 6, 6 };
|
2022-12-11 13:29:51 +03:00
|
|
|
if (!m_tabs[i].modified) {
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(icon_rect.top_left(), icon_rect.bottom_right().translated(-1), palette().button_text());
|
|
|
|
painter.draw_line(icon_rect.top_right().moved_left(1), icon_rect.bottom_left().moved_up(1), palette().button_text());
|
2022-12-11 13:29:51 +03:00
|
|
|
} else {
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(icon_rect.top_left().moved_right(1), icon_rect.bottom_right().translated(-2, -2), palette().button_text());
|
2023-05-28 19:35:37 +03:00
|
|
|
painter.draw_line(icon_rect.top_right().moved_left(2), icon_rect.bottom_left().translated(1, -2), palette().button_text());
|
2023-05-22 01:41:18 +03:00
|
|
|
painter.draw_line(icon_rect.bottom_left(), icon_rect.bottom_right().moved_left(1), palette().button_text(), 1, Painter::LineStyle::Dotted);
|
2022-12-11 13:29:51 +03:00
|
|
|
}
|
2021-06-19 13:10:43 +03:00
|
|
|
}
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-24 23:36:25 +03:00
|
|
|
int TabWidget::uniform_tab_width() const
|
|
|
|
{
|
2022-05-17 13:47:35 +03:00
|
|
|
int total_tab_width = m_tabs.size() * get_max_tab_width();
|
|
|
|
int tab_width = get_max_tab_width();
|
|
|
|
|
|
|
|
if (this->has_vertical_tabs())
|
|
|
|
return tab_width;
|
|
|
|
|
2021-04-09 23:30:41 +03:00
|
|
|
int available_width = width() - bar_margin() * 2;
|
|
|
|
if (total_tab_width > available_width)
|
|
|
|
tab_width = available_width / m_tabs.size();
|
2022-05-17 13:47:35 +03:00
|
|
|
return max(tab_width, m_min_tab_width);
|
2020-04-24 23:36:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 00:44:08 +03:00
|
|
|
void TabWidget::set_bar_visible(bool bar_visible)
|
|
|
|
{
|
|
|
|
m_bar_visible = bar_visible;
|
|
|
|
if (m_active_widget)
|
|
|
|
m_active_widget->set_relative_rect(child_rect_for_size(size()));
|
|
|
|
update_bar();
|
|
|
|
}
|
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
Gfx::IntRect TabWidget::button_rect(size_t index) const
|
2022-05-17 13:47:35 +03:00
|
|
|
{
|
|
|
|
if (this->has_vertical_tabs())
|
|
|
|
return vertical_button_rect(index);
|
|
|
|
return horizontal_button_rect(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Gfx::IntRect TabWidget::vertical_button_rect(size_t index) const
|
|
|
|
{
|
|
|
|
int offset = bar_margin() + (bar_height() * index);
|
|
|
|
Gfx::IntRect rect { 0, offset, get_max_tab_width() - 1, bar_height() };
|
|
|
|
|
|
|
|
if (m_tabs[index].widget != m_active_widget) {
|
|
|
|
rect.translate_by(m_tab_position == TabPosition::Left ? 2 : 0, 0);
|
|
|
|
rect.set_width(rect.width() - 2);
|
|
|
|
} else {
|
|
|
|
rect.translate_by(0, -2);
|
|
|
|
rect.set_height(rect.height() + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
rect.translate_by(bar_rect().location());
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gfx::IntRect TabWidget::horizontal_button_rect(size_t index) const
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2021-04-09 23:30:41 +03:00
|
|
|
int x_offset = bar_margin();
|
2021-06-19 13:10:43 +03:00
|
|
|
int close_button_offset = m_close_button_enabled ? 16 : 0;
|
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
for (size_t i = 0; i < index; ++i) {
|
2021-06-19 13:10:43 +03:00
|
|
|
auto tab_width = m_uniform_tabs ? uniform_tab_width() : m_tabs[i].width(font()) + close_button_offset;
|
2020-04-24 23:36:25 +03:00
|
|
|
x_offset += tab_width;
|
|
|
|
}
|
2021-06-19 13:10:43 +03:00
|
|
|
Gfx::IntRect rect { x_offset, 0, m_uniform_tabs ? uniform_tab_width() : m_tabs[index].width(font()) + close_button_offset, bar_height() };
|
2019-05-05 15:39:37 +03:00
|
|
|
if (m_tabs[index].widget != m_active_widget) {
|
2021-04-12 21:47:09 +03:00
|
|
|
rect.translate_by(0, m_tab_position == TabPosition::Top ? 2 : 0);
|
2019-05-05 15:39:37 +03:00
|
|
|
rect.set_height(rect.height() - 2);
|
|
|
|
} else {
|
2021-04-12 21:47:09 +03:00
|
|
|
rect.translate_by(-2, 0);
|
2019-05-05 15:39:37 +03:00
|
|
|
rect.set_width(rect.width() + 4);
|
|
|
|
}
|
2021-04-12 21:47:09 +03:00
|
|
|
rect.translate_by(bar_rect().location());
|
2019-05-05 15:39:37 +03:00
|
|
|
return rect;
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
Gfx::IntRect TabWidget::close_button_rect(size_t index) const
|
2021-06-19 13:10:43 +03:00
|
|
|
{
|
|
|
|
auto rect = button_rect(index);
|
|
|
|
Gfx::IntRect close_button_rect { 0, 0, 12, 12 };
|
|
|
|
|
2023-05-22 01:41:18 +03:00
|
|
|
close_button_rect.translate_by(rect.right() - 1, rect.top());
|
2022-05-17 13:47:35 +03:00
|
|
|
close_button_rect.translate_by(-(close_button_rect.width() + 4), (rect.height() / 2) - (close_button_rect.height() / 2));
|
2021-06-19 13:10:43 +03:00
|
|
|
|
|
|
|
return close_button_rect;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
int TabWidget::TabData::width(Gfx::Font const& font) const
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2023-04-14 15:52:47 +03:00
|
|
|
auto width = 16 + font.width_rounded_up(title) + (icon ? (16 + 4) : 0);
|
2021-07-28 01:51:59 +03:00
|
|
|
// NOTE: This needs to always be an odd number, because the button rect
|
|
|
|
// includes 3px of light and shadow on the left and right edges. If
|
|
|
|
// the button rect width is not an odd number, the area left for the
|
|
|
|
// text and the focus rect has an odd number of pixels, and this
|
|
|
|
// causes the text (and subsequently the focus rect) to not be aligned
|
|
|
|
// to the center perfectly.
|
2023-05-22 01:41:18 +03:00
|
|
|
if (width % 2 == 0)
|
2021-07-28 01:51:59 +03:00
|
|
|
width++;
|
|
|
|
|
|
|
|
return width;
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::mousedown_event(MouseEvent& event)
|
2019-05-05 02:31:02 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
2019-05-05 02:31:02 +03:00
|
|
|
auto button_rect = this->button_rect(i);
|
2021-06-19 13:10:43 +03:00
|
|
|
auto close_button_rect = this->close_button_rect(i);
|
|
|
|
|
2019-05-05 02:31:02 +03:00
|
|
|
if (!button_rect.contains(event.position()))
|
|
|
|
continue;
|
2021-06-19 13:10:43 +03:00
|
|
|
|
2021-10-27 14:20:27 +03:00
|
|
|
if (event.button() == MouseButton::Primary) {
|
2021-06-19 13:10:43 +03:00
|
|
|
if (m_close_button_enabled && close_button_rect.contains(event.position())) {
|
|
|
|
m_pressed_close_button_index = i;
|
|
|
|
update_bar();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-07 06:03:03 +03:00
|
|
|
set_active_widget(m_tabs[i].widget);
|
2021-10-07 10:52:04 +03:00
|
|
|
drag_tab(i);
|
2020-05-07 06:03:03 +03:00
|
|
|
} else if (event.button() == MouseButton::Middle) {
|
|
|
|
auto* widget = m_tabs[i].widget;
|
2021-08-30 21:12:48 +03:00
|
|
|
deferred_invoke([this, widget] {
|
2020-05-07 06:03:03 +03:00
|
|
|
if (on_middle_click && widget)
|
|
|
|
on_middle_click(*widget);
|
|
|
|
});
|
|
|
|
}
|
2019-05-07 15:01:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 13:10:43 +03:00
|
|
|
void TabWidget::mouseup_event(MouseEvent& event)
|
|
|
|
{
|
2021-10-27 14:20:27 +03:00
|
|
|
if (event.button() != MouseButton::Primary)
|
2021-06-19 13:10:43 +03:00
|
|
|
return;
|
|
|
|
|
2021-10-07 10:52:04 +03:00
|
|
|
if (m_dragging_active_tab) {
|
|
|
|
m_dragging_active_tab = false;
|
|
|
|
update_bar();
|
|
|
|
}
|
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
if (!m_close_button_enabled || !m_pressed_close_button_index.has_value())
|
2021-06-19 13:10:43 +03:00
|
|
|
return;
|
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
auto close_button_rect = this->close_button_rect(m_pressed_close_button_index.value());
|
2021-10-07 10:52:04 +03:00
|
|
|
update_bar();
|
2021-06-20 22:28:27 +03:00
|
|
|
|
|
|
|
if (close_button_rect.contains(event.position())) {
|
2021-07-28 03:11:41 +03:00
|
|
|
auto* widget = m_tabs[m_pressed_close_button_index.value()].widget;
|
2021-08-30 21:12:48 +03:00
|
|
|
deferred_invoke([this, widget] {
|
2021-06-20 22:28:27 +03:00
|
|
|
if (on_tab_close_click && widget)
|
|
|
|
on_tab_close_click(*widget);
|
|
|
|
});
|
2021-06-19 13:10:43 +03:00
|
|
|
}
|
2021-07-28 03:11:41 +03:00
|
|
|
m_pressed_close_button_index = {};
|
2021-06-19 13:10:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::mousemove_event(MouseEvent& event)
|
2019-05-07 15:01:20 +03:00
|
|
|
{
|
2021-07-28 03:11:41 +03:00
|
|
|
Optional<size_t> hovered_tab = {};
|
|
|
|
Optional<size_t> hovered_close_button = {};
|
2021-06-19 13:10:43 +03:00
|
|
|
|
2022-05-17 13:47:35 +03:00
|
|
|
m_mouse_pos = this->has_vertical_tabs() ? event.position().y() : event.position().x();
|
2021-10-07 10:52:04 +03:00
|
|
|
if (m_dragging_active_tab) {
|
|
|
|
recalculate_tab_order();
|
|
|
|
update_bar();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
2019-05-07 15:01:20 +03:00
|
|
|
auto button_rect = this->button_rect(i);
|
2021-06-19 13:10:43 +03:00
|
|
|
auto close_button_rect = this->close_button_rect(i);
|
|
|
|
|
|
|
|
if (close_button_rect.contains(event.position()))
|
|
|
|
hovered_close_button = i;
|
|
|
|
|
2019-05-07 15:01:20 +03:00
|
|
|
if (!button_rect.contains(event.position()))
|
|
|
|
continue;
|
|
|
|
hovered_tab = i;
|
|
|
|
if (m_tabs[i].widget == m_active_widget)
|
|
|
|
break;
|
|
|
|
}
|
2021-07-28 03:11:41 +03:00
|
|
|
if (!hovered_tab.has_value() && !hovered_close_button.has_value())
|
2019-05-07 15:01:20 +03:00
|
|
|
return;
|
|
|
|
m_hovered_tab_index = hovered_tab;
|
2021-06-19 13:10:43 +03:00
|
|
|
m_hovered_close_button_index = hovered_close_button;
|
2019-05-07 15:01:20 +03:00
|
|
|
update_bar();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::leave_event(Core::Event&)
|
2019-05-07 15:01:20 +03:00
|
|
|
{
|
2021-07-28 03:11:41 +03:00
|
|
|
if (m_hovered_tab_index.has_value() || m_hovered_close_button_index.has_value()) {
|
|
|
|
m_hovered_tab_index = {};
|
|
|
|
m_hovered_close_button_index = {};
|
2019-05-07 15:01:20 +03:00
|
|
|
update_bar();
|
2019-05-05 02:31:02 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-07 15:01:20 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::update_bar()
|
2019-05-07 15:01:20 +03:00
|
|
|
{
|
2021-07-28 03:11:41 +03:00
|
|
|
if (m_tabs.is_empty())
|
|
|
|
return;
|
2019-05-07 15:01:20 +03:00
|
|
|
auto invalidation_rect = bar_rect();
|
|
|
|
invalidation_rect.set_height(invalidation_rect.height() + 1);
|
|
|
|
update(invalidation_rect);
|
|
|
|
}
|
2019-07-28 18:39:04 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void TabWidget::set_tab_position(TabPosition tab_position)
|
2019-07-28 18:39:04 +03:00
|
|
|
{
|
|
|
|
if (m_tab_position == tab_position)
|
|
|
|
return;
|
|
|
|
m_tab_position = tab_position;
|
2022-05-17 13:47:35 +03:00
|
|
|
if (this->has_vertical_tabs())
|
|
|
|
m_uniform_tabs = true;
|
2019-07-28 18:39:04 +03:00
|
|
|
if (m_active_widget)
|
|
|
|
m_active_widget->set_relative_rect(child_rect_for_size(size()));
|
|
|
|
update();
|
|
|
|
}
|
2019-09-07 17:13:26 +03:00
|
|
|
|
2021-07-28 03:11:41 +03:00
|
|
|
Optional<size_t> TabWidget::active_tab_index() const
|
2019-09-07 17:13:26 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); i++) {
|
2019-09-07 17:57:26 +03:00
|
|
|
if (m_tabs.at(i).widget == m_active_widget)
|
2019-09-07 17:13:26 +03:00
|
|
|
return i;
|
|
|
|
}
|
2021-07-28 03:11:41 +03:00
|
|
|
return {};
|
2019-09-07 17:13:26 +03:00
|
|
|
}
|
2020-04-23 22:13:47 +03:00
|
|
|
|
2023-03-10 22:25:41 +03:00
|
|
|
void TabWidget::set_tab_title(Widget& tab, String title)
|
2020-04-23 22:13:47 +03:00
|
|
|
{
|
|
|
|
for (auto& t : m_tabs) {
|
|
|
|
if (t.widget == &tab) {
|
|
|
|
if (t.title != title) {
|
2023-03-10 22:25:41 +03:00
|
|
|
t.title = move(title);
|
2020-04-23 22:13:47 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void TabWidget::set_tab_icon(Widget& tab, Gfx::Bitmap const* icon)
|
2020-04-24 23:27:46 +03:00
|
|
|
{
|
|
|
|
for (auto& t : m_tabs) {
|
|
|
|
if (t.widget == &tab) {
|
|
|
|
t.icon = icon;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 19:17:07 +03:00
|
|
|
// FIXME: Also accept an action to be triggered when the action icon is clicked. If the action is non-null, then also
|
|
|
|
// paint the icon as a button (with hover/click effects).
|
|
|
|
void TabWidget::set_tab_action_icon(Widget& tab, Gfx::Bitmap const* action_icon)
|
|
|
|
{
|
|
|
|
for (auto& t : m_tabs) {
|
|
|
|
if (t.widget == &tab) {
|
|
|
|
t.action_icon = action_icon;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:48:47 +03:00
|
|
|
bool TabWidget::is_tab_modified(Widget& tab_input)
|
|
|
|
{
|
|
|
|
auto it = m_tabs.find_if([&](auto t) { return t.widget == &tab_input; });
|
|
|
|
if (it.is_end())
|
|
|
|
return false;
|
|
|
|
auto& tab = *it;
|
|
|
|
return tab.modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabWidget::set_tab_modified(Widget& tab_input, bool modified)
|
|
|
|
{
|
|
|
|
auto it = m_tabs.find_if([&](auto t) { return t.widget == &tab_input; });
|
|
|
|
if (it.is_end())
|
|
|
|
return;
|
|
|
|
auto& tab = *it;
|
|
|
|
if (tab.modified != modified) {
|
|
|
|
tab.modified = modified;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabWidget::is_any_tab_modified()
|
|
|
|
{
|
|
|
|
return any_of(m_tabs, [](auto& t) { return t.modified; });
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:43:08 +03:00
|
|
|
void TabWidget::activate_next_tab()
|
|
|
|
{
|
|
|
|
if (m_tabs.size() <= 1)
|
|
|
|
return;
|
2021-07-28 03:11:41 +03:00
|
|
|
auto index = active_tab_index();
|
|
|
|
if (!index.has_value())
|
|
|
|
return;
|
|
|
|
auto next_index = index.value() + 1;
|
|
|
|
if (next_index >= m_tabs.size())
|
|
|
|
next_index = 0;
|
|
|
|
set_active_widget(m_tabs.at(next_index).widget);
|
2020-04-23 22:43:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabWidget::activate_previous_tab()
|
|
|
|
{
|
|
|
|
if (m_tabs.size() <= 1)
|
|
|
|
return;
|
2021-07-28 03:11:41 +03:00
|
|
|
auto index = active_tab_index();
|
|
|
|
if (!index.has_value())
|
|
|
|
return;
|
|
|
|
size_t previous_index = 0;
|
|
|
|
if (index.value() == 0)
|
|
|
|
previous_index = m_tabs.size() - 1;
|
|
|
|
else
|
|
|
|
previous_index = index.value() - 1;
|
|
|
|
set_active_widget(m_tabs.at(previous_index).widget);
|
2020-04-23 22:43:08 +03:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:06:27 +03:00
|
|
|
void TabWidget::activate_last_tab()
|
|
|
|
{
|
|
|
|
size_t number_of_tabs = m_tabs.size();
|
|
|
|
if (number_of_tabs == 0)
|
|
|
|
return;
|
|
|
|
set_active_widget(m_tabs.at(number_of_tabs - 1).widget);
|
|
|
|
}
|
|
|
|
|
2020-05-19 00:44:08 +03:00
|
|
|
void TabWidget::keydown_event(KeyEvent& event)
|
2020-04-30 10:04:03 +03:00
|
|
|
{
|
|
|
|
if (event.ctrl() && event.key() == Key_Tab) {
|
|
|
|
if (event.shift())
|
|
|
|
activate_previous_tab();
|
|
|
|
else
|
|
|
|
activate_next_tab();
|
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
}
|
2020-10-30 14:21:52 +03:00
|
|
|
if (is_focused()) {
|
|
|
|
if (!event.modifiers() && event.key() == Key_Left) {
|
|
|
|
activate_previous_tab();
|
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!event.modifiers() && event.key() == Key_Right) {
|
|
|
|
activate_next_tab();
|
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 10:04:03 +03:00
|
|
|
Widget::keydown_event(event);
|
|
|
|
}
|
|
|
|
|
2020-05-19 01:36:02 +03:00
|
|
|
void TabWidget::context_menu_event(ContextMenuEvent& context_menu_event)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
|
|
|
auto button_rect = this->button_rect(i);
|
|
|
|
if (!button_rect.contains(context_menu_event.position()))
|
|
|
|
continue;
|
|
|
|
auto* widget = m_tabs[i].widget;
|
2021-08-30 21:12:48 +03:00
|
|
|
deferred_invoke([this, widget, context_menu_event] {
|
2020-05-19 01:36:02 +03:00
|
|
|
if (on_context_menu_request && widget)
|
|
|
|
on_context_menu_request(*widget, context_menu_event);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:18:28 +03:00
|
|
|
void TabWidget::doubleclick_event(MouseEvent& mouse_event)
|
2021-11-30 19:42:13 +03:00
|
|
|
{
|
2022-03-10 14:18:28 +03:00
|
|
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
|
|
|
auto button_rect = this->button_rect(i);
|
|
|
|
if (!button_rect.contains(mouse_event.position()))
|
|
|
|
continue;
|
|
|
|
if (auto* widget = m_tabs[i].widget) {
|
2021-11-30 19:42:13 +03:00
|
|
|
deferred_invoke([this, widget] {
|
|
|
|
if (on_double_click)
|
|
|
|
on_double_click(*widget);
|
|
|
|
});
|
|
|
|
}
|
2022-03-10 14:18:28 +03:00
|
|
|
return;
|
2021-11-30 19:42:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 15:28:39 +03:00
|
|
|
void TabWidget::set_container_margins(GUI::Margins const& margins)
|
|
|
|
{
|
|
|
|
m_container_margins = margins;
|
2022-12-05 16:00:35 +03:00
|
|
|
layout_relevant_change_occurred();
|
2021-06-15 15:28:39 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-07-04 06:39:24 +03:00
|
|
|
Optional<UISize> TabWidget::calculated_min_size() const
|
|
|
|
{
|
|
|
|
if (!m_active_widget)
|
|
|
|
return {};
|
|
|
|
auto content_min_size = m_active_widget->effective_min_size();
|
|
|
|
UIDimension width = MUST(content_min_size.width().shrink_value()), height = MUST(content_min_size.height().shrink_value());
|
|
|
|
width.add_if_int(container_margins().vertical_total()
|
|
|
|
+ (first_is_one_of(m_tab_position, TabPosition::Left, TabPosition::Right) ? bar_rect().width() : 0));
|
|
|
|
height.add_if_int(container_margins().vertical_total()
|
|
|
|
+ (first_is_one_of(m_tab_position, TabPosition::Top, TabPosition::Bottom) ? bar_rect().height() : 0));
|
|
|
|
|
|
|
|
return UISize { width, height };
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<UISize> TabWidget::calculated_preferred_size() const
|
|
|
|
{
|
|
|
|
if (!m_active_widget)
|
|
|
|
return {};
|
|
|
|
auto content_preferred_size = m_active_widget->effective_preferred_size();
|
|
|
|
UIDimension width = MUST(content_preferred_size.width().shrink_value()), height = MUST(content_preferred_size.height().shrink_value());
|
|
|
|
width.add_if_int(container_margins().vertical_total()
|
|
|
|
+ (first_is_one_of(m_tab_position, TabPosition::Left, TabPosition::Right) ? bar_rect().width() : 0));
|
|
|
|
height.add_if_int(
|
|
|
|
container_margins().vertical_total()
|
|
|
|
+ (first_is_one_of(m_tab_position, TabPosition::Top, TabPosition::Bottom) ? bar_rect().height() : 0));
|
|
|
|
return UISize { width, height };
|
|
|
|
}
|
|
|
|
|
2021-10-07 10:52:04 +03:00
|
|
|
void TabWidget::drag_tab(size_t index)
|
|
|
|
{
|
|
|
|
m_dragging_active_tab = m_reorder_allowed;
|
2022-05-17 13:47:35 +03:00
|
|
|
m_grab_offset = m_mouse_pos - (this->has_vertical_tabs() ? button_rect(index).y() : button_rect(index).x());
|
2021-10-07 10:52:04 +03:00
|
|
|
m_hovered_tab_index = {};
|
|
|
|
m_hovered_close_button_index = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabWidget::recalculate_tab_order()
|
|
|
|
{
|
|
|
|
if (!m_dragging_active_tab)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t active;
|
|
|
|
for (active = 0; active < m_tabs.size(); ++active) {
|
|
|
|
if (m_tabs[active].widget == m_active_widget)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t target;
|
|
|
|
for (target = 0; target < active; ++target) {
|
2022-05-17 13:47:35 +03:00
|
|
|
auto button_pos = this->has_vertical_tabs() ? (this->button_rect(target)).y() : (this->button_rect(target)).x();
|
|
|
|
if (m_mouse_pos - m_grab_offset < button_pos) {
|
2021-10-07 10:52:04 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target == active) {
|
|
|
|
for (target = m_tabs.size() - 1; target > active; --target) {
|
2022-05-17 13:47:35 +03:00
|
|
|
auto button_pos = this->has_vertical_tabs() ? (this->button_rect(target)).y() : (this->button_rect(target)).x();
|
|
|
|
if (m_mouse_pos - m_grab_offset > button_pos) {
|
2021-10-07 10:52:04 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active == target)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto tab = m_tabs.take(active);
|
|
|
|
m_tabs.insert(target, tab);
|
|
|
|
update_bar();
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|