0480a265a1da7d4c8f37a32afdd70f0b6cf9daaf
[WebKit-https.git] / Tools / MiniBrowser / qt / qml / BrowserWindow.qml
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2010 University of Szeged
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 import QtQuick 2.0
30 import QtWebKit 3.0
31
32 Rectangle {
33     // Do not define anchors or an initial size here! This would mess up with QSGView::SizeRootObjectToView.
34
35     property alias webview: webView
36
37     signal pageTitleChanged(string title)
38
39     function load(address) {
40         webView.load(address)
41     }
42
43     function focusAddressBar() {
44         addressLine.forceActiveFocus()
45         addressLine.selectAll()
46     }
47
48     Rectangle {
49         id: navigationBar
50         color: "#efefef"
51         height: 38
52         anchors {
53             top: parent.top
54             left: parent.left
55             right: parent.right
56         }
57
58         Row {
59             height: parent.height - 6
60             anchors {
61                 verticalCenter: parent.verticalCenter
62                 left: parent.left
63                 leftMargin: 3
64             }
65             spacing: 3
66             id: controlsRow
67             Rectangle {
68                 id: backButton
69                 height: parent.height
70                 width: height
71                 color: "#efefef"
72                 radius: 6
73
74                 property alias enabled: webView.canGoBack
75
76                 Image {
77                     anchors.centerIn: parent
78                     source: "../icons/previous.png"
79                 }
80
81                 Rectangle {
82                     anchors.fill: parent
83                     color: parent.color
84                     radius: parent.radius
85                     opacity: 0.8
86                     visible: !parent.enabled
87                 }
88
89                 MouseArea {
90                     anchors.fill: parent
91                     onPressed: { if (parent.enabled) parent.color = "#cfcfcf" }
92                     onReleased: { parent.color = "#efefef" }
93                     onClicked: {
94                         if (parent.enabled) {
95                             console.log("MiniBrowser: Going backward in session history.")
96                             webView.goBack()
97                         }
98                     }
99                 }
100             }
101             Rectangle {
102                 id: forwardButton
103                 height: parent.height
104                 width: height
105                 color: "#efefef"
106                 radius: 6
107
108                 property alias enabled: webView.canGoForward
109
110                 Image {
111                     anchors.centerIn: parent
112                     source: "../icons/next.png"
113                 }
114
115                 Rectangle {
116                     anchors.fill: parent
117                     color: parent.color
118                     radius: parent.radius
119                     opacity: 0.8
120                     visible: !parent.enabled
121                 }
122
123                 MouseArea {
124                     anchors.fill: parent
125                     onPressed: { if (parent.enabled) parent.color = "#cfcfcf" }
126                     onReleased: { parent.color = "#efefef" }
127                     onClicked: {
128                         if (parent.enabled) {
129                             console.log("MiniBrowser: Going forward in session history.")
130                             webView.goForward()
131                         }
132                     }
133                 }
134             }
135             Rectangle {
136                 id: reloadButton
137                 height: parent.height
138                 width: height
139                 color: "#efefef"
140                 radius: 6
141
142                 Image {
143                     anchors.centerIn: parent
144                     source: webView.loading ? "../icons/stop.png" : "../icons/refresh.png"
145                 }
146
147                 MouseArea {
148                     anchors.fill: parent
149                     onPressed: { parent.color = "#cfcfcf" }
150                     onReleased: { parent.color = "#efefef" }
151                     onClicked: {
152                         if (webView.loading) {
153                             console.log("stop loading")
154                             webView.stop()
155                         } else {
156                             console.log("reloading")
157                             webView.reload()
158                         }
159                     }
160                 }
161             }
162         }
163         Rectangle {
164             color: "white"
165             height: 26
166             border.width: 1
167             border.color: "#bfbfbf"
168             radius: 3
169             anchors {
170                 left: controlsRow.right
171                 right: parent.right
172                 margins: 6
173                 verticalCenter: parent.verticalCenter
174             }
175             Rectangle {
176                 anchors {
177                     top: parent.top
178                     bottom: parent.bottom
179                     left: parent.left
180                 }
181                 radius: 3
182                 width: parent.width / 100 * webView.loadProgress
183                 color: "blue"
184                 opacity: 0.3
185                 visible: webView.loadProgress != 100
186             }
187             TextInput {
188                 id: addressLine
189                 clip: true
190                 selectByMouse: true
191                 font {
192                     pointSize: 11
193                     family: "Sans"
194                 }
195                 anchors {
196                     verticalCenter: parent.verticalCenter
197                     left: parent.left
198                     right: parent.right
199                     margins: 6
200                 }
201
202                 Keys.onReturnPressed:{
203                     console.log("going to: ", addressLine.text)
204                     webView.load(utils.urlFromUserInput(addressLine.text))
205                 }
206
207                 Keys.onPressed: {
208                     if (((event.modifiers & Qt.ControlModifier) && event.key == Qt.Key_L) || event.key == Qt.key_F6) {
209                         focusAddressBar()
210                         event.accepted = true
211                     }
212                 }
213             }
214         }
215     }
216
217     WebView {
218         id: webView
219         anchors {
220             top: navigationBar.bottom
221             left: parent.left
222             right: parent.right
223             bottom: parent.bottom
224         }
225
226         onTitleChanged: pageTitleChanged(title)
227         onUrlChanged: {
228             addressLine.text = url
229             if (options.printLoadedUrls)
230                 console.log("Loaded:", webView.url);
231             forceActiveFocus();
232         }
233     }
234
235     Keys.onPressed: {
236         if (((event.modifiers & Qt.ControlModifier) && event.key == Qt.Key_L) || event.key == Qt.key_F6) {
237             focusAddressBar()
238             event.accepted = true
239         }
240     }
241 }