-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.qml
261 lines (197 loc) · 8.94 KB
/
Menu.qml
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import QtQuick 2.15
FocusScope {
property alias currentIndex: menuRoot.currentIndex
property alias menuListView: menuRoot.listView
Item {
id: menuRoot
property alias currentIndex: menuListView.currentIndex
property alias listView: menuListView
width: parent.width * 0.95
height: parent.height * 0.95
property string testString: "You got it!!"
anchors {
horizontalCenter: parent.horizontalCenter
top: parent.top
}
ListView {
id: menuListView
property int fontSize: 12
property int columns: {
if (menuListView.count <= 5) {
return menuListView.count;
}
else {
// Keep max columns to 5 for top menu
return 5;
}
}
focus: menuRoot.focus
height: parent.height
width: parent.width
// https://stackoverflow.com/questions/47062880/qml-text-size-based-on-container
// https://stackoverflow.com/questions/51162423/qml-binding-loop-detected-when-trying-to-calculate-font-size-automatically
TextMetrics {
id: menuListFont
font.family: "HackRegular"
font.bold: true
font.capitalization: Font.AllUppercase
}
function calculateFontSize(minSize = 8, maxSize = 72) {
// Minimum size
var size = maxSize;
var eWidth = (menuListView.width / menuListView.columns) * 0.8;
var eHeight = menuListView.height;
console.log("The width of e:" + eWidth);
console.log("The height of e: " + eHeight)
model.forEach((item) => {
var fSize;
menuListFont.font.pixelSize = eHeight;
menuListFont.text = item.title;
var brect = menuListFont.boundingRect;
console.log("brect width: " + brect.width);
console.log("brect height: " + brect.height);
if (brect.width > eWidth) {
var k = eWidth / brect.width;
fSize = Math.floor(menuListFont.font.pixelSize * k);
}
else {
fSize = menuListFont.font.pixelSize;
}
if (fSize < size) {
size = fSize;
}
});
return size;
}
model: menuOptions
delegate: menuListDelegate
orientation: ListView.Horizontal
interactive: false
clip: true
currentIndex: root.currentmenuIndex
Keys.onPressed: {
if (api.keys.isNextPage(event)) {
event.accepted = true;
menuListView.incrementCurrentIndex();
}
else if (api.keys.isPrevPage(event)) {
event.accepted = true;
menuListView.decrementCurrentIndex();
}
}
function resizeFont() {
fontSize = utils.calculateFontSizeModel(menuListFont.font,
(menuListView.width / menuListView.columns) * 0.8,
menuListView.height,
model.map((x) => x.title)
);
}
onHeightChanged: resizeFont()
onWidthChanged: resizeFont()
Component.onCompleted: {
resizeFont()
console.log("menu Listview loaded")
console.log("Root Current Index: " + root.currentmenuIndex)
}
onCurrentIndexChanged: {
console.log("menu current index changed")
positionViewAtIndex(currentIndex, ListView.View)
}
}
Component {
id: menuListDelegate
Rectangle {
id: menuTextRect
property alias menuText: menuText
width: ListView.view.width / ListView.view.columns
height: ListView.view.height
color: ListView.isCurrentItem ? themeData.colorTheme[theme].background : themeData.colorTheme[theme].background
Text {
id: menuText
text: modelData.title
font.family: "HackRegular"
font.pixelSize: menuListView.fontSize
font.bold: true
color: themeData.colorTheme[theme].primary
font.capitalization: Font.AllUppercase
anchors {
bottom: parent.bottom
bottomMargin: parent.height * 0.02
horizontalCenter: parent.horizontalCenter
}
}
Rectangle {
width: parent.width
height: parent.height * 0.03
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? false : true
anchors.bottom: parent.bottom
}
Item {
id: menuTextActiveBorder
width: parent.width
height: parent.height
TextMetrics {
id: menuTextMetric
font: menuTextRect.menuText.font
text: menuTextRect.menuText.text
}
Rectangle {
id: menuTextActiveBorderBottomLeft
width: (parent.width - menuTextMetric.width) / 4
height: parent.height * 0.03
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.bottom: parent.bottom
anchors.left: parent.left
}
Rectangle {
id: menuTextActiveBorderLeft
width: parent.height * 0.03
height: menuTextMetric.height / 2
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.left: menuTextActiveBorderBottomLeft.right
anchors.bottom: parent.bottom
}
Rectangle {
id: menuTextActiveBorderTopLeft
height: parent.height * 0.03
width: (parent.width - menuTextMetric.width) / 5
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.left: menuTextActiveBorderLeft.right
anchors.top: menuTextActiveBorderLeft.top
}
Rectangle {
id: menuTextActiveBorderBottomRight
width: (parent.width - menuTextMetric.width) / 4
height: parent.height * 0.03
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.bottom: parent.bottom
anchors.right: parent.right
}
Rectangle {
id: menuTextActiveBorderRight
width: parent.height * 0.03
height: menuTextMetric.height / 2
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.right: menuTextActiveBorderBottomRight.left
anchors.bottom: parent.bottom
}
Rectangle {
id: menuTextActiveBorderTopRight
height: parent.height * 0.03
width: (parent.width - menuTextMetric.width) / 5
color: themeData.colorTheme[theme].primary
visible: menuTextRect.ListView.isCurrentItem ? true : false
anchors.right: menuTextActiveBorderRight.left
anchors.top: menuTextActiveBorderRight.top
}
}
}
}
}
}