-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.qml
204 lines (181 loc) · 5.71 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
/**
* MixedMenu - Simple library for QML allowing the mixed use of Native Menus and QtQuick ones.
* Copyright (C) 2021 Ad5001 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.12
import QtQuick.Controls 2.12 as Controls
import Qt.labs.platform 1.1 as Native
/*!
\qmltype Menu
\inqmlmodule eu.ad5001.MixedMenu
\brief Platform specific Menu.
Either loading native menu or Qt's one.
*/
Item {
id: root
signal aboutToShow()
signal aboutToHide()
property bool useNative: Qt.platform.os == "osx"
property var trueItem: useNative ? nativeMenu : controlsMenu
property bool enabled: true
property string title: ""
property int count: 0
property font font: root.trueItem.font
Native.Menu {
id: nativeMenu
enabled: root.enabled
title: root.title
onAboutToShow: root.aboutToShow()
onAboutToHide: root.aboutToHide()
}
Controls.Menu {
id: controlsMenu
enabled: root.enabled
title: root.title
onAboutToShow: root.aboutToShow()
onAboutToHide: root.aboutToHide()
}
Component.onCompleted: { // Adding items to the menu
for(var i = 0; i < root.children.length; i++) {
var item = root.children[i]
if(item instanceof MenuSeparator || item instanceof MenuItem) {
addItem(item)
} else if(item instanceof Action) {
addAction(item)
} else if(item instanceof Menu) {
addMenu(item)
}
}
}
/*!
\qmlmethod void Menu::addAction(Action action)
Adds an \a action to end of the menu.
*/
function addAction(action){
if(root.useNative){
nativeMenu.addItem(action.trueItem) // Is a MenuItem, Action being unavailable for Qt.labs
} else {
controlsMenu.addAction(action.trueItem)
}
root.count++
}
/*!
\qmlmethod void Menu::addItem(MenuItem item)
Adds an \a item to end of the menu.
*/
function addItem(item){
root.trueItem.addItem(item.trueItem)
root.count++
}
/*!
\qmlmethod void Menu::addMenu(Menu submenu)
Adds a \a submenu to end of the menu.
*/
function addMenu(submenu){
root.trueItem.addMenu(submenu.trueItem)
root.count++
}
/*!
\qmlmethod void Menu::insertAction(index, Action action)
Inserts an \a action at the specified \a index in the menu.
*/
function insertAction(index, action){
if(root.useNative){
nativeMenu.insertItem(index, action.trueItem) // Is a MenuItem, Action being unavailable for Qt.labs
} else {
controlsMenu.insertAction(index, action.trueItem)
}
root.count++
}
/*!
\qmlmethod void Menu::insertItem(index, MenuItem item)
Inserts an \a item at the specified \a index in the menu.
*/
function insertItem(index, item){
root.trueItem.insertItem(index, item.trueItem)
root.count++
}
/*!
\qmlmethod void Menu::insertMenu(index, Menu submenu)
Inserts a \a submenu at the specified \a index in the menu.
*/
function insertMenu(index, submenu){
root.trueItem.insertMenu(index, submenu.trueItem)
root.count++
}
/*!
\qmlmethod void Menu::removeAction(Action action)
Removes an \a action to from the menu.
*/
function removeAction(action){
if(root.useNative){
nativeMenu.removeItem(action.trueItem) // Is a MenuItem, Action being unavailable for Qt.labs
} else {
controlsMenu.removeAction(action.trueItem)
}
root.count--
}
/*!
\qmlmethod void Menu::removeMenu(MenuItem item)
Removes an \a item from the menu.
*/
function removeItem(item){
root.trueItem.removeItem(submenu.item)
root.count--
}
/*!
\qmlmethod void Menu::removeMenu(Menu submenu)
Removes a \a submenu from the menu.
*/
function removeMenu(submenu){
root.trueItem.removeMenu(submenu.trueItem)
root.count--
}
/*!
\qmlmethod void Menu::clear()
Removes all items from the menu.
*/
function clear(){
root.trueItem.clear()
root.count = 0
}
/*!
\qmlmethod void Menu::close()
Closes the menu.
*/
function close(){
root.trueItem.close()
}
/*!
\qmlmethod void Menu::open()
Opens the menu at the mouse's position
*/
function open(){
if(root.useNative){
nativeMenu.open()
} else {
controlsMenu.popup()
}
}
/*!
\qmlmethod void Menu::addShortcut(keysequence sequence, var trigger)
Forwarding to MenuBar. Little hack to make Action shortcuts work properly, because otherwise, they would not trigger
\sa MenuBar::addShortcut
*/
function addShortcut(sequence, trigger) {
parent.parent.addShortcut(sequence, trigger)
}
}