2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, 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
|
|
|
*/
|
|
|
|
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/StackWidget.h>
|
2020-10-30 18:25:52 +03:00
|
|
|
#include <LibGUI/Window.h>
|
2019-03-15 18:12:06 +03:00
|
|
|
|
2021-03-25 08:54:07 +03:00
|
|
|
REGISTER_WIDGET(GUI, StackWidget);
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
void StackWidget::set_active_widget(Widget* widget)
|
2019-03-15 18:12:06 +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-09-18 19:02:06 +03:00
|
|
|
|
2019-03-15 18:12:06 +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(rect());
|
2020-10-30 17:49:16 +03:00
|
|
|
if (active_widget_had_focus)
|
2020-09-18 19:02:06 +03:00
|
|
|
m_active_widget->set_focus(true);
|
2019-03-15 18:12:06 +03:00
|
|
|
m_active_widget->set_visible(true);
|
|
|
|
}
|
2020-10-30 17:49:16 +03:00
|
|
|
|
|
|
|
set_focus_proxy(m_active_widget);
|
|
|
|
|
2019-07-07 22:50:38 +03:00
|
|
|
if (on_active_widget_change)
|
|
|
|
on_active_widget_change(m_active_widget);
|
2019-03-15 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void StackWidget::resize_event(ResizeEvent& event)
|
2019-03-15 18:12:06 +03:00
|
|
|
{
|
|
|
|
if (!m_active_widget)
|
|
|
|
return;
|
2019-06-07 12:46:02 +03:00
|
|
|
m_active_widget->set_relative_rect({ {}, event.size() });
|
2019-03-15 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void StackWidget::child_event(Core::ChildEvent& event)
|
2019-03-15 18:12:06 +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-03-15 18:44:51 +03:00
|
|
|
if (!m_active_widget)
|
2019-03-15 18:12:06 +03:00
|
|
|
set_active_widget(&child);
|
2019-03-23 05:53:51 +03:00
|
|
|
else if (m_active_widget != &child)
|
2019-03-15 18:12:06 +03:00
|
|
|
child.set_visible(false);
|
2020-02-02 17:07:41 +03:00
|
|
|
} else if (event.type() == Event::ChildRemoved) {
|
2019-03-15 18:12:06 +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 05:06:01 +03:00
|
|
|
new_active_widget = &new_child;
|
2019-06-07 18:13:23 +03:00
|
|
|
return IterationDecision::Break;
|
2019-05-27 05:06:01 +03:00
|
|
|
});
|
2019-03-15 18:12:06 +03:00
|
|
|
set_active_widget(new_active_widget);
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
Widget::child_event(event);
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:22:53 +03:00
|
|
|
Optional<UISize> StackWidget::calculated_min_size() const
|
|
|
|
{
|
|
|
|
return m_active_widget->calculated_min_size();
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:12:06 +03:00
|
|
|
}
|