Skip to content

Commit dff216f

Browse files
committed
Fix dependencies. No scrollbars, add headers, fix second load.
1 parent df388c7 commit dff216f

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

composeApp/build.gradle.kts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.android.build.api.variant.FilterConfiguration.FilterType.ABI
2+
import org.gradle.internal.os.OperatingSystem
23
import org.jetbrains.compose.ExperimentalComposeLibrary
34
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
45
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
@@ -15,7 +16,6 @@ plugins {
1516
alias(libs.plugins.ktlint)
1617
alias(libs.plugins.sqldelight)
1718
alias(libs.plugins.conveyor)
18-
id("org.openjfx.javafxplugin").version("0.1.0")
1919
}
2020

2121
val organization: String? by project
@@ -44,11 +44,6 @@ val appConfig = mapOf(
4444

4545
val config = appConfig[organization] ?: appConfig["ooni"]!!
4646

47-
javafx {
48-
version = "17"
49-
modules = listOf("javafx.base", "javafx.graphics", "javafx.controls", "javafx.media", "javafx.web", "javafx.swing")
50-
}
51-
5247
kotlin {
5348
androidTarget {
5449
@OptIn(ExperimentalKotlinGradlePluginApi::class)
@@ -129,12 +124,14 @@ kotlin {
129124
implementation(files("./src/desktopMain/libs/oonimkall.jar"))
130125
implementation(compose.desktop.currentOs)
131126
implementation(libs.bundles.desktop)
132-
implementation("org.openjfx:javafx-base:17:mac-aarch64")
133-
implementation("org.openjfx:javafx-graphics:17:mac-aarch64")
134-
implementation("org.openjfx:javafx-controls:17:mac-aarch64")
135-
implementation("org.openjfx:javafx-media:17:mac-aarch64")
136-
implementation("org.openjfx:javafx-web:17:mac-aarch64")
137-
implementation("org.openjfx:javafx-swing:17:mac-aarch64")
127+
128+
// As JavaFX have platform-specific dependencies, we need to add them manually
129+
val fxParts = listOf("base", "graphics", "controls", "media", "web", "swing")
130+
val jvmVersion = 17
131+
val fxSuffix = getJavaFxSuffix()
132+
fxParts.forEach {
133+
implementation("org.openjfx:javafx-$it:$jvmVersion:$fxSuffix")
134+
}
138135
}
139136
}
140137
// Testing
@@ -554,3 +551,14 @@ fun isFdroidTaskRequested(): Boolean {
554551
fun isDebugTaskRequested(): Boolean {
555552
return gradle.startParameter.taskRequests.flatMap { it.args }.any { it.contains("Debug") }
556553
}
554+
555+
fun getJavaFxSuffix(): String {
556+
val os = OperatingSystem.current()
557+
val arch = System.getProperty("os.arch")
558+
return when {
559+
os.isMacOsX -> if (arch == "aarch64") "mac-aarch64" else "mac"
560+
os.isWindows -> "win"
561+
os.isLinux -> if (arch == "aarch64") "linux-aarch64" else "linux"
562+
else -> throw IllegalStateException("Unknown OS: $os")
563+
}
564+
}

composeApp/src/androidMain/kotlin/org/ooni/probe/ui/shared/OoniWebView.kt renamed to composeApp/src/androidMain/kotlin/org/ooni/probe/ui/shared/OoniWebView.android.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.webkit.WebView
1212
import android.webkit.WebViewClient
1313
import androidx.activity.compose.BackHandler
1414
import androidx.compose.runtime.Composable
15-
import androidx.compose.runtime.getValue
1615
import androidx.compose.ui.Modifier
1716
import androidx.compose.ui.viewinterop.AndroidView
1817

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.ooni.probe.ui.shared
22

33
import androidx.compose.runtime.Composable
4-
import androidx.compose.runtime.DisposableEffect
5-
import androidx.compose.runtime.getValue
6-
import androidx.compose.runtime.rememberUpdatedState
74
import androidx.compose.ui.Modifier
85
import androidx.compose.ui.awt.SwingPanel
96
import javafx.application.Platform
@@ -13,24 +10,29 @@ import javafx.scene.Scene
1310
import javafx.scene.layout.StackPane
1411
import javafx.scene.web.WebView
1512
import java.net.URL
13+
import kotlin.io.encoding.Base64
1614

1715
@Composable
1816
actual fun OoniWebView(
1917
controller: OoniWebViewController,
2018
modifier: Modifier,
2119
allowedDomains: List<String>,
2220
) {
23-
val onCreated = {}
24-
val onDispose = {}
25-
val currentOnDispose by rememberUpdatedState(onDispose)
2621
val event = controller.rememberNextEvent()
2722

2823
SwingPanel(
2924
factory = {
25+
controller.state = OoniWebViewController.State.Initializing
26+
27+
println("webview: factory")
3028
JFXPanel().apply {
29+
Platform.setImplicitExit(false) // Otherwise, webView will not show the second time
3130
Platform.runLater {
31+
println("webview: factory runLater")
32+
3233
val webView = WebView().apply {
3334
isVisible = true
35+
@Suppress("SetJavaScriptEnabled")
3436
engine.isJavaScriptEnabled = true
3537

3638
// Set up load listeners
@@ -39,18 +41,23 @@ actual fun OoniWebView(
3941
Worker.State.SCHEDULED -> {
4042
controller.state = OoniWebViewController.State.Loading(0f)
4143
}
44+
4245
Worker.State.RUNNING -> {
4346
val progress = engine.loadWorker.progress
44-
controller.state = OoniWebViewController.State.Loading(progress.toFloat())
47+
controller.state =
48+
OoniWebViewController.State.Loading(progress.toFloat())
4549
}
50+
4651
Worker.State.SUCCEEDED -> {
4752
controller.state = OoniWebViewController.State.Successful
4853
controller.canGoBack = engine.history.currentIndex > 0
4954
}
55+
5056
Worker.State.FAILED -> {
5157
controller.state = OoniWebViewController.State.Failure
5258
controller.canGoBack = engine.history.currentIndex > 0
5359
}
60+
5461
else -> {}
5562
}
5663
}
@@ -71,45 +78,59 @@ actual fun OoniWebView(
7178
}
7279
controller.canGoBack = engine.history.currentIndex > 0
7380
}
81+
82+
val css = """
83+
body {
84+
-ms-overflow-style: none; /* Internet Explorer 10+ */
85+
scrollbar-width: none; /* Firefox */
86+
}
87+
body::-webkit-scrollbar {
88+
display: none; /* Safari and Chrome */
89+
}
90+
""".trimIndent()
91+
val cssData = Base64.encode(css.encodeToByteArray())
92+
engine.userStyleSheetLocation =
93+
"data:text/css;charset=utf-8;base64,$cssData"
7494
}
7595

7696
val root = StackPane()
7797
root.children.add(webView)
7898
this.scene = Scene(root)
79-
onCreated()
8099
}
81100
}
82101
},
83102
modifier = modifier,
84103
update = { jfxPanel ->
104+
println("webview: update")
85105
Platform.runLater {
86-
val scene = jfxPanel.scene
87-
val root = scene?.root as? StackPane
88-
val webView = root?.children?.get(0) as? WebView
89-
webView?.let {
90-
when (event) {
91-
is OoniWebViewController.Event.Load -> {
92-
webView.engine.load(event.url)
106+
println("webview: update runLater")
107+
val root = jfxPanel.scene?.root as? StackPane
108+
val webView = (root?.children?.get(0) as? WebView) ?: return@runLater
109+
when (event) {
110+
is OoniWebViewController.Event.Load -> {
111+
val headers = event.additionalHttpHeaders.entries.joinToString {
112+
"\n${it.key}: it.value"
93113
}
94-
OoniWebViewController.Event.Reload -> {
95-
webView.engine.reload()
96-
}
97-
OoniWebViewController.Event.Back -> {
98-
if (webView.engine.history.currentIndex > 0) {
99-
webView.engine.history.go(-1)
100-
}
114+
// Hack to send HTTP headers by taking advantage of userAgent
115+
webView.engine.userAgent = "ooni$headers"
116+
println("webview: Loading: ${event.url}")
117+
webView.engine.load(event.url)
118+
}
119+
120+
OoniWebViewController.Event.Reload -> {
121+
webView.engine.reload()
122+
}
123+
124+
OoniWebViewController.Event.Back -> {
125+
if (webView.engine.history.currentIndex > 0) {
126+
webView.engine.history.go(-1)
101127
}
102-
null -> Unit
103128
}
129+
130+
null -> Unit
104131
}
132+
event?.let(controller::onEventHandled)
105133
}
106-
event?.let(controller::onEventHandled)
107134
},
108135
)
109-
110-
DisposableEffect(Unit) {
111-
onDispose {
112-
currentOnDispose()
113-
}
114-
}
115136
}
File renamed without changes.

0 commit comments

Comments
 (0)