2 * Copyright 2020 Robin Krens <robin@robinkrens.nl>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2 or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 import QtQuick.Controls 1.2
22 import QtQuick.Layouts 1.0
24 import org.kde.plasma.plasmoid 2.0 as Plasmoid
25 import org.kde.plasma.core 2.0 as PlasmaCore
26 import org.kde.plasma.components 2.0 as PlasmaComponents
28 import "../code/utils.js" as Utils
33 property var snow: [];
34 property int snowCount: Utils.getSnowCount(plasmoid.configuration.userCount);
35 readonly property int screenWidth: Qt.application.screens[0].width;
36 readonly property int screenHeight: Qt.application.screens[0].height;
41 function initializeSnow() {
43 if (snowFalling.running)
46 for (var i = 0; i < snowCount; i++) {
48 var component = Qt.createComponent("snowWindow.qml");
50 snow[i] = component.createObject(root);
52 snow[i].x = Math.random() * screenWidth;
53 snow[i].y = Math.random() * screenHeight;
54 snow[i].width = (Math.random() * 100) % snow[i].radius;
55 snow[i].height = snow[i].width;
59 function startSnow() {
61 for (var i = 0; i < snowCount; i++) {
62 snow[i].showMaximized();
64 snowFalling.running = true;
67 /* End snow flake and start a 'random' new one */
68 function endSnowFlake(i) {
73 var component = Qt.createComponent("snowWindow.qml");
74 snow[i] = component.createObject(root);
76 snow[i].x = Math.random() * screenWidth;
78 snow[i].width = (Math.random() * 100) % snow[i].radius;
79 snow[i].height = snow[i].width;
81 snow[i].showMaximized();
85 function destroySnow() {
87 if (!snowFalling.running)
90 snowFalling.running = false;
92 for (var i = 0; i < snowCount; i++) {
98 function updateSnow() {
100 if (userSpeed.currentText == '') { // hack
101 console.log('not initialized yet');
105 /* set config for new snow flakes */
106 plasmoid.configuration.userStyle = userStyle.textAt(userStyle.currentIndex);
107 plasmoid.configuration.userSize = userSize.textAt(userSize.currentIndex);
108 plasmoid.configuration.userSpeed = userSpeed.textAt(userSpeed.currentIndex);
110 if (!snowFalling.running || plasmoid.configuration.userSpeed == userSpeed.currentText) // don't update random speed if same
113 /* update current snow flakes*/
114 for (var i = 0; i < snowCount; i++) {
115 snow[i].fallingSpeed = Utils.getFallingSpeed(userSpeed.currentText);
119 /* Update snow count works a bit differently;
120 * can either kill all (implemented now) and start over;
121 * or slowly add/delete new snow flakes */
122 function updateCount() {
124 var alreadyRunning = snowFalling.running;
127 plasmoid.configuration.userCount = userCount.textAt(userCount.currentIndex);
138 flow: GridLayout.TopToBottom
141 Label { text: "Speed"; color: theme.textColor; }
142 Label { text: "Size"; color: theme.textColor; }
143 Label { text: "Style"; color: theme.textColor; }
144 Label { text: "Count"; color: theme.textColor; }
145 Button { id: letitsnow; text: "Let it snow!"; onClicked: { initializeSnow(); startSnow(); } }
147 ComboBox { id: userSpeed; width: 100; model: ["Slow", "Normal", "Fast"]; currentIndex: 1; onCurrentIndexChanged: updateSnow(); }
148 ComboBox { id: userSize; width: 100; model: ["Tiny", "Small", "Big"]; currentIndex: 2; onCurrentIndexChanged: updateSnow(); }
149 ComboBox { id: userStyle; width: 100; model: ["Classic", "Plain", "Romantic"]; currentIndex: 0; onCurrentIndexChanged: updateSnow(); }
150 ComboBox { id: userCount; width: 100; model: ["Few", "Medium", "Many"]; currentIndex: 1; onCurrentIndexChanged: updateCount(); }
152 Button { width: 100; text: "Stop"; onClicked: destroySnow(); }
160 interval: 50; running: false; repeat: true
164 for (var i = 0; i < snowCount; i++) {
165 if(snow[i].dir == 0) {
171 if (snow[i].disposition++ % snow[i].swirl == 0) {
172 snow[i].dir = !snow[i].dir;
175 if(snow[i].x > screenWidth) {
177 } else if (snow[i].x < 0) {
181 snow[i].y += snow[i].fallingSpeed;
183 if (snow[i].y > screenHeight) {
187 if(snow[i].rotationDirection == 0) {
188 snow[i].snowFlakeRotation += snow[i].rotationSpeed;
190 snow[i].snowFlakeRotation -= snow[i].rotationSpeed;