diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5dfc9c60..abfbcd19 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/test_window_crusor/CMakeLists.txt b/tests/test_window_crusor/CMakeLists.txt new file mode 100644 index 00000000..42b49c9f --- /dev/null +++ b/tests/test_window_crusor/CMakeLists.txt @@ -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 +) diff --git a/tests/test_window_crusor/main.cpp b/tests/test_window_crusor/main.cpp new file mode 100644 index 00000000..be6bc355 --- /dev/null +++ b/tests/test_window_crusor/main.cpp @@ -0,0 +1,101 @@ +// Copyright (C) 2024 rewine . +// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include +#include +#include +#include +#include +#include +#include + +#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" diff --git a/tests/test_window_crusor/personalization_manager.cpp b/tests/test_window_crusor/personalization_manager.cpp new file mode 100644 index 00000000..8368a4e2 --- /dev/null +++ b/tests/test_window_crusor/personalization_manager.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2024 rewine . +// 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 +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +PersonalizationManager::PersonalizationManager() + : QWaylandClientExtensionTemplate(1) +{ + +} + +PersonalizationWindow::PersonalizationWindow(struct ::personalization_window_context_v1 *object) + : QWaylandClientExtensionTemplate(1) + , QtWayland::personalization_window_context_v1(object) +{ + +} + +PersonalizationWallpaper::PersonalizationWallpaper(struct ::personalization_wallpaper_context_v1 *object) + : QWaylandClientExtensionTemplate(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(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 diff --git a/tests/test_window_crusor/personalization_manager.h b/tests/test_window_crusor/personalization_manager.h new file mode 100644 index 00000000..52d48d5e --- /dev/null +++ b/tests/test_window_crusor/personalization_manager.h @@ -0,0 +1,52 @@ +// Copyright (C) 2024 rewine . +// 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 +#include + +#include "qwayland-treeland-personalization-manager-v1.h" + +QT_BEGIN_NAMESPACE + +class PersonalizationManager : public QWaylandClientExtensionTemplate, public QtWayland::treeland_personalization_manager_v1 +{ + Q_OBJECT +public: + explicit PersonalizationManager(); +}; + +class PersonalizationWindow : public QWaylandClientExtensionTemplate, public QtWayland::personalization_window_context_v1 +{ + Q_OBJECT +public: + explicit PersonalizationWindow(struct ::personalization_window_context_v1 *object); +}; + +class PersonalizationWallpaper : public QWaylandClientExtensionTemplate, 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, 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 diff --git a/tests/test_xdgport_wallpaper/CMakeLists.txt b/tests/test_xdgport_wallpaper/CMakeLists.txt new file mode 100644 index 00000000..46f9f313 --- /dev/null +++ b/tests/test_xdgport_wallpaper/CMakeLists.txt @@ -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() + diff --git a/tests/test_xdgport_wallpaper/main.cpp b/tests/test_xdgport_wallpaper/main.cpp new file mode 100644 index 00000000..941bd9b7 --- /dev/null +++ b/tests/test_xdgport_wallpaper/main.cpp @@ -0,0 +1,13 @@ +// Copyright (C) 2024 rewine . +// 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 + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/tests/test_xdgport_wallpaper/mainwindow.cpp b/tests/test_xdgport_wallpaper/mainwindow.cpp new file mode 100644 index 00000000..e262a0bc --- /dev/null +++ b/tests/test_xdgport_wallpaper/mainwindow.cpp @@ -0,0 +1,55 @@ +// Copyright (C) 2024 rewine . +// 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 +#include +#include +#include +#include + +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; +} diff --git a/tests/test_xdgport_wallpaper/mainwindow.h b/tests/test_xdgport_wallpaper/mainwindow.h new file mode 100644 index 00000000..bd4004c6 --- /dev/null +++ b/tests/test_xdgport_wallpaper/mainwindow.h @@ -0,0 +1,31 @@ +// Copyright (C) 2024 rewine . +// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void setWallpaper(); + +private: + Ui::MainWindow *ui; + QDBusInterface *interface; +}; + +#endif // MAINWINDOW_H diff --git a/tests/test_xdgport_wallpaper/mainwindow.ui b/tests/test_xdgport_wallpaper/mainwindow.ui new file mode 100644 index 00000000..e140314c --- /dev/null +++ b/tests/test_xdgport_wallpaper/mainwindow.ui @@ -0,0 +1,45 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 310 + 150 + 106 + 36 + + + + Set Wallpaper + + + + + + + 0 + 0 + 800 + 35 + + + + + + + +