2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/StackWidget.h>
|
2020-10-30 16:25:52 +01:00
|
|
|
#include <LibGUI/Window.h>
|
2019-03-15 16:12:06 +01:00
|
|
|
|
2021-03-25 10:24:07 +04:30
|
|
|
REGISTER_WIDGET(GUI, StackWidget);
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
void StackWidget::set_active_widget(Widget* widget)
|
2019-03-15 16:12:06 +01:00
|
|
|
{
|
|
|
|
if (widget == m_active_widget)
|
|
|
|
return;
|
|
|
|
|
2020-10-30 23:38:42 +01:00
|
|
|
bool active_widget_had_focus = m_active_widget && m_active_widget->has_focus_within();
|
2020-09-18 18:02:06 +02:00
|
|
|
|
2019-03-15 16:12:06 +01: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(rect());
|
2020-10-30 15:49:16 +01:00
|
|
|
if (active_widget_had_focus)
|
2020-09-18 18:02:06 +02:00
|
|
|
m_active_widget->set_focus(true);
|
2019-03-15 16:12:06 +01:00
|
|
|
m_active_widget->set_visible(true);
|
|
|
|
}
|
2020-10-30 15:49:16 +01:00
|
|
|
|
|
|
|
set_focus_proxy(m_active_widget);
|
|
|
|
|
2019-07-07 21:50:38 +02:00
|
|
|
if (on_active_widget_change)
|
|
|
|
on_active_widget_change(m_active_widget);
|
2019-03-15 16:12:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void StackWidget::resize_event(ResizeEvent& event)
|
2019-03-15 16:12:06 +01:00
|
|
|
{
|
|
|
|
if (!m_active_widget)
|
|
|
|
return;
|
2019-06-07 11:46:02 +02:00
|
|
|
m_active_widget->set_relative_rect({ {}, event.size() });
|
2019-03-15 16:12:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void StackWidget::child_event(Core::ChildEvent& event)
|
2019-03-15 16:12:06 +01:00
|
|
|
{
|
2020-07-26 17:16:35 +02:00
|
|
|
if (!event.child() || !is<Widget>(*event.child()))
|
2020-02-02 15:07:41 +01:00
|
|
|
return Widget::child_event(event);
|
2021-06-24 19:53:42 +02:00
|
|
|
auto& child = verify_cast<Widget>(*event.child());
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.type() == Event::ChildAdded) {
|
2019-03-15 16:44:51 +01:00
|
|
|
if (!m_active_widget)
|
2019-03-15 16:12:06 +01:00
|
|
|
set_active_widget(&child);
|
2019-03-23 03:53:51 +01:00
|
|
|
else if (m_active_widget != &child)
|
2019-03-15 16:12:06 +01:00
|
|
|
child.set_visible(false);
|
2020-02-02 15:07:41 +01:00
|
|
|
} else if (event.type() == Event::ChildRemoved) {
|
2019-03-15 16:12:06 +01:00
|
|
|
if (m_active_widget == &child) {
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget* new_active_widget = nullptr;
|
2019-06-07 11:46:02 +02:00
|
|
|
for_each_child_widget([&](auto& new_child) {
|
2019-05-27 04:06:01 +02:00
|
|
|
new_active_widget = &new_child;
|
2019-06-07 17:13:23 +02:00
|
|
|
return IterationDecision::Break;
|
2019-05-27 04:06:01 +02:00
|
|
|
});
|
2019-03-15 16:12:06 +01:00
|
|
|
set_active_widget(new_active_widget);
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::child_event(event);
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:22:53 -04:00
|
|
|
Optional<UISize> StackWidget::calculated_min_size() const
|
|
|
|
{
|
|
|
|
return m_active_widget->calculated_min_size();
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:12:06 +01:00
|
|
|
}
|