on 6d80eaa help sign in
unity8/qml/Components/PageHeader.qml
368 lines 13 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17import QtQuick 2.2
18import Ubuntu.Components 1.1
19import Ubuntu.Components.Themes.Ambiance 1.1
20import Ubuntu.Components.Popups 1.0
21import Ubuntu.Components.ListItems 1.0
22import "SearchHistoryModel"
24Item {
25 id: root
26 objectName: "pageHeader"
27 implicitHeight: headerContainer.height + bottomContainer.height + (showSignatureLine ? units.gu(2) : 0)
29 property bool showBackButton: false
30 property string title
32 property bool searchEntryEnabled: false
33 property ListModel searchHistory: SearchHistoryModel
34 property alias searchQuery: searchTextField.text
35 property alias searchHint: searchTextField.placeholderText
36 property alias showSignatureLine: bottomBorder.visible
38 property alias bottomItem: bottomContainer.children
39 property int paginationCount: 0
40 property int paginationIndex: -1
42 // TODO We should use foreground for the icons
43 // of the toolbar but unfortunately Action does not have
44 // the keyColor property as Icon does :-/
45 property var scopeStyle: null
47 signal backClicked()
49 onScopeStyleChanged: refreshLogo()
50 onSearchQueryChanged: {
51 // Make sure we are at the search page if the search query changes behind our feet
52 if (searchQuery) {
53 headerContainer.showSearch = true;
54 }
55 }
57 function triggerSearch() {
58 if (searchEntryEnabled) {
59 headerContainer.showSearch = true;
60 searchTextField.forceActiveFocus();
61 }
62 }
64 function closePopup() {
65 if (headerContainer.popover != null) {
66 PopupUtils.close(headerContainer.popover);
67 }
68 }
70 function resetSearch(keepFocus) {
71 if (searchHistory) {
72 searchHistory.addQuery(searchTextField.text);
73 }
74 if (!keepFocus) {
75 unfocus();
76 }
77 searchTextField.text = "";
78 closePopup();
79 }
81 function unfocus() {
82 searchTextField.focus = false;
83 if (!searchTextField.text) {
84 headerContainer.showSearch = false;
85 }
86 }
88 function openSearchHistory() {
89 if (openSearchAnimation.running) {
90 openSearchAnimation.openSearchHistory = true;
91 } else if (root.searchHistory.count > 0 && headerContainer.popover == null) {
92 headerContainer.popover = PopupUtils.open(popoverComponent, searchTextField,
93 {
94 "contentWidth": searchTextField.width,
95 "edgeMargins": units.gu(1)
96 }
97 );
98 }
99 }
101 function refreshLogo() {
102 if (scopeStyle ? scopeStyle.headerLogo != "" : false) {
103 header.contents = imageComponent.createObject();
104 } else if (header.contents) {
105 header.contents.destroy();
106 header.contents = null;
107 }
108 }
110 Connections {
111 target: root.scopeStyle
112 onHeaderLogoChanged: root.refreshLogo()
113 }
115 InverseMouseArea {
116 anchors { fill: parent; margins: units.gu(1); bottomMargin: units.gu(3) + bottomContainer.height }
117 visible: headerContainer.showSearch
118 onPressed: {
119 closePopup();
120 if (!searchTextField.text) {
121 headerContainer.showSearch = false;
122 }
123 searchTextField.focus = false;
124 mouse.accepted = false;
125 }
126 }
128 Flickable {
129 id: headerContainer
130 objectName: "headerContainer"
131 clip: contentY < height
132 anchors { left: parent.left; top: parent.top; right: parent.right }
133 height: units.gu(6.5)
134 contentHeight: headersColumn.height
135 interactive: false
136 contentY: showSearch ? 0 : height
138 property bool showSearch: false
139 property var popover: null
141 Background {
142 objectName: "headerBackground"
143 style: scopeStyle.headerBackground
144 }
146 Behavior on contentY {
147 UbuntuNumberAnimation {
148 id: openSearchAnimation
149 property bool openSearchHistory: false
151 onRunningChanged: {
152 if (!running && openSearchAnimation.openSearchHistory) {
153 openSearchAnimation.openSearchHistory = false;
154 root.openSearchHistory();
155 }
156 }
157 }
158 }
160 Column {
161 id: headersColumn
162 anchors { left: parent.left; right: parent.right }
164 PageHeadStyle {
165 id: searchHeader
166 anchors { left: parent.left; right: parent.right }
167 height: headerContainer.height
168 contentHeight: height
169 opacity: headerContainer.clip || headerContainer.showSearch ? 1 : 0 // setting visible false cause column to relayout
170 separatorSource: ""
171 // Required to keep PageHeadStyle noise down as it expects the Page's properties around.
172 property var styledItem: searchHeader
173 property string title
174 property var config: PageHeadConfiguration {
175 foregroundColor: root.scopeStyle ? root.scopeStyle.headerForeground : "grey"
176 backAction: Action {
177 iconName: "back"
178 onTriggered: {
179 root.resetSearch();
180 headerContainer.showSearch = false;
181 }
182 }
183 }
184 property var contents: TextField {
185 id: searchTextField
186 objectName: "searchTextField"
187 inputMethodHints: Qt.ImhNoPredictiveText
188 hasClearButton: false
189 anchors {
190 fill: parent
191 leftMargin: units.gu(1)
192 topMargin: units.gu(1)
193 bottomMargin: units.gu(1)
194 rightMargin: root.width > units.gu(60) ? root.width - units.gu(40) : units.gu(1)
195 }
197 secondaryItem: AbstractButton {
198 height: searchTextField.height
199 width: height
200 enabled: searchTextField.text.length > 0
202 Image {
203 objectName: "clearIcon"
204 anchors.fill: parent
205 anchors.margins: units.gu(.75)
206 source: "image://theme/clear"
207 opacity: searchTextField.text.length > 0
208 visible: opacity > 0
209 Behavior on opacity {
210 UbuntuNumberAnimation { duration: UbuntuAnimation.FastDuration }
211 }
212 }
214 onClicked: {
215 root.resetSearch(true);
216 root.openSearchHistory();
217 }
218 }
220 onActiveFocusChanged: {
221 if (activeFocus) {
222 root.openSearchHistory();
223 }
224 }
226 onTextChanged: {
227 if (text != "") {
228 closePopup();
229 }
230 }
231 }
232 }
234 PageHeadStyle {
235 id: header
236 objectName: "innerPageHeader"
237 anchors { left: parent.left; right: parent.right }
238 height: headerContainer.height
239 contentHeight: height
240 opacity: headerContainer.clip || !headerContainer.showSearch ? 1 : 0 // setting visible false cause column to relayout
241 separatorSource: ""
242 property var styledItem: header
243 property string title: root.title
244 property var config: PageHeadConfiguration {
245 foregroundColor: root.scopeStyle ? root.scopeStyle.headerForeground : "grey"
246 backAction: Action {
247 iconName: "back"
248 visible: root.showBackButton
249 onTriggered: {
250 root.backClicked();
251 }
252 }
254 actions: [
255 Action {
256 objectName: "search"
257 iconName: "search"
258 visible: root.searchEntryEnabled
259 onTriggered: {
260 headerContainer.showSearch = true;
261 searchTextField.forceActiveFocus();
262 }
263 }
264 ]
265 }
267 property var contents: null
268 Component.onCompleted: root.refreshLogo()
270 Component {
271 id: imageComponent
273 Item {
274 anchors { fill: parent; topMargin: units.gu(1); bottomMargin: units.gu(1) }
275 clip: true
276 Image {
277 objectName: "titleImage"
278 anchors.fill: parent
279 source: root.scopeStyle ? root.scopeStyle.headerLogo : ""
280 fillMode: Image.PreserveAspectFit
281 horizontalAlignment: Image.AlignLeft
282 sourceSize.height: height
283 }
284 }
285 }
286 }
287 }
288 }
290 Row {
291 spacing: units.gu(.5)
292 Repeater {
293 objectName: "paginationRepeater"
294 model: root.paginationCount
295 Image {
296 objectName: "paginationDots_" + index
297 height: units.gu(1)
298 width: height
299 source: (index == root.paginationIndex) ? "graphics/pagination_dot_on.png" : "graphics/pagination_dot_off.png"
300 }
301 }
302 anchors {
303 top: headerContainer.bottom
304 horizontalCenter: headerContainer.horizontalCenter
305 topMargin: units.gu(.5)
306 }
307 }
309 Component {
310 id: popoverComponent
311 Popover {
312 id: popover
313 autoClose: false
315 Component.onDestruction: {
316 headerContainer.popover = null;
317 }
319 Column {
320 anchors {
321 top: parent.top
322 left: parent.left
323 right: parent.right
324 }
326 Repeater {
327 id: recentSearches
328 objectName: "recentSearches"
329 model: searchHistory
331 delegate: Standard {
332 showDivider: index < recentSearches.count - 1
333 text: query
334 onClicked: {
335 searchHistory.addQuery(text);
336 searchTextField.text = text;
337 closePopup();
338 unfocus();
339 }
340 }
341 }
342 }
343 }
344 }
346 BorderImage {
347 id: bottomBorder
348 anchors {
349 top: headerContainer.bottom
350 left: parent.left
351 right: parent.right
352 bottom: bottomContainer.top
353 }
355 source: "graphics/PageHeaderBaseDivider.sci"
356 }
358 Item {
359 id: bottomContainer
361 anchors {
362 left: parent.left
363 right: parent.right
364 bottom: parent.bottom
365 }
366 height: childrenRect.height
367 }