Debian Codegraph @

app/ui/CalcKeyboard.qml

raw
1 /*
2 * Copyright (C) 2017-2019 UBports
3 * Copyright (C) 2014-2015 Canonical Ltd
4 *
5 * This file is part of Lomiri Calculator App
6 *
7 * Lomiri Calculator App is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
10 *
11 * Lomiri Calculator App is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 import QtQuick 2.4
20 import Lomiri.Components 1.3
21 import QtQuick.Controls.Suru 2.2
22
23 Rectangle {
24 id: virtualKeyboard
25 height: flickableKeyboard.height + units.gu(1)
26 color: Suru.backgroundColor
27
28 property int pressedKey: -1
29 property string pressedKeyText: ""
30
31 default property alias children: keyboardsRow.children
32
33 Flickable {
34 id: flickableKeyboard
35 anchors { left: parent.left; bottom: parent.bottom; right: parent.right }
36 boundsBehavior: Flickable.DragOverBounds
37 flickableDirection: Flickable.HorizontalFlick
38 contentWidth: virtualKeyboard.width * keyboardsRow.children.length
39 contentHeight: keyboardsRow.height
40 height: contentHeight
41 interactive: keyboardsRow.children.length === 1 ? false : true
42
43 property int currentIndex: 0
44
45 onDragEnded: {
46 var index = 0;
47 if (horizontalVelocity > units.gu(50)) {
48 index = Math.min(keyboardsRow.children.length - 1, currentIndex + 1)
49 } else if (horizontalVelocity < -units.gu(50)) {
50 index = Math.max(0, currentIndex - 1)
51 } else {
52 index = Math.round(contentX / (width + keyboardsRow.spacing))
53 index = Math.max(0, index)
54 index = Math.min(keyboardsRow.children.length - 1, index)
55 }
56
57 currentIndex = index;
58 snapAnimation.to = index * (flickableKeyboard.width)
59 snapAnimation.start()
60 textInputField.focus = true
61 }
62
63 LomiriNumberAnimation {
64 id: snapAnimation
65 target: flickableKeyboard
66 to: 0
67 property: "contentX"
68 duration: LomiriAnimation.BriskDuration
69 }
70
71 Row {
72 id: keyboardsRow
73 anchors { left: parent.left; right: parent.right }
74
75 onChildrenChanged: {
76 for (var i = 0; i < children.length; i++) {
77 children[i].width = Qt.binding(function() {
78 return (keyboardsRow.width - spacing * (children.length - 2)) / children.length;
79 }
80 )
81 }
82 }
83 }
84 }
85 }