File indexing completed on 2024-05-05 05:35:17

0001 #ifndef OXYGEN_DECORATION_H
0002 #define OXYGEN_DECORATION_H
0003 
0004 /*
0005  * SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0006  * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007  * SPDX-FileCopyrightText: 2015 David Edmundson <davidedmundson@kde.org>
0008  *
0009  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0010  */
0011 
0012 #include "oxygen.h"
0013 #include "oxygensettings.h"
0014 
0015 #include <KDecoration2/DecoratedClient>
0016 #include <KDecoration2/Decoration>
0017 #include <KDecoration2/DecorationSettings>
0018 
0019 #include <QPalette>
0020 #include <QPropertyAnimation>
0021 #include <QVariant>
0022 
0023 namespace KDecoration2
0024 {
0025 class DecorationButton;
0026 class DecorationButtonGroup;
0027 }
0028 
0029 namespace Oxygen
0030 {
0031 class SizeGrip;
0032 
0033 class Decoration : public KDecoration2::Decoration
0034 {
0035     Q_OBJECT
0036 
0037     //* declare active state opacity
0038     Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
0039 
0040 public:
0041     //* constructor
0042     explicit Decoration(QObject *parent = nullptr, const QVariantList &args = QVariantList());
0043 
0044     //* destructor
0045     ~Decoration() override;
0046 
0047     //* paint
0048     void paint(QPainter *painter, const QRect &repaintRegion) override;
0049 
0050     //* internal settings
0051     InternalSettingsPtr internalSettings() const
0052     {
0053         return m_internalSettings;
0054     }
0055 
0056     //* caption height
0057     int captionHeight() const;
0058 
0059     //* button height
0060     int buttonHeight() const;
0061 
0062     //*@name active state change animation
0063     //@{
0064     bool isAnimated(void) const
0065     {
0066         return m_animation->state() == QPropertyAnimation::Running;
0067     }
0068 
0069     void setOpacity(qreal);
0070 
0071     qreal opacity(void) const
0072     {
0073         return m_opacity;
0074     }
0075 
0076     //@}
0077 
0078     //*@name colors
0079     //@{
0080 
0081     QColor titleBarColor(const QPalette &) const;
0082     QColor titleBarColor(const QPalette &, bool active) const;
0083 
0084     QColor fontColor(const QPalette &) const;
0085     QColor fontColor(const QPalette &palette, bool active) const;
0086 
0087     QColor contrastColor(const QPalette &palette) const;
0088     QColor contrastColor(const QColor &color) const;
0089 
0090     //@}
0091 
0092 public Q_SLOTS:
0093     bool init() override;
0094 
0095 private Q_SLOTS:
0096     void reconfigure();
0097     void recalculateBorders();
0098     void updateButtonsGeometry();
0099     void updateButtonsGeometryDelayed();
0100     void updateTitleBar();
0101     void updateAnimationState();
0102     void updateSizeGripVisibility();
0103     void updateShadow();
0104 
0105 private:
0106     //* return the rect in which caption will be drawn
0107     QPair<QRect, Qt::Alignment> captionRect(void) const;
0108 
0109     void createButtons();
0110 
0111     //* window background
0112     void renderWindowBackground(QPainter *, const QRect &, const QPalette &) const;
0113 
0114     //* window border
0115     void renderWindowBorder(QPainter *, const QRect &, const QPalette &) const;
0116 
0117     //* title text
0118     void renderTitleText(QPainter *, const QPalette &) const;
0119 
0120     //* corners
0121     void renderCorners(QPainter *, const QRect &, const QPalette &) const;
0122 
0123     //*@name border size
0124     //@{
0125     int borderSize(bool bottom = false) const;
0126     inline bool hasNoBorders(void) const;
0127     inline bool hasNoSideBorders(void) const;
0128     //@}
0129 
0130     //*@name maximization modes
0131     //@{
0132     inline bool isMaximized(void) const;
0133     inline bool isMaximizedHorizontally(void) const;
0134     inline bool isMaximizedVertically(void) const;
0135     inline bool hideTitleBar(void) const;
0136     //@}
0137 
0138     //*@name size grip
0139     //@{
0140     void createSizeGrip(void);
0141     void deleteSizeGrip(void);
0142     SizeGrip *sizeGrip(void) const
0143     {
0144         return m_sizeGrip;
0145     }
0146     //@}
0147 
0148     InternalSettingsPtr m_internalSettings;
0149 
0150     QList<KDecoration2::DecorationButton *> m_buttons;
0151     KDecoration2::DecorationButtonGroup *m_leftButtons = nullptr;
0152     KDecoration2::DecorationButtonGroup *m_rightButtons = nullptr;
0153 
0154     //* size grip widget
0155     SizeGrip *m_sizeGrip = nullptr;
0156 
0157     //* active state change animation
0158     QPropertyAnimation *m_animation;
0159 
0160     //* active state change opacity
0161     qreal m_opacity = 0;
0162 };
0163 
0164 bool Decoration::hasNoBorders(void) const
0165 {
0166     if (m_internalSettings && m_internalSettings->mask() & BorderSize)
0167         return m_internalSettings->borderSize() == InternalSettings::BorderNone;
0168     else
0169         return settings()->borderSize() == KDecoration2::BorderSize::None;
0170 }
0171 
0172 bool Decoration::hasNoSideBorders(void) const
0173 {
0174     if (m_internalSettings && m_internalSettings->mask() & BorderSize)
0175         return m_internalSettings->borderSize() == InternalSettings::BorderNoSides;
0176     else
0177         return settings()->borderSize() == KDecoration2::BorderSize::NoSides;
0178 }
0179 
0180 bool Decoration::isMaximized(void) const
0181 {
0182     return client()->isMaximized() && !m_internalSettings->drawBorderOnMaximizedWindows();
0183 }
0184 bool Decoration::isMaximizedHorizontally(void) const
0185 {
0186     return client()->isMaximizedHorizontally() && !m_internalSettings->drawBorderOnMaximizedWindows();
0187 }
0188 bool Decoration::isMaximizedVertically(void) const
0189 {
0190     return client()->isMaximizedVertically() && !m_internalSettings->drawBorderOnMaximizedWindows();
0191 }
0192 bool Decoration::hideTitleBar(void) const
0193 {
0194     return m_internalSettings->hideTitleBar() && !client()->isShaded();
0195 }
0196 }
0197 
0198 #endif