Skip to content

Commit

Permalink
Window fly in/out animation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiesler committed Jul 24, 2020
1 parent 2070d8c commit bcea1fe
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 84 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ add_executable(VolumeController
src/volumecontroller/ui/gridlayout.h
src/volumecontroller/ui/volumecontrollist.cpp
src/volumecontroller/ui/volumecontrollist.h
src/volumecontroller/ui/windowfadeanimation.cpp
src/volumecontroller/ui/windowfadeanimation.h
src/volumecontroller/ui/animations.cpp
src/volumecontroller/ui/animations.h
src/volumecontroller/ui/volumeicons.cpp
src/volumecontroller/ui/volumeicons.h
src/volumecontroller/ui/devicevolumecontroller.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/volumecontroller/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char *argv[])
QApplication::setStyle(style);

VolumeController w(nullptr, *style);
w.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
w.setWindowFlags(Qt::Tool | Qt::FramelessWindowHint/* | Qt::WindowStaysOnTopHint*/);
w.setAttribute(Qt::WA_TranslucentBackground);
w.setAttribute(Qt::WA_QuitOnClose);

Expand Down
28 changes: 28 additions & 0 deletions src/volumecontroller/ui/animations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "animations.h"
#include <QWidget>
#include <QDebug>

void FadeAnimation::beforeIn() {
InOutAnimation::beforeIn();
target()->show();
}

void FadeAnimation::afterOut() {
InOutAnimation::afterOut();
target()->hide();
}

void FadeAnimation::afterCancelAnimation(bool in) {
target()->setWindowOpacity(in ? endValue() : startValue());
target()->repaint();
}

FadeAnimation::FadeAnimation(QWidget *target)
: InOutAnimation(target, "windowOpacity", 200, QEasingCurve::OutQuint, QEasingCurve::InQuad, 0.0, 1.0) {}

void FlyAnimation::afterCancelAnimation(bool in) {
target()->move(in ? endValue() : startValue());
}

FlyAnimation::FlyAnimation(QWidget *target, const QPoint &inStart, const QPoint &inEnd)
: InOutAnimation(target, "pos", 200, QEasingCurve::OutQuint, QEasingCurve::OutQuint, inStart, inEnd) {}
154 changes: 154 additions & 0 deletions src/volumecontroller/ui/animations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#ifndef WINDOWFADEANIMATION_H
#define WINDOWFADEANIMATION_H

#include <QPropertyAnimation>

#define IS_METHOD_OVERWRITTEN(method, base, derived) (&base::method != &derived::method);

template<typename T, typename Derived>
class InOutAnimation : public QObject {
protected:
void beforeIn() {}

void beforeOut() {}

void afterIn() {}

void afterOut() {}

void afterCancelAnimation(bool in) {}

Derived &derived() noexcept { return static_cast<Derived &>(*this); }

public:
explicit InOutAnimation(QWidget *target, const QByteArray &propertyName, int duration, const QEasingCurve &inCurve, const QEasingCurve &outCurve, const T &inStartValue, const T &inEndValue, QObject *parent = nullptr)
: QObject(parent),
target_(target),
in_(target, propertyName),
out_(target, propertyName) {
in_.setDuration(duration);
in_.setStartValue(inStartValue);
in_.setEndValue(inEndValue);
in_.setEasingCurve(inCurve);

out_.setDuration(duration);
out_.setStartValue(inEndValue);
out_.setEndValue(inStartValue);
out_.setEasingCurve(outCurve);

constexpr bool isAfterOutOverwritten = IS_METHOD_OVERWRITTEN(afterOut, InOutAnimation, Derived);
constexpr bool isAfterInOverwritten = IS_METHOD_OVERWRITTEN(afterIn, InOutAnimation, Derived);
if constexpr(isAfterOutOverwritten) {
QObject::connect(&out_, &QPropertyAnimation::finished, &derived(), &Derived::afterOut);
}
if constexpr(isAfterInOverwritten) {
QObject::connect(&in_, &QPropertyAnimation::finished, &derived(), &Derived::afterIn);
}
}

void in() {
if(isRunningAnimation(in_))
return;
checkNoAnimation();
derived().beforeIn();
currentAnimation_ = &in_;
currentAnimation_->start();
}

void out() {
if(isRunningAnimation(out_))
return;
checkNoAnimation();
derived().beforeOut();
currentAnimation_ = &out_;
currentAnimation_->start();
}

void finishAnimation() {
if(currentAnimation_ != nullptr) {
const bool finished = currentAnimation_->state() == QAbstractAnimation::State::Stopped;
if(!finished)
currentAnimation_->stop();

const auto isIn = currentAnimation_ == &in_;
currentAnimation_ = nullptr;
if(!finished)
derived().afterCancelAnimation(isIn);
}
}

void setStartValue(const T &value) {
startValue_ = value;
out_.setEndValue(value);
in_.setStartValue(value);
}

void setTargetValue(const T &value) {
endValue_ = value;
out_.setStartValue(value);
in_.setEndValue(value);
}

const T &startValue() const noexcept { return startValue_; }
const T &endValue() const noexcept { return endValue_; }

QWidget * target() noexcept { return target_; }

bool isRunning() const noexcept {
return currentAnimation_ && isCurrentRunning();
}

private:
bool isCurrentRunning() const noexcept {
return currentAnimation_->state() == QPropertyAnimation::State::Running;
}

bool isRunningAnimation(const QPropertyAnimation &animation) const noexcept {
return currentAnimation_ == &animation && isCurrentRunning();
}

void clearCurrentAnimation() noexcept { currentAnimation_ = nullptr; }

void checkNoAnimation() {
if(!currentAnimation_)
return;

Q_ASSERT(!isCurrentRunning());
clearCurrentAnimation();
}

QWidget * const target_;

T startValue_;
T endValue_;

QPropertyAnimation in_;
QPropertyAnimation out_;
QPropertyAnimation *currentAnimation_ = nullptr;
};

