|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Layouts |
| 3 | +import Quickshell |
| 4 | + |
| 5 | +Scope { |
| 6 | + id: root |
| 7 | + property bool failed; |
| 8 | + property string errorString; |
| 9 | + |
| 10 | + // Connect to the Quickshell global to listen for the reload signals. |
| 11 | + Connections { |
| 12 | + target: Quickshell |
| 13 | + |
| 14 | + function onReloadCompleted() { |
| 15 | + root.failed = false; |
| 16 | + popupLoader.loading = true; |
| 17 | + } |
| 18 | + |
| 19 | + function onReloadFailed(error: string) { |
| 20 | + // Close any existing popup before making a new one. |
| 21 | + popupLoader.active = false; |
| 22 | + |
| 23 | + root.failed = true; |
| 24 | + root.errorString = error; |
| 25 | + popupLoader.loading = true; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Keep the popup in a loader because it isn't needed most of the timeand will take up |
| 30 | + // memory that could be used for something else. |
| 31 | + LazyLoader { |
| 32 | + id: popupLoader |
| 33 | + |
| 34 | + PanelWindow { |
| 35 | + id: popup |
| 36 | + |
| 37 | + anchors { |
| 38 | + top: true |
| 39 | + left: true |
| 40 | + } |
| 41 | + |
| 42 | + margins { |
| 43 | + top: 25 |
| 44 | + left: 25 |
| 45 | + } |
| 46 | + |
| 47 | + width: rect.width |
| 48 | + height: rect.height |
| 49 | + |
| 50 | + // color blending is a bit odd as detailed in the type reference. |
| 51 | + color: "transparent" |
| 52 | + |
| 53 | + Rectangle { |
| 54 | + id: rect |
| 55 | + color: failed ? "#40802020" : "#40009020" |
| 56 | + |
| 57 | + implicitHeight: layout.implicitHeight + 50 |
| 58 | + implicitWidth: layout.implicitWidth + 30 |
| 59 | + |
| 60 | + // Fills the whole area of the rectangle, making any clicks go to it, |
| 61 | + // which dismiss the popup. |
| 62 | + MouseArea { |
| 63 | + id: mouseArea |
| 64 | + anchors.fill: parent |
| 65 | + onClicked: popupLoader.active = false |
| 66 | + |
| 67 | + // makes the mouse area track mouse hovering, so the hide animation |
| 68 | + // can be paused when hovering. |
| 69 | + hoverEnabled: true |
| 70 | + } |
| 71 | + |
| 72 | + ColumnLayout { |
| 73 | + id: layout |
| 74 | + anchors { |
| 75 | + top: parent.top |
| 76 | + topMargin: 20 |
| 77 | + horizontalCenter: parent.horizontalCenter |
| 78 | + } |
| 79 | + |
| 80 | + Text { |
| 81 | + text: root.failed ? "Reload failed." : "Reloaded completed!" |
| 82 | + color: "white" |
| 83 | + } |
| 84 | + |
| 85 | + Text { |
| 86 | + text: root.errorString |
| 87 | + color: "white" |
| 88 | + // When visible is false, it also takes up no space. |
| 89 | + visible: root.errorString != "" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // A progress bar on the bottom of the screen, showing how long until the |
| 94 | + // popup is removed. |
| 95 | + Rectangle { |
| 96 | + id: bar |
| 97 | + color: "#20ffffff" |
| 98 | + anchors.bottom: parent.bottom |
| 99 | + anchors.left: parent.left |
| 100 | + height: 20 |
| 101 | + |
| 102 | + PropertyAnimation { |
| 103 | + id: anim |
| 104 | + target: bar |
| 105 | + property: "width" |
| 106 | + from: rect.width |
| 107 | + to: 0 |
| 108 | + duration: failed ? 10000 : 800 |
| 109 | + onFinished: popupLoader.active = false |
| 110 | + |
| 111 | + // Pause the animation when the mouse is hovering over the popup, |
| 112 | + // so it stays onscreen while reading. This updates reactively |
| 113 | + // when the mouse moves on and off the popup. |
| 114 | + paused: mouseArea.containsMouse |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // We could set `running: true` inside the animation, but the width of the |
| 119 | + // rectangle might not be calculated yet, due to the layout. |
| 120 | + // In the `Component.onCompleted` event handler, all of the component's |
| 121 | + // properties and children have been initialized. |
| 122 | + Component.onCompleted: anim.start() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments