| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2017 Malte Veerman <[email protected]> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
import QtQuick 2.15 |
| 18 |
import QtQuick.Controls 2.15 |
| 19 |
import QtQml.Models 2.15 |
| 20 |
import QtQuick.Layouts 1.15 |
| 21 |
import org.kde.kirigami 2.14 as Kirigami |
| 22 |
|
| 23 |
|
| 24 |
Rectangle { |
| 25 |
id: root |
| 26 |
|
| 27 |
readonly property alias configEnabled: configureButton.checked |
| 28 |
property var addItem |
| 29 |
property var flickable |
| 30 |
property var contextRoot |
| 31 |
|
| 32 |
function resizeApplets() { |
| 33 |
var items = []; |
| 34 |
var children = toolbarAppletRow.contentItem.visibleChildren; |
| 35 |
for (var i=0; i<children.length; i++) { |
| 36 |
if (!!children[i].status && children[i].status != Loader.Error) |
| 37 |
items.push(children[i]); |
| 38 |
} |
| 39 |
if (items.length > 0) { |
| 40 |
var space = toolbarAppletRow.width - (items.length - 1) * toolbarAppletRow.spacing; |
| 41 |
var threshold = space / items.length; |
| 42 |
var smallApplets = []; |
| 43 |
var largeApplets = []; |
| 44 |
for (var i=0; i<items.length; i++) { |
| 45 |
if (!!items[i] && !!items[i].status && items[i].status != Loader.Error) |
| 46 |
smallApplets.push(items[i]); |
| 47 |
} |
| 48 |
while (smallApplets.length > 0 && space > 0) { |
| 49 |
var countSmallApplets = smallApplets.length; |
| 50 |
smallApplets.forEach(function (applet, index) { |
| 51 |
if (applet.implicitWidth >= threshold) { |
| 52 |
largeApplets.push(applet); |
| 53 |
applet.width = applet.implicitWidth; |
| 54 |
space -= applet.width; |
| 55 |
} |
| 56 |
}); |
| 57 |
smallApplets = smallApplets.filter(function (applet) { return largeApplets.indexOf(applet) == -1 }); |
| 58 |
if (countSmallApplets == smallApplets.length) { |
| 59 |
smallApplets.forEach(function (applet, index) { |
| 60 |
applet.width = space / countSmallApplets; |
| 61 |
}); |
| 62 |
return; |
| 63 |
} |
| 64 |
threshold = space / smallApplets.length; |
| 65 |
} |
| 66 |
if (smallApplets.length == 0) return; |
| 67 |
for (var i=0; i<smallApplets.length; i++) { |
| 68 |
var applet = smallApplets[i]; |
| 69 |
applet.width = applet.implicitWidth; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
height: configureButton.height + Kirigami.Units.smallSpacing |
| 75 |
radius: 4 |
| 76 |
color: palette.mid |
| 77 |
|
| 78 |
ListView { |
| 79 |
id: toolbarAppletRow |
| 80 |
|
| 81 |
anchors { |
| 82 |
top: parent.top |
| 83 |
bottom: parent.bottom |
| 84 |
left: parent.left |
| 85 |
right: configureButton.left |
| 86 |
leftMargin: spacing |
| 87 |
rightMargin: spacing |
| 88 |
} |
| 89 |
orientation: ListView.Horizontal |
| 90 |
spacing: Kirigami.Units.smallSpacing |
| 91 |
boundsBehavior: Flickable.StopAtBounds |
| 92 |
interactive: true |
| 93 |
clip: true |
| 94 |
|
| 95 |
model: AppletProxyModel |
| 96 |
|
| 97 |
delegate: Loader { |
| 98 |
active: true |
| 99 |
anchors.verticalCenter: parent.verticalCenter |
| 100 |
|
| 101 |
Component.onCompleted: { |
| 102 |
setSource("AppletToolbarAppletItem.qml", { |
| 103 |
"name": name, |
| 104 |
"appletId": appletId, |
| 105 |
"toolbar": root, |
| 106 |
"flickable": root.flickable, |
| 107 |
"contextRoot": root.contextRoot |
| 108 |
}); |
| 109 |
root.resizeApplets(); |
| 110 |
} |
| 111 |
onStatusChanged: { |
| 112 |
if (status == Loader.Error) { |
| 113 |
Context.error("Error loading toolbar item for applet: " + name); |
| 114 |
Context.error(sourceComponent.errorString()); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
onWidthChanged: root.resizeApplets() |
| 119 |
onCountChanged: root.resizeApplets() |
| 120 |
} |
| 121 |
|
| 122 |
ToolButton { |
| 123 |
id: configureButton |
| 124 |
|
| 125 |
anchors.verticalCenter: parent.verticalCenter |
| 126 |
anchors.right: parent.right |
| 127 |
anchors.margins: (parent.height - height) / 2 |
| 128 |
icon.name: "configure" |
| 129 |
checkable: true |
| 130 |
ToolTip.text: i18n( "Configure Applets..." ) |
| 131 |
ToolTip.visible: hovered |
| 132 |
ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval |
| 133 |
hoverEnabled: true |
| 134 |
} |
| 135 |
|
| 136 |
SystemPalette { |
| 137 |
id: palette |
| 138 |
} |
| 139 |
} |