Skip to content

Commit

Permalink
feat: add cursor and port wallpaper test demo
Browse files Browse the repository at this point in the history
Signed-off-by: pengwenhao <[email protected]>
  • Loading branch information
pengwenhao authored and justforlxz committed Jun 26, 2024
1 parent 16f8505 commit 57c79a9
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
add_subdirectory(test_window_bg)
add_subdirectory(test_wallpaper_color)
add_subdirectory(test_show_desktop)
add_subdirectory(test_window_crusor)
add_subdirectory(test_xdgport_wallpaper)
21 changes: 21 additions & 0 deletions tests/test_window_crusor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
find_package(Qt6 REQUIRED COMPONENTS WaylandClient Widgets)

qt_add_executable(test-window-cursor
main.cpp
personalization_manager.h
personalization_manager.cpp
)

qt_generate_wayland_protocol_client_sources(test-window-cursor
FILES
${CMAKE_CURRENT_SOURCE_DIR}/../../src/modules/treeland-personalization-manager/protocols/treeland-personalization-manager-v1.xml
)

target_link_libraries(test-window-cursor
PRIVATE
Qt${QT_MAJOR_VERSION}::Gui
Qt${QT_MAJOR_VERSION}::Widgets
Qt::WaylandClient
Qt::GuiPrivate
Qt::WaylandClientPrivate
)
101 changes: 101 additions & 0 deletions tests/test_window_crusor/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (C) 2024 rewine <[email protected]>.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QGuiApplication>
#include <QRasterWindow>
#include <QPainter>
#include <QMouseEvent>
#include <QPlatformSurfaceEvent>
#include <QDebug>
#include <QTimer>

#include "personalization_manager.h"

class TestWindow : public QRasterWindow
{
Q_OBJECT
public:
TestWindow()
: m_manager(new PersonalizationManager)
, rect1(50, 50, 200, 50)
, rect2(50, 200, 200, 50)
, rect3(50, 350, 200, 50)
, rect4(50, 500, 200, 50)
{
setTitle(tr("C++ Client"));

connect(m_manager, &PersonalizationManager::activeChanged, this, [this] {
qDebug() << "personalization manager active changed";

if (m_manager->isActive()) {
cursor_context = new PersonalizationCursor(m_manager->get_cursor_context());
}
});
}

~TestWindow()
{
if (m_manager != nullptr)
{
delete m_manager;
m_manager = nullptr;
}
}

void mousePressEvent(QMouseEvent *ev) override
{
if (cursor_context == nullptr)
return;

if (rect1.contains(ev->position())) {
cursor_context->set_theme("Breeze_Light");
cursor_context->commit();
}
else if (rect2.contains(ev->position())) {
cursor_context->set_size(32);
cursor_context->commit();
}
else if (rect3.contains(ev->position()))
cursor_context->get_theme();
else if (rect4.contains(ev->position()))
cursor_context->get_size();
}

protected:
void paintEvent(QPaintEvent *) override
{
QPainter p(this);
p.setFont(m_font);
p.fillRect(QRect(0, 0, width(), height()), Qt::gray);
p.fillRect(rect1, QColor::fromString("#C0FFEE"));
p.drawText(rect1, Qt::TextWordWrap, "set cursor theme");
p.fillRect(rect2, QColor::fromString("#decaff"));
p.drawText(rect2, Qt::TextWordWrap, "set cursor size");
p.fillRect(rect3, QColor::fromString("#7EA"));
p.drawText(rect3, Qt::TextWordWrap, "get cursor theme");
p.fillRect(rect4, QColor::fromString("#7EB"));
p.drawText(rect4, Qt::TextWordWrap, "get cursor size");
}

private:
PersonalizationManager *m_manager = nullptr;
PersonalizationCursor* cursor_context = nullptr;

QRectF rect1;
QRectF rect2;
QRectF rect3;
QRectF rect4;
QFont m_font;
};

int main (int argc, char **argv)
{
QGuiApplication app(argc, argv);

TestWindow window;
window.show();

return app.exec();
}

#include "main.moc"
59 changes: 59 additions & 0 deletions tests/test_window_crusor/personalization_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2024 rewine <[email protected]>.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "personalization_manager.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QWindow>
#include <QtGui/QPlatformSurfaceEvent>
#include <QtGui/qpa/qplatformnativeinterface.h>
#include <QDebug>

QT_BEGIN_NAMESPACE

PersonalizationManager::PersonalizationManager()
: QWaylandClientExtensionTemplate<PersonalizationManager>(1)
{

}

PersonalizationWindow::PersonalizationWindow(struct ::personalization_window_context_v1 *object)
: QWaylandClientExtensionTemplate<PersonalizationWindow>(1)
, QtWayland::personalization_window_context_v1(object)
{

}

