StatusNotifierItem.qml 4.52 KB
Newer Older
Marco Martin's avatar
Marco Martin committed
1
/*
Marco Martin's avatar
Marco Martin committed
2
 *   Copyright 2016 Marco Martin <mart@kde.org>
Marco Martin's avatar
Marco Martin committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *   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  02110-1301, USA.
 */

import QtQuick 2.1
import org.kde.plasma.core 2.0 as PlasmaCore

Marco Martin's avatar
Marco Martin committed
23
AbstractItem {
Marco Martin's avatar
Marco Martin committed
24
    id: taskIcon
Marco Martin's avatar
Marco Martin committed
25

26
    itemId: Id
Marco Martin's avatar
Marco Martin committed
27
    text: Title
28
29
    mainText: ToolTipTitle != "" ? ToolTipTitle : Title
    subText: ToolTipSubTitle
30
    icon: ToolTipIcon != "" ? ToolTipIcon : Icon ? Icon : IconName
Marco Martin's avatar
Marco Martin committed
31
    textFormat: Text.AutoText
32
    category: Category
Marco Martin's avatar
Marco Martin committed
33

34
35
36
37
38
39
40
41
42
43
44
    status: {
        switch (Status) {
        case "Active":
            return PlasmaCore.Types.ActiveStatus;
        case "NeedsAttention":
            return PlasmaCore.Types.NeedsAttentionStatus;
        //just assume passive
        default:
            return PlasmaCore.Types.PassiveStatus;
        }
    }
Marco Martin's avatar
Marco Martin committed
45

46
47
48
49
50
    onStatusChanged: {
        //binding is not working in PlasmaCore.IconItem
        iconItem.source = status === PlasmaCore.Types.NeedsAttentionStatus ? (AttentionIcon ? AttentionIcon : AttentionIconName) : (Icon ? Icon : IconName)
    }

51
52
53
54
    iconItem: iconItem

    PlasmaCore.IconItem {
        id: iconItem
55
        source: Icon ? Icon : IconName
56
57
        width: Math.min(parent.width, parent.height)
        height: width
58
        active: taskIcon.containsMouse
59
60
61
62
63

        anchors {
            left: parent.left
            verticalCenter: parent.verticalCenter
        }
Marco Martin's avatar
Marco Martin committed
64
65
    }

66
67
68
69
    onContextMenu: {
        openContextMenu(plasmoid.nativeInterface.popupPosition(taskIcon, mouse.x, mouse.y))
    }

Marco Martin's avatar
Marco Martin committed
70
    onClicked: {
71
        var pos = plasmoid.nativeInterface.popupPosition(taskIcon, mouse.x, mouse.y);
72

Marco Martin's avatar
Marco Martin committed
73
74
        switch (mouse.button) {
        case Qt.LeftButton: {
75
            var service = statusNotifierSource.serviceForSource(DataEngineSource);
Marco Martin's avatar
Marco Martin committed
76
            var operation = service.operationDescription("Activate");
77
            operation.x = pos.x;
78
            operation.y = pos.y;
79
80
81
82
83
            var job = service.startOperationCall(operation);
            job.finished.connect(function () {
                if (!job.result) {
                    // On error try to invoke the context menu.
                    // Workaround primarily for apps using libappindicator.
84
                    openContextMenu(pos);
85
86
                }
            });
Marco Martin's avatar
Marco Martin committed
87
88
            break;
        }
89
        case Qt.RightButton:
90
            openContextMenu(pos);
91
92
            break;

Marco Martin's avatar
Marco Martin committed
93
        case Qt.MiddleButton:
94
            var service = statusNotifierSource.serviceForSource(DataEngineSource);
95
            var operation = service.operationDescription("SecondaryActivate");
96
            operation.x = pos.x;
97

98
            operation.y = pos.y;
99
100
            service.startOperationCall(operation);
            break;
Marco Martin's avatar
Marco Martin committed
101
        }
Marco Martin's avatar
Marco Martin committed
102
    }
Marco Martin's avatar
Marco Martin committed
103

104
    function openContextMenu(pos) {
105
        var service = statusNotifierSource.serviceForSource(DataEngineSource);
106
107
108
109
110
111
112
113
114
115
        var operation = service.operationDescription("ContextMenu");
        operation.x = pos.x;
        operation.y = pos.y;

        var job = service.startOperationCall(operation);
        job.finished.connect(function () {
            plasmoid.nativeInterface.showStatusNotifierContextMenu(job, taskIcon);
        });
    }

Marco Martin's avatar
Marco Martin committed
116
117
118
    onWheel: {
        //don't send activateVertScroll with a delta of 0, some clients seem to break (kmix)
        if (wheel.angleDelta.y !== 0) {
119
            var service = statusNotifierSource.serviceForSource(DataEngineSource);
Marco Martin's avatar
Marco Martin committed
120
121
122
123
124
125
            var operation = service.operationDescription("Scroll");
            operation.delta =wheel.angleDelta.y;
            operation.direction = "Vertical";
            service.startOperationCall(operation);
        }
        if (wheel.angleDelta.x !== 0) {
126
            var service = statusNotifierSource.serviceForSource(DataEngineSource);
Marco Martin's avatar
Marco Martin committed
127
128
129
130
131
132
            var operation = service.operationDescription("Scroll");
            operation.delta =wheel.angleDelta.x;
            operation.direction = "Horizontal";
            service.startOperationCall(operation);
        }
    }
Marco Martin's avatar
Marco Martin committed
133
}