/** Copyright 2013 by Sebastian KΓΌgler <sebas@kde.org>* Copyright 2014 by Martin GrΓ€Γlin <mgraesslin@kde.org>* Copyright 2016 by Kai Uwe Broulik <kde@privat.broulik.de>* Copyright 2017 by Roman Gilg <subdiff@gmail.com>* Copyright 2020 by Nate Graham <nate@kde.org>** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU Library General Public License as* published by the Free Software Foundation; either version 2, 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 Library General Public License for more details** You should have received a copy of the GNU Library General Public* License along with this program; if not, write to the* Free Software Foundation, Inc.,* 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA.*/import QtQuick 2.6import QtQuick.Layouts 1.1import QtGraphicalEffects 1.0import QtQml.Models 2.2import org.kde.plasma.core 2.0 as PlasmaCore// for Highlightimport org.kde.plasma.components 2.0 as PlasmaComponentsimport org.kde.plasma.components 3.0 as PlasmaComponents3import org.kde.plasma.extras 2.0 as PlasmaExtrasimport org.kde.kquickcontrolsaddons 2.0 as KQuickControlsAddonsimport org.kde.taskmanager 0.1 as TaskManagerColumnLayout {property var submodelIndexproperty int flatIndex: isGroup && index != undefined ? index : 0readonly property string mprisSourceName: mpris2Source.sourceNameForLauncherUrl(toolTipDelegate.launcherUrl, isGroup ? AppPid : pidParent)readonly property var playerData: mprisSourceName != "" ? mpris2Source.data[mprisSourceName] : 0readonly property bool hasPlayer: !!mprisSourceName && !!playerDatareadonly property bool playing: hasPlayer && playerData.PlaybackStatus === "Playing"readonly property bool canControl: hasPlayer && playerData.CanControlreadonly property bool canPlay: hasPlayer && playerData.CanPlayreadonly property bool canPause: hasPlayer && playerData.CanPausereadonly property bool canGoBack: hasPlayer && playerData.CanGoPreviousreadonly property bool canGoNext: hasPlayer && playerData.CanGoNextreadonly property bool canRaise: hasPlayer && playerData.CanRaisereadonly property var currentMetadata: hasPlayer ? playerData.Metadata : ({})readonly property string track: {var xesamTitle = currentMetadata["xesam:title"]if (xesamTitle) {return xesamTitle;}// if no track title is given, print out the file namevar xesamUrl = currentMetadata["xesam:url"] ? currentMetadata["xesam:url"].toString() : ""if (!xesamUrl) {return "";}var lastSlashPos = xesamUrl.lastIndexOf('/')if (lastSlashPos < 0) {return "";}var lastUrlPart = xesamUrl.substring(lastSlashPos + 1)return decodeURIComponent(lastUrlPart);}readonly property string artist: currentMetadata["xesam:artist"] || ""readonly property string albumArt: currentMetadata["mpris:artUrl"] || ""spacing: units.smallSpacing// text labels + close buttonRowLayout {id: header// match spacing of DefaultToolTip.qml in plasma-frameworkspacing: isWin ? units.smallSpacing : units.largeSpacing// This number controls the overall size of the window tooltipsLayout.maximumWidth: units.gridUnit * 16Layout.minimumWidth: isWin ? Layout.maximumWidth : 0Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter// match margins of DefaultToolTip.qml in plasma-frameworkLayout.margins: isWin ? 0 : units.gridUnit / 2// all textlabelsColumnLayout {spacing: 0// app namePlasmaExtras.Heading {id: appNameHeadinglevel: 3maximumLineCount: 1lineHeight: isWin ? 1 : appNameHeading.lineHeightLayout.fillWidth: trueelide: Text.ElideRighttext: appNameopacity: flatIndex == 0visible: text.length !== 0}// window titlePlasmaExtras.Heading {id: winTitlelevel: 5maximumLineCount: 1lineHeight: isWin ? 1 : winTitle.lineHeightLayout.fillWidth: trueelide: Text.ElideRighttext: generateTitle()opacity: 0.75visible: !hasPlayer && text.length !== 0 && text !== appNameHeading.text}// subtextPlasmaExtras.Heading {id: subtextlevel: 5maximumLineCount: 1lineHeight: isWin ? 1 : subtext.lineHeightLayout.fillWidth: trueelide: Text.ElideRighttext: isWin ? generateSubText() : ""opacity: 0.6visible: text.length !== 0 && text !== appNameHeading.text}}// Count badge.// The badge itself is inside an item to better center the text in the bubbleItem {Layout.alignment: Qt.AlignRight | Qt.AlignTopLayout.preferredHeight: closeButton.heightLayout.preferredWidth: closeButton.widthvisible: flatIndex === 0 && smartLauncherCountVisibleBadge {anchors.centerIn: parentheight: units.iconSizes.smallMediumnumber: smartLauncherCount}}// close buttonPlasmaComponents3.ToolButton {id: closeButtonLayout.alignment: Qt.AlignRight | Qt.AlignTopvisible: isWinicon.name: "window-close"icon.width: units.iconSizes.smallicon.height: units.iconSizes.smallonClicked: {backend.cancelHighlightWindows();tasksModel.requestClose(submodelIndex);}}}// thumbnail containerItem {id: thumbnailSourceItemLayout.minimumWidth: header.widthLayout.preferredHeight: header.width / 2visible: isWinreadonly property bool isMinimized: isGroup ? IsMinimized == true : isMinimizedParent// TODO: this causes XCB error message when being visible the first timeproperty int winId: isWin && windows[flatIndex] !== undefined ? windows[flatIndex] : 0// There's no PlasmaComponents3 versionPlasmaComponents.Highlight {anchors.fill: hoverHandlervisible: hoverHandler.containsMousepressed: hoverHandler.containsPress}PlasmaCore.WindowThumbnail {anchors.fill: hoverHandler// Indent by one pixel to make sure we never cover up the entire highlightanchors.margins: 1visible: !albumArtImage.visible && !thumbnailSourceItem.isMinimizedwinId: thumbnailSourceItem.winId}Image {id: albumArtBackgroundsource: albumArtanchors.fill: parentfillMode: Image.PreserveAspectCropvisible: albumArtImage.availablelayer.enabled: trueopacity: 0.25layer.effect: FastBlur {source: albumArtBackgroundanchors.fill: parentradius: 30}}Image {id: albumArtImage// also Image.Loading to prevent loading thumbnails just because the album art takes a split second to loadreadonly property bool available: status === Image.Ready || status === Image.Loadinganchors.fill: hoverHandler// Indent by one pixel to make sure we never cover up the entire highlightanchors.margins: 1sourceSize: Qt.size(parent.width, parent.height)asynchronous: truesource: albumArtfillMode: Image.PreserveAspectFitvisible: available}// when minimized, we don't have a preview, so show the iconPlasmaCore.IconItem {width: parent.widthheight: thumbnailSourceItem.heightanchors.horizontalCenter: parent.horizontalCentersource: thumbnailSourceItem.isMinimized && !albumArtImage.visible ? icon : ""animated: falseusesPlasmaTheme: falsevisible: valid}ToolTipWindowMouseArea {id: hoverHandleranchors.fill: parentrootTask: parentTaskmodelIndex: submodelIndexwinId: thumbnailSourceItem.winId}}// Player controls rowRowLayout {Layout.maximumWidth: header.widthvisible: hasPlayerenabled: canControlColumnLayout {Layout.fillWidth: trueLayout.topMargin: units.smallSpacingLayout.bottomMargin: units.smallSpacingspacing: 0ScrollableTextWrapper {id: songTextWrapperLayout.fillWidth: trueLayout.preferredHeight: songText.heightPlasmaComponents3.Label {id: songTextparent: songTextWrapperwidth: parent.widthheight: undefinedlineHeight: 1maximumLineCount: artistText.visible? 1 : 2wrapMode: Text.NoWrapelide: parent.state ? Text.ElideNone : Text.ElideRighttext: track || ""}}ScrollableTextWrapper {id: artistTextWrapperLayout.fillWidth: trueLayout.preferredHeight: artistText.heightvisible: artistText.text !== ""PlasmaExtras.DescriptiveLabel {id: artistTextparent: artistTextWrapperwidth: parent.widthheight: undefinedwrapMode: Text.NoWraplineHeight: 1elide: parent.state ? Text.ElideNone : Text.ElideRighttext: artist || ""font.pointSize: theme.smallestFont.pointSize}}}PlasmaComponents3.ToolButton {enabled: canGoBackicon.name: LayoutMirroring.enabled ? "media-skip-forward" : "media-skip-backward"onClicked: mpris2Source.goPrevious(mprisSourceName)}PlasmaComponents3.ToolButton {enabled: playing ? canPause : canPlayicon.name: playing ? "media-playback-pause" : "media-playback-start"onClicked: {if (!playing) {mpris2Source.play(mprisSourceName);} else {mpris2Source.pause(mprisSourceName);}}}PlasmaComponents3.ToolButton {enabled: canGoNexticon.name: LayoutMirroring.enabled ? "media-skip-backward" : "media-skip-forward"onClicked: mpris2Source.goNext(mprisSourceName)}}function generateTitle() {if (!isWin) {return genericName != undefined ? genericName : "";}var text;if (isGroup) {if (model.display === undefined) {return "";}text = model.display.toString();} else {text = displayParent;}// KWin appends increasing integers in between pointy brackets to otherwise equal window titles.// In this case save <#number> as counter and delete it at the end of text.var counter = text.match(/<\d+>\W*$/);text = text.replace(/\s*<\d+>\W*$/, "");// Remove appName from the end of text.var appNameRegex = new RegExp(appName + "$", "i");text = text.replace(appNameRegex, "");text = text.replace(/\s*(?:-|β)*\s*$/, "");// Add counter back at the end.if (counter !== null) {if (text === "") {text = counter;} else {text = text + " " + counter;}}// In case the window title had only redundant information (i.e. appName), text is now empty.// Add a hyphen to indicate that and avoid empty space.if (text === "") {text = "β";}return text.toString();}function generateSubText() {if (activitiesParent === undefined) {return "";}var subTextEntries = [];var virtualDesktops = isGroup ? VirtualDesktops : virtualDesktopParent;var virtualDesktopNameList = new Array();for (var i = 0; i < virtualDesktops.length; ++i) {virtualDesktopNameList.push(virtualDesktopInfo.desktopNames[virtualDesktops[i] - 1]);}if (!plasmoid.configuration.showOnlyCurrentDesktop&& virtualDesktopInfo.numberOfDesktops > 1&& (isGroup ? IsOnAllVirtualDesktops : isOnAllVirtualDesktopsParent) !== true&& virtualDesktops.length > 0) {subTextEntries.push(i18nc("Comma-separated list of desktops", "On %1", virtualDesktopNameList.join()));}var act = isGroup ? Activities : activitiesParent;if (act === undefined) {return subTextEntries.join("\n");}if (act.length === 0 && activityInfo.numberOfRunningActivities > 1) {subTextEntries.push(i18nc("Which virtual desktop a window is currently on","Available on all activities"));} else if (act.length > 0) {var activityNames = [];for (var i = 0; i < act.length; i++) {var activity = act[i];var activityName = activityInfo.activityName(act[i]);if (activityName === "") {continue;}if (plasmoid.configuration.showOnlyCurrentActivity) {if (activity !== activityInfo.currentActivity) {activityNames.push(activityName);}} else if (activity !== activityInfo.currentActivity) {activityNames.push(activityName);}}if (plasmoid.configuration.showOnlyCurrentActivity) {if (activityNames.length > 0) {subTextEntries.push(i18nc("Activities a window is currently on (apart from the current one)","Also available on %1", activityNames.join(", ")));}} else if (activityNames.length > 0) {subTextEntries.push(i18nc("Which activities a window is currently on","Available on %1", activityNames.join(", ")));}}return subTextEntries.join("\n");}}