PersonalizationWallpaper::PersonalizationWallpaper(struct ::personalization_wallpaper_context_v1 *object)
: QWaylandClientExtensionTemplate<PersonalizationWallpaper>(1)
, QtWayland::personalization_wallpaper_context_v1(object)
{

}

void PersonalizationWallpaper::personalization_wallpaper_context_v1_metadata(const QString &metadata)
{
qDebug() << "=========================================== " << metadata;
}

PersonalizationCursor::PersonalizationCursor(struct ::personalization_cursor_context_v1 *object)
: QWaylandClientExtensionTemplate<PersonalizationCursor>(1)
, QtWayland::personalization_cursor_context_v1(object)
{
}

void PersonalizationCursor::personalization_cursor_context_v1_verfity(int32_t success)
{
qDebug() << "=========================================== " << success;
}

void PersonalizationCursor::personalization_cursor_context_v1_theme(const QString &name)
{
qDebug() << "=========================================== " << name;
}

void PersonalizationCursor::personalization_cursor_context_v1_size(uint32_t size)
{
qDebug() << "=========================================== " << size;
}

QT_END_NAMESPACE
52 changes: 52 additions & 0 deletions tests/test_window_crusor/personalization_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2024 rewine <[email protected]>.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef CUSTOMEXTENSION_H
#define CUSTOMEXTENSION_H

#include <QtWaylandClient/QWaylandClientExtension>
#include <QtGui/QWindow>

#include "qwayland-treeland-personalization-manager-v1.h"

QT_BEGIN_NAMESPACE

class PersonalizationManager : public QWaylandClientExtensionTemplate<PersonalizationManager>, public QtWayland::treeland_personalization_manager_v1
{
Q_OBJECT
public:
explicit PersonalizationManager();
};

class PersonalizationWindow : public QWaylandClientExtensionTemplate<PersonalizationWindow>, public QtWayland::personalization_window_context_v1
{
Q_OBJECT
public:
explicit PersonalizationWindow(struct ::personalization_window_context_v1 *object);
};

class PersonalizationWallpaper : public QWaylandClientExtensionTemplate<PersonalizationWallpaper>, public QtWayland::personalization_wallpaper_context_v1
{
Q_OBJECT
public:
explicit PersonalizationWallpaper(struct ::personalization_wallpaper_context_v1 *object);

protected:
void personalization_wallpaper_context_v1_metadata(const QString &metadata) override;
};

class PersonalizationCursor : public QWaylandClientExtensionTemplate<PersonalizationCursor>, public QtWayland::personalization_cursor_context_v1
{
Q_OBJECT
public:
explicit PersonalizationCursor(struct ::personalization_cursor_context_v1 *object);

protected:
void personalization_cursor_context_v1_verfity(int32_t success);
void personalization_cursor_context_v1_theme(const QString &name);
void personalization_cursor_context_v1_size(uint32_t size);
};

QT_END_NAMESPACE

#endif // CUSTOMEXTENSION_H
74 changes: 74 additions & 0 deletions tests/test_xdgport_wallpaper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
cmake_minimum_required(VERSION 3.5)

project(test_xdgport_wallpaper VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(test_xdgport_wallpaper
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET test_xdgport_wallpaper APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(test_xdgport_wallpaper SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(test_xdgport_wallpaper
${PROJECT_SOURCES}
)
endif()
endif()

target_link_libraries(test_xdgport_wallpaper PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::DBus
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.test_xdgport_wallpaper)
endif()
set_target_properties(test_xdgport_wallpaper PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS test_xdgport_wallpaper
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(test_xdgport_wallpaper)
endif()

13 changes: 13 additions & 0 deletions tests/test_xdgport_wallpaper/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2024 rewine <[email protected]>.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
55 changes: 55 additions & 0 deletions tests/test_xdgport_wallpaper/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2024 rewine <[email protected]>.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusArgument>
#include <QVariantMap>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->setButton, &QPushButton::clicked, this, &MainWindow::setWallpaper);

interface = new QDBusInterface("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Wallpaper",
QDBusConnection::sessionBus(),
this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::setWallpaper()
{
QString app_id = "G4";
QString parent_window = "";
QString uri = "/home/uos/Pictures/Wallpapers/abc-123.jpg";

QVariantMap options;
options.insert("output", "X11-2");
options.insert("set-on", "background");

QDBusMessage reply = interface->call("SetWallpaperURI",
parent_window,
uri,
QVariant::fromValue(options));

// 检查返回值
if (reply.type() == QDBusMessage::ErrorMessage) {
qWarning() << "Failed to set wallpaper:" << reply.errorMessage();
return ;
}

// 处理返回值
uint response = reply.arguments().at(0).toUInt();
qDebug() << "Wallpaper set with response code:" << response;
}
Loading

0 comments on commit 57c79a9

Please sign in to comment.