~osomon/webbrowser-app/create-downloads-folder

« back to all changes in this revision

Viewing changes to src/Ubuntu/Browser/Browser.qml

Implement selection upon long-click and copy to clipboard.

Approved by PS Jenkins bot, Bill Filler, Ugo Riboni.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import QtWebKit 3.0
21
21
import QtWebKit.experimental 1.0
22
22
import Ubuntu.Components 0.1
 
23
import Ubuntu.Components.Popups 0.1
23
24
 
24
25
FocusScope {
25
26
    id: browser
42
43
        }
43
44
 
44
45
        focus: true
 
46
        interactive: !selection.visible
 
47
 
 
48
        property real scale: experimental.test.contentsScale * experimental.test.devicePixelRatio
45
49
 
46
50
        // iOS 5.0’s iPhone user agent
47
51
        experimental.userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
48
52
 
49
 
        onLoadingChanged: {
50
 
            error.visible = (loadRequest.status === WebView.LoadFailedStatus)
 
53
        experimental.preferences.navigatorQtObjectEnabled: true
 
54
        experimental.userScripts: [Qt.resolvedUrl("selection.js")]
 
55
        experimental.onMessageReceived: {
 
56
            var data = null
 
57
            try {
 
58
                data = JSON.parse(message.data)
 
59
            } catch (error) {
 
60
                console.debug('DEBUG:', message.data)
 
61
                return
 
62
            }
 
63
            if ('event' in data) {
 
64
                if (data.event === 'longpress') {
 
65
                    selection.clearData()
 
66
                    selection.createData()
 
67
                    if ('html' in data) {
 
68
                        selection.mimedata.html = data.html
 
69
                    }
 
70
                    if ('text' in data) {
 
71
                        selection.mimedata.text = data.text
 
72
                    }
 
73
                    if ('images' in data) {
 
74
                        // TODO: download and cache the images locally
 
75
                        // (grab them from the webview’s cache, if possible),
 
76
                        // and forward local URLs.
 
77
                        selection.mimedata.urls = data.images
 
78
                    }
 
79
                    selection.show(data.left * scale, data.top * scale,
 
80
                                   data.width * scale, data.height * scale)
 
81
                }
 
82
            }
51
83
        }
52
84
 
53
85
        onUrlChanged: {
61
93
                revealingBar.hide()
62
94
            }
63
95
        }
 
96
 
 
97
        onLoadingChanged: {
 
98
            error.visible = (loadRequest.status === WebView.LoadFailedStatus)
 
99
        }
 
100
    }
 
101
 
 
102
    Selection {
 
103
        id: selection
 
104
 
 
105
        anchors.fill: webview
 
106
        visible: false
 
107
 
 
108
        property Item __popover: null
 
109
        property var mimedata: null
 
110
 
 
111
        function createData() {
 
112
            if (mimedata === null) {
 
113
                mimedata = Clipboard.newData()
 
114
            }
 
115
        }
 
116
 
 
117
        function clearData() {
 
118
            if (mimedata !== null) {
 
119
                delete mimedata
 
120
                mimedata = null
 
121
            }
 
122
        }
 
123
 
 
124
        function __showPopover() {
 
125
            __popover = PopupUtils.open(Qt.resolvedUrl("SelectionPopover.qml"), selection.rect)
 
126
            __popover.selection = selection
 
127
        }
 
128
 
 
129
        function show(x, y, width, height) {
 
130
            rect.x = x
 
131
            rect.y = y
 
132
            rect.width = width
 
133
            rect.height = height
 
134
            visible = true
 
135
            __showPopover()
 
136
        }
 
137
 
 
138
        function dismiss() {
 
139
            visible = false
 
140
            if (__popover != null) {
 
141
                PopupUtils.close(__popover)
 
142
                __popover = null
 
143
            }
 
144
        }
 
145
 
 
146
        onResized: {
 
147
            // TODO: talk to the DOM to compute the block element below the
 
148
            // selection, resize the rectangle to fit it, and update the
 
149
            // contents of the corresponding MIME data.
 
150
            __showPopover()
 
151
        }
 
152
 
 
153
        function share() {
 
154
            console.log("TODO: share selection")
 
155
        }
 
156
 
 
157
        function save() {
 
158
            console.log("TODO: save selection")
 
159
        }
 
160
 
 
161
        function copy() {
 
162
            Clipboard.push(mimedata)
 
163
            clearData()
 
164
        }
64
165
    }
65
166
 
66
167
    ErrorSheet {