Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/cmake/ui-components.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ target_sources(
components/SceneTree.hpp
components/SilentUpdateCheckBox.hpp
components/SilentUpdateSpinBox.hpp
components/SlidingStackedWidget.cpp
components/SlidingStackedWidget.hpp
components/SourceToolbar.cpp
components/SourceToolbar.hpp
components/SourceTree.cpp
Expand Down
227 changes: 227 additions & 0 deletions frontend/components/SlidingStackedWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/******************************************************************************
Copyright (C) 2025 by Taylor Giampaolo <warchamp7@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "SlidingStackedWidget.hpp"

#include <QTimer>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QGraphicsOpacityEffect>

#include "moc_SlidingStackedWidget.cpp"

SlidingStackedWidget::SlidingStackedWidget(QWidget *parent) : QStackedWidget(parent) {}

void SlidingStackedWidget::setDuration(int duration)
{
duration_ = duration;
}

int SlidingStackedWidget::duration()
{
// TODO: Add Reduced Motion accessibility option in the future and return 0 here if it's set
return duration_;
}

void SlidingStackedWidget::setEasingCurve(enum QEasingCurve::Type easingCurve)
{
easingCurve_ = easingCurve;
}

void SlidingStackedWidget::setVertical(bool vertical)
{
vertical_ = vertical;
}

void SlidingStackedWidget::setWrap(bool wrap)
{
wrap_ = wrap;
}

bool SlidingStackedWidget::advanceNext()
{
int current = currentIndex();

if (wrap_ || (current < count() - 1)) {
advanceToIndex(current + 1);
} else {
return false;
}

return true;
}

bool SlidingStackedWidget::advancePrevious()
{
int current = currentIndex();

if (wrap_ || (current > 0)) {
advanceToIndex(current - 1);
} else {
return false;
}

return true;
}

void SlidingStackedWidget::setNextPageInstantly()
{
nextPageInstant = true;
}

void SlidingStackedWidget::setCurrentIndex(int idx)
{
advanceToIndex(idx);
}

void SlidingStackedWidget::setCurrentWidget(QWidget *widget)
{
advanceToWidget(widget);
}

void SlidingStackedWidget::advanceToIndex(int idx, enum SlideDirection direction)
{
if (idx > count() - 1) {
direction = vertical_ ? TOP_TO_BOTTOM : RIGHT_TO_LEFT;
idx = (idx) % count();
} else if (idx < 0) {
direction = vertical_ ? BOTTOM_TO_TOP : LEFT_TO_RIGHT;
idx = (idx + count()) % count();
}

advanceToWidget(widget(idx), direction);
}

void SlidingStackedWidget::advanceToWidget(QWidget *newWidget, enum SlideDirection direction)
{
int index = currentIndex();
int nextIndex = indexOf(newWidget);

if (isAnimating) {
/* Avoid interrupting animation if trying to advance from and to the same indexes */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a C++-style comment in my opinion, but this is not spelt-out by the code style guidelines that unfortunately can only cover pure C code (as those are based on the Linux kernel guidelines).

The C++ style is part of the language reference doc, but makes functionally no difference, so it's entirely a question of code style.

Right now we don't have any C++ code style guidelines (because the kernel guidelines can only cover C) so we have to introduce some. And in that case I'd vote to use C++-style comments in C++ code as an informal agreement until we have C++ style guidelines in writing.

if (index == activeIndex_ && nextIndex == nextIndex_) {
return;
}

/* Skip rest of animation */
animGroup->setCurrentTime(animGroup->duration());
animGroup->stop();

/* Allow event loop to tick before advancing again */
QTimer::singleShot(1, this, [this, newWidget, direction]() { advanceToWidget(newWidget, direction); });

return;
}

isAnimating = true;
int animDuration = nextPageInstant ? 0 : duration();

enum SlideDirection directionHint;

if (index == nextIndex) {
isAnimating = false;
return;
} else if (index < nextIndex) {
directionHint = vertical_ ? BOTTOM_TO_TOP : RIGHT_TO_LEFT;
} else {
directionHint = vertical_ ? TOP_TO_BOTTOM : LEFT_TO_RIGHT;
}

if (direction == AUTOMATIC) {
direction = directionHint;
}

int offsetX = frameRect().width();
int offsetY = frameRect().height();

widget(nextIndex)->setGeometry(0, 0, offsetX, offsetY);
if (direction == BOTTOM_TO_TOP) {
offsetX = 0;
offsetY = -offsetY;
} else if (direction == TOP_TO_BOTTOM) {
offsetX = 0;
} else if (direction == RIGHT_TO_LEFT) {
offsetX = -offsetX;
offsetY = 0;
} else if (direction == LEFT_TO_RIGHT) {
offsetY = 0;
}

QPoint nextPos = widget(nextIndex)->pos();
QPoint currentPos = widget(index)->pos();
activePos_ = currentPos;
widget(nextIndex)->move(nextPos.x() - offsetX, nextPos.y() - offsetY);

