Libplasma

ScrollBar.qml
1/*
2 SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Templates as T
9import org.kde.ksvg as KSvg
10//NOTE: importing PlasmaCore is necessary in order to make KSvg load the current Plasma Theme
11import org.kde.plasma.core as PlasmaCore
12import org.kde.kirigami as Kirigami
13
14T.ScrollBar {
15 id: controlRoot
16
17 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
18 implicitContentWidth + leftPadding + rightPadding)
19 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
20 implicitContentHeight + topPadding + bottomPadding)
21
22 hoverEnabled: interactive
23
24 visible: (size > 0 && size < 1 && policy === T.ScrollBar.AsNeeded) || policy === T.ScrollBar.AlwaysOn
25 minimumSize: horizontal ? height / width : width / height
26
27 // Working around weird default values for `margins` (== `fixedMargins`) and `inset` (== -1)
28 // TODO KF6: Use 0 as the default value for inset and margins
29 leftPadding: scrollbarSvg.hasElement(`${handle.usedPrefix}-hint-left-inset`) ? handle.inset.left : horizontalPadding
30 rightPadding: scrollbarSvg.hasElement(`${handle.usedPrefix}-hint-right-inset`) ? handle.inset.right : horizontalPadding
31 topPadding: scrollbarSvg.hasElement(`${handle.usedPrefix}-hint-top-inset`) ? handle.inset.top : verticalPadding
32 bottomPadding: scrollbarSvg.hasElement(`${handle.usedPrefix}-hint-bottom-inset`) ? handle.inset.bottom : verticalPadding
33 leftInset: scrollbarSvg.hasElement(`${bgFrame.usedPrefix}-hint-left-inset`) ? bgFrame.inset.left : 0
34 rightInset: scrollbarSvg.hasElement(`${bgFrame.usedPrefix}-hint-right-inset`) ? bgFrame.inset.right : 0
35 topInset: scrollbarSvg.hasElement(`${bgFrame.usedPrefix}-hint-top-inset`) ? bgFrame.inset.top : 0
36 bottomInset: scrollbarSvg.hasElement(`${bgFrame.usedPrefix}-hint-bottom-inset`) ? bgFrame.inset.bottom : 0
37
38 Rectangle {
39 id: separator
40 anchors.left: parent.left
41 width: controlRoot.horizontal ? parent.width : undefined
42 height: controlRoot.vertical ? parent.height : undefined
43 // I'm wary of adding things that could be considered official features
44 // of the theming system willy-nilly, so this hint is marked private.
45 // Technically, there's nothing stopping theme authors from using this
46 // anyway, but I don't want to have to support it long term until we're
47 // sure we want this.
48 visible: scrollbarSvg.hasElement("private-hint-show-separator")
49 && controlRoot.interactive
50 && (controlRoot.mirrored ? controlRoot.rightInset > 0 : controlRoot.leftInset > 0)
51 implicitWidth: 1
52 implicitHeight: implicitWidth
53 color: Kirigami.Theme.textColor
54 opacity: 0.1
55 }
56
57 background: KSvg.FrameSvgItem {
58 id: bgFrame
59 implicitWidth: Math.max(scrollbarSvg.elementSize("hint-scrollbar-size").width, fixedMargins.left + fixedMargins.right)
60 implicitHeight: Math.max(scrollbarSvg.elementSize("hint-scrollbar-size").height, fixedMargins.top + fixedMargins.bottom)
61 imagePath:"widgets/scrollbar"
62 prefix: controlRoot.horizontal ? "background-horizontal" : "background-vertical"
63 opacity: controlRoot.hovered && controlRoot.interactive
64 visible: opacity > 0
65 Behavior on opacity {
66 enabled: Kirigami.Units.longDuration > 0
67 NumberAnimation {
68 duration: Kirigami.Units.longDuration
69 easing.type: Easing.OutCubic
70 }
71 }
72
73 TapHandler {
74 id: tapHandler
75 acceptedButtons: Qt.MiddleButton
76 acceptedDevices: PointerDevice.Stylus
77 gesturePolicy: TapHandler.ReleaseWithinBounds // Exclusive Grab
78 grabPermissions: PointerHandler.ApprovesTakeOverByAnything // But not that exclusive in case any pointer handler outside wants the exclusive grab
79 target: null
80 }
81
82 Connections { // Whenever the position changes, tapHandler.pressed only needs checking once using Connections.enabled
83 enabled: tapHandler.pressed
84 target: tapHandler
85 function onPointChanged() {
86 controlRoot.position = Math.min(1 - controlRoot.size, Math.max(0,
87 (controlRoot.horizontal
88 ? tapHandler.point.position.x / bgFrame.width
89 : tapHandler.point.position.y / bgFrame.height
90 ) - controlRoot.size / 2
91 ));
92 }
93 }
94 }
95
96 contentItem: KSvg.FrameSvgItem {
97 id: handle
98 imagePath:"widgets/scrollbar"
99 implicitWidth: Math.max(scrollbarSvg.elementSize("hint-scrollbar-size").width, fixedMargins.left + fixedMargins.right)
100 implicitHeight: Math.max(scrollbarSvg.elementSize("hint-scrollbar-size").height, fixedMargins.top + fixedMargins.bottom)
101 prefix: controlRoot.interactive && (controlRoot.pressed || controlRoot.hovered) && controlRoot.enabled ? "mouseover-slider" : "slider"
102 opacity: enabled ? 1 : 0.5
103 }
104
105 KSvg.Svg {
106 id: scrollbarSvg
107 imagePath: "widgets/scrollbar"
108 //TODO: support arrows?
109 property bool arrowPresent: scrollbarSvg.hasElement("arrow-up")
110 //new theme may be different
111 onRepaintNeeded: arrowPresent = scrollbarSvg.hasElement("arrow-up")
112 }
113}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.