forked from hydrogen-sailfishos/harbour-hydrogen
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHydrogenWebView.qml
129 lines (119 loc) · 4.48 KB
/
HydrogenWebView.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright © 2021-2023 Thilo Kogge (thigg)
// Copyright © 2023 The SailfishOS Hackathon Bucharest Team
//
// SPDX-FileCopyrightText: 2024 Mirian Margiani
//
// SPDX-License-Identifier: Apache-2.0
import QtQuick 2.0
import Sailfish.Silica 1.0
import Sailfish.Silica.private 1.0 as Private
import Sailfish.WebView 1.0
import Sailfish.WebEngine 1.0
import io.thp.pyotherside 1.5
WebViewFlickable {
id: webviewFlickable
property string urlFromParent
function runJavaScript(script) {
webview.runJavaScript(script)
}
function enterSettingsView() {
var settingsURL = app.getSessionURL(webview.url) + '/settings'
webview.url = settingsURL
}
quickScroll: false
flickableDirection: Flickable.VerticalFlick
PullDownMenu {
// Workaround for bug #42:
// Enable on the sessions view (that's usually not scrollable),
// but make sure it is disabled in the sessions list view and in
// all rooms.
visible: /\/session$/.test(webview.url.toString()) ||
(!/\/room\/[^/]+$/.test(webview.url.toString()) &&
!/\/session\/[^/]+$/.test(webview.url.toString()))
MenuItem {
text: qsTr('App Settings')
onClicked: pageStack.push(Qt.resolvedUrl("pages/AppSettingsPage.qml"))
}
MenuItem {
visible: enabled
enabled: app.isSettingsAvailable
text: qsTr('Hydrogen Settings')
onClicked: enterSettingsView()
}
}
Component.onCompleted: {
startWebEngine()
}
webView {
id: webview
anchors.fill: parent
viewportHeight: isPortrait ? Screen.height - virtualKeyboardMargin
: Screen.width - virtualKeyboardMargin
onViewInitialized: {
console.log("loading framescript")
webview.loadFrameScript(Qt.resolvedUrl("framescript.js"))
webview.addMessageListener("webview:log")
webview.addMessageListener("webview:notificationCount")
}
onUrlChanged: {
app.handleUrlChange(webview.url, app.openingArgument)
}
onRecvAsyncMessage: {
switch (message) {
case "webview:log":
console.log("webapp-log: " + JSON.stringify(data))
break
case "webview:notificationCount":
app.coverTitle = ""
app.notificationCount = data.count
app.coverMessages = data.coverPreview
break
case "embed:linkclicked":
var url = '/^http:\/\/localhost/'
if (!data.uri.match('http://localhost'))
Qt.openUrlExternally(data['uri'])
break
default:
console.log("Message: " + JSON.stringify(
message) + " data: " + JSON.stringify(data))
}
}
onLoadedChanged: {
if (webview.loaded) {
var readFile = function(path) {
var file = new XMLHttpRequest();
file.open("GET", path, false); // Synchronous
file.send(null);
return file.responseText;
}
var notificationScript = readFile(Qt.resolvedUrl("notificationCount.js"));
webview.runJavaScript(notificationScript);
}
}
}
function startWebEngine() {
// NOTE: Settings are set and saved in prefs.js.
//
// If you need to disable one of the following settings, you must set
// it to default explicitly, not just comment it out.
// disable CORS
WebEngineSettings.setPreference(
"security.disable_cors_checks", true,
WebEngineSettings.BoolPref)
/*** User Settings ***/
// Zoom/scale
WebEngineSettings.pixelRatio = (wvConfig.zoom*Theme.pixelRatio*10)/10.0
// Follow Ambience
if (wvConfig.ambienceMode) {
WebEngineSettings.colorScheme = wvConfig.ambienceMode
}
// Memory Cache
const wantCache = (wvConfig.memCache == 11) // automatic -> -1
? -1 // automatic
: Math.round(wvConfig.memCache * 12.8 * 1024) // slider display shows value * 12.8 in MB, we want KB
console.debug("Setting Mozilla memory cache to", wantCache)
WebEngineSettings.setPreference(
"browser.cache.memory.capacity", wantCache,
WebEngineSettings.IntPref);
}
}