7#include "decoratedwindow.h"
8#include "decoration_p.h"
9#include "decorationbutton.h"
10#include "decorationsettings.h"
11#include "private/decoratedwindowprivate.h"
12#include "private/decorationbridge.h"
14#include <QCoreApplication>
23DecorationBridge *findBridge(
const QVariantList &args)
25 for (
const auto &arg : args) {
26 if (
auto bridge = arg.toMap().value(QStringLiteral(
"bridge")).value<DecorationBridge *>()) {
40DecorationState::DecorationState()
41 : d(new DecorationStateData)
45DecorationState::DecorationState(
const DecorationState &other)
50DecorationState::~DecorationState()
54std::shared_ptr<DecorationState> DecorationState::clone()
const
56 return std::make_shared<DecorationState>(*
this);
64void DecorationState::setBorders(
const QMarginsF &borders)
69Decoration::Private::Private(Decoration *deco,
const QVariantList &args)
71 , bridge(findBridge(args))
72 , client(std::shared_ptr<DecoratedWindow>(new DecoratedWindow(deco, bridge)))
80 if (sectionUnderMouse == section) {
83 sectionUnderMouse = section;
84 Q_EMIT q->sectionUnderMouseChanged(sectionUnderMouse);
87void Decoration::Private::updateSectionUnderMouse(
const QPoint &mousePosition)
89 if (titleBar.toRect().contains(mousePosition)) {
93 const QSizeF size = q->size();
94 const QMarginsF borders = current->borders();
95 const int corner = 2 * settings->largeSpacing();
96 const bool left = mousePosition.
x() < borders.
left();
97 const bool top = mousePosition.
y() < borders.
top();
98 const bool bottom = mousePosition.
y() >= size.
height() - borders.
bottom();
101 if (top && mousePosition.
y() < titleBar.top() + corner) {
103 }
else if (mousePosition.
y() >= size.
height() - borders.
bottom() - corner && mousePosition.
y() >= titleBar.bottom()) {
111 if (top && mousePosition.
y() < titleBar.top() + corner) {
113 }
else if (mousePosition.
y() >= size.
height() - borders.
bottom() - corner && mousePosition.
y() >= titleBar.bottom()) {
121 if (mousePosition.
y() >= titleBar.bottom()) {
122 if (mousePosition.
x() < borders.
left() + corner) {
124 }
else if (mousePosition.
x() >= size.
width() - borders.
right() - corner) {
135 if (mousePosition.
y() < titleBar.top()) {
136 if (mousePosition.
x() < borders.
left() + corner) {
138 }
else if (mousePosition.
x() >= size.
width() - borders.
right() - corner) {
151void Decoration::Private::addButton(DecorationButton *button)
153 Q_ASSERT(!buttons.contains(button));
156 auto it = buttons.begin();
157 while (it != buttons.end()) {
158 if (*it ==
static_cast<DecorationButton *
>(o)) {
159 it = buttons.erase(it);
167Decoration::Decoration(
QObject *parent,
const QVariantList &args)
169 , d(new Private(this, args))
173Decoration::~Decoration() =
default;
177 return d->client.get();
180void Decoration::requestClose()
182 d->client->d->requestClose();
185void Decoration::requestContextHelp()
187 d->client->d->requestContextHelp();
190void Decoration::requestMinimize()
192 d->client->d->requestMinimize();
195void Decoration::requestToggleOnAllDesktops()
197 d->client->d->requestToggleOnAllDesktops();
200void Decoration::requestToggleShade()
202 d->client->d->requestToggleShade();
205void Decoration::requestToggleKeepAbove()
207 d->client->d->requestToggleKeepAbove();
210void Decoration::requestToggleKeepBelow()
212 d->client->d->requestToggleKeepBelow();
215#if KDECORATIONS3_ENABLE_DEPRECATED_SINCE(5, 21)
224 d->client->d->requestShowWindowMenu(
rect);
227void Decoration::requestShowToolTip(
const QString &text)
229 d->client->d->requestShowToolTip(text);
232void Decoration::requestHideToolTip()
234 d->client->d->requestHideToolTip();
239 d->client->d->requestToggleMaximization(buttons);
242void Decoration::showApplicationMenu(
int actionId)
244 const auto it = std::find_if(d->buttons.constBegin(), d->buttons.constEnd(), [](DecorationButton *button) {
245 return button->type() == DecorationButtonType::ApplicationMenu;
247 if (it != d->buttons.constEnd()) {
248 requestShowApplicationMenu((*it)->geometry().toRect(), actionId);
252void Decoration::requestShowApplicationMenu(
const QRect &rect,
int actionId)
254 d->client->d->requestShowApplicationMenu(
rect, actionId);
257void Decoration::setBlurRegion(
const QRegion ®ion)
259 if (d->blurRegion != region) {
260 d->blurRegion = region;
261 Q_EMIT blurRegionChanged();
265void Decoration::setBorders(
const QMarginsF &borders)
267 if (d->next->borders() != borders) {
268 setState([borders](DecorationState *state) {
269 state->setBorders(borders);
274void Decoration::setResizeOnlyBorders(
const QMarginsF &borders)
276 if (d->resizeOnlyBorders != borders) {
277 d->resizeOnlyBorders = borders;
278 Q_EMIT resizeOnlyBordersChanged();
284 if (d->titleBar !=
rect) {
290void Decoration::setOpaque(
bool opaque)
292 if (d->opaque !=
opaque) {
298void Decoration::setShadow(
const std::shared_ptr<DecorationShadow> &shadow)
300 if (d->shadow !=
shadow) {
308 return d->blurRegion;
313 return d->current->borders();
316QMarginsF Decoration::resizeOnlyBorders()
const
318 return d->resizeOnlyBorders;
328 return d->sectionUnderMouse;
336bool Decoration::isOpaque()
const
341qreal Decoration::borderLeft()
const
343 return d->current->borders().left();
346qreal Decoration::resizeOnlyBorderLeft()
const
348 return d->resizeOnlyBorders.left();
351qreal Decoration::borderRight()
const
353 return d->current->borders().right();
356qreal Decoration::resizeOnlyBorderRight()
const
358 return d->resizeOnlyBorders.right();
361qreal Decoration::borderTop()
const
363 return d->current->borders().top();
366qreal Decoration::resizeOnlyBorderTop()
const
368 return d->resizeOnlyBorders.top();
371qreal Decoration::borderBottom()
const
373 return d->current->borders().bottom();
376qreal Decoration::resizeOnlyBorderBottom()
const
378 return d->resizeOnlyBorders.bottom();
381QSizeF Decoration::size()
const
383 const QMarginsF b = d->current->borders();
384 return QSizeF(d->client->width() + b.
left() + b.
right(), (d->client->isShaded() ? 0 : d->client->height()) + b.
top() + b.
bottom());
392bool Decoration::event(
QEvent *event)
394 switch (event->type()) {
396 hoverEnterEvent(
static_cast<QHoverEvent *
>(event));
399 hoverLeaveEvent(
static_cast<QHoverEvent *
>(event));
405 mousePressEvent(
static_cast<QMouseEvent *
>(event));
408 mouseReleaseEvent(
static_cast<QMouseEvent *
>(event));
421void Decoration::hoverEnterEvent(
QHoverEvent *event)
423 for (DecorationButton *button : d->buttons) {
426 auto flooredPos =
QPoint(std::floor(
event->position().x()), std::floor(
event->position().y()));
427 d->updateSectionUnderMouse(flooredPos);
430void Decoration::hoverLeaveEvent(
QHoverEvent *event)
432 for (DecorationButton *button : d->buttons) {
440 for (DecorationButton *button : d->buttons) {
441 if (!button->isEnabled() || !button->isVisible()) {
444 const bool hovered = button->isHovered();
445 const bool contains = button->contains(
event->position());
446 if (!hovered && contains) {
449 }
else if (hovered && !contains) {
452 }
else if (hovered && contains) {
456 auto flooredPos =
QPoint(std::floor(
event->position().x()), std::floor(
event->position().y()));
457 d->updateSectionUnderMouse(flooredPos);
462 for (DecorationButton *button : d->buttons) {
463 if (button->isPressed()) {
471void Decoration::mousePressEvent(
QMouseEvent *event)
473 for (DecorationButton *button : d->buttons) {
474 if (button->isHovered()) {
475 if (button->acceptedButtons().testFlag(
event->button())) {
478 event->setAccepted(
true);
484void Decoration::mouseReleaseEvent(
QMouseEvent *event)
486 for (DecorationButton *button : d->buttons) {
487 if (button->isPressed() && button->acceptedButtons().testFlag(
event->button())) {
493 d->updateSectionUnderMouse(
event->pos());
498 for (DecorationButton *button : d->buttons) {
499 if (button->contains(
event->position())) {
501 event->setAccepted(
true);
506void Decoration::update(
const QRectF &r)
511void Decoration::update()
528 return std::make_shared<DecorationState>();
549 callback(d->next.get());
550 Q_EMIT nextStateChanged(d->next);
555 if (d->current == state) {
559 const auto previous = d->current;
563 if (previous->borders() != state->borders()) {
567 Q_EMIT currentStateChanged(state);
572#include "moc_decoration.cpp"
The Client which gets decorated.
std::shared_ptr< DecorationState > currentState() const
Returns the currently applied state.
virtual std::shared_ptr< DecorationState > createState()
Create a state container.
DecoratedWindow * window() const
The DecoratedWindow for this Decoration.
QRectF titleBar
The titleBar is the area inside the Decoration containing all controls (e.g.
std::shared_ptr< DecorationState > nextState() const
Returns the next state, i.e.
void requestShowWindowMenu(const QRect &rect)
void setSettings(const std::shared_ptr< DecorationSettings > &settings)
Invoked by the framework to set the Settings for this Decoration before init is invoked.
QRegion blurRegion() const
The decoration's blur region in local coordinates.
QRectF rect() const
The decoration's geometry in local coordinates.
void apply(std::shared_ptr< DecorationState > state)
Qt::WindowFrameSection sectionUnderMouse
This property denotes the part of the Decoration which is currently under the mouse pointer.
void setTitleBar(const QRectF &rect)
An implementation has to invoke this method whenever the area containing the controls and caption cha...
std::shared_ptr< DecorationSettings > settings() const
void setState(std::function< void(DecorationState *state)> callback)
Notifies the framework that the decoration state has changed.
bool opaque
Whether the Decoration is fully opaque.
std::shared_ptr< DecorationShadow > shadow() const
DecorationShadow for this Decoration.
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Framework for creating window decorations.
QCoreApplication * instance()
bool sendEvent(QObject *receiver, QEvent *event)
qreal bottom() const const
qreal right() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
virtual bool event(QEvent *e)
bool isNull() const const
QRect toAlignedRect() const const
qreal height() const const
qreal width() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)