use the logic of the panel to put applets

This commit is contained in:
Marco Martin 2015-03-11 11:50:00 +01:00
parent 50117f9e07
commit 0c531de5b0
3 changed files with 265 additions and 15 deletions

View file

@ -0,0 +1,189 @@
/*
* Copyright 2013 Marco Martin <mart@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 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.
*/
.pragma library
var layout;
var root;
var plasmoid;
var lastSpacer;
function restore() {
var configString = String(plasmoid.configuration.AppletOrder)
//array, a cell for encoded item order
var itemsArray = configString.split(";");
//map applet id->order in panel
var idsOrder = new Object();
//map order in panel -> applet pointer
var appletsOrder = new Object();
for (var i = 0; i < itemsArray.length; i++) {
//property name: applet id
//property value: order
idsOrder[itemsArray[i]] = i;
}
for (var i = 0; i < plasmoid.applets.length; ++i) {
if (idsOrder[plasmoid.applets[i].id] !== undefined) {
appletsOrder[idsOrder[plasmoid.applets[i].id]] = plasmoid.applets[i];
//ones that weren't saved in AppletOrder go to the end
} else {
appletsOrder["unordered"+i] = plasmoid.applets[i];
}
}
//finally, restore the applets in the correct order
for (var i in appletsOrder) {
root.addApplet(appletsOrder[i], -1, -1)
}
//rewrite, so if in the orders there were now invalid ids or if some were missing creates a correct list instead
save();
}
function save() {
var ids = new Array();
for (var i = 0; i < layout.children.length; ++i) {
var child = layout.children[i];
if (child.applet) {
ids.push(child.applet.id);
}
}
plasmoid.configuration.AppletOrder = ids.join(';');
}
function removeApplet (applet) {
for (var i = layout.children.length - 1; i >= 0; --i) {
var child = layout.children[i];
if (child.applet === applet) {
child.destroy();
}
}
}
//insert item2 before item1
function insertBefore(item1, item2) {
if (item1 === item2) {
return;
}
var removed = new Array();
var child;
var i;
for (i = layout.children.length - 1; i >= 0; --i) {
child = layout.children[i];
removed.push(child);
child.parent = root;
if (child === item1) {
break;
}
}
item2.parent = layout;
for (var j = removed.length - 1; j >= 0; --j) {
removed[j].parent = layout;
}
return i;
}
//insert item2 after item1
function insertAfter(item1, item2) {
if (item1 === item2) {
return;
}
var removed = new Array();
var child;
var i;
for (i = layout.children.length - 1; i >= 0; --i) {
child = layout.children[i];
//never ever insert after lastSpacer
if (child === lastSpacer && item1 === lastSpacer) {
removed.push(child);
child.parent = root;
break;
} else if (child === item1) {
break;
}
removed.push(child);
child.parent = root;
}
item2.parent = layout;
for (var j = removed.length - 1; j >= 0; --j) {
removed[j].parent = layout;
}
return i;
}
function insertAtIndex(item, position) {
if (position < 0 || position >= layout.children.length) {
return;
}
//never ever insert after lastSpacer
if (layout.children[position] === lastSpacer) {
--position;
}
var removedItems = new Array();
for (var i = position; i < layout.children.length; ++i) {
var child = layout.children[position];
child.parent = root;
removedItems.push(child);
}
item.parent = layout;
for (var i in removedItems) {
removedItems[i].parent = layout;
}
}
function insertAtCoordinates(item, x, y) {
if (root.isHorizontal) {
y = layout.height / 2;
} else {
x = layout.width / 2;
}
var child = layout.childAt(x, y);
if (!child || child === item) {
child = layout.children[0];
}
item.parent = root;
//PlasmaCore.Types.Vertical = 3
if ((plasmoid.formFactor === 3 && y < child.y + child.height/2) ||
(plasmoid.formFactor !== 3 && x < child.x + child.width/2)) {
return insertBefore(child, item);
} else {
return insertAfter(child, item);
}
}

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile name=""/>
<group name="General">
<entry name="AppletOrder" type="String">
<label>encoded order of items</label>
</entry>
</group>
</kcfg>

View file

@ -25,6 +25,8 @@ import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.satellite.components 0.1 as SatelliteComponents
import "plasmapackage:/code/LayoutManager.js" as LayoutManager
Item {
id: root
width: 480
@ -35,6 +37,62 @@ Item {
property int buttonHeight: width/4
property bool reorderingApps: false
Containment.onAppletAdded: {
addApplet(applet, x, y);
LayoutManager.save();
}
function addApplet(applet, x, y) {
var container = appletContainerComponent.createObject(appletsSpace.layout)
container.visible = true
print("Applet added: " + applet)
var appletWidth = applet.width;
var appletHeight = applet.height;
applet.parent = container;
container.applet = applet;
applet.anchors.fill = container;
applet.visible = true;
container.visible = true;
// If the provided position is valid, use it.
if (x >= 0 && y >= 0) {
var index = LayoutManager.insertAtCoordinates(container, x , y);
// Fall through to determining an appropriate insert position.
} else {
var before = null;
container.animationsEnabled = false;
if (appletsSpace.lastSpacer.parent === appletsSpace.layout) {
before = appletsSpace.lastSpacer;
}
if (before) {
LayoutManager.insertBefore(before, container);
// Fall through to adding at the end.
} else {
container.parent = appletsSpace.layout;
}
//event compress the enable of animations
//startupTimer.restart();
}
if (applet.Layout.fillWidth) {
appletsSpace.lastSpacer.parent = root;
}
}
Component.onCompleted: {
LayoutManager.plasmoid = plasmoid;
LayoutManager.root = root;
LayoutManager.layout = appletsSpace.layout;
LayoutManager.lastSpacer = appletsSpace.lastSpacer;
LayoutManager.restore();
}
SatelliteComponents.ApplicationListModel {
id: appListModel
}
@ -53,21 +111,12 @@ Item {
}
}
}
Containment.onAppletAdded: {
var container = appletContainerComponent.createObject(appletsSpace.layout)
container.visible = true
print("Applet added: " + applet)
applet.parent = container
container.applet = applet
applet.anchors.fill = applet.parent
applet.visible = true
container.width = 500
container.height = 500
}
Component {
id: appletContainerComponent
Item {
//not used yet
property bool animationsEnabled: false
property Item applet
Layout.fillWidth: true
Layout.fillHeight: applet && applet.Layout.fillHeight
@ -118,6 +167,7 @@ Item {
header: MouseArea {
z: 999
property Item layout: mainLayout
property Item lastSpacer: spacer
width: root.width
height: Math.max(root.height, ((root.height - units.gridUnit * 2)/2) * mainLayout.children.length)
@ -132,6 +182,7 @@ Item {
bottomMargin: stripe.height + units.gridUnit * 2
}
Item {
id: spacer
Layout.fillWidth: true
Layout.fillHeight: true
}
@ -201,8 +252,4 @@ Item {
}
}
}
Component.onCompleted: {
print("root Containment loaded")
}
}