on 22ed985 help sign in
unity8/qml/Components/PageHeader.qml
359 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 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 inputMethodHints: Qt.ImhNoPredictiveText
186 hasClearButton: false
187 anchors {
188 fill: parent
189 leftMargin: units.gu(1)
190 topMargin: units.gu(1)
191 bottomMargin: units.gu(1)
192 rightMargin: root.width > units.gu(60) ? root.width - units.gu(40) : units.gu(1)
193 }
195 secondaryItem: AbstractButton {
196 height: searchTextField.height
197 width: height
198 enabled: searchTextField.text.length > 0
200 Image {
201 objectName: "clearIcon"
202 anchors.fill: parent
203 anchors.margins: units.gu(.75)
204 source: "image://theme/clear"
205 opacity: searchTextField.text.length > 0 && !searchActivityIndicator.running
206 visible: opacity > 0
207 Behavior on opacity {
208 UbuntuNumberAnimation { duration: UbuntuAnimation.FastDuration }
209 }
210 }
212 ActivityIndicator {
213 id: searchActivityIndicator
214 objectName: "searchIndicator"
215 anchors.fill: parent
216 anchors.margins: units.gu(.75)
217 running: root.searchInProgress
218 opacity: running ? 1 : 0
219 Behavior on opacity {
220 UbuntuNumberAnimation { duration: UbuntuAnimation.FastDuration }
221 }
222 }
224 onClicked: {
225 root.resetSearch(true);
226 root.openSearchHistory();
227 }
228 }
230 onActiveFocusChanged: {
231 if (activeFocus) {
232 root.openSearchHistory();
233 }
234 }
236 onTextChanged: {
237 if (text != "") {
238 closePopup();
239 }
240 }
241 }
242 }
244 PageHeadStyle {
245 id: header
246 objectName: "innerPageHeader"
247 anchors { left: parent.left; right: parent.right }
248 height: headerContainer.height
249 contentHeight: height
250 opacity: headerContainer.clip || !headerContainer.showSearch ? 1 : 0 // setting visible false cause column to relayout
251 separatorSource: ""
252 textColor: root.scopeStyle ? root.scopeStyle.headerForeground : "grey"
253 property var styledItem: header
254 property string title: root.title
255 property var config: PageHeadConfiguration {
256 backAction: Action {
257 iconName: "back"
258 visible: root.showBackButton
259 onTriggered: {
260 root.backClicked();
261 }
262 }
264 actions: [
265 Action {
266 objectName: "search"
267 iconName: "search"
268 visible: root.searchEntryEnabled
269 onTriggered: {
270 headerContainer.showSearch = true;
271 searchTextField.forceActiveFocus();
272 }
273 }
274 ]
275 }
277 property var contents: null
278 Component.onCompleted: root.refreshLogo()
280 Component {
281 id: imageComponent
283 Item {
284 anchors { fill: parent; topMargin: units.gu(1); bottomMargin: units.gu(1) }
285 clip: true
286 Image {
287 objectName: "titleImage"
288 anchors.fill: parent
289 source: root.scopeStyle ? root.scopeStyle.headerLogo : ""
290 fillMode: Image.PreserveAspectFit
291 horizontalAlignment: Image.AlignLeft
292 sourceSize.height: height
293 }
294 }
295 }
296 }
297 }
298 }
300 Component {
301 id: popoverComponent
302 Popover {
303 id: popover
304 autoClose: false
306 Component.onDestruction: {
307 headerContainer.popover = null;
308 }
310 Column {
311 anchors {
312 top: parent.top
313 left: parent.left
314 right: parent.right
315 }
317 Repeater {
318 id: recentSearches
319 objectName: "recentSearches"
320 model: searchHistory
322 delegate: Standard {
323 showDivider: index < recentSearches.count - 1
324 text: query
325 onClicked: {
326 searchHistory.addQuery(text);
327 searchTextField.text = text;
328 closePopup();
329 unfocus();
330 }
331 }
332 }
333 }
334 }
335 }
337 BorderImage {
338 id: bottomBorder
339 anchors {
340 top: headerContainer.bottom
341 left: parent.left
342 right: parent.right
343 bottom: bottomContainer.top
344 }
346 source: "graphics/PageHeaderBaseDivider.sci"
347 }
349 Item {
350 id: bottomContainer
352 anchors {
353 left: parent.left
354 right: parent.right
355 bottom: parent.bottom
356 }
357 height: childrenRect.height
358 }