Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/window/floatingwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "floatingwindow.hpp"

#include <qnamespace.h>
#include <qobject.h>
#include <qqmlengine.h>
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <qwindow.h>

#include "proxywindow.hpp"
#include "windowinterface.hpp"
Expand Down Expand Up @@ -55,6 +57,7 @@ FloatingWindowInterface::FloatingWindowInterface(QObject* parent)
QObject::connect(this->window, &ProxyFloatingWindow::titleChanged, this, &FloatingWindowInterface::titleChanged);
QObject::connect(this->window, &ProxyFloatingWindow::minimumSizeChanged, this, &FloatingWindowInterface::minimumSizeChanged);
QObject::connect(this->window, &ProxyFloatingWindow::maximumSizeChanged, this, &FloatingWindowInterface::maximumSizeChanged);
QObject::connect(this->window, &ProxyWindowBase::windowConnected, this, &FloatingWindowInterface::onWindowConnected);
// clang-format on
}

Expand All @@ -66,3 +69,85 @@ void FloatingWindowInterface::onReload(QObject* oldInstance) {
}

ProxyWindowBase* FloatingWindowInterface::proxyWindow() const { return this->window; }

void FloatingWindowInterface::onWindowConnected() {
auto* qw = this->window->backingWindow();
if (qw) {
QObject::connect(
qw,
&QWindow::windowStateChanged,
this,
&FloatingWindowInterface::onWindowStateChanged
);
}
}

void FloatingWindowInterface::onWindowStateChanged() {
auto* qw = this->window->backingWindow();
auto states = qw ? qw->windowStates() : Qt::WindowStates();

auto minimized = states.testFlag(Qt::WindowMinimized);
auto maximized = states.testFlag(Qt::WindowMaximized);
auto fullscreen = states.testFlag(Qt::WindowFullScreen);

if (minimized != this->mMinimized) {
this->mMinimized = minimized;
emit this->minimizedChanged();
}

if (maximized != this->mMaximized) {
this->mMaximized = maximized;
emit this->maximizedChanged();
}

if (fullscreen != this->mFullscreen) {
this->mFullscreen = fullscreen;
emit this->fullscreenChanged();
}
}

bool FloatingWindowInterface::isMinimized() const {
auto* qw = this->window->backingWindow();
if (!qw) return this->mMinimized;
return qw->windowStates().testFlag(Qt::WindowMinimized);
}

bool FloatingWindowInterface::isMaximized() const {
auto* qw = this->window->backingWindow();
if (!qw) return this->mMaximized;
return qw->windowStates().testFlag(Qt::WindowMaximized);
}

bool FloatingWindowInterface::isFullscreen() const {
auto* qw = this->window->backingWindow();
if (!qw) return this->mFullscreen;
return qw->windowStates().testFlag(Qt::WindowFullScreen);
}

bool FloatingWindowInterface::startSystemMove() const {
auto* qw = this->window->backingWindow();
if (!qw) return false;
return qw->startSystemMove();
}

bool FloatingWindowInterface::startSystemResize(Qt::Edges edges) const {
auto* qw = this->window->backingWindow();
if (!qw) return false;
return qw->startSystemResize(edges);
}

void FloatingWindowInterface::showNormal() const {
if (auto* qw = this->window->backingWindow()) qw->showNormal();
}

void FloatingWindowInterface::maximize() const {
if (auto* qw = this->window->backingWindow()) qw->showMaximized();
}

void FloatingWindowInterface::minimize() const {
if (auto* qw = this->window->backingWindow()) qw->showMinimized();
}

void FloatingWindowInterface::fullscreen() const {
if (auto* qw = this->window->backingWindow()) qw->showFullScreen();
}
41 changes: 38 additions & 3 deletions src/window/floatingwindow.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <qnamespace.h>
#include <qobject.h>
#include <qproperty.h>
#include <qsize.h>
Expand Down Expand Up @@ -68,6 +69,12 @@ class FloatingWindowInterface: public WindowInterface {
Q_PROPERTY(QSize minimumSize READ default WRITE default NOTIFY minimumSizeChanged BINDABLE bindableMinimumSize);
/// Maximum window size given to the window system.
Q_PROPERTY(QSize maximumSize READ default WRITE default NOTIFY maximumSizeChanged BINDABLE bindableMaximumSize);
/// Whether the window is currently minimized.
Q_PROPERTY(bool minimized READ isMinimized NOTIFY minimizedChanged);
/// Whether the window is currently maximized.
Q_PROPERTY(bool maximized READ isMaximized NOTIFY maximizedChanged);
/// Whether the window is currently fullscreen.
Q_PROPERTY(bool fullscreen READ isFullscreen NOTIFY fullscreenChanged);
// clang-format on
QML_NAMED_ELEMENT(FloatingWindow);

Expand All @@ -78,15 +85,43 @@ class FloatingWindowInterface: public WindowInterface {

[[nodiscard]] ProxyWindowBase* proxyWindow() const override;

QBindable<QSize> bindableMinimumSize() { return &this->window->bMinimumSize; }
QBindable<QSize> bindableMaximumSize() { return &this->window->bMaximumSize; }
QBindable<QString> bindableTitle() { return &this->window->bTitle; }
[[nodiscard]] QBindable<QSize> bindableMinimumSize() { return &this->window->bMinimumSize; }
[[nodiscard]] QBindable<QSize> bindableMaximumSize() { return &this->window->bMaximumSize; }
[[nodiscard]] QBindable<QString> bindableTitle() { return &this->window->bTitle; }

[[nodiscard]] bool isMinimized() const;
[[nodiscard]] bool isMaximized() const;
[[nodiscard]] bool isFullscreen() const;

/// Start a system move operation. Must be called during a pointer press/drag.
Q_INVOKABLE [[nodiscard]] bool startSystemMove() const;
/// Start a system resize operation. Must be called during a pointer press/drag.
Q_INVOKABLE [[nodiscard]] bool startSystemResize(Qt::Edges edges) const;

/// Show the window in normal (restored) state.
Q_INVOKABLE void showNormal() const;
/// Maximize the window.
Q_INVOKABLE void maximize() const;
/// Minimize the window.
Q_INVOKABLE void minimize() const;
/// Fullscreen the window.
Q_INVOKABLE void fullscreen() const;

signals:
void minimumSizeChanged();
void maximumSizeChanged();
void titleChanged();
void minimizedChanged();
void maximizedChanged();
void fullscreenChanged();

private slots:
void onWindowConnected();
void onWindowStateChanged();

private:
ProxyFloatingWindow* window;
bool mMinimized = false;
bool mMaximized = false;
bool mFullscreen = false;
};