widget(nextIndex)->show();
widget(nextIndex)->raise();

QPropertyAnimation *currentPosAnimation = new QPropertyAnimation(widget(index), "pos");
currentPosAnimation->setDuration(animDuration);
currentPosAnimation->setEasingCurve(easingCurve_);
currentPosAnimation->setStartValue(QPoint(currentPos.x(), currentPos.y()));
currentPosAnimation->setEndValue(QPoint(currentPos.x() + offsetX, currentPos.y() + offsetY));

QGraphicsOpacityEffect *currentOpacityEffect = new QGraphicsOpacityEffect();
widget(index)->setGraphicsEffect(currentOpacityEffect);

QPropertyAnimation *currentOpacityAnimation = new QPropertyAnimation(currentOpacityEffect, "opacity");
currentOpacityAnimation->setDuration(animDuration * 0.5);
currentOpacityAnimation->setStartValue(1);
currentOpacityAnimation->setEndValue(0);

QGraphicsOpacityEffect *nextOpacityEffect = new QGraphicsOpacityEffect();
nextOpacityEffect->setOpacity(0);
widget(nextIndex)->setGraphicsEffect(nextOpacityEffect);

QPropertyAnimation *nextOpacityAnimation = new QPropertyAnimation(nextOpacityEffect, "opacity");
nextOpacityAnimation->setDuration(animDuration * 0.5);
nextOpacityAnimation->setStartValue(0);
nextOpacityAnimation->setEndValue(1);

QPropertyAnimation *nextPosAnimation = new QPropertyAnimation(widget(nextIndex), "pos");
nextPosAnimation->setDuration(animDuration);
nextPosAnimation->setEasingCurve(easingCurve_);
nextPosAnimation->setStartValue(QPoint(-offsetX + nextPos.x(), -offsetY + nextPos.y()));
nextPosAnimation->setEndValue(QPoint(nextPos.x(), nextPos.y()));

animGroup = new QParallelAnimationGroup;
animGroup->addAnimation(currentPosAnimation);
animGroup->addAnimation(nextPosAnimation);
animGroup->addAnimation(currentOpacityAnimation);
animGroup->addAnimation(nextOpacityAnimation);

QObject::connect(animGroup, &QParallelAnimationGroup::finished, this, &SlidingStackedWidget::animationDoneSlot);

nextIndex_ = nextIndex;
activeIndex_ = index;
isAnimating = true;
animGroup->start(QAbstractAnimation::DeleteWhenStopped);

nextPageInstant = false;
}

void SlidingStackedWidget::animationDoneSlot()
{
QStackedWidget::setCurrentIndex(nextIndex_);

widget(activeIndex_)->hide();
widget(activeIndex_)->move(activePos_);

isAnimating = false;

emit animationFinished();
}
73 changes: 73 additions & 0 deletions frontend/components/SlidingStackedWidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/******************************************************************************
Copyright (C) 2025 by Taylor Giampaolo <warchamp7@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#pragma once

#include <QStackedWidget>
#include <QEasingCurve>
#include <QParallelAnimationGroup>

class SlidingStackedWidget : public QStackedWidget {
Q_OBJECT

public:
enum SlideDirection { LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP, AUTOMATIC };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum SlideDirection { LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP, AUTOMATIC };
enum class SlideDirection { SlideInvalid, SlideLeftToRight, SlideRightToLeft, SlideTopToBottom, SlideBottomToTop, SlideAutomatic };

Use class enum for type safety, use less generic names that spell out their meaning even by just looking at the value in isolation, ensure that uninitialised default value is never a "valid" value, so that it always has to be set/initialised explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't Slide be redundant as a prefix since I would generally expect these values to be used as Ex. SlideDirection::LeftToRight?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, also if it's part of the widget's API, you get additional context.

SlidingStackedWidget::Direction::LeftToRight (as its full name would be) provides enough context I'd like to think.


explicit SlidingStackedWidget(QWidget *parent);

void setDuration(int duration);
int duration();
void setEasingCurve(enum QEasingCurve::Type easingCurve);
void setVertical(bool vertical = true);

void setNextPageInstantly();
void advanceToIndex(int idx, enum SlideDirection direction = AUTOMATIC);
void advanceToWidget(QWidget *widget, enum SlideDirection direction = AUTOMATIC);

void setWrap(bool wrap);

public slots:
void setCurrentIndex(int idx);
void setCurrentWidget(QWidget *widget);

bool advanceNext();
bool advancePrevious();

signals:
void animationFinished(void);

protected:
bool nextPageInstant = false;
bool isAnimating = false;

int duration_ = 340;

bool vertical_ = false;
bool wrap_ = false;
enum QEasingCurve::Type easingCurve_ = QEasingCurve::OutQuad;

int activeIndex_ = 0;
QPoint activePos_ = QPoint(0, 0);

int nextIndex_ = 0;

QList<QWidget *> blockedPageList;
QParallelAnimationGroup *animGroup = nullptr;

protected slots:
void animationDoneSlot(void);
};