Skip to content

Commit

Permalink
Clean up decoration code
Browse files Browse the repository at this point in the history
  • Loading branch information
plfiorini committed Feb 8, 2024
1 parent e9d255f commit 9252018
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
44 changes: 23 additions & 21 deletions src/plugins/decorations/material/materialdecoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ QT_BEGIN_NAMESPACE

namespace QtWaylandClient {

#define BUTTON_WIDTH dp(24)
#define BUTTON_SPACING dp(12)
#define TITLE_BAR_HEIGHT dp(32)
#define TITLE_FONT_SIZE dp(16)
#define WINDOW_BORDER 4 // big enough to resize
static constexpr int ceButtonWidth = 24;
static constexpr int ceButtonSpacing = 12;
static constexpr int ceTitlebarHeight = 32;
static constexpr int ceTitleFontSize = 16;
static constexpr int ceWindowBorderWidth = 4;

QWaylandMaterialDecoration::QWaylandMaterialDecoration()
: QWaylandAbstractDecoration()
Expand Down Expand Up @@ -81,20 +81,20 @@ void QWaylandMaterialDecoration::setIconColor(const QColor &color)

QRectF QWaylandMaterialDecoration::closeButtonRect() const
{
return QRectF(window()->frameGeometry().width() - BUTTON_WIDTH - BUTTON_SPACING,
(margins().top() - BUTTON_WIDTH) / 2, BUTTON_WIDTH, BUTTON_WIDTH);
return QRectF(window()->frameGeometry().width() - ceButtonWidth - ceButtonSpacing,
(margins().top() - ceButtonWidth) / 2, ceButtonWidth, ceButtonWidth);
}

QRectF QWaylandMaterialDecoration::maximizeButtonRect() const
{
return QRectF(window()->frameGeometry().width() - BUTTON_WIDTH * 2 - BUTTON_SPACING * 2,
(margins().top() - BUTTON_WIDTH) / 2, BUTTON_WIDTH, BUTTON_WIDTH);
return QRectF(window()->frameGeometry().width() - ceButtonWidth * 2 - ceButtonSpacing * 2,
(margins().top() - ceButtonWidth) / 2, ceButtonWidth, ceButtonWidth);
}

QRectF QWaylandMaterialDecoration::minimizeButtonRect() const
{
return QRectF(window()->frameGeometry().width() - BUTTON_WIDTH * 3 - BUTTON_SPACING * 3,
(margins().top() - BUTTON_WIDTH) / 2, BUTTON_WIDTH, BUTTON_WIDTH);
return QRectF(window()->frameGeometry().width() - ceButtonWidth * 3 - ceButtonSpacing * 3,
(margins().top() - ceButtonWidth) / 2, ceButtonWidth, ceButtonWidth);
}

QMargins QWaylandMaterialDecoration::margins(MarginsType marginsType) const
Expand All @@ -108,8 +108,9 @@ QMargins QWaylandMaterialDecoration::margins(MarginsType marginsType) const
if (window()
&& ((window()->windowStates() & Qt::WindowMaximized)
|| (window()->windowStates() & Qt::WindowFullScreen)))
return QMargins(0, TITLE_BAR_HEIGHT, 0, 0);
return QMargins(WINDOW_BORDER, TITLE_BAR_HEIGHT, WINDOW_BORDER, WINDOW_BORDER);
return QMargins(0, ceTitlebarHeight, 0, 0);
return QMargins(ceWindowBorderWidth, ceTitlebarHeight, ceWindowBorderWidth,
ceWindowBorderWidth);
}

void QWaylandMaterialDecoration::paint(QPaintDevice *device)
Expand Down Expand Up @@ -138,17 +139,18 @@ void QWaylandMaterialDecoration::paint(QPaintDevice *device)
}

const QRect frameGeometry = window()->frameGeometry();
QRect top(QPoint(WINDOW_BORDER, WINDOW_BORDER), QSize(frameGeometry.width(), margins().top()));
QRect top(QPoint(ceWindowBorderWidth, ceWindowBorderWidth),
QSize(frameGeometry.width(), margins().top()));

QPainter p(device);
p.setRenderHint(QPainter::Antialiasing);

// Title bar
int radius = window()->windowStates() & Qt::WindowMaximized ? 0 : dp(3);
QPainterPath roundedRect;
roundedRect.addRoundedRect(margins().left(), margins().top() - TITLE_BAR_HEIGHT,
roundedRect.addRoundedRect(margins().left(), margins().top() - ceTitlebarHeight,
frameGeometry.width() - margins().left() - margins().right(),
TITLE_BAR_HEIGHT + radius * 2, radius, radius);
ceTitlebarHeight + radius * 2, radius, radius);
p.fillPath(roundedRect.simplified(), m_backgroundColor);

// Borders (transparent so the border is not noticeable)
Expand All @@ -170,21 +172,21 @@ void QWaylandMaterialDecoration::paint(QPaintDevice *device)
}

QRect titleBar = top;
titleBar.setLeft(margins().left() + BUTTON_SPACING);
titleBar.setRight(qFloor(minimizeButtonRect().left()) - BUTTON_SPACING);
titleBar.setLeft(margins().left() + ceButtonSpacing);
titleBar.setRight(qFloor(minimizeButtonRect().left()) - ceButtonSpacing);

p.save();
p.setClipRect(titleBar);
p.setPen(m_textColor);
QSizeF size = m_windowTitle.size();
int dx = qFloor((top.width() - size.width()) / 2);
int dy = qFloor((top.height() - TITLE_FONT_SIZE) / 2);
int dy = qFloor((top.height() - ceTitleFontSize) / 2);
QFont font = p.font();
font.setBold(true);
font.setFamily("Roboto"_L1);
font.setPixelSize(TITLE_FONT_SIZE);
font.setPixelSize(ceTitleFontSize);
p.setFont(font);
QPoint windowTitlePoint(dx, dy - WINDOW_BORDER / 2);
QPoint windowTitlePoint(dx, dy - ceWindowBorderWidth / 2);
p.drawStaticText(windowTitlePoint, m_windowTitle);
p.restore();
}
Expand Down
18 changes: 10 additions & 8 deletions src/plugins/decorations/material/materialdecoration.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ QT_BEGIN_NAMESPACE

namespace QtWaylandClient {

enum Button {
None,
Close,
Maximize,
Minimize
};

class Q_WAYLANDCLIENT_EXPORT QWaylandMaterialDecoration : public QWaylandAbstractDecoration
class QWaylandMaterialDecoration : public QWaylandAbstractDecoration
{
Q_OBJECT
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY
backgroundColorChanged)
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
Q_PROPERTY(QColor iconColor READ iconColor WRITE setIconColor NOTIFY iconColorChanged)
public:
enum Button {
None,
Close,
Maximize,
Minimize
};
Q_DECLARE_FLAGS(Buttons, Button);

QWaylandMaterialDecoration();
virtual ~QWaylandMaterialDecoration() = default;

QColor backgroundColor() const;
void setBackgroundColor(const QColor &color);
Expand Down

0 comments on commit 9252018

Please sign in to comment.