mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:27:45 +00:00
LibGUI: Convert indexes to size_t and use Optional in TabWidget
Previously it was using int for indexes and using the -1 magic value.
Supersedes b9d51b8601
This commit is contained in:
parent
b9d51b8601
commit
3ec4ad8a73
2 changed files with 45 additions and 37 deletions
|
@ -202,7 +202,7 @@ void TabWidget::paint_event(PaintEvent& event)
|
||||||
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
||||||
if (m_tabs[i].widget == m_active_widget)
|
if (m_tabs[i].widget == m_active_widget)
|
||||||
continue;
|
continue;
|
||||||
bool hovered = static_cast<int>(i) == m_hovered_tab_index;
|
bool hovered = m_hovered_tab_index.has_value() && i == m_hovered_tab_index.value();
|
||||||
auto button_rect = this->button_rect(i);
|
auto button_rect = this->button_rect(i);
|
||||||
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
|
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
|
||||||
auto tab_button_content_rect = button_rect.translated(4, m_tab_position == TabPosition::Top ? 1 : 0);
|
auto tab_button_content_rect = button_rect.translated(4, m_tab_position == TabPosition::Top ? 1 : 0);
|
||||||
|
@ -221,7 +221,7 @@ void TabWidget::paint_event(PaintEvent& event)
|
||||||
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
||||||
if (m_tabs[i].widget != m_active_widget)
|
if (m_tabs[i].widget != m_active_widget)
|
||||||
continue;
|
continue;
|
||||||
bool hovered = static_cast<int>(i) == m_hovered_tab_index;
|
bool hovered = m_hovered_tab_index.has_value() && i == m_hovered_tab_index.value();
|
||||||
auto button_rect = this->button_rect(i);
|
auto button_rect = this->button_rect(i);
|
||||||
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
|
Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
|
||||||
auto tab_button_content_rect = button_rect.translated(4, m_tab_position == TabPosition::Top ? 1 : 0);
|
auto tab_button_content_rect = button_rect.translated(4, m_tab_position == TabPosition::Top ? 1 : 0);
|
||||||
|
@ -254,8 +254,8 @@ void TabWidget::paint_event(PaintEvent& event)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
||||||
bool hovered_close_button = static_cast<int>(i) == m_hovered_close_button_index;
|
bool hovered_close_button = m_hovered_close_button_index.has_value() && i == m_hovered_close_button_index.value();
|
||||||
bool pressed_close_button = static_cast<int>(i) == m_pressed_close_button_index;
|
bool pressed_close_button = m_pressed_close_button_index.has_value() && i == m_pressed_close_button_index.value();
|
||||||
auto close_button_rect = this->close_button_rect(i);
|
auto close_button_rect = this->close_button_rect(i);
|
||||||
|
|
||||||
if (hovered_close_button)
|
if (hovered_close_button)
|
||||||
|
@ -287,12 +287,12 @@ void TabWidget::set_bar_visible(bool bar_visible)
|
||||||
update_bar();
|
update_bar();
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::IntRect TabWidget::button_rect(int index) const
|
Gfx::IntRect TabWidget::button_rect(size_t index) const
|
||||||
{
|
{
|
||||||
int x_offset = bar_margin();
|
int x_offset = bar_margin();
|
||||||
int close_button_offset = m_close_button_enabled ? 16 : 0;
|
int close_button_offset = m_close_button_enabled ? 16 : 0;
|
||||||
|
|
||||||
for (int i = 0; i < index; ++i) {
|
for (size_t i = 0; i < index; ++i) {
|
||||||
auto tab_width = m_uniform_tabs ? uniform_tab_width() : m_tabs[i].width(font()) + close_button_offset;
|
auto tab_width = m_uniform_tabs ? uniform_tab_width() : m_tabs[i].width(font()) + close_button_offset;
|
||||||
x_offset += tab_width;
|
x_offset += tab_width;
|
||||||
}
|
}
|
||||||
|
@ -308,7 +308,7 @@ Gfx::IntRect TabWidget::button_rect(int index) const
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::IntRect TabWidget::close_button_rect(int index) const
|
Gfx::IntRect TabWidget::close_button_rect(size_t index) const
|
||||||
{
|
{
|
||||||
auto rect = button_rect(index);
|
auto rect = button_rect(index);
|
||||||
Gfx::IntRect close_button_rect { 0, 0, 12, 12 };
|
Gfx::IntRect close_button_rect { 0, 0, 12, 12 };
|
||||||
|
@ -358,26 +358,26 @@ void TabWidget::mouseup_event(MouseEvent& event)
|
||||||
if (event.button() != MouseButton::Left)
|
if (event.button() != MouseButton::Left)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!m_close_button_enabled || m_pressed_close_button_index == -1)
|
if (!m_close_button_enabled || !m_pressed_close_button_index.has_value())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto close_button_rect = this->close_button_rect(m_pressed_close_button_index);
|
auto close_button_rect = this->close_button_rect(m_pressed_close_button_index.value());
|
||||||
m_pressed_close_button_index = -1;
|
|
||||||
|
|
||||||
if (close_button_rect.contains(event.position())) {
|
if (close_button_rect.contains(event.position())) {
|
||||||
auto* widget = m_tabs[m_pressed_close_button_index].widget;
|
auto* widget = m_tabs[m_pressed_close_button_index.value()].widget;
|
||||||
deferred_invoke([this, widget](auto&) {
|
deferred_invoke([this, widget](auto&) {
|
||||||
if (on_tab_close_click && widget)
|
if (on_tab_close_click && widget)
|
||||||
on_tab_close_click(*widget);
|
on_tab_close_click(*widget);
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_pressed_close_button_index = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabWidget::mousemove_event(MouseEvent& event)
|
void TabWidget::mousemove_event(MouseEvent& event)
|
||||||
{
|
{
|
||||||
int hovered_tab = -1;
|
Optional<size_t> hovered_tab = {};
|
||||||
int hovered_close_button = -1;
|
Optional<size_t> hovered_close_button = {};
|
||||||
|
|
||||||
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
for (size_t i = 0; i < m_tabs.size(); ++i) {
|
||||||
auto button_rect = this->button_rect(i);
|
auto button_rect = this->button_rect(i);
|
||||||
|
@ -392,7 +392,7 @@ void TabWidget::mousemove_event(MouseEvent& event)
|
||||||
if (m_tabs[i].widget == m_active_widget)
|
if (m_tabs[i].widget == m_active_widget)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (hovered_tab == m_hovered_tab_index && hovered_close_button == m_hovered_close_button_index)
|
if (!hovered_tab.has_value() && !hovered_close_button.has_value())
|
||||||
return;
|
return;
|
||||||
m_hovered_tab_index = hovered_tab;
|
m_hovered_tab_index = hovered_tab;
|
||||||
m_hovered_close_button_index = hovered_close_button;
|
m_hovered_close_button_index = hovered_close_button;
|
||||||
|
@ -401,18 +401,20 @@ void TabWidget::mousemove_event(MouseEvent& event)
|
||||||
|
|
||||||
void TabWidget::leave_event(Core::Event&)
|
void TabWidget::leave_event(Core::Event&)
|
||||||
{
|
{
|
||||||
if (m_hovered_tab_index != -1 || m_hovered_close_button_index != -1) {
|
if (m_hovered_tab_index.has_value() || m_hovered_close_button_index.has_value()) {
|
||||||
m_hovered_tab_index = -1;
|
m_hovered_tab_index = {};
|
||||||
m_hovered_close_button_index = -1;
|
m_hovered_close_button_index = {};
|
||||||
update_bar();
|
update_bar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabWidget::update_bar()
|
void TabWidget::update_bar()
|
||||||
{
|
{
|
||||||
|
if (m_tabs.is_empty())
|
||||||
|
return;
|
||||||
auto invalidation_rect = bar_rect();
|
auto invalidation_rect = bar_rect();
|
||||||
invalidation_rect.set_height(invalidation_rect.height() + 1);
|
invalidation_rect.set_height(invalidation_rect.height() + 1);
|
||||||
invalidation_rect.set_right(button_rect(static_cast<int>(m_tabs.size() - 1)).right());
|
invalidation_rect.set_right(button_rect(m_tabs.size() - 1).right());
|
||||||
update(invalidation_rect);
|
update(invalidation_rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,13 +428,13 @@ void TabWidget::set_tab_position(TabPosition tab_position)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
int TabWidget::active_tab_index() const
|
Optional<size_t> TabWidget::active_tab_index() const
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_tabs.size(); i++) {
|
for (size_t i = 0; i < m_tabs.size(); i++) {
|
||||||
if (m_tabs.at(i).widget == m_active_widget)
|
if (m_tabs.at(i).widget == m_active_widget)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabWidget::set_tab_title(Widget& tab, const StringView& title)
|
void TabWidget::set_tab_title(Widget& tab, const StringView& title)
|
||||||
|
@ -463,22 +465,28 @@ void TabWidget::activate_next_tab()
|
||||||
{
|
{
|
||||||
if (m_tabs.size() <= 1)
|
if (m_tabs.size() <= 1)
|
||||||
return;
|
return;
|
||||||
int index = active_tab_index();
|
auto index = active_tab_index();
|
||||||
++index;
|
if (!index.has_value())
|
||||||
if (index >= (int)m_tabs.size())
|
return;
|
||||||
index = 0;
|
auto next_index = index.value() + 1;
|
||||||
set_active_widget(m_tabs.at(index).widget);
|
if (next_index >= m_tabs.size())
|
||||||
|
next_index = 0;
|
||||||
|
set_active_widget(m_tabs.at(next_index).widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabWidget::activate_previous_tab()
|
void TabWidget::activate_previous_tab()
|
||||||
{
|
{
|
||||||
if (m_tabs.size() <= 1)
|
if (m_tabs.size() <= 1)
|
||||||
return;
|
return;
|
||||||
int index = active_tab_index();
|
auto index = active_tab_index();
|
||||||
--index;
|
if (!index.has_value())
|
||||||
if (index < 0)
|
return;
|
||||||
index = m_tabs.size() - 1;
|
size_t previous_index = 0;
|
||||||
set_active_widget(m_tabs.at(index).widget);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabWidget::keydown_event(KeyEvent& event)
|
void TabWidget::keydown_event(KeyEvent& event)
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
TabPosition tab_position() const { return m_tab_position; }
|
TabPosition tab_position() const { return m_tab_position; }
|
||||||
void set_tab_position(TabPosition);
|
void set_tab_position(TabPosition);
|
||||||
|
|
||||||
int active_tab_index() const;
|
Optional<size_t> active_tab_index() const;
|
||||||
|
|
||||||
Widget* active_widget() { return m_active_widget.ptr(); }
|
Widget* active_widget() { return m_active_widget.ptr(); }
|
||||||
const Widget* active_widget() const { return m_active_widget.ptr(); }
|
const Widget* active_widget() const { return m_active_widget.ptr(); }
|
||||||
|
@ -88,8 +88,8 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Gfx::IntRect child_rect_for_size(const Gfx::IntSize&) const;
|
Gfx::IntRect child_rect_for_size(const Gfx::IntSize&) const;
|
||||||
Gfx::IntRect button_rect(int index) const;
|
Gfx::IntRect button_rect(size_t index) const;
|
||||||
Gfx::IntRect close_button_rect(int index) const;
|
Gfx::IntRect close_button_rect(size_t index) const;
|
||||||
Gfx::IntRect bar_rect() const;
|
Gfx::IntRect bar_rect() const;
|
||||||
Gfx::IntRect container_rect() const;
|
Gfx::IntRect container_rect() const;
|
||||||
void update_bar();
|
void update_bar();
|
||||||
|
@ -106,9 +106,9 @@ private:
|
||||||
};
|
};
|
||||||
Vector<TabData> m_tabs;
|
Vector<TabData> m_tabs;
|
||||||
TabPosition m_tab_position { TabPosition::Top };
|
TabPosition m_tab_position { TabPosition::Top };
|
||||||
int m_hovered_tab_index { -1 };
|
Optional<size_t> m_hovered_tab_index;
|
||||||
int m_hovered_close_button_index { -1 };
|
Optional<size_t> m_hovered_close_button_index;
|
||||||
int m_pressed_close_button_index { -1 };
|
Optional<size_t> m_pressed_close_button_index;
|
||||||
GUI::Margins m_container_margins { 2, 2, 2, 2 };
|
GUI::Margins m_container_margins { 2, 2, 2, 2 };
|
||||||
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
||||||
bool m_uniform_tabs { false };
|
bool m_uniform_tabs { false };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue