Skip to content

Commit e671fac

Browse files
Use API extensions instead of Java builders for Kotlin snippets (#156)
* Use API extensions instead of builders * Bump JxBrowser → `8.0.0-eap.6`
1 parent ce68272 commit e671fac

File tree

8 files changed

+39
-39
lines changed

8 files changed

+39
-39
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ plugins {
3131
id("org.jetbrains.kotlin.plugin.compose") version "2.0.0"
3232
}
3333

34-
val jxBrowserVersion by extra { "8.0.0-eap.5" } // The version of JxBrowser used in the examples.
34+
val jxBrowserVersion by extra { "8.0.0-eap.6" } // The version of JxBrowser used in the examples.
3535
val guavaVersion by extra { "29.0-jre" } // Some of the examples use Guava.
3636

3737
allprojects {

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/CustomProtocol.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import androidx.compose.runtime.LaunchedEffect
2424
import androidx.compose.ui.window.singleWindowApplication
2525
import com.teamdev.jxbrowser.dsl.Engine
2626
import com.teamdev.jxbrowser.dsl.browser.navigation
27+
import com.teamdev.jxbrowser.dsl.net.HttpHeader
28+
import com.teamdev.jxbrowser.dsl.net.Scheme
2729
import com.teamdev.jxbrowser.dsl.net.UrlRequestJobOptions
2830
import com.teamdev.jxbrowser.engine.RenderingMode
2931
import com.teamdev.jxbrowser.net.HttpStatus
30-
import com.teamdev.jxbrowser.net.Scheme
3132
import com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback
3233
import com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback.Params
3334
import com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback.Response
@@ -56,15 +57,16 @@ fun main() {
5657
/**
5758
* URL protocol for custom handling.
5859
*/
59-
private val PROTOCOL = Scheme.of("jxb")
60+
private val PROTOCOL = Scheme("jxb")
6061

6162
/**
6263
* An interceptor, which always sends "Hello there!" text in a response.
6364
*/
6465
private class RespondWithGreetings : InterceptUrlRequestCallback {
6566

6667
override fun on(params: Params): Response {
67-
val options = UrlRequestJobOptions(HttpStatus.OK, "text/html")
68+
val contentType = HttpHeader("Content-Type", "text/html")
69+
val options = UrlRequestJobOptions(HttpStatus.OK, listOf(contentType))
6870
val job = params.newUrlRequestJob(options).apply {
6971
write("<html><body><p>Hello there!</p></body></html>".toByteArray())
7072
complete()

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/DispatchKeyEvents.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import com.teamdev.jxbrowser.dsl.Engine
2828
import com.teamdev.jxbrowser.dsl.browser.mainFrame
2929
import com.teamdev.jxbrowser.dsl.browser.navigation
3030
import com.teamdev.jxbrowser.dsl.subscribe
31+
import com.teamdev.jxbrowser.dsl.ui.event.KeyPressed
32+
import com.teamdev.jxbrowser.dsl.ui.event.KeyReleased
33+
import com.teamdev.jxbrowser.dsl.ui.event.KeyTyped
3134
import com.teamdev.jxbrowser.engine.RenderingMode
3235
import com.teamdev.jxbrowser.navigation.event.FrameLoadFinished
3336
import com.teamdev.jxbrowser.ui.KeyCode
34-
import com.teamdev.jxbrowser.ui.event.KeyPressed
35-
import com.teamdev.jxbrowser.ui.event.KeyReleased
36-
import com.teamdev.jxbrowser.ui.event.KeyTyped
3737
import com.teamdev.jxbrowser.view.compose.BrowserView
3838
import java.util.concurrent.CountDownLatch
3939

@@ -66,20 +66,9 @@ private fun Browser.loadHtmlAndWait(html: String) {
6666

6767
private fun Browser.dispatchKeyEvent(character: Char) {
6868
val keyCode = KEY_CODES[character]!!
69-
dispatch(
70-
KeyPressed.newBuilder(keyCode)
71-
.keyChar(character)
72-
.build()
73-
)
74-
dispatch(
75-
KeyTyped.newBuilder(keyCode)
76-
.keyChar(character)
77-
.build()
78-
)
79-
dispatch(
80-
KeyReleased.newBuilder(keyCode)
81-
.build()
82-
)
69+
dispatch(KeyPressed(keyCode, char = character))
70+
dispatch(KeyTyped(keyCode, char = character))
71+
dispatch(KeyReleased(keyCode))
8372
}
8473

8574
private val KEY_CODES = mapOf(

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/DispatchMouseEvents.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ import com.teamdev.jxbrowser.dsl.Engine
3131
import com.teamdev.jxbrowser.dsl.browser.mainFrame
3232
import com.teamdev.jxbrowser.dsl.browser.navigation
3333
import com.teamdev.jxbrowser.dsl.subscribe
34+
import com.teamdev.jxbrowser.dsl.ui.Point
35+
import com.teamdev.jxbrowser.dsl.ui.event.MousePressed
36+
import com.teamdev.jxbrowser.dsl.ui.event.MouseReleased
3437
import com.teamdev.jxbrowser.engine.RenderingMode
3538
import com.teamdev.jxbrowser.navigation.event.FrameLoadFinished
3639
import com.teamdev.jxbrowser.ui.MouseButton
37-
import com.teamdev.jxbrowser.ui.Point
3840
import com.teamdev.jxbrowser.ui.event.MousePressed
3941
import com.teamdev.jxbrowser.ui.event.MouseReleased
4042
import com.teamdev.jxbrowser.view.compose.BrowserView
@@ -67,14 +69,18 @@ fun main() {
6769
*/
6870
private fun Browser.dispatchMouseEvent() {
6971
dispatch(
70-
MousePressed.newBuilder(Point.of(50, 50))
71-
.button(MouseButton.SECONDARY).clickCount(1)
72-
.build()
72+
MousePressed(
73+
location = Point(50, 50),
74+
button = MouseButton.SECONDARY,
75+
clickCount = 1
76+
)
7377
)
7478
dispatch(
75-
MouseReleased.newBuilder(Point.of(50, 50))
76-
.button(MouseButton.SECONDARY).clickCount(1)
77-
.build()
79+
MouseReleased(
80+
location = Point(50, 50),
81+
button = MouseButton.SECONDARY,
82+
clickCount = 1
83+
)
7884
)
7985
}
8086

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/InterceptRequest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.runtime.LaunchedEffect
2424
import androidx.compose.ui.window.singleWindowApplication
2525
import com.teamdev.jxbrowser.dsl.Engine
2626
import com.teamdev.jxbrowser.dsl.browser.navigation
27+
import com.teamdev.jxbrowser.dsl.net.HttpHeader
2728
import com.teamdev.jxbrowser.dsl.net.UrlRequestJobOptions
2829
import com.teamdev.jxbrowser.engine.RenderingMode
2930
import com.teamdev.jxbrowser.net.HttpStatus
@@ -59,11 +60,10 @@ fun main() {
5960
private class RespondWithSalutation : InterceptUrlRequestCallback {
6061

6162
override fun on(params: Params): Response {
62-
val headers = setOf(
63-
"Content-Type" to "text/html",
64-
"Content-Type" to "charset=utf-8"
63+
val headers = listOf(
64+
HttpHeader("Content-Type", "text/html"),
65+
HttpHeader("Content-Type", "charset=utf-8")
6566
)
66-
6767
val options = UrlRequestJobOptions(HttpStatus.OK, headers)
6868
val job = params.newUrlRequestJob(options)
6969

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/JarProtocol.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package com.teamdev.jxbrowser.examples
2323
import androidx.compose.ui.window.singleWindowApplication
2424
import com.teamdev.jxbrowser.dsl.Engine
2525
import com.teamdev.jxbrowser.dsl.browser.navigation
26+
import com.teamdev.jxbrowser.dsl.net.HttpHeader
2627
import com.teamdev.jxbrowser.dsl.net.UrlRequestJobOptions
2728
import com.teamdev.jxbrowser.engine.RenderingMode
2829
import com.teamdev.jxbrowser.net.HttpStatus
@@ -98,7 +99,8 @@ private class InterceptJarRequestCallback : InterceptUrlRequestCallback {
9899
entry: JarEntry,
99100
params: Params
100101
): UrlRequestJob {
101-
val options = UrlRequestJobOptions(HttpStatus.OK, entry.mimeType)
102+
val contentType = HttpHeader("Content-Type", entry.mimeType)
103+
val options = UrlRequestJobOptions(HttpStatus.OK, listOf(contentType))
102104
val job = params.newUrlRequestJob(options).apply {
103105
write(entry.data)
104106
complete()

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/LoadHtmlThroughInterceptRequest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package com.teamdev.jxbrowser.examples
2323
import androidx.compose.ui.window.singleWindowApplication
2424
import com.teamdev.jxbrowser.dsl.Engine
2525
import com.teamdev.jxbrowser.dsl.browser.navigation
26+
import com.teamdev.jxbrowser.dsl.net.HttpHeader
2627
import com.teamdev.jxbrowser.dsl.net.UrlRequestJobOptions
2728
import com.teamdev.jxbrowser.engine.RenderingMode
2829
import com.teamdev.jxbrowser.net.HttpStatus
@@ -42,7 +43,8 @@ fun main() {
4243
proceed()
4344
}
4445
val bytes = "<html><body>Hello!</body></html>".toByteArray()
45-
val options = UrlRequestJobOptions(HttpStatus.OK, "text/html")
46+
val contentType = HttpHeader("Content-Type", "text/html")
47+
val options = UrlRequestJobOptions(HttpStatus.OK, listOf(contentType))
4648
val job = params.newUrlRequestJob(options).apply {
4749
write(bytes)
4850
complete()

examples/src/main/kotlin/com/teamdev/jxbrowser/examples/UploadData.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ package com.teamdev.jxbrowser.examples
2222

2323
import com.teamdev.jxbrowser.dsl.Engine
2424
import com.teamdev.jxbrowser.dsl.browser.navigation
25+
import com.teamdev.jxbrowser.dsl.navigation.LoadUrlParams
2526
import com.teamdev.jxbrowser.dsl.net.FormData
27+
import com.teamdev.jxbrowser.dsl.net.StringEntry
2628
import com.teamdev.jxbrowser.dsl.net.TextData
2729
import com.teamdev.jxbrowser.dsl.network
2830
import com.teamdev.jxbrowser.dsl.register
2931
import com.teamdev.jxbrowser.engine.RenderingMode
30-
import com.teamdev.jxbrowser.navigation.LoadUrlParams
3132
import com.teamdev.jxbrowser.net.callback.BeforeSendUploadDataCallback
3233

3334
/**
@@ -49,10 +50,8 @@ fun main() = Engine(RenderingMode.OFF_SCREEN).use { engine ->
4950
val browser = engine.newBrowser()
5051

5152
// Prepare form data, which is going to be overridden.
52-
val formData = FormData("key" to "value")
53-
val localhost = LoadUrlParams.newBuilder("http://localhost/")
54-
.uploadData(formData)
55-
.build()
53+
val formData = FormData(StringEntry("key", "value"))
54+
val localhost = LoadUrlParams("http://localhost/", formData)
5655

5756
// Load URL request using POST method and send form data
5857
browser.navigation.loadUrlAndWait(localhost)

0 commit comments

Comments
 (0)