Kirigami2

PrivateActionToolButton.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 QtQml
9import QtQuick.Layouts
10import QtQuick.Controls as QQC2
11import QtQuick.Templates as T
12
13import org.kde.kirigami as Kirigami
14
15QQC2.ToolButton {
16 id: control
17
18 signal menuAboutToShow()
19
20 hoverEnabled: true
21
22 display: QQC2.ToolButton.TextBesideIcon
23
24 property bool showMenuArrow: !Kirigami.DisplayHint.displayHintSet(action, Kirigami.DisplayHint.HideChildIndicator)
25
26 property list<T.Action> menuActions: {
27 if (action instanceof Kirigami.Action) {
28 return action.children;
29 }
30 return []
31 }
32
33 property Component menuComponent: ActionsMenu {
34 submenuComponent: ActionsMenu { }
35 }
36
37 property QtObject menu: null
38
39 // We create the menu instance only when there are any actual menu items.
40 // This also happens in the background, avoiding slowdowns due to menu item
41 // creation on the main thread.
42 onMenuActionsChanged: {
43 if (menuComponent && menuActions.length > 0) {
44 if (!menu) {
45 const setupIncubatedMenu = incubatedMenu => {
46 menu = incubatedMenu
47 // Important: We handle the press on parent in the parent, so ignore it here.
48 menu.closePolicy = QQC2.Popup.CloseOnEscape | QQC2.Popup.CloseOnPressOutsideParent
49 menu.closed.connect(() => control.checked = false)
50 menu.actions = control.menuActions
51 }
52 const incubator = menuComponent.incubateObject(control, { actions: menuActions })
53 if (incubator.status !== Component.Ready) {
54 incubator.onStatusChanged = status => {
55 if (status === Component.Ready) {
56 setupIncubatedMenu(incubator.object)
57 }
58 }
59 } else {
60 setupIncubatedMenu(incubator.object);
61 }
62 } else {
63 menu.actions = menuActions
64 }
65 }
66 }
67
68 visible: action instanceof Kirigami.Action ? action.visible : true
69
70 // Workaround for QTBUG-85941
71 Binding {
72 target: control
73 property: "checkable"
74 value: (control.action?.checkable ?? false) || (control.menuActions.length > 0)
75 restoreMode: Binding.RestoreBinding
76 }
77
78 onToggled: {
79 if (menuActions.length > 0 && menu) {
80 if (checked) {
81 control.menuAboutToShow();
82 menu.popup(control, 0, control.height)
83 } else {
84 menu.dismiss()
85 }
86 }
87 }
88
89 QQC2.ToolTip {
90 visible: control.hovered && text.length > 0 && !(control.menu && control.menu.visible) && !control.pressed
91 text: {
92 const a = control.action;
93 if (a) {
94 if (a.tooltip) {
95 return a.tooltip;
96 } else if (control.display === QQC2.Button.IconOnly) {
97 return a.text;
98 }
99 }
100 return "";
101 }
102 }
103
104 // This will set showMenuArrow when using qqc2-desktop-style.
105 Accessible.role: (control.showMenuArrow && control.menuActions.length > 0) ? Accessible.ButtonMenu : Accessible.Button
106 Accessible.ignored: !visible
107}
Q_SCRIPTABLE CaptureState status()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:13:10 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.