Warning, /plasma/latte-dock/plasmoid/package/contents/ui/abilities/launchers/Validator.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 import QtQuick 2.0
0007 
0008 import org.kde.plasma.plasmoid 2.0
0009 
0010 import org.kde.latte.core 0.2 as LatteCore
0011 
0012 //! Launchers Validate Timer
0013 Timer{
0014     id:launchersOrderValidatorTimer
0015     interval: 400
0016 
0017     property var launchers: []
0018 
0019     function launchersAreInSync() {
0020         return arraysAreEqual(_launchers.currentShownLauncherList(), launchers);
0021     }
0022 
0023     function launcherValidPos(url) {
0024         for (var i=0; i<launchers.length; ++i) {
0025             if (launchers[i] === url) {
0026                 return i;
0027             }
0028         }
0029 
0030         return -1;
0031     }
0032 
0033     function arraysAreEqual(list1, list2) {
0034         if (list1.length !== list2.length) {
0035             console.log("  arrays have different size...")
0036             return false;
0037         }
0038 
0039         for (var i=0; i<list1.length; ++i) {
0040             if (list1[i] !== list2[i]) {
0041                 return false;
0042             }
0043         }
0044 
0045         return true;
0046     }
0047 
0048     //! true if upward is the best way to iterate through current
0049     //! in order to make it equal with goal
0050     function upwardIsBetter(current, goal)
0051     {
0052         var tCurrent = current.slice();
0053 
0054         if (!arraysAreEqual(tCurrent, goal)) {
0055             for (var i=0; i<tCurrent.length; ++i) {
0056                 if (tCurrent[i] !== goal[i]) {
0057                     var val = tCurrent[i];
0058                     tCurrent.splice(i, 1);
0059                     tCurrent.splice(goal.indexOf(val), 0, val);
0060 
0061                     if (arraysAreEqual(tCurrent, goal)){
0062                         return true;
0063                     } else {
0064                         return false;
0065                     }
0066                 }
0067             }
0068         }
0069 
0070         return false;
0071     }
0072 
0073     onTriggered: {
0074         if (launchersAreInSync()) {
0075             stop();
0076             console.log("launchers synced at:" + launchers);
0077             launchers.length = 0;
0078             _launchers.tasksModel.syncLaunchers();
0079         } else {
0080             var currentLaunchers = _launchers.currentShownLauncherList();
0081 
0082             if (upwardIsBetter(currentLaunchers, launchers)) {
0083                 console.log("UPWARD....");
0084                 for (var i=0; i<currentLaunchers.length; ++i) {
0085                     if (currentLaunchers[i] !== launchers[i]) {
0086                         var p = launcherValidPos(currentLaunchers[i]);
0087                         if (p === -1) {
0088                             console.log("No pos found for :"+currentLaunchers[i] + " at: "+launchers);
0089                             restart();
0090                             return;
0091                         }
0092                         var launcherLayoutIndex = _launchers.indexOfLayoutLauncher(currentLaunchers[i]);
0093 
0094                         if (launcherLayoutIndex === -1) {
0095                             console.log(" launcher was not found in model, syncing stopped...");
0096                             stop();
0097                             return;
0098                         }
0099 
0100                         console.log(" moving:" +launcherLayoutIndex + " _ " + p );
0101                         _launchers.tasksModel.move(launcherLayoutIndex, p);
0102                         restart();
0103                         return;
0104                     }
0105                 }
0106             } else {
0107                 console.log("DOWNWARD....");
0108                 for (var i=currentLaunchers.length-1; i>=0; --i) {
0109                     if (currentLaunchers[i] !== launchers[i]) {
0110                         var p = launcherValidPos(currentLaunchers[i]);
0111                         if (p === -1) {
0112                             console.log("No pos found for :"+currentLaunchers[i] + " at: "+launchers);
0113                             restart();
0114                             return;
0115                         }
0116                         var launcherLayoutIndex = _launchers.indexOfLayoutLauncher(currentLaunchers[i]);
0117 
0118                         if (launcherLayoutIndex === -1) {
0119                             console.log(" launcher was not found in model, syncing stopped...");
0120                             stop();
0121                             return;
0122                         }
0123 
0124                         console.log(" moving:" +launcherLayoutIndex + " _ " + p );
0125                         _launchers.tasksModel.move(launcherLayoutIndex, p);
0126                         restart();
0127                         return;
0128                     }
0129                 }
0130             }
0131 
0132             console.log("why we reached ??? ");
0133             console.log("CURRENT ::: " + currentLaunchers);
0134             console.log("VALID   ::: " + launchers);
0135         }
0136     }
0137 }