on 45e4d3e help sign in
unity8/qml/Components/PageHeader.qml
358 lines 12 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 bool searchInProgress: false
37 property alias showSignatureLine: bottomBorder.visible
39 property alias bottomItem: bottomContainer.children
41 // TODO We should use foreground for the icons
42 // of the toolbar but unfortunately Action does not have
43 // the keyColor property as Icon does :-/
44 property var scopeStyle: null
46 signal backClicked()
48 onScopeStyleChanged: refreshLogo()
49 onSearchQueryChanged: {
50 // Make sure we are at the search page if the search query changes behind our feet
51 if (searchQuery) {
52 headerContainer.showSearch = true;
53 }
54 }
56 function triggerSearch() {
57 if (searchEntryEnabled) {
58 headerContainer.showSearch = true;
59 searchTextField.forceActiveFocus();
60 }
61 }
63 function closePopup() {
64 if (headerContainer.popover != null) {
65 PopupUtils.close(headerContainer.popover);
66 }
67 }
69 function resetSearch(keepFocus) {
70 if (searchHistory) {
71 searchHistory.addQuery(searchTextField.text);
72 }
73 if (!keepFocus) {
74 unfocus();
75 }
76 searchTextField.text = "";
77 closePopup();
78 }
80 function unfocus() {
81 searchTextField.focus = false;
82 if (!searchTextField.text) {
83 headerContainer.showSearch = false;
84 }
85 }
87 function openSearchHistory() {
88 if (openSearchAnimation.running) {
89 openSearchAnimation.openSearchHistory = true;
90 } else if (root.searchHistory.count > 0 && headerContainer.popover == null) {
91 headerContainer.popover = PopupUtils.open(popoverComponent, searchTextField,
92 {
93 "contentWidth": searchTextField.width,
94 "edgeMargins": units.gu(1)
95 }
96 );
97 }
98 }
100 function refreshLogo() {
101 if (scopeStyle ? scopeStyle.headerLogo != "" : false) {
102 header.contents = imageComponent.createObject();
103 } else if (header.contents) {
104 header.contents.destroy();
105 header.contents = null;
106 }
107 }
109 Connections {
110 target: root.scopeStyle
111 onHeaderLogoChanged: root.refreshLogo()
112 }
114 InverseMouseArea {
115 anchors { fill: parent; margins: units.gu(1); bottomMargin: units.gu(3) + bottomContainer.height }
116 visible: headerContainer.showSearch
117 onPressed: {
118 closePopup();
119 if (!searchTextField.text) {
120 headerContainer.showSearch = false;
121 }
122 searchTextField.focus = false;
123 mouse.accepted = false;
124 }
125 }
127 Flickable {
128 id: headerContainer
129 objectName: "headerContainer"
130 clip: openSearchAnimation.running
131 anchors { left: parent.left; top: parent.top; right: parent.right }
132 height: units.gu(6.5)
133 contentHeight: headersColumn.height
134 interactive: false
135 contentY: showSearch ? 0 : height
137 property bool showSearch: false
138 property var popover: null
140 Background {
141 objectName: "headerBackground"
142 style: scopeStyle.headerBackground
143 }
145 Behavior on contentY {
146 UbuntuNumberAnimation {
147 id: openSearchAnimation
148 property bool openSearchHistory: false
150 onRunningChanged: {
151 if (!running && openSearchAnimation.openSearchHistory) {
152 openSearchAnimation.openSearchHistory = false;
153 root.openSearchHistory();
154 }
155 }
156 }
157 }
159 Column {
160 id: headersColumn
161 anchors { left: parent.left; right: parent.right }
163 PageHeadStyle {
164 id: searchHeader
165 anchors { left: parent.left; right: parent.right }
166 height: headerContainer.height
167 contentHeight: height
168 opacity: headerContainer.clip || headerContainer.showSearch ? 1 : 0 // setting visible false cause column to relayout
169 separatorSource: ""
170 // Required to keep PageHeadStyle noise down as it expects the Page's properties around.
171 property var styledItem: searchHeader
172 property string title
173 property var config: PageHeadConfiguration {
174 backAction: Action {
175 iconName: "back"
176 onTriggered: {
177 root.resetSearch();
178 headerContainer.showSearch = false;
179 }
180 }
181 }
182 property var contents: TextField {
183 id: searchTextField
184 objectName: "searchTextField"
185 hasClearButton: false
186 anchors {
187 fill: parent
188 leftMargin: units.gu(1)
189 topMargin: units.gu(1)
190 bottomMargin: units.gu(1)
191 rightMargin: root.width > units.gu(60) ? root.width - units.gu(40) : units.gu(1)
192 }
194 secondaryItem: AbstractButton {
195 height: searchTextField.height
196 width: height
197 enabled: searchTextField.text.length > 0
199 Image {
200 objectName: "clearIcon"
201 anchors.fill: parent
202 anchors.margins: units.gu(.75)
203 source: "image://theme/clear"
204 opacity: searchTextField.text.length > 0 && !searchActivityIndicator.running
205 visible: opacity > 0
206 Behavior on opacity {
207 UbuntuNumberAnimation { duration: UbuntuAnimation.FastDuration }
208 }
209 }
211 ActivityIndicator {
212 id: searchActivityIndicator
213 objectName: "searchIndicator"
214 anchors.fill: parent
215 anchors.margins: units.gu(.75)
216 running: root.searchInProgress
217 opacity: running ? 1 : 0
218 Behavior on opacity {
219 UbuntuNumberAnimation { duration: UbuntuAnimation.FastDuration }
220 }
221 }
223 onClicked: {
224 root.resetSearch(true);
225 root.openSearchHistory();
226 }
227 }
229 onActiveFocusChanged: {
230 if (activeFocus) {
231 root.openSearchHistory();
232 }
233 }
235 onTextChanged: {
236 if (text != "") {
237 closePopup();
238 }
239 }
240 }
241 }
243 PageHeadStyle {
244 id: header
245 objectName: "innerPageHeader"
246 anchors { left: parent.left; right: parent.right }
247 height: headerContainer.height
248 contentHeight: height
249 opacity: headerContainer.clip || !headerContainer.showSearch ? 1 : 0 // setting visible false cause column to relayout
250 separatorSource: ""
251 textColor: root.scopeStyle ? root.scopeStyle.headerForeground : "grey"
252 property var styledItem: header
253 property string title: root.title
254 property var config: PageHeadConfiguration {
255 backAction: Action {
256 iconName: "back"
257 visible: root.showBackButton
258 onTriggered: {
259 root.backClicked();
260 }
261 }
263 actions: [
264 Action {
265 objectName: "search"
266 iconName: "search"
267 visible: root.searchEntryEnabled
268 onTriggered: {
269 headerContainer.showSearch = true;
270 searchTextField.forceActiveFocus();
271 }
272 }
273 ]
274 }
276 property var contents: null
277 Component.onCompleted: root.refreshLogo()
279 Component {
280 id: imageComponent
282 Item {
283 anchors { fill: parent; topMargin: units.gu(1); bottomMargin: units.gu(1) }
284 clip: true
285 Image {
286 objectName: "titleImage"
287 anchors.fill: parent
288 source: root.scopeStyle ? root.scopeStyle.headerLogo : ""
289 fillMode: Image.PreserveAspectFit
290 horizontalAlignment: Image.AlignLeft
291 sourceSize.height: height
292 }
293 }
294 }
295 }
296 }
297 }
299 Component {
300 id: popoverComponent
301 Popover {
302 id: popover
303 autoClose: false
305 Component.onDestruction: {
306 headerContainer.popover = null;
307 }
309 Column {
310 anchors {
311 top: parent.top
312 left: parent.left
313 right: parent.right
314 }
316 Repeater {
317 id: recentSearches
318 objectName: "recentSearches"
319 model: searchHistory
321 delegate: Standard {
322 showDivider: index < recentSearches.count - 1
323 text: query
324 onClicked: {
325 searchHistory.addQuery(text);
326 searchTextField.text = text;
327 closePopup();
328 unfocus();
329 }
330 }
331 }
332 }
333 }
334 }
336 BorderImage {
337 id: bottomBorder
338 anchors {
339 top: headerContainer.bottom
340 left: parent.left
341 right: parent.right
342 bottom: bottomContainer.top
343 }
345 source: "graphics/PageHeaderBaseDivider.sci"
346 }
348 Item {
349 id: bottomContainer
351 anchors {
352 left: parent.left
353 right: parent.right
354 bottom: parent.bottom
355 }
356 height: childrenRect.height
357 }