aboutsummaryrefslogtreecommitdiff
path: root/ScrollBar.qml
blob: 18f149cf895b6d8eb32e3a747b3ca1797e13ba33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import QtQuick 2.0;

Item {
	id: scrollbar
	width: (handleSize + 2)
	visible: (flickable.visibleArea.heightRatio < 1.0)

	anchors {
		top: flickable.top
		right: flickable.right
		bottom: flickable.bottom
	}

	property Flickable flickable
	property int       handleSize

	Item {
		id: bar

		anchors.fill: parent

		Rectangle {
			anchors.fill: parent
			color: "black"
			opacity: 0.5
		}

		MouseArea {
			id: control
			anchors.fill: parent

			drag {
				target: handle
				minimumY: 0
				maximumY: (bar.height - handle.height)
				axis: Drag.YAxis
			}

			onClicked: {
				flickable.contentY = (mouse.y / bar.height * (flickable.contentHeight - flickable.height));
			}
		}

		Item {
			id: handle;
			height: Math.max(20, (flickable.visibleArea.heightRatio * bar.height))

			anchors {
				left: parent.left
				right: parent.right
			}

			Rectangle {
				id: backHandle
				anchors {
					fill: parent
					margins: 1
				}

				color: (control.pressed ? "gray" : "white")
			}
		}
	}

	Binding {
		target: handle
		property: "y"
		value: (flickable.contentY * control.drag.maximumY / (flickable.contentHeight - flickable.height))
		when: (!control.drag.active)
	}

	Binding {
		target: flickable
		property: "contentY"
		value: (handle.y * (flickable.contentHeight - flickable.height) / control.drag.maximumY)
		when: (control.drag.active || control.pressed)
	}
}