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