mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 17:28:11 +00:00
LibGUI: Add ScrollableContainerWidget :^)
This widget provides a scrollable view onto another (child) widget. If the child is larger than the parent, scrollbars are provided for panning around the child.
This commit is contained in:
parent
89f5f92b7e
commit
18d344609f
3 changed files with 111 additions and 0 deletions
74
Userland/Libraries/LibGUI/ScrollableContainerWidget.cpp
Normal file
74
Userland/Libraries/LibGUI/ScrollableContainerWidget.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGUI/Layout.h>
|
||||
#include <LibGUI/ScrollableContainerWidget.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
ScrollableContainerWidget::ScrollableContainerWidget()
|
||||
{
|
||||
}
|
||||
|
||||
ScrollableContainerWidget::~ScrollableContainerWidget()
|
||||
{
|
||||
}
|
||||
|
||||
void ScrollableContainerWidget::did_scroll()
|
||||
{
|
||||
AbstractScrollableWidget::did_scroll();
|
||||
update_widget_position();
|
||||
}
|
||||
|
||||
void ScrollableContainerWidget::update_widget_position()
|
||||
{
|
||||
if (!m_widget)
|
||||
return;
|
||||
m_widget->move_to(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
||||
}
|
||||
|
||||
void ScrollableContainerWidget::update_widget_size()
|
||||
{
|
||||
if (!m_widget)
|
||||
return;
|
||||
m_widget->do_layout();
|
||||
if (m_widget->is_shrink_to_fit() && m_widget->layout()) {
|
||||
auto new_size = frame_inner_rect().size();
|
||||
auto preferred_size = m_widget->layout()->preferred_size();
|
||||
if (preferred_size.width() != -1)
|
||||
new_size.set_width(preferred_size.width());
|
||||
if (preferred_size.height() != -1)
|
||||
new_size.set_height(preferred_size.height());
|
||||
m_widget->resize(new_size);
|
||||
set_content_size(new_size);
|
||||
}
|
||||
}
|
||||
|
||||
void ScrollableContainerWidget::resize_event(GUI::ResizeEvent& event)
|
||||
{
|
||||
AbstractScrollableWidget::resize_event(event);
|
||||
update_widget_size();
|
||||
}
|
||||
|
||||
void ScrollableContainerWidget::set_widget(GUI::Widget* widget)
|
||||
{
|
||||
if (m_widget == widget)
|
||||
return;
|
||||
|
||||
if (m_widget)
|
||||
remove_child(*m_widget);
|
||||
|
||||
m_widget = widget;
|
||||
|
||||
if (m_widget) {
|
||||
add_child(*m_widget);
|
||||
m_widget->move_to_back();
|
||||
}
|
||||
update_widget_size();
|
||||
update_widget_position();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue