added buttons to the custom window frame

This commit is contained in:
fourtf 2018-01-24 20:27:56 +01:00
parent 8c51df31e6
commit f4863cbccb
6 changed files with 210 additions and 41 deletions

View file

@ -167,7 +167,8 @@ SOURCES += \
src/widgets/settingspages/ignoreuserspage.cpp \ src/widgets/settingspages/ignoreuserspage.cpp \
src/widgets/settingspages/ignoremessagespage.cpp \ src/widgets/settingspages/ignoremessagespage.cpp \
src/widgets/settingspages/specialchannelspage.cpp \ src/widgets/settingspages/specialchannelspage.cpp \
src/widgets/settingspages/keyboardsettingspage.cpp src/widgets/settingspages/keyboardsettingspage.cpp \
src/widgets/helper/titlebarbutton.cpp
HEADERS += \ HEADERS += \
src/precompiled_header.hpp \ src/precompiled_header.hpp \
@ -275,7 +276,8 @@ HEADERS += \
src/widgets/settingspages/ignoremessagespage.hpp \ src/widgets/settingspages/ignoremessagespage.hpp \
src/widgets/settingspages/specialchannelspage.hpp \ src/widgets/settingspages/specialchannelspage.hpp \
src/widgets/settingspages/keyboardsettings.hpp \ src/widgets/settingspages/keyboardsettings.hpp \
src/widgets/settingspages/keyboardsettingspage.hpp src/widgets/settingspages/keyboardsettingspage.hpp \
src/widgets/helper/titlebarbutton.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc resources/resources.qrc

View file

@ -23,7 +23,7 @@
#define WM_DPICHANGED 0x02E0 #define WM_DPICHANGED 0x02E0
#endif #endif
#include "widgets/helper/rippleeffectlabel.hpp" #include "widgets/helper/titlebarbutton.hpp"
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -72,38 +72,39 @@ void BaseWindow::init()
this->titleLabel = title; this->titleLabel = title;
// buttons // buttons
RippleEffectLabel *min = new RippleEffectLabel; TitleBarButton *_minButton = new TitleBarButton;
min->getLabel().setText("min"); _minButton->setFixedSize(46, 30);
min->setFixedSize(46, 30); _minButton->setButtonStyle(TitleBarButton::Minimize);
RippleEffectLabel *max = new RippleEffectLabel; TitleBarButton *_maxButton = new TitleBarButton;
max->setFixedSize(46, 30); _maxButton->setFixedSize(46, 30);
max->getLabel().setText("max"); _maxButton->setButtonStyle(TitleBarButton::Maximize);
RippleEffectLabel *exit = new RippleEffectLabel; TitleBarButton *_exitButton = new TitleBarButton;
exit->setFixedSize(46, 30); _exitButton->setFixedSize(46, 30);
exit->getLabel().setText("exit"); _exitButton->setButtonStyle(TitleBarButton::Close);
QObject::connect(min, &RippleEffectLabel::clicked, this, [this] { QObject::connect(_minButton, &TitleBarButton::clicked, this, [this] {
this->setWindowState(Qt::WindowMinimized | this->windowState()); this->setWindowState(Qt::WindowMinimized | this->windowState());
}); });
QObject::connect(max, &RippleEffectLabel::clicked, this, [this] { QObject::connect(_maxButton, &TitleBarButton::clicked, this, [this] {
this->setWindowState(this->windowState() == Qt::WindowMaximized this->setWindowState(this->windowState() == Qt::WindowMaximized
? Qt::WindowActive ? Qt::WindowActive
: Qt::WindowMaximized); : Qt::WindowMaximized);
}); });
QObject::connect(exit, &RippleEffectLabel::clicked, this, [this] { this->close(); }); QObject::connect(_exitButton, &TitleBarButton::clicked, this,
[this] { this->close(); });
this->minButton = min; this->minButton = _minButton;
this->maxButton = max; this->maxButton = _maxButton;
this->exitButton = exit; this->exitButton = _exitButton;
this->buttons.push_back(min); this->buttons.push_back(_minButton);
this->buttons.push_back(max); this->buttons.push_back(_maxButton);
this->buttons.push_back(exit); this->buttons.push_back(_exitButton);
buttonLayout->addStretch(1); buttonLayout->addStretch(1);
buttonLayout->addWidget(min); buttonLayout->addWidget(_minButton);
buttonLayout->addWidget(max); buttonLayout->addWidget(_maxButton);
buttonLayout->addWidget(exit); buttonLayout->addWidget(_exitButton);
buttonLayout->setSpacing(0); buttonLayout->setSpacing(0);
} }
this->layoutBase = new QWidget(this); this->layoutBase = new QWidget(this);
@ -161,23 +162,34 @@ void BaseWindow::refreshTheme()
palette.setColor(QPalette::Foreground, this->themeManager.windowText); palette.setColor(QPalette::Foreground, this->themeManager.windowText);
this->setPalette(palette); this->setPalette(palette);
for (RippleEffectLabel *label : this->buttons) { for (RippleEffectButton *button : this->buttons) {
label->setMouseEffectColor(this->themeManager.windowText); button->setMouseEffectColor(this->themeManager.windowText);
} }
} }
void BaseWindow::addTitleBarButton(const QString &text, std::function<void()> onClicked) void BaseWindow::addTitleBarButton(const TitleBarButton::Style &style,
std::function<void()> onClicked)
{ {
RippleEffectLabel *label = new RippleEffectLabel; TitleBarButton *button = new TitleBarButton;
label->getLabel().setText(text);
this->buttons.push_back(label); this->buttons.push_back(button);
this->titlebarBox->insertWidget(2, label); this->titlebarBox->insertWidget(2, button);
QObject::connect(label, &RippleEffectLabel::clicked, this, [onClicked] { onClicked(); }); button->setButtonStyle(style);
QObject::connect(button, &TitleBarButton::clicked, this, [onClicked] { onClicked(); });
} }
void BaseWindow::changeEvent(QEvent *) void BaseWindow::changeEvent(QEvent *)
{ {
TooltipWidget::getInstance()->hide(); TooltipWidget::getInstance()->hide();
#ifdef USEWINSDK
if (this->hasCustomWindowFrame()) {
this->maxButton->setButtonStyle(this->windowState() & Qt::WindowMaximized
? TitleBarButton::Unmaximize
: TitleBarButton::Maximize);
}
#endif
} }
void BaseWindow::leaveEvent(QEvent *) void BaseWindow::leaveEvent(QEvent *)

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "basewidget.hpp" #include "basewidget.hpp"
#include "widgets/helper/titlebarbutton.hpp"
#include <functional> #include <functional>
@ -8,7 +9,8 @@ class QHBoxLayout;
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
class RippleEffectLabel; class RippleEffectButton;
class TitleBarButton;
class BaseWindow : public BaseWidget class BaseWindow : public BaseWidget
{ {
@ -20,7 +22,7 @@ public:
QWidget *getLayoutContainer(); QWidget *getLayoutContainer();
bool hasCustomWindowFrame(); bool hasCustomWindowFrame();
void addTitleBarButton(const QString &text, std::function<void()> onClicked); void addTitleBarButton(const TitleBarButton::Style &style, std::function<void()> onClicked);
void setStayInScreenRect(bool value); void setStayInScreenRect(bool value);
bool getStayInScreenRect() const; bool getStayInScreenRect() const;
@ -50,11 +52,11 @@ private:
QHBoxLayout *titlebarBox; QHBoxLayout *titlebarBox;
QWidget *titleLabel; QWidget *titleLabel;
RippleEffectLabel *minButton; TitleBarButton *minButton;
RippleEffectLabel *maxButton; TitleBarButton *maxButton;
RippleEffectLabel *exitButton; TitleBarButton *exitButton;
QWidget *layoutBase; QWidget *layoutBase;
std::vector<RippleEffectLabel *> buttons; std::vector<RippleEffectButton *> buttons;
}; };
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -0,0 +1,127 @@
#include "titlebarbutton.hpp"
namespace chatterino {
namespace widgets {
TitleBarButton::TitleBarButton()
: RippleEffectButton(nullptr)
{
}
TitleBarButton::Style TitleBarButton::getButtonStyle() const
{
return this->style;
}
void TitleBarButton::setButtonStyle(Style _style)
{
this->style = _style;
this->update();
}
void TitleBarButton::resizeEvent(QResizeEvent *)
{
if (this->style & (Maximize | Minimize | Unmaximize | Close)) {
this->setFixedWidth(this->height() * 46 / 30);
} else {
this->setFixedWidth(this->height());
}
}
void TitleBarButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QColor color = "#000";
QColor background = "#fff";
int xD = this->height() / 3;
int centerX = this->width() / 2;
painter.setRenderHint(QPainter::Antialiasing, false);
switch (this->style) {
case Minimize: {
painter.fillRect(centerX - xD / 2, xD * 3 / 2, xD, 1, color);
break;
}
case Maximize: {
painter.setPen(color);
painter.drawRect(centerX - xD / 2, xD, xD - 1, xD - 1);
break;
}
case Unmaximize: {
int xD2 = xD * 1 / 5;
int xD3 = xD * 4 / 5;
painter.drawRect(centerX - xD / 2 + xD2, xD, xD3, xD3);
painter.fillRect(centerX - xD / 2, xD + xD2, xD3, xD3, QColor("#fff"));
painter.drawRect(centerX - xD / 2, xD + xD2, xD3, xD3);
break;
}
case Close: {
QRect rect(centerX - xD / 2, xD, xD - 1, xD - 1);
painter.setPen(QPen(color, 1));
painter.drawLine(rect.topLeft(), rect.bottomRight());
painter.drawLine(rect.topRight(), rect.bottomLeft());
break;
}
case User: {
color = QColor("#333");
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
auto a = xD / 3;
QPainterPath path;
painter.save();
painter.translate(3, 3);
path.arcMoveTo(a, 4 * a, 6 * a, 6 * a, 0);
path.arcTo(a, 4 * a, 6 * a, 6 * a, 0, 180);
painter.fillPath(path, color);
painter.setBrush(background);
painter.drawEllipse(2 * a, 1 * a, 4 * a, 4 * a);
painter.setBrush(color);
painter.drawEllipse(2.5 * a, 1.5 * a, 3 * a + 1, 3 * a);
painter.restore();
break;
}
case Settings: {
color = QColor("#333");
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
painter.save();
painter.translate(3, 3);
auto a = xD / 3;
QPainterPath path;
path.arcMoveTo(a, a, 6 * a, 6 * a, 0 - (360 / 32.0));
for (int i = 0; i < 8; i++) {
path.arcTo(a, a, 6 * a, 6 * a, i * (360 / 8.0) - (360 / 32.0), (360 / 32.0));
path.arcTo(2 * a, 2 * a, 4 * a, 4 * a, i * (360 / 8.0) + (360 / 32.0),
(360 / 32.0));
}
painter.strokePath(path, color);
painter.fillPath(path, color);
painter.setBrush(background);
painter.drawEllipse(3 * a, 3 * a, 2 * a, 2 * a);
painter.restore();
break;
}
}
this->fancyPaint(painter);
}
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,25 @@
#pragma once
#include "widgets/helper/rippleeffectbutton.hpp"
namespace chatterino {
namespace widgets {
class TitleBarButton : public RippleEffectButton
{
public:
enum Style { Minimize = 1, Maximize = 2, Unmaximize = 4, Close = 8, User = 16, Settings = 32 };
TitleBarButton();
Style getButtonStyle() const;
void setButtonStyle(Style style);
protected:
virtual void paintEvent(QPaintEvent *) override;
virtual void resizeEvent(QResizeEvent *) override;
private:
Style style;
};
} // namespace widgets
} // namespace chatterino

View file

@ -37,9 +37,10 @@ Window::Window(const QString &windowName, singletons::ThemeManager &_themeManage
}); });
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
this->addTitleBarButton( this->addTitleBarButton(TitleBarButton::Settings, [] {
"preferences", [] { singletons::WindowManager::getInstance().showSettingsDialog(); }); singletons::WindowManager::getInstance().showSettingsDialog();
this->addTitleBarButton("user", [this] { });
this->addTitleBarButton(TitleBarButton::User, [this] {
singletons::WindowManager::getInstance().showAccountSelectPopup(QCursor::pos()); singletons::WindowManager::getInstance().showAccountSelectPopup(QCursor::pos());
}); });
} }