diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -29,6 +29,7 @@ tst_icon.qml tst_actiontoolbar.qml tst_pagerouter.qml + tst_routerwindow.qml pagepool/tst_pagepool.qml ) diff --git a/autotests/tst_routerwindow.qml b/autotests/tst_routerwindow.qml new file mode 100644 --- /dev/null +++ b/autotests/tst_routerwindow.qml @@ -0,0 +1,109 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 as QQC2 +import org.kde.kirigami 2.12 as Kirigami +import QtTest 1.0 + +TestCase { + name: "RouterWindowTests" + property alias router: root.router + when: windowShown + + function test_a_init() { + compare(router.currentRoutes().length, 1) + } + function test_b_navigate() { + router.navigateToRoute(["home", "login"]) + compare(router.currentRoutes().length, 2) + } + function test_c_data() { + router.navigateToRoute(["home", {"route": "login", "data": "red"}]) + compare(router.routeActive(["home", {"route": "login", "data": "red"}]), true) + compare(router.routeActive(["home", {"route": "login", "data": "blue"}]), false) + } + function test_d_cache_works() { + router.navigateToRoute(["home", {"route": "login", "data": "red"}, {"route": "login", "data": "blue"}]) + compare(router.currentRoutes().length, 3) + } + function test_e_push() { + router.pushRoute("home") + compare(router.currentRoutes().length, 4) + } + function test_f_pop() { + router.popRoute() + compare(router.currentRoutes().length, 3) + } + function test_g_bring_to_view() { + router.bringToView("home") + compare(root.pageStack.currentIndex, 0) + router.bringToView({"route": "login", "data": "red"}) + compare(root.pageStack.currentIndex, 1) + router.bringToView({"route": "login", "data": "blue"}) + compare(root.pageStack.currentIndex, 2) + } + function test_h_routeactive() { + compare(router.routeActive(["home"]), true) + compare(router.routeActive(["home", "login"]), true) + compare(router.routeActive(["home", {"route": "login", "data": "red"}]), true) + compare(router.routeActive(["home", {"route": "login", "data": "blue"}]), false) + } + function test_i_initial_route() { + router.initialRoute = "login" + compare(router.routeActive(["login"]), false) + compare(router.currentRoutes().length, 3) + } + function test_j_navigation_two() { + router.navigateToRoute(["home", {"route": "login", "data": "red"}, {"route": "login", "data": "blue"}]) + compare(router.currentRoutes().length, 3) + router.navigateToRoute(["home"]) + compare(router.currentRoutes().length, 1) + compare(router.pageStack.count, 1) + } + + Kirigami.RouterWindow { + id: root + initialRoute: "home" + Kirigami.PageRoute { + name: "home" + cache: false + Component { + Kirigami.Page { + Column { + Kirigami.Heading { + text: "Welcome" + } + QQC2.Button { + text: "Red Login" + onClicked: Kirigami.PageRouter.navigateToRoute(["home", {"route": "login", "data": "red"}]) + } + QQC2.Button { + text: "Blue Login" + onClicked: Kirigami.PageRouter.navigateToRoute(["home", {"route": "login", "data": "blue"}]) + } + } + } + } + } + Kirigami.PageRoute { + name: "login" + cache: true + Component { + Kirigami.Page { + Column { + Kirigami.Heading { + text: "Login" + } + Rectangle { + height: 50 + width: 50 + color: Kirigami.PageRouter.data + } + QQC2.Button { + text: "Back to Home" + onClicked: Kirigami.PageRouter.navigateToRoute("home") + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/controls/RouterWindow.qml b/src/controls/RouterWindow.qml new file mode 100644 --- /dev/null +++ b/src/controls/RouterWindow.qml @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2020 Carson Black + * + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +import QtQuick 2.5 +import org.kde.kirigami 2.12 as Kirigami + +/** + * An org::kde::kirigami::ApplicationWindow with a preconfigured PageRouter. + * + * In order to call functions on the PageRouter, use @link PageRouterAttached the attached Kirigami.PageRouter object @endlink. + */ + +Kirigami.ApplicationWindow { + id: __kirigamiApplicationWindow + + /** + * @see PageRouter::routes + */ + default property alias routes: __kirigamiPageRouter.routes + + /** + * @see PageRouter::initialRoute + */ + property alias initialRoute: __kirigamiPageRouter.initialRoute + + /** + * The PageRouter of this window. + */ + property alias router: __kirigamiPageRouter + + Kirigami.PageRouter { + id: __kirigamiPageRouter + pageStack: __kirigamiApplicationWindow.pageStack.columnView + } +} \ No newline at end of file diff --git a/src/kirigamiplugin.cpp b/src/kirigamiplugin.cpp --- a/src/kirigamiplugin.cpp +++ b/src/kirigamiplugin.cpp @@ -253,6 +253,7 @@ qmlRegisterType(uri, 2, 12, "PageRouter"); qmlRegisterType(uri, 2, 12, "PageRoute"); qmlRegisterUncreatableType(uri, 2, 12, "PageRouterAttached", QStringLiteral("PageRouterAttached cannot be created")); + qmlRegisterType(componentUrl(QStringLiteral("RouterWindow.qml")), uri, 2, 12, "RouterWindow"); qmlProtectModule(uri, 2); }