Kirigami2

controls/templates/AbstractApplicationHeader.qml
1/*
2 * SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Controls as QQC2
10import org.kde.kirigami as Kirigami
11
12/**
13 * @brief An item that can be used as a title for the application.
14 *
15 * Scrolling the main page will make it taller or shorter (through the point of going away)
16 * It's a behavior similar to the typical mobile web browser addressbar
17 * the minimum, preferred and maximum heights of the item can be controlled with
18 * * minimumHeight: default is 0, i.e. hidden
19 * * preferredHeight: default is Units.gridUnit * 1.6
20 * * maximumHeight: default is Units.gridUnit * 3
21 *
22 * To achieve a titlebar that stays completely fixed just set the 3 sizes as the same
23 *
24 * @inherit QtQuick.Item
25 */
26Item {
27 id: root
28 z: 90
29 property int minimumHeight: 0
30 // Use an inline arrow function, referring to an external normal function makes QV4 crash, see https://bugreports.qt.io/browse/QTBUG-119395
31 property int preferredHeight: mainItem.children.reduce((accumulator, item) => {
32 return Math.max(accumulator, item.implicitHeight);
33 }, 0) + topPadding + bottomPadding
34 property int maximumHeight: Kirigami.Units.gridUnit * 3
35
36 property int position: QQC2.ToolBar.Header
37
38 property Kirigami.PageRow pageRow: __appWindow?.pageStack ?? null
39 property Kirigami.Page page: pageRow?.currentItem as Kirigami.Page ?? null
40
41 default property alias contentItem: mainItem.data
42 readonly property int paintedHeight: headerItem.y + headerItem.height - 1
43
44 property int leftPadding: 0
45 property int topPadding: 0
46 property int rightPadding: 0
47 property int bottomPadding: 0
48 property bool separatorVisible: true
49
50 /**
51 * This property specifies whether the header should be pushed back when
52 * scrolling using the touch screen.
53 */
54 property bool hideWhenTouchScrolling: root.pageRow?.globalToolBar.hideWhenTouchScrolling ?? false
55
56 LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
57 LayoutMirroring.childrenInherit: true
58
59 Kirigami.Theme.inherit: true
60
61 // FIXME: remove
62 property QtObject __appWindow: typeof applicationWindow !== "undefined" ? applicationWindow() : null
63 implicitHeight: preferredHeight
64 height: Layout.preferredHeight
65
66 /**
67 * @brief This property holds the background item.
68 * @note the background will be automatically sized to fill the whole control
69 */
70 property Item background
71
72 onBackgroundChanged: {
73 background.z = -1;
74 background.parent = headerItem;
75 background.anchors.fill = headerItem;
76 }
77
78 Component.onCompleted: AppHeaderSizeGroup.items.push(this)
79
80 onMinimumHeightChanged: implicitHeight = preferredHeight;
81 onPreferredHeightChanged: implicitHeight = preferredHeight;
82
83 opacity: height > 0 ? 1 : 0
84
85 NumberAnimation {
86 id: heightAnim
87 target: root
88 property: "implicitHeight"
89 duration: Kirigami.Units.longDuration
90 easing.type: Easing.InOutQuad
91 }
92
93 Connections {
94 target: root.__appWindow
95
96 function onControlsVisibleChanged() {
97 heightAnim.from = root.implicitHeight;
98 heightAnim.to = root.__appWindow.controlsVisible ? root.preferredHeight : 0;
99 heightAnim.restart();
100 }
101 }
102
103 Connections {
104 target: root.page?.Kirigami.ColumnView ?? null
105
106 function onScrollIntention(event) {
107 headerItem.scrollIntentHandler(event);
108 }
109 }
110
111 Item {
112 id: headerItem
113 anchors {
114 left: parent.left
115 right: parent.right
116 bottom: !Kirigami.Settings.isMobile || root.position === QQC2.ToolBar.Header ? parent.bottom : undefined
117 top: !Kirigami.Settings.isMobile || root.position === QQC2.ToolBar.Footer ? parent.top : undefined
118 }
119
120 height: Math.max(root.height, root.minimumHeight > 0 ? root.minimumHeight : root.preferredHeight)
121
122 function scrollIntentHandler(event) {
123 if (!root.hideWhenTouchScrolling) {
124 return
125 }
126
127 if (root.pageRow
128 && root.pageRow.globalToolBar.actualStyle !== Kirigami.ApplicationHeaderStyle.Breadcrumb) {
129 return;
130 }
131 if (!root.page.flickable || (root.page.flickable.atYBeginning && root.page.flickable.atYEnd)) {
132 return;
133 }
134
135 root.implicitHeight = Math.max(0, Math.min(root.preferredHeight, root.implicitHeight + event.delta.y))
136 event.accepted = root.implicitHeight > 0 && root.implicitHeight < root.preferredHeight;
137 slideResetTimer.restart();
138 if ((root.page.flickable instanceof ListView) && root.page.flickable.verticalLayoutDirection === ListView.BottomToTop) {
139 root.page.flickable.contentY -= event.delta.y;
140 }
141 }
142
143 Connections {
144 target: root.page?.globalToolBarItem ?? null
145 enabled: target
146 function onImplicitHeightChanged() {
147 root.implicitHeight = root.page.globalToolBarItem.implicitHeight;
148 }
149 }
150
151 Timer {
152 id: slideResetTimer
153 interval: 500
154 onTriggered: {
155 if ((root.pageRow?.wideMode ?? (root.__appWindow?.wideScreen ?? false)) || !Kirigami.Settings.isMobile) {
156 return;
157 }
158 if (root.height > root.minimumHeight + (root.preferredHeight - root.minimumHeight) / 2) {
159 heightAnim.to = root.preferredHeight;
160 } else {
161 heightAnim.to = root.minimumHeight;
162 }
163 heightAnim.from = root.implicitHeight
164 heightAnim.restart();
165 }
166 }
167
168 Connections {
169 target: pageRow
170 function onCurrentItemChanged() {
171 if (!root.page) {
172 return;
173 }
174
175 heightAnim.from = root.implicitHeight;
176 heightAnim.to = root.preferredHeight;
177
178 heightAnim.restart();
179 }
180 }
181
182 Item {
183 id: mainItem
184 clip: childrenRect.width > width
185
186 onChildrenChanged: {
187 for (const child of children) {
188 child.anchors.verticalCenter = verticalCenter;
189 }
190 }
191
192 anchors {
193 fill: parent
194 topMargin: root.topPadding
195 leftMargin: root.leftPadding
196 rightMargin: root.rightPadding
197 bottomMargin: root.bottomPadding
198 }
199 }
200 }
201}
PageRow implements a row-based navigation model, which can be used with a set of interlinked informat...
Definition PageRow.qml:21
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:51:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.