Libplasma

BasicPlasmoidHeading.qml
1/*
2 SPDX-FileCopyrightText: 2020 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Templates as T
10
11import org.kde.plasma.plasmoid
12import org.kde.plasma.core as PlasmaCore
13import org.kde.plasma.extras as PlasmaExtras
14import org.kde.plasma.components as PlasmaComponents
15import org.kde.kirigami as Kirigami
16
17 /**
18 * A standard basic header for plasmoids which has title, a config button and
19 * a popup menu with all extra plasmoid actions.
20 *
21 * By default, it will be invisible when the plasmoid is in the system tray,
22 * as it provides a replacement header with the same features
23 *
24 * @inherit PlasmoidHeading
25 */
26PlasmoidHeading {
27 /**
28 * extraControls: list<QtObject>
29 *
30 * Any extra control and button that may be inserted in the heading
31 */
32 default property alias extraControls: extraControlsLayout.data
33
34 visible: !(Plasmoid.containmentDisplayHints & PlasmaCore.Types.ContainmentDrawsPlasmoidHeading)
35
36 contentItem: RowLayout {
37 Heading {
38 elide: Text.ElideRight
39 wrapMode: Text.NoWrap
40 Layout.fillWidth: true
41 level: 1
42 text: Plasmoid.title
43 }
44 RowLayout {
45 id: extraControlsLayout
46 visible: children.length > 0
47 Layout.fillHeight: true
48 }
49 PlasmaComponents.ToolButton {
50 id: actionsButton
51 visible: visibleActions > 0
52 checked: configMenu.status !== PlasmaExtras.Menu.Closed
53 property int visibleActions: menuItemFactory.count
54 property QtObject singleAction: visibleActions === 1 ? menuItemFactory.object.action : null
55 icon.name: "open-menu-symbolic"
56 checkable: visibleActions > 1
57 contentItem.opacity: visibleActions > 1
58 // NOTE: it needs an Icon because QtQuickControls2 buttons cannot load QIcons as their icon
59 Kirigami.Icon {
60 parent: actionsButton
61 anchors.centerIn: parent
62 active: actionsButton.hovered
63 implicitWidth: Kirigami.Units.iconSizes.smallMedium
64 implicitHeight: implicitWidth
65 source: actionsButton.singleAction !== null ? actionsButton.singleAction.icon : ""
66 visible: actionsButton.singleAction
67 }
68 onToggled: {
69 if (checked) {
70 configMenu.openRelative();
71 } else {
72 configMenu.close();
73 }
74 }
75 onClicked: {
76 if (singleAction) {
77 singleAction.trigger();
78 }
79 }
80 PlasmaComponents.ToolTip {
81 text: actionsButton.singleAction ? actionsButton.singleAction.text : i18nd("libplasma6", "More actions")
82 }
83 PlasmaExtras.Menu {
84 id: configMenu
85 visualParent: actionsButton
86 placement: PlasmaExtras.Menu.BottomPosedLeftAlignedPopup
87 }
88
89 Instantiator {
90 id: menuItemFactory
91 model: {
92 configMenu.clearMenuItems();
93 const configureAction = Plasmoid.internalAction("configure");
94 const actions = Plasmoid.contextualActions
95 .filter(action => action !== configureAction);
96 return actions;
97 }
98 delegate: PlasmaExtras.MenuItem {
99 required property QtObject modelData // type: QAction
100 action: modelData
101 }
102 onObjectAdded: {
103 configMenu.addMenuItem(object);
104 }
105 }
106 }
107 PlasmaComponents.ToolButton {
108 id: configureButton
109
110 property PlasmaCore.Action internalAction
111
112 function fetchInternalAction() {
113 internalAction = Plasmoid.internalAction("configure");
114 }
115
116 Connections {
117 target: Plasmoid
118 function onInternalActionsChanged(actions) {
119 configureButton.fetchInternalAction();
120 }
121 }
122
123 Component.onCompleted: fetchInternalAction()
124
125 icon.name: "configure"
126 visible: internalAction !== null
127 text: internalAction?.text ?? ""
128 display: T.AbstractButton.IconOnly
129 PlasmaComponents.ToolTip {
130 text: configureButton.text
131 }
132 onClicked: internalAction?.trigger();
133 }
134 }
135}
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.