summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/ScrollableContainerWidget.cpp
blob: 4a348195a5c514a25a9e94fd84fd73b33d8f47ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * 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_position();
    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();
}

}