Kirigami2

PrivateActionToolButton.qml
1 /*
2  * SPDX-FileCopyrightText: 2016 Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 import QtQuick 2.15
8 import QtQml 2.15
9 import QtQuick.Layouts 1.15
10 import QtQuick.Controls 2.15 as Controls
11 
12 import org.kde.kirigami 2.20 as Kirigami
13 
14 Controls.ToolButton {
15  id: control
16 
17  signal menuAboutToShow()
18 
19  Kirigami.Icon {
20  id: kirigamiIcon
21  visible: false
22  source: control.icon.name
23  }
24 
25  hoverEnabled: true
26 
27  display: Controls.ToolButton.TextBesideIcon
28 
29  property bool showMenuArrow: !Kirigami.DisplayHint.displayHintSet(action, Kirigami.DisplayHint.HideChildIndicator)
30 
31  property var menuActions: {
32  if (action && action.hasOwnProperty("children")) {
33  return Array.prototype.slice.call(action.children)
34  }
35  return []
36  }
37 
38  property Component menuComponent: ActionsMenu {
39  submenuComponent: ActionsMenu { }
40  }
41 
42  property QtObject menu: null
43 
44  // We create the menu instance only when there are any actual menu items.
45  // This also happens in the background, avoiding slowdowns due to menu item
46  // creation on the main thread.
47  onMenuActionsChanged: {
48  if (menuComponent && menuActions.length > 0) {
49  if (!menu) {
50  const setupIncubatedMenu = incubatedMenu => {
51  menu = incubatedMenu
52  // Important: We handle the press on parent in the parent, so ignore it here.
53  menu.closePolicy = Controls.Popup.CloseOnEscape | Controls.Popup.CloseOnPressOutsideParent
54  menu.closed.connect(() => control.checked = false)
55  menu.actions = control.menuActions
56  }
57  const incubator = menuComponent.incubateObject(control, {"actions": menuActions})
58  if (incubator.status !== Component.Ready) {
59  incubator.onStatusChanged = status => {
60  if (status === Component.Ready) {
61  setupIncubatedMenu(incubator.object)
62  }
63  }
64  } else {
65  setupIncubatedMenu(incubator.object);
66  }
67  } else {
68  menu.actions = menuActions
69  }
70  }
71  }
72 
73  visible: (action && action.hasOwnProperty("visible")) ? action.visible : true
74 
75  // Workaround for QTBUG-85941
76  Binding {
77  target: control
78  property: "checkable"
79  value: (control.action && control.action.checkable) || (control.menuActions && control.menuActions.length > 0)
80  restoreMode: Binding.RestoreBinding
81  }
82 
83  onToggled: {
84  if (menuActions.length > 0 && menu) {
85  if (checked) {
86  control.menuAboutToShow();
87  menu.popup(control, 0, control.height)
88  } else {
89  menu.dismiss()
90  }
91  }
92  }
93 
94  Controls.ToolTip {
95  visible: control.hovered && text.length > 0 && !(control.menu && control.menu.visible) && !control.pressed
96  text: {
97  const a = control.action;
98  if (a) {
99  if (a.tooltip) {
100  return a.tooltip;
101  } else if (control.display === Controls.Button.IconOnly) {
102  return a.text;
103  }
104  }
105  return "";
106  }
107  }
108 
109  // This will set showMenuArrow when using qqc2-desktop-style.
110  Accessible.role: (control.showMenuArrow && control.menuActions.length > 0) ? Accessible.ButtonMenu : Accessible.Button
111  Accessible.ignored: !visible
112 }
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_SCRIPTABLE CaptureState status()
Class for rendering an icon in UI.
Definition: icon.h:26
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.