DolphinQt: Make WrapInScrollArea and GetWrappedWidget less hacky.

This commit is contained in:
Jordan Woyak
2025-05-12 22:17:49 -05:00
parent e796e82e8c
commit 543b85a451
18 changed files with 93 additions and 82 deletions

View File

@ -7,43 +7,59 @@
#include <QLayout>
#include <QPalette>
#include <QScrollArea>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QWidget>
QWidget* GetWrappedWidget(QWidget* wrapped_widget, QWidget* to_resize, int margin_width,
int margin_height)
namespace
{
auto* scroll = new QScrollArea;
scroll->setWidget(wrapped_widget);
scroll->setWidgetResizable(true);
scroll->setFrameStyle(QFrame::NoFrame);
if (to_resize != nullptr)
// This scroll area prefers the size of its underlying widget.
class HintingScrollArea final : public QScrollArea
{
public:
HintingScrollArea()
{
// For some reason width() is bigger than it needs to be.
auto min_size = wrapped_widget->minimumSizeHint();
int recommended_width = min_size.width() + margin_width;
int recommended_height = min_size.height() + margin_height;
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setWidgetResizable(true);
to_resize->resize(std::max(recommended_width, to_resize->width()),
std::max(recommended_height, to_resize->height()));
// Make things not look horrendous on Windows.
setFrameStyle(QFrame::NoFrame);
}
QSize sizeHint() const override
{
// Including the scrollbars in our hint can prevent
// a window undersized in one direction gaining unnecessary scrolling in both directions.
const auto scrollbar_padding =
QSize{verticalScrollBar()->sizeHint().width(), horizontalScrollBar()->sizeHint().height()};
const auto size_hint = widget()->sizeHint();
return size_hint + scrollbar_padding;
}
};
} // namespace
QWidget* GetWrappedWidget(QWidget* wrapped_widget)
{
auto* const scroll = new HintingScrollArea;
scroll->setWidget(wrapped_widget);
// Workaround for transparency issues on macOS. Not sure if this is still needed.
scroll->viewport()->setAutoFillBackground(false);
wrapped_widget->setAutoFillBackground(false);
return scroll;
}
void WrapInScrollArea(QWidget* parent, QLayout* wrapped_layout, QWidget* to_resize)
void WrapInScrollArea(QWidget* parent, QLayout* wrapped_layout)
{
if (to_resize == nullptr)
to_resize = parent;
auto* widget = new QWidget;
widget->setLayout(wrapped_layout);
auto* scroll_area = GetWrappedWidget(widget, to_resize, 0, 0);
auto* scroll_area = GetWrappedWidget(widget);
auto* scroll_layout = new QVBoxLayout;
scroll_layout->addWidget(scroll_area);