class FadeAnimation : public InOutAnimation<qreal, FadeAnimation> {
friend class InOutAnimation<qreal, FadeAnimation>;

protected:
void beforeIn();
void afterOut();

void afterCancelAnimation(bool in);

public:
FadeAnimation(QWidget *target);
};

class FlyAnimation : public InOutAnimation<QPoint, FlyAnimation> {
public:
friend class InOutAnimation<QPoint, FlyAnimation>;

protected:
void afterCancelAnimation(bool in);

public:
FlyAnimation(QWidget *target, const QPoint &inStart, const QPoint &inEnd);
};

#endif // WINDOWFADEANIMATION_H
16 changes: 13 additions & 3 deletions src/volumecontroller/ui/volumecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const struct {
VolumeController::VolumeController(QWidget *parent, CustomStyle &style)
: QWidget(parent),
windowFadeAnimation(this),
windowFlyAnimation(this, {0, 0}, {0, 0}),
_style(style)
{
settingsPath = QDir::cleanPath(QApplication::applicationDirPath() + QDir::separator() + "settings.ini");
Expand Down Expand Up @@ -163,18 +164,22 @@ void VolumeController::onApplicationInactive(const QWidget *activeWindow) {

void VolumeController::showEvent(QShowEvent *) {
windowFadeAnimation.finishAnimation();
windowFlyAnimation.finishAnimation();
}

void VolumeController::hideEvent(QHideEvent *) {
windowFadeAnimation.finishAnimation();
windowFlyAnimation.finishAnimation();
}

void VolumeController::fadeOut() {
windowFadeAnimation.fadeOut();
windowFadeAnimation.out();
windowFlyAnimation.out();
}

void VolumeController::fadeIn() {
windowFadeAnimation.fadeIn();
windowFadeAnimation.in();
windowFlyAnimation.in();
activateWindow();
}

Expand Down Expand Up @@ -214,5 +219,10 @@ void VolumeController::setStyleTheme(const Theme &theme){

void VolumeController::reposition() {
const auto rect = QApplication::primaryScreen()->availableGeometry();
move(rect.width() - width(), rect.height() - height());
const QPoint visiblePoint(rect.width() - width(), rect.height() - height());
const QPoint fadeOutPoint(visiblePoint.x(), visiblePoint.y() + height());
if(isVisible() && !windowFlyAnimation.isRunning())
move(visiblePoint);
windowFlyAnimation.setStartValue(fadeOutPoint);
windowFlyAnimation.setTargetValue(visiblePoint);
}
5 changes: 3 additions & 2 deletions src/volumecontroller/ui/volumecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "volumecontrollist.h"
#include "volumelistitem.h"
#include "devicevolumecontroller.h"
#include "windowfadeanimation.h"
#include "animations.h"
#include "customstyle.h"

#include <QSystemTrayIcon>
Expand Down Expand Up @@ -63,7 +63,8 @@ class VolumeController : public QWidget {
void reposition();

DeviceVolumeController *deviceVolumeController = nullptr;
WindowFadeAnimation windowFadeAnimation;
FadeAnimation windowFadeAnimation;
FlyAnimation windowFlyAnimation;
CustomStyle &_style;

bool transparentTheme = false;
Expand Down
49 changes: 0 additions & 49 deletions src/volumecontroller/ui/windowfadeanimation.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions src/volumecontroller/ui/windowfadeanimation.h

This file was deleted.

0 comments on commit bcea1fe

Please sign in to comment.