From c24bc323023f775d307269b245201af6b63b13ff Mon Sep 17 00:00:00 2001
From: Michail Vourlakos <mvourlakos@gmail.com>
Date: Sat, 18 Feb 2017 18:53:41 +0200
Subject: [PATCH] improve window previews experience

--make the window previews experience more
robust
---
 plasmoid/contents/ui/MouseHandler.qml     |  7 ++-
 plasmoid/contents/ui/TaskDelegate.qml     | 16 +++----
 plasmoid/contents/ui/ToolTipDelegate2.qml |  9 ++--
 plasmoid/contents/ui/main.qml             | 57 +++++++++++------------
 4 files changed, 43 insertions(+), 46 deletions(-)

diff --git a/plasmoid/contents/ui/MouseHandler.qml b/plasmoid/contents/ui/MouseHandler.qml
index 75cc0fdbf..02d2d2967 100644
--- a/plasmoid/contents/ui/MouseHandler.qml
+++ b/plasmoid/contents/ui/MouseHandler.qml
@@ -144,9 +144,8 @@ Item {
                 activationTimer.stop();
             }
 
-            if (hoveredItem && windowsPreviewDlg.visible && toolTipDelegate.currentItem !== hoveredItem.itemIndex ) {
-                windowsPreviewDlg.hide();
-                toolTipDelegate.currentItem=-1;
+            if (hoveredItem && windowsPreviewDlg.visible && toolTipDelegate.parentIndex !== hoveredItem.itemIndex ) {
+                windowsPreviewDlg.hide(6);
             }
         }
 
@@ -188,7 +187,7 @@ Item {
                    // groupDialog.visible = true;
                 } else if (parent.hoveredItem.m.IsLauncher !== true) {
                     if(windowsPreviewDlg.visible && toolTipDelegate.currentItem !==parent.hoveredItem.itemIndex ) {
-                        windowsPreviewDlg.hide();
+                        windowsPreviewDlg.hide(5);
                         toolTipDelegate.currentItem=-1;
                     }
 
diff --git a/plasmoid/contents/ui/TaskDelegate.qml b/plasmoid/contents/ui/TaskDelegate.qml
index 6b9bfc221..ca29bfe8a 100644
--- a/plasmoid/contents/ui/TaskDelegate.qml
+++ b/plasmoid/contents/ui/TaskDelegate.qml
@@ -502,8 +502,7 @@ MouseArea{
     ///////////////// Mouse Area Events ///////////////////
     onEntered: {
         if ((icList.hoveredIndex !== itemIndex) && isLauncher && windowsPreviewDlg.visible) {
-            windowsPreviewDlg.hide();
-            toolTipDelegate.currentItem = -1;
+            windowsPreviewDlg.hide(1);
         }
 
         checkListHovered.stop();
@@ -623,7 +622,7 @@ MouseArea{
 
     onPressed: {
         if (windowSystem.compositingActive) {
-            windowsPreviewDlg.hide();
+            windowsPreviewDlg.hide(2);
         }
 
         if ((mouse.button == Qt.LeftButton)||(mouse.button == Qt.MidButton)) {
@@ -678,10 +677,10 @@ MouseArea{
                             root.presentWindows(model.LegacyWinIdList);
                         } else {
                             if ((windowsPreviewDlg.visualParent === mainItemContainer)&&(windowsPreviewDlg.visible)) {
-                                windowsPreviewDlg.hide();
+                                windowsPreviewDlg.hide(3);
                             } else {
                                 preparePreviewWindow(false);
-                                windowsPreviewDlg.show();
+                                windowsPreviewDlg.show(mainItemContainer);
                             }
                         }
                     } else {
@@ -875,7 +874,7 @@ MouseArea{
     function slotShowPreviewForTasks(group) {
         if (group === mainItemContainer) {
             preparePreviewWindow(true);
-            windowsPreviewDlg.show();
+            windowsPreviewDlg.show(mainItemContainer);
         }
     }
 
@@ -1036,11 +1035,10 @@ MouseArea{
             repeat: false
 
             onTriggered: {
-                if(mainItemContainer.containsMouse && toolTipDelegate.parentTask !== mainItemContainer){
+                if(mainItemContainer.containsMouse && windowsPreviewDlg.activeItem !== mainItemContainer){
                     //console.log("Hovered Timer....");
                     mainItemContainer.preparePreviewWindow(false);
-                    windowsPreviewDlg.show();
-                    //windowsPreviewDlg.visible = true;
+                    windowsPreviewDlg.show(mainItemContainer);
                 }
 
                 hoveredTimer.destroy();
diff --git a/plasmoid/contents/ui/ToolTipDelegate2.qml b/plasmoid/contents/ui/ToolTipDelegate2.qml
index 104bb90f0..cb4b79acc 100644
--- a/plasmoid/contents/ui/ToolTipDelegate2.qml
+++ b/plasmoid/contents/ui/ToolTipDelegate2.qml
@@ -36,6 +36,7 @@ import org.kde.taskmanager 0.1 as TaskManager
 
 
 PlasmaExtras.ScrollArea {
+    id: mainToolTip
     property Item parentTask
     property int parentIndex
 
@@ -52,6 +53,8 @@ PlasmaExtras.ScrollArea {
     property bool isLauncher
     property bool isMinimizedParent
 
+    property bool containsMouse: false
+
     // Needed for generateSubtext()
     property string displayParent
     property string genericName
@@ -121,15 +124,15 @@ PlasmaExtras.ScrollArea {
             }
 
             function checkMouseInside(){
-                var isInside = containsMouse || childrenContainMouse();
+                var isInside = containsMouse || childrenContainMouse() || parentTask.containsMouse;
                 if (isInside){
                     root.disableRestoreZoom = true;
+                    mainToolTip.containsMouse = true;
                     checkListHovered.stop();
-                    toolTipDelegate.currentItem = parentIndex;
                 }
                 else{
                     root.disableRestoreZoom = false;
-                    toolTipDelegate.currentItem = -1;
+                    mainToolTip.containsMouse = false;
                     checkListHovered.restart();
                 }
             }
diff --git a/plasmoid/contents/ui/main.qml b/plasmoid/contents/ui/main.qml
index 0f5300228..74b4c6a23 100644
--- a/plasmoid/contents/ui/main.qml
+++ b/plasmoid/contents/ui/main.qml
@@ -194,8 +194,6 @@ Item {
     ToolTipDelegate2 {
         id: toolTipDelegate
         visible: false
-
-        property int currentItem: -1
     }
 
     ////BEGIN interfaces
@@ -215,57 +213,55 @@ Item {
 
         property Item activeItem: null
 
-        function hide(){
+        function hide(debug){
+            //console.log("on hide event called: "+debug);
+
             if (latteDock) {
                 //it is used to unblock dock hiding
                 root.signalDraggingState(false);
             }
-            //console.log("on hide event called...");
-            visible = false;
-            activeItem = null;
 
-            initializePreviewComponent.createObject(windowsPreviewDlg);
+            windowsPreviewDlg.activeItem = null;
+            toolTipDelegate.parentTask = null;
+            toolTipDelegate.parentIndex = -1;
+            toolTipDelegate.windows = [];
+            toolTipDelegate.isGroup = false;
+            visible = false;
         }
 
-        function show(){
+        function show(taskItem){
             //console.log("preview show called...");
-            if (activeItem !== toolTipDelegate.parentTask) {
-                //used to initialize windows previews buffers
-                visible = false;
-
-                var tasks = icList.contentItem.children;
+            if (!activeItem || (activeItem !== taskItem)) {
+                //console.log("preview show called: accepted...");
 
-                for(var i=0; i<tasks.length; ++i){
-                    var task = tasks[i];
-
-                    if(task && task.isActive){
-                        activeItem = task;
-                        break;
-                    }
-                }
+                //used to initialize windows previews buffers from task to task
+                visible = false;
+                activeItem = taskItem;
+                toolTipDelegate.parentTask = taskItem;
 
                 if (latteDock) {
                     //it is used to block dock hiding
                     root.signalDraggingState(true);
                 }
 
-                activeItem = toolTipDelegate.parentTask;
-                visible = true;
+                //small delay to show in order to not mess up with the buffers clearing
+                //from previous visible:false
+                initializePreviewComponent.createObject(windowsPreviewDlg);
             }
         }
     }
 
-    //A Timer to delay the initialization of the active item in order
-    //to not break then active item animation
+    //A Timer to delay to show in order to not mess up with the buffers clearing
+    //from previous visible:false
     Component {
         id: initializePreviewComponent
         Timer {
             id: initializePreviewTimer
-            interval: 300
+            interval: 100
             repeat: false
 
             onTriggered: {
-                windowsPreviewDlg.activeItem = null;
+                windowsPreviewDlg.visible = true;
                 initializePreviewTimer.destroy();
             }
 
@@ -905,7 +901,7 @@ Item {
 
         var tasks = icList.contentItem.children;
 
-        if(toolTipDelegate.currentItem !== -1)
+        if(toolTipDelegate.parentIndex !== -1)
             return true;
 
         for(var i=0; i<tasks.length; ++i){
@@ -927,8 +923,9 @@ Item {
 
         var result = root.outsideContainsMouse();
 
-        if ((!result || toolTipDelegate.parentIndex !== icList.hoveredIndex) && windowSystem.compositingActive) {
-            windowsPreviewDlg.hide();
+        if ((!result || (!toolTipDelegate.parentTask.containsMouse && !toolTipDelegate.containsMouse) ) && windowSystem.compositingActive) {
+            windowsPreviewDlg.hide(4);
+            return false;
         }
 
         if (result)