Plasma-framework

BusyIndicator.qml
1/*
2 SPDX-FileCopyrightText: 2014 Kai Uwe Broulik <kde@privat.broulik.de>
3 SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
4 SPDX-FileCopyrightText: 2022 ivan tkachenko <me@ratijas.tk>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9import QtQuick
10import QtQuick.Templates as T
11import org.kde.kirigami as Kirigami
12import org.kde.ksvg as KSvg
13//NOTE: importing PlasmaCore is necessary in order to make KSvg load the current Plasma Theme
14import org.kde.plasma.core as PlasmaCore
15
16T.BusyIndicator {
17 id: control
18
19 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
20 implicitContentWidth + leftPadding + rightPadding)
21 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
22 implicitContentHeight + topPadding + bottomPadding)
23
24 // BusyIndicator doesn't need padding since it has no background.
25 // A Control containing a BusyIndicator can have padding instead
26 // (e.g., a ToolBar, a Page or maybe a widget in a Plasma panel).
27 padding: 0
28
29 hoverEnabled: false
30
31 contentItem: Item {
32 /* Binding on `visible` implicitly takes care of `control.visible`,
33 * `control.running` and `opacity > 0` at once.
34 * Also, don't animate at all if the user has disabled animations,
35 * and don't animate when window is hidden (which somehow does not
36 * affect items' visibility).
37 */
38 readonly property bool animationShouldBeRunning:
39 visible
40 && Window.visibility !== Window.Hidden
41 && Kirigami.Units.longDuration > 1
42
43 /* implicitWidth and implicitHeight won't work unless they come
44 * from a child of the contentItem. No idea why.
45 */
46 implicitWidth: Kirigami.Units.gridUnit * 2
47 implicitHeight: Kirigami.Units.gridUnit * 2
48
49 // We can't bind directly to opacity, as Animator won't update its value immediately.
50 visible: control.running || opacityAnimator.running
51 opacity: control.running ? 1 : 0
52 Behavior on opacity {
53 enabled: Kirigami.Units.shortDuration > 0
54 OpacityAnimator {
55 id: opacityAnimator
56 duration: Kirigami.Units.shortDuration
57 easing.type: Easing.OutCubic
58 }
59 }
60
61 // sync all busy animations so they start at a common place in the rotation
62 onAnimationShouldBeRunningChanged: startOrStopAnimation();
63
64 function startOrStopAnimation() {
65 if (rotationAnimator.running === animationShouldBeRunning) {
66 return;
67 }
68 if (animationShouldBeRunning) {
69 const date = new Date;
70 const ms = date.valueOf();
71 const startAngle = ((ms % rotationAnimator.duration) / rotationAnimator.duration) * 360;
72 rotationAnimator.from = startAngle;
73 rotationAnimator.to = startAngle + 360
74 }
75 rotationAnimator.running = animationShouldBeRunning;
76 }
77
79 /* Do not use `anchors.fill: parent` in here or else
80 * the aspect ratio won't always be 1:1.
81 */
82 anchors.centerIn: parent
83 width: Math.min(parent.width, parent.height)
84 height: width
85
86 imagePath: "widgets/busywidget"
87 elementId: "busywidget"
88
89 RotationAnimator on rotation {
90 id: rotationAnimator
91 from: 0
92 to: 360
93 // Not using a standard duration value because we don't want the
94 // animation to spin faster or slower based on the user's animation
95 // scaling preferences; it doesn't make sense in this context
96 duration: 2000
97 loops: Animation.Infinite
98 // Initially false, will be set as appropriate after
99 // initialization. Can't be bound declaratively due to the
100 // procedural nature of to/from adjustments: order of
101 // assignments is crucial, as animator won't use new to/from
102 // values while running.
103 running: false
104 }
105 }
106
107 Component.onCompleted: startOrStopAnimation();
108 }
109}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.