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