From 7588890bcaa89fb8c6780d9b9be101c57cef0091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 21 Mar 2013 12:09:08 +0100 Subject: [PATCH] Use a Loader for the Virtual Desktop OSD The complete dialog is moved into a Loader item and it gets loaded on first change of virtual desktop. This change should result in less parsing on startup and moving the cost of the loading to the time when its really needed. REVIEW: 109639 --- scripts/desktopchangeosd/contents/ui/main.qml | 271 +--------------- scripts/desktopchangeosd/contents/ui/osd.qml | 297 ++++++++++++++++++ 2 files changed, 301 insertions(+), 267 deletions(-) create mode 100644 scripts/desktopchangeosd/contents/ui/osd.qml diff --git a/scripts/desktopchangeosd/contents/ui/main.qml b/scripts/desktopchangeosd/contents/ui/main.qml index c93896ccbf..8ab4863acb 100644 --- a/scripts/desktopchangeosd/contents/ui/main.qml +++ b/scripts/desktopchangeosd/contents/ui/main.qml @@ -18,283 +18,20 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ import QtQuick 1.1; -import org.kde.plasma.core 0.1 as PlasmaCore; -import org.kde.plasma.components 0.1 as Plasma; -import org.kde.qtextracomponents 0.1 as QtExtra; -import org.kde.kwin 0.1; Item { id: root - property int screenWidth: 0 - property int screenHeight: 0 - // we count desktops starting from 0 to have it better match the layout in the Grid - property int currentDesktop: 0 - property int previousDesktop: 0 - property int animationDuration: 1000 - property bool showGrid: true - function loadConfig() { - root.animationDuration = readConfig("PopupHideDelay", 1000); - if (readConfig("TextOnly", "false") == "true") { - root.showGrid = false; - } else { - root.showGrid = true; - } - } - Component.onCompleted: { - var screen = workspace.clientArea(KWin.FullScreenArea, workspace.activeScreen, workspace.currentDesktop); - root.screenWidth = screen.width; - root.screenHeight = screen.height; - loadConfig(); - } - PlasmaCore.Dialog { - id: dialog - visible: false - windowFlags: Qt.X11BypassWindowManagerHint - - mainItem: Item { - id: dialogItem - width: root.showGrid ? view.itemWidth * view.columns : textElement.width - height: root.showGrid ? view.itemHeight * view.rows + textElement.height : textElement.height - Plasma.Label { - id: textElement - anchors.top: root.showGrid ? parent.top : undefined - anchors.horizontalCenter: parent.horizontalCenter - text: workspace.desktopName(workspace.currentDesktop) - } - Grid { - id: view - columns: 1 - rows: 1 - property int itemWidth: root.screenWidth * Math.min(0.8/columns, 0.1) - property int itemHeight: Math.min(itemWidth * (root.screenHeight / root.screenWidth), root.screenHeight * Math.min(0.8/rows, 0.1)) - anchors { - top: textElement.bottom - left: parent.left - right: parent.right - bottom: parent.bottom - } - visible: root.showGrid - Repeater { - id: repeater - model: workspace.desktops - Item { - width: view.itemWidth - height: view.itemHeight - PlasmaCore.FrameSvgItem { - anchors.fill: parent - imagePath: "widgets/pager" - prefix: "normal" - } - PlasmaCore.FrameSvgItem { - id: activeElement - anchors.fill: parent - imagePath: "widgets/pager" - prefix: "active" - opacity: 0.0 - Behavior on opacity { - NumberAnimation { duration: root.animationDuration/2 } - } - } - Item { - id: arrowsContainer - anchors.fill: parent - QtExtra.QIconItem { - anchors.fill: parent - icon: "go-up" - visible: false - } - QtExtra.QIconItem { - anchors.fill: parent - icon: "go-down" - visible: { - if (root.currentDesktop <= index) { - // don't show for target desktop - return false; - } - if (index < root.previousDesktop) { - return false; - } - if (root.currentDesktop < root.previousDesktop) { - // we only go down if the new desktop is higher - return false; - } - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // don't show icons in same row as target desktop - return false; - } - if (root.previousDesktop % view.columns == index % view.columns) { - // show arrows for icons in same column as the previous desktop - return true; - } - return false; - } - } - QtExtra.QIconItem { - anchors.fill: parent - icon: "go-up" - visible: { - if (root.currentDesktop >= index) { - // don't show for target desktop - return false; - } - if (index > root.previousDesktop) { - return false; - } - if (root.currentDesktop > root.previousDesktop) { - // we only go down if the new desktop is higher - return false; - } - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // don't show icons in same row as target desktop - return false; - } - if (root.previousDesktop % view.columns == index % view.columns) { - // show arrows for icons in same column as the previous desktop - return true; - } - return false; - } - } - QtExtra.QIconItem { - anchors.fill: parent - icon: "go-next" - visible: { - if (root.currentDesktop <= index) { - // we don't show for desktops not on the path - return false; - } - if (index < root.previousDesktop) { - // we might have to show this icon in case we go up and to the right - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // can only happen in same row - if (index % view.columns >= root.previousDesktop % view.columns) { - // but only for items in the same column or after of the previous desktop - return true; - } - } - return false; - } - if (root.currentDesktop < root.previousDesktop) { - // we only go right if the new desktop is higher - return false; - } - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // show icons in same row as target desktop - if (index % view.columns < root.previousDesktop % view.columns) { - // but only for items in the same column or after of the previous desktop - return false; - } - return true; - } - return false; - } - } - QtExtra.QIconItem { - anchors.fill: parent - icon: "go-previous" - visible: { - if (root.currentDesktop >= index) { - // we don't show for desktops not on the path - return false; - } - if (index > root.previousDesktop) { - // we might have to show this icon in case we go down and to the left - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // can only happen in same row - if (index % view.columns <= root.previousDesktop % view.columns) { - // but only for items in the same column or before the previous desktop - return true; - } - } - return false; - } - if (root.currentDesktop > root.previousDesktop) { - // we only go left if the new desktop is lower - return false; - } - if (Math.floor(root.currentDesktop/view.columns) == Math.floor(index/view.columns)) { - // show icons in same row as target desktop - if (index % view.columns > root.previousDesktop % view.columns) { - // but only for items in the same column or before of the previous desktop - return false; - } - return true; - } - return false; - } - } - } - states: [ - State { - name: "NORMAL" - when: index != root.currentDesktop - PropertyChanges { - target: activeElement - opacity: 0.0 - } - }, - State { - name: "SELECTED" - when: index == root.currentDesktop - PropertyChanges { - target: activeElement - opacity: 1.0 - } - } - ] - Component.onCompleted: { - view.state = (index == root.currentDesktop) ? "SELECTED" : "NORMAL" - } - } - } - Component.onCompleted: { - columns = workspace.desktopGridWidth; - rows = workspace.desktopGridHeight; - } - } - } - } - - Timer { - id: timer - repeat: false - interval: root.animationDuration - onTriggered: dialog.visible = false + Loader { + id: mainItemLoader } Connections { target: workspace onCurrentDesktopChanged: { - if (root.currentDesktop == workspace.currentDesktop - 1) { - return; + if (!mainItemLoader.item) { + mainItemLoader.source = "osd.qml"; } - dialog.visible = true; - root.previousDesktop = root.currentDesktop; - timer.stop(); - root.currentDesktop = workspace.currentDesktop - 1; - textElement.text = workspace.desktopName(workspace.currentDesktop); - // screen geometry might have changed - var screen = workspace.clientArea(KWin.FullScreenArea, workspace.activeScreen, workspace.currentDesktop); - root.screenWidth = screen.width; - root.screenHeight = screen.height; - if (root.showGrid) { - // non dependable properties might have changed - view.columns = workspace.desktopGridWidth; - view.rows = workspace.desktopGridHeight; - } - // position might have changed - dialog.x = screen.x + screen.width/2 - dialogItem.width/2; - dialog.y = screen.y + screen.height/2 - dialogItem.height/2; - // start the hide timer - timer.start(); } - onNumberDesktopsChanged: { - repeater.model = workspace.desktops; - } - } - Connections { - target: options - onConfigChanged: root.loadConfig() } } diff --git a/scripts/desktopchangeosd/contents/ui/osd.qml b/scripts/desktopchangeosd/contents/ui/osd.qml new file mode 100644 index 0000000000..af49d45a30 --- /dev/null +++ b/scripts/desktopchangeosd/contents/ui/osd.qml @@ -0,0 +1,297 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2012, 2013 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +import QtQuick 1.1; +import org.kde.plasma.core 0.1 as PlasmaCore; +import org.kde.plasma.components 0.1 as Plasma; +import org.kde.qtextracomponents 0.1 as QtExtra; +import org.kde.kwin 0.1; + +PlasmaCore.Dialog { + id: dialog + visible: false + windowFlags: Qt.X11BypassWindowManagerHint + + mainItem: Item { + function loadConfig() { + dialogItem.animationDuration = readConfig("PopupHideDelay", 1000); + if (readConfig("TextOnly", "false") == "true") { + dialogItem.showGrid = false; + } else { + dialogItem.showGrid = true; + } + } + + function show() { + if (dialogItem.currentDesktop == workspace.currentDesktop - 1) { + return; + } + dialog.visible = true; + dialogItem.previousDesktop = dialogItem.currentDesktop; + timer.stop(); + dialogItem.currentDesktop = workspace.currentDesktop - 1; + textElement.text = workspace.desktopName(workspace.currentDesktop); + // screen geometry might have changed + var screen = workspace.clientArea(KWin.FullScreenArea, workspace.activeScreen, workspace.currentDesktop); + dialogItem.screenWidth = screen.width; + dialogItem.screenHeight = screen.height; + if (dialogItem.showGrid) { + // non dependable properties might have changed + view.columns = workspace.desktopGridWidth; + view.rows = workspace.desktopGridHeight; + } + // position might have changed + dialog.x = screen.x + screen.width/2 - dialogItem.width/2; + dialog.y = screen.y + screen.height/2 - dialogItem.height/2; + // start the hide timer + timer.start(); + } + + id: dialogItem + property int screenWidth: 0 + property int screenHeight: 0 + // we count desktops starting from 0 to have it better match the layout in the Grid + property int currentDesktop: 0 + property int previousDesktop: 0 + property int animationDuration: 1000 + property bool showGrid: true + + width: dialogItem.showGrid ? view.itemWidth * view.columns : textElement.width + height: dialogItem.showGrid ? view.itemHeight * view.rows + textElement.height : textElement.height + + Plasma.Label { + id: textElement + anchors.top: dialogItem.showGrid ? parent.top : undefined + anchors.horizontalCenter: parent.horizontalCenter + text: workspace.desktopName(workspace.currentDesktop) + } + Grid { + id: view + columns: 1 + rows: 1 + property int itemWidth: dialogItem.screenWidth * Math.min(0.8/columns, 0.1) + property int itemHeight: Math.min(itemWidth * (dialogItem.screenHeight / dialogItem.screenWidth), dialogItem.screenHeight * Math.min(0.8/rows, 0.1)) + anchors { + top: textElement.bottom + left: parent.left + right: parent.right + bottom: parent.bottom + } + visible: dialogItem.showGrid + Repeater { + id: repeater + model: workspace.desktops + Item { + width: view.itemWidth + height: view.itemHeight + PlasmaCore.FrameSvgItem { + anchors.fill: parent + imagePath: "widgets/pager" + prefix: "normal" + } + PlasmaCore.FrameSvgItem { + id: activeElement + anchors.fill: parent + imagePath: "widgets/pager" + prefix: "active" + opacity: 0.0 + Behavior on opacity { + NumberAnimation { duration: dialogItem.animationDuration/2 } + } + } + Item { + id: arrowsContainer + anchors.fill: parent + QtExtra.QIconItem { + anchors.fill: parent + icon: "go-up" + visible: false + } + QtExtra.QIconItem { + anchors.fill: parent + icon: "go-down" + visible: { + if (dialogItem.currentDesktop <= index) { + // don't show for target desktop + return false; + } + if (index < dialogItem.previousDesktop) { + return false; + } + if (dialogItem.currentDesktop < dialogItem.previousDesktop) { + // we only go down if the new desktop is higher + return false; + } + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // don't show icons in same row as target desktop + return false; + } + if (dialogItem.previousDesktop % view.columns == index % view.columns) { + // show arrows for icons in same column as the previous desktop + return true; + } + return false; + } + } + QtExtra.QIconItem { + anchors.fill: parent + icon: "go-up" + visible: { + if (dialogItem.currentDesktop >= index) { + // don't show for target desktop + return false; + } + if (index > dialogItem.previousDesktop) { + return false; + } + if (dialogItem.currentDesktop > dialogItem.previousDesktop) { + // we only go down if the new desktop is higher + return false; + } + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // don't show icons in same row as target desktop + return false; + } + if (dialogItem.previousDesktop % view.columns == index % view.columns) { + // show arrows for icons in same column as the previous desktop + return true; + } + return false; + } + } + QtExtra.QIconItem { + anchors.fill: parent + icon: "go-next" + visible: { + if (dialogItem.currentDesktop <= index) { + // we don't show for desktops not on the path + return false; + } + if (index < dialogItem.previousDesktop) { + // we might have to show this icon in case we go up and to the right + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // can only happen in same row + if (index % view.columns >= dialogItem.previousDesktop % view.columns) { + // but only for items in the same column or after of the previous desktop + return true; + } + } + return false; + } + if (dialogItem.currentDesktop < dialogItem.previousDesktop) { + // we only go right if the new desktop is higher + return false; + } + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // show icons in same row as target desktop + if (index % view.columns < dialogItem.previousDesktop % view.columns) { + // but only for items in the same column or after of the previous desktop + return false; + } + return true; + } + return false; + } + } + QtExtra.QIconItem { + anchors.fill: parent + icon: "go-previous" + visible: { + if (dialogItem.currentDesktop >= index) { + // we don't show for desktops not on the path + return false; + } + if (index > dialogItem.previousDesktop) { + // we might have to show this icon in case we go down and to the left + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // can only happen in same row + if (index % view.columns <= dialogItem.previousDesktop % view.columns) { + // but only for items in the same column or before the previous desktop + return true; + } + } + return false; + } + if (dialogItem.currentDesktop > dialogItem.previousDesktop) { + // we only go left if the new desktop is lower + return false; + } + if (Math.floor(dialogItem.currentDesktop/view.columns) == Math.floor(index/view.columns)) { + // show icons in same row as target desktop + if (index % view.columns > dialogItem.previousDesktop % view.columns) { + // but only for items in the same column or before of the previous desktop + return false; + } + return true; + } + return false; + } + } + } + states: [ + State { + name: "NORMAL" + when: index != dialogItem.currentDesktop + PropertyChanges { + target: activeElement + opacity: 0.0 + } + }, + State { + name: "SELECTED" + when: index == dialogItem.currentDesktop + PropertyChanges { + target: activeElement + opacity: 1.0 + } + } + ] + Component.onCompleted: { + view.state = (index == dialogItem.currentDesktop) ? "SELECTED" : "NORMAL" + } + } + } + } + + Timer { + id: timer + repeat: false + interval: dialogItem.animationDuration + onTriggered: dialog.visible = false + } + + Connections { + target: workspace + onCurrentDesktopChanged: dialogItem.show() + onNumberDesktopsChanged: { + repeater.model = workspace.desktops; + } + } + Connections { + target: options + onConfigChanged: dialogItem.loadConfig() + } + Component.onCompleted: { + columns = workspace.desktopGridWidth; + rows = workspace.desktopGridHeight; + dialogItem.loadConfig(); + dialogItem.show(); + } + } +}