Skip to content

Commit 05ed9ff

Browse files
committed
wayland/screencopy: add constrained implicitSize for ScreencopyView
1 parent 7390ae2 commit 05ed9ff

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/wayland/screencopy/view.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "view.hpp"
22

3+
#include <qnamespace.h>
34
#include <qobject.h>
45
#include <qqmlinfo.h>
56
#include <qquickitem.h>
@@ -12,6 +13,23 @@
1213

1314
namespace qs::wayland::screencopy {
1415

16+
ScreencopyView::ScreencopyView(QQuickItem* parent): QQuickItem(parent) {
17+
this->bImplicitSize.setBinding([this] {
18+
auto constraint = this->bConstraintSize.value();
19+
auto size = this->bSourceSize.value().toSizeF();
20+
21+
if (constraint.width() != 0 && constraint.height() != 0) {
22+
size.scale(constraint.width(), constraint.height(), Qt::KeepAspectRatio);
23+
} else if (constraint.width() != 0) {
24+
size = QSizeF(constraint.width(), size.height() / constraint.width());
25+
} else if (constraint.height() != 0) {
26+
size = QSizeF(size.width() / constraint.height(), constraint.height());
27+
}
28+
29+
return size;
30+
});
31+
}
32+
1533
void ScreencopyView::setCaptureSource(QObject* captureSource) {
1634
if (captureSource == this->mCaptureSource) return;
1735
auto hadContext = this->context != nullptr;
@@ -148,4 +166,9 @@ QSGNode* ScreencopyView::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*
148166
return node;
149167
}
150168

169+
void ScreencopyView::updateImplicitSize() {
170+
auto size = this->bImplicitSize.value();
171+
this->setImplicitSize(size.width(), size.height());
172+
}
173+
151174
} // namespace qs::wayland::screencopy

src/wayland/screencopy/view.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ class ScreencopyView: public QQuickItem {
3636
Q_PROPERTY(bool hasContent READ default NOTIFY hasContentChanged BINDABLE bindableHasContent);
3737
/// The size of the source image. Valid when @@hasContent is true.
3838
Q_PROPERTY(QSize sourceSize READ default NOTIFY sourceSizeChanged BINDABLE bindableSourceSize);
39+
/// If nonzero, the width and height constraints set for this property will constrain those
40+
/// dimensions of the ScreencopyView's implicit size, maintaining the image's aspect ratio.
41+
Q_PROPERTY(QSizeF constraintSize READ default WRITE default NOTIFY constraintSizeChanged BINDABLE bindableConstraintSize);
3942
// clang-format on
4043

4144
public:
42-
explicit ScreencopyView(QQuickItem* parent = nullptr): QQuickItem(parent) {}
45+
explicit ScreencopyView(QQuickItem* parent = nullptr);
4346

4447
void componentComplete() override;
4548

@@ -57,6 +60,7 @@ class ScreencopyView: public QQuickItem {
5760

5861
[[nodiscard]] QBindable<bool> bindableHasContent() { return &this->bHasContent; }
5962
[[nodiscard]] QBindable<QSize> bindableSourceSize() { return &this->bSourceSize; }
63+
[[nodiscard]] QBindable<QSizeF> bindableConstraintSize() { return &this->bConstraintSize; }
6064

6165
signals:
6266
/// The compositor has ended the video stream. Attempting to restart it may or may not work.
@@ -67,6 +71,7 @@ class ScreencopyView: public QQuickItem {
6771
void liveChanged();
6872
void hasContentChanged();
6973
void sourceSizeChanged();
74+
void constraintSizeChanged();
7075

7176
protected:
7277
QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* data) override;
@@ -80,10 +85,13 @@ private slots:
8085
private:
8186
void destroyContext(bool update = true);
8287
void createContext();
88+
void updateImplicitSize();
8389

8490
// clang-format off
8591
Q_OBJECT_BINDABLE_PROPERTY(ScreencopyView, bool, bHasContent, &ScreencopyView::hasContentChanged);
8692
Q_OBJECT_BINDABLE_PROPERTY(ScreencopyView, QSize, bSourceSize, &ScreencopyView::sourceSizeChanged);
93+
Q_OBJECT_BINDABLE_PROPERTY(ScreencopyView, QSizeF, bConstraintSize, &ScreencopyView::constraintSizeChanged);
94+
Q_OBJECT_BINDABLE_PROPERTY(ScreencopyView, QSizeF, bImplicitSize, &ScreencopyView::updateImplicitSize);
8795
// clang-format on
8896

8997
QObject* mCaptureSource = nullptr;

0 commit comments

Comments
 (0)