/*SPDX-FileCopyrightText: 2012-2013 Eike Hein <hein@kde.org>SPDX-License-Identifier: GPL-2.0-or-later*/import QtQuick 2.15import org.kde.plasma.core as PlasmaCoreimport org.kde.ksvg 1.0 as KSvgimport org.kde.plasma.extras 2.0 as PlasmaExtrasimport org.kde.plasma.components 3.0 as PlasmaComponents3import org.kde.kirigami 2.20 as Kirigamiimport org.kde.plasma.private.taskmanager 0.1 as TaskManagerAppletimport org.kde.plasma.plasmoid 2.0import "code/layout.js" as LayoutManagerimport "code/tools.js" as TaskToolsPlasmaCore.ToolTipArea {id: taskactiveFocusOnTab: trueheight: Math.max(Kirigami.Units.iconSizes.sizeForLabels, Kirigami.Units.iconSizes.medium) + LayoutManager.verticalMargins()visible: false// To achieve a bottom to top layout, the task manager is rotated by 180 degrees(see main.qml).// This makes the tasks mirrored, so we mirror them again to fix that.rotation: Plasmoid.configuration.reverseMode && Plasmoid.formFactor === PlasmaCore.Types.Vertical ? 180 : 0LayoutMirroring.enabled: (Qt.application.layoutDirection == Qt.RightToLeft)LayoutMirroring.childrenInherit: (Qt.application.layoutDirection == Qt.RightToLeft)required property var modelrequired property int indexreadonly property int pid: model.AppPidreadonly property string appName: model.AppNamereadonly property string appId: model.AppId.replace(/\.desktop/, '')property bool toolTipOpen: falseproperty bool inPopup: falseproperty bool isWindow: model.IsWindowproperty int childCount: model.ChildCountproperty int previousChildCount: 0property alias labelText: label.textproperty QtObject contextMenu: nullreadonly property bool smartLauncherEnabled: !inPopup && !model.IsStartupproperty QtObject smartLauncherItem: nullproperty Item audioStreamIcon: nullproperty var audioStreams: []property bool delayAudioStreamIndicator: falsereadonly property bool audioIndicatorsEnabled: Plasmoid.configuration.indicateAudioStreamsreadonly property bool hasAudioStream: audioStreams.length > 0readonly property bool playingAudio: hasAudioStream && audioStreams.some(function (item) {return !item.corked})readonly property bool muted: hasAudioStream && audioStreams.every(function (item) {return item.muted})readonly property bool highlighted: (inPopup && activeFocus) || (!inPopup && containsMouse)|| (task.contextMenu && task.contextMenu.status === PlasmaExtras.Menu.Open)|| (!!tasks.groupDialog && tasks.groupDialog.visualParent === task)active: (Plasmoid.configuration.showToolTips || tasks.toolTipOpenedByClick === task) && !inPopup && !tasks.groupDialoginteractive: model.IsWindow || mainItem.playerDatalocation: Plasmoid.locationmainItem: model.IsWindow ? openWindowToolTipDelegate : pinnedAppToolTipDelegateAccessible.name: model.displayAccessible.description: {if (!model.display) {return "";}if (model.IsLauncher) {return i18nc("@info:usagetip %1 application name", "Launch %1", model.display)}let smartLauncherDescription = "";if (iconBox.active) {smartLauncherDescription += i18ncp("@info:tooltip", "There is %1 new message.", "There are %1 new messages.", task.smartLauncherItem.count);}if (model.IsGroupParent) {switch (Plasmoid.configuration.groupedTaskVisualization) {case 0:break; // Use the default descriptioncase 1: {if (Plasmoid.configuration.showToolTips) {return `${i18nc("@info:usagetip %1 task name", "Show Task tooltip for %1", model.display)}; ${smartLauncherDescription}`;}// fallthrough}case 2: {if (backend.windowViewAvailable) {return `${i18nc("@info:usagetip %1 task name", "Show windows side by side for %1", model.display)}; ${smartLauncherDescription}`;}// fallthrough}default:return `${i18nc("@info:usagetip %1 task name", "Open textual list of windows for %1", model.display)}; ${smartLauncherDescription}`;}}return `${i18n("Activate %1", model.display)}; ${smartLauncherDescription}`;}Accessible.role: Accessible.ButtononToolTipVisibleChanged: toolTipVisible => {task.toolTipOpen = toolTipVisible;if (!toolTipVisible) {tasks.toolTipOpenedByClick = null;} else {tasks.toolTipAreaItem = task;}}onContainsMouseChanged: if (containsMouse) {task.forceActiveFocus(Qt.MouseFocusReason);task.updateMainItemBindings();} else {tasks.toolTipOpenedByClick = null;}onHighlightedChanged: {// ensure it doesn't get stuck with a window highlightedbackend.cancelHighlightWindows();}onPidChanged: updateAudioStreams({delay: false})onAppNameChanged: updateAudioStreams({delay: false})onIsWindowChanged: {if (model.IsWindow) {taskInitComponent.createObject(task);updateAudioStreams({delay: false});}}onChildCountChanged: {if (TaskTools.taskManagerInstanceCount < 2 && childCount > previousChildCount) {tasksModel.requestPublishDelegateGeometry(modelIndex(), backend.globalRect(task), task);}previousChildCount = childCount;}onIndexChanged: {hideToolTip();if (!inPopup && !tasks.vertical&& (LayoutManager.calculateStripes() > 1 || !Plasmoid.configuration.separateLaunchers)) {tasks.requestLayout();}}onSmartLauncherEnabledChanged: {if (smartLauncherEnabled && !smartLauncherItem) {const smartLauncher = Qt.createQmlObject(`import org.kde.plasma.private.taskmanager 0.1 as TaskManagerAppletTaskManagerApplet.SmartLauncherItem { }`, task);smartLauncher.launcherUrl = Qt.binding(() => model.LauncherUrlWithoutIcon);smartLauncherItem = smartLauncher;}}onHasAudioStreamChanged: {const audioStreamIconActive = hasAudioStream && audioIndicatorsEnabled;if (!audioStreamIconActive) {if (audioStreamIcon !== null) {audioStreamIcon.destroy();audioStreamIcon = null;}return;}// Create item on demand instead of using Loader to reduce memory consumption,// because only a few applications have audio streams.const component = Qt.createComponent("AudioStream.qml");audioStreamIcon = component.createObject(task);component.destroy();}onAudioIndicatorsEnabledChanged: task.hasAudioStreamChanged()Keys.onMenuPressed: menuTapHandler.longPressed()Keys.onReturnPressed: TaskTools.activateTask(modelIndex(), model, event.modifiers, task, Plasmoid, tasks)Keys.onEnterPressed: Keys.returnPressed(event);Keys.onSpacePressed: Keys.returnPressed(event);Keys.onUpPressed: Keys.leftPressed(event)Keys.onDownPressed: Keys.rightPressed(event)Keys.onLeftPressed: if (!inPopup && (event.modifiers & Qt.ControlModifier) && (event.modifiers & Qt.ShiftModifier)) {tasksModel.move(task.index, task.index - 1);} else {event.accepted = false;}Keys.onRightPressed: if (!inPopup && (event.modifiers & Qt.ControlModifier) && (event.modifiers & Qt.ShiftModifier)) {tasksModel.move(task.index, task.index + 1);} else {event.accepted = false;}function modelIndex() {return (inPopup ? tasksModel.makeModelIndex(groupDialog.visualParent.index, index): tasksModel.makeModelIndex(index));}function showContextMenu(args) {task.hideImmediately();contextMenu = tasks.createContextMenu(task, modelIndex(), args);contextMenu.show();}function updateAudioStreams(args) {if (args) {// When the task just appeared (e.g. virtual desktop switch), show the audio indicator// right away. Only when audio streams change during the lifetime of this task, delay// showing that to avoid distraction.delayAudioStreamIndicator = !!args.delay;}var pa = pulseAudio.item;if (!pa || !task.isWindow) {task.audioStreams = [];return;}// Check appid first for app using portal// https://docs.pipewire.org/page_portal.htmlvar streams = pa.streamsForAppId(task.appId);if (!streams.length) {streams = pa.streamsForPid(model.AppPid);if (streams.length) {pa.registerPidMatch(model.AppName);} else {// We only want to fall back to appName matching if we never managed to map// a PID to an audio stream window. Otherwise if you have two instances of// an application, one playing and the other not, it will look up appName// for the non-playing instance and erroneously show an indicator on both.if (!pa.hasPidMatch(model.AppName)) {streams = pa.streamsForAppName(model.AppName);}}}task.audioStreams = streams;}function toggleMuted() {if (muted) {task.audioStreams.forEach(function (item) { item.unmute(); });} else {task.audioStreams.forEach(function (item) { item.mute(); });}}// Will also be called in activateTaskAtIndex(index)function updateMainItemBindings() {if ((mainItem.parentTask === task && mainItem.rootIndex.row === task.index) || (tasks.toolTipOpenedByClick === null && !task.active) || (tasks.toolTipOpenedByClick !== null && tasks.toolTipOpenedByClick !== task)) {return;}mainItem.blockingUpdates = (mainItem.isGroup !== model.IsGroupParent); // BUG 464597 Force unload the previous componentmainItem.parentTask = task;mainItem.rootIndex = tasksModel.makeModelIndex(index, -1);mainItem.appName = Qt.binding(() => model.AppName);mainItem.pidParent = Qt.binding(() => model.AppPid);mainItem.windows = Qt.binding(() => model.WinIdList);mainItem.isGroup = Qt.binding(() => model.IsGroupParent);mainItem.icon = Qt.binding(() => model.decoration);mainItem.launcherUrl = Qt.binding(() => model.LauncherUrlWithoutIcon);mainItem.isLauncher = Qt.binding(() => model.IsLauncher);mainItem.isMinimizedParent = Qt.binding(() => model.IsMinimized);mainItem.displayParent = Qt.binding(() => model.display);mainItem.genericName = Qt.binding(() => model.GenericName);mainItem.virtualDesktopParent = Qt.binding(() => model.VirtualDesktops);mainItem.isOnAllVirtualDesktopsParent = Qt.binding(() => model.IsOnAllVirtualDesktops);mainItem.activitiesParent = Qt.binding(() => model.Activities);mainItem.smartLauncherCountVisible = Qt.binding(() => task.smartLauncherItem && task.smartLauncherItem.countVisible);mainItem.smartLauncherCount = Qt.binding(() => mainItem.smartLauncherCountVisible ? task.smartLauncherItem.count : 0);mainItem.blockingUpdates = false;tasks.toolTipAreaItem = task;}Connections {target: pulseAudio.itemignoreUnknownSignals: true // Plasma-PA might not be availablefunction onStreamsChanged() {task.updateAudioStreams({delay: true})}}TapHandler {id: menuTapHandleracceptedButtons: Qt.LeftButtonacceptedDevices: PointerDevice.TouchScreen | PointerDevice.StylusonLongPressed: {// When we're a launcher, there's no window controls, so we can show all// places without the menu getting super huge.if (model.IsLauncher) {showContextMenu({showAllPlaces: true})} else {showContextMenu();}}}TapHandler {acceptedButtons: Qt.RightButtonacceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.StylusgesturePolicy: TapHandler.WithinBounds // Release grab when menu appearslongPressThreshold: 0.001 // https://invent.kde.org/qt/qt/qtdeclarative/-/commit/8f6809681ec82da783ae8dcd76fa2c209b28fde6onLongPressed: menuTapHandler.longPressed()}TapHandler {acceptedButtons: Qt.LeftButtononTapped: {if (Plasmoid.configuration.showToolTips && task.active) {hideToolTip();}TaskTools.activateTask(modelIndex(), model, point.modifiers, task, Plasmoid, tasks);}}TapHandler {acceptedButtons: Qt.MiddleButton | Qt.BackButton | Qt.ForwardButtononTapped: (eventPoint, button) => {if (button === Qt.MiddleButton) {if (Plasmoid.configuration.middleClickAction === TaskManagerApplet.Backend.NewInstance) {tasksModel.requestNewInstance(modelIndex());} else if (Plasmoid.configuration.middleClickAction === TaskManagerApplet.Backend.Close) {tasks.taskClosedWithMouseMiddleButton = model.WinIdList.slice()tasksModel.requestClose(modelIndex());} else if (Plasmoid.configuration.middleClickAction === TaskManagerApplet.Backend.ToggleMinimized) {tasksModel.requestToggleMinimized(modelIndex());} else if (Plasmoid.configuration.middleClickAction === TaskManagerApplet.Backend.ToggleGrouping) {tasksModel.requestToggleGrouping(modelIndex());} else if (Plasmoid.configuration.middleClickAction === TaskManagerApplet.Backend.BringToCurrentDesktop) {tasksModel.requestVirtualDesktops(modelIndex(), [virtualDesktopInfo.currentDesktop]);}} else if (button === Qt.BackButton || button === Qt.ForwardButton) {const playerData = mpris2Source.playerForLauncherUrl(model.LauncherUrlWithoutIcon, model.AppPid);if (playerData) {if (button === Qt.BackButton) {playerData.Previous();} else {playerData.Next();}} else {eventPoint.accepted = false;}}backend.cancelHighlightWindows();}}KSvg.FrameSvgItem {id: frameanchors {fill: parenttopMargin: (!tasks.vertical && taskList.rows > 1) ? LayoutManager.iconMargin : 0bottomMargin: (!tasks.vertical && taskList.rows > 1) ? LayoutManager.iconMargin : 0leftMargin: ((inPopup || tasks.vertical) && taskList.columns > 1) ? LayoutManager.iconMargin : 0rightMargin: ((inPopup || tasks.vertical) && taskList.columns > 1) ? LayoutManager.iconMargin : 0}imagePath: "widgets/tasks"property bool isHovered: task.highlighted && Plasmoid.configuration.taskHoverEffectproperty string basePrefix: "normal"prefix: isHovered ? TaskTools.taskPrefixHovered(basePrefix, Plasmoid.location) : TaskTools.taskPrefix(basePrefix, Plasmoid.location)// Avoid repositioning delegate item after dragFinishedDragHandler {id: dragHandlergrabPermissions: PointerHandler.TakeOverForbiddenfunction setRequestedInhibitDnd(value) {// This is modifying the value in the panel containment that// inhibits accepting drag and drop, so that we don't accidentally// drop the task on this panel.let item = this;while (item.parent) {item = item.parent;if (item.appletRequestsInhibitDnD !== undefined) {item.appletRequestsInhibitDnD = value}}}onActiveChanged: if (active) {icon.grabToImage((result) => {if (!dragHandler.active) {// BUG 466675 grabToImage is async, so avoid updating dragSource when active is falsereturn;}setRequestedInhibitDnd(true);tasks.dragSource = task;dragHelper.Drag.imageSource = result.url;dragHelper.Drag.mimeData = {"text/x-orgkdeplasmataskmanager_taskurl": backend.tryDecodeApplicationsUrl(model.LauncherUrlWithoutIcon).toString(),[model.MimeType]: model.MimeData,"application/x-orgkdeplasmataskmanager_taskbuttonitem": model.MimeData,};dragHelper.Drag.active = dragHandler.active;});} else {setRequestedInhibitDnd(false);dragHelper.Drag.active = false;dragHelper.Drag.imageSource = "";}}}Loader {id: taskProgressOverlayLoaderanchors.fill: frameasynchronous: trueactive: model.IsWindow && task.smartLauncherItem && task.smartLauncherItem.progressVisiblesource: "TaskProgressOverlay.qml"}Loader {id: iconBoxanchors {left: parent.leftleftMargin: adjustMargin(true, parent.width, taskFrame.margins.left)top: parent.toptopMargin: adjustMargin(false, parent.height, taskFrame.margins.top)}width: heightheight: (parent.height - adjustMargin(false, parent.height, taskFrame.margins.top)- adjustMargin(false, parent.height, taskFrame.margins.bottom))asynchronous: trueactive: height >= Kirigami.Units.iconSizes.small&& task.smartLauncherItem && task.smartLauncherItem.countVisiblesource: "TaskBadgeOverlay.qml"function adjustMargin(vert, size, margin) {if (!size) {return margin;}var margins = vert ? LayoutManager.horizontalMargins() : LayoutManager.verticalMargins();if ((size - margins) < Kirigami.Units.iconSizes.small) {return Math.ceil((margin * (Kirigami.Units.iconSizes.small / size)) / 2);}return margin;}Kirigami.Icon {id: iconanchors.fill: parentactive: task.highlightedenabled: truesource: model.decoration}states: [// Using a state transition avoids a binding loop between label.visible and// the text label margin, which derives from the icon width.State {name: "standalone"when: !label.visibleAnchorChanges {target: iconBoxanchors.left: undefinedanchors.horizontalCenter: parent.horizontalCenter}PropertyChanges {target: iconBoxanchors.leftMargin: 0width: parent.width - adjustMargin(true, task.width, taskFrame.margins.left)- adjustMargin(true, task.width, taskFrame.margins.right)}}]Loader {anchors.centerIn: parentwidth: Math.min(parent.width, parent.height)height: widthactive: model.IsStartupsourceComponent: busyIndicator}}PlasmaComponents3.Label {id: labelvisible: (inPopup || !iconsOnly && !model.IsLauncher&& (parent.width - iconBox.height - Kirigami.Units.smallSpacing) >= (Kirigami.Units.iconSizes.sizeForLabels * LayoutManager.minimumMColumns()))anchors {fill: parentleftMargin: taskFrame.margins.left + iconBox.width + LayoutManager.labelMargintopMargin: taskFrame.margins.toprightMargin: taskFrame.margins.right + (audioStreamIcon !== null && audioStreamIcon.visible ? (audioStreamIcon.width + LayoutManager.labelMargin) : 0)bottomMargin: taskFrame.margins.bottom}wrapMode: (maximumLineCount == 1) ? Text.NoWrap : Text.Wrapelide: Text.ElideRighttextFormat: Text.PlainTextverticalAlignment: Text.AlignVCentermaximumLineCount: Plasmoid.configuration.maxTextLines || undefined// use State to avoid unnecessary re-evaluation when the label is invisiblestates: State {name: "labelVisible"when: label.visiblePropertyChanges {target: labeltext: model.display}}}states: [State {name: "launcher"when: model.IsLauncherPropertyChanges {target: framebasePrefix: ""}},State {name: "attention"when: model.IsDemandingAttention || (task.smartLauncherItem && task.smartLauncherItem.urgent)PropertyChanges {target: framebasePrefix: "attention"}},State {name: "minimized"when: model.IsMinimizedPropertyChanges {target: framebasePrefix: "minimized"}},State {name: "active"when: model.IsActivePropertyChanges {target: framebasePrefix: "focus"}}]Component.onCompleted: {if (!inPopup && model.IsWindow) {var component = Qt.createComponent("GroupExpanderOverlay.qml");component.createObject(task);component.destroy();updateAudioStreams({delay: false});}if (!inPopup && !model.IsWindow) {taskInitComponent.createObject(task);}}}