Browse Source

Try to fix images with preview in tooling (#1458)

Maybe it will fix #1160
Fix for refactored Paragraph.getHorizontalPosition (#115)
pull/1467/head
Alexey Tsvetkov 3 years ago committed by GitHub
parent
commit
2a3027a5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      gradle-plugins/preview-rpc/src/main/kotlin/org/jetbrains/compose/desktop/ui/tooling/preview/rpc/RemotePreviewHost.kt
  2. 4
      idea-plugin/examples/simple-preview-example/build.gradle.kts
  3. 4
      idea-plugin/examples/simple-preview-example/pure-jvm/build.gradle.kts
  4. 38
      idea-plugin/examples/simple-preview-example/pure-jvm/src/main/kotlin/preview.kt
  5. BIN
      idea-plugin/examples/simple-preview-example/pure-jvm/src/main/resources/compose-logo-white.png

18
gradle-plugins/preview-rpc/src/main/kotlin/org/jetbrains/compose/desktop/ui/tooling/preview/rpc/RemotePreviewHost.kt

@ -7,16 +7,12 @@ package org.jetbrains.compose.desktop.ui.tooling.preview.rpc
import java.io.File import java.io.File
import java.lang.RuntimeException import java.lang.RuntimeException
import java.lang.reflect.Method
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.net.URLClassLoader import java.net.URLClassLoader
import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.system.exitProcess import kotlin.system.exitProcess
import kotlin.system.measureTimeMillis import kotlin.system.measureTimeMillis
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
val PREVIEW_HOST_CLASS_NAME: String val PREVIEW_HOST_CLASS_NAME: String
get() = PreviewHost::class.java.canonicalName get() = PreviewHost::class.java.canonicalName
@ -111,6 +107,20 @@ internal class PreviewHost(private val log: PreviewLogger, connection: RemoteCon
request: FrameRequest request: FrameRequest
): ByteArray { ): ByteArray {
val classloader = classloaderProvider.getClassloader(classpath) val classloader = classloaderProvider.getClassloader(classpath)
val thread = Thread.currentThread()
val prevContextClassloader = thread.contextClassLoader
thread.contextClassLoader = classloader
return try {
renderFrame(classloader, request)
} finally {
thread.contextClassLoader = prevContextClassloader
}
}
private fun renderFrame(
classloader: ClassLoader,
request: FrameRequest
): ByteArray {
val previewFacade = classloader.loadClass(PREVIEW_FACADE_CLASS_NAME) val previewFacade = classloader.loadClass(PREVIEW_FACADE_CLASS_NAME)
val renderArgsClasses = arrayOf( val renderArgsClasses = arrayOf(
String::class.java, String::class.java,

4
idea-plugin/examples/simple-preview-example/build.gradle.kts

@ -6,9 +6,9 @@ buildscript {
} }
dependencies { dependencies {
// __LATEST_COMPOSE_RELEASE_VERSION__ // __LATEST_COMPOSE_RELEASE_VERSION__
classpath("org.jetbrains.compose:compose-gradle-plugin:1.0.0-alpha1") classpath("org.jetbrains.compose:compose-gradle-plugin:0.0.9-preview-images")
// __KOTLIN_COMPOSE_VERSION__ // __KOTLIN_COMPOSE_VERSION__
classpath(kotlin("gradle-plugin", version = "1.5.21")) classpath(kotlin("gradle-plugin", version = "1.5.31"))
} }
} }

4
idea-plugin/examples/simple-preview-example/pure-jvm/build.gradle.kts

@ -8,3 +8,7 @@ plugins {
dependencies { dependencies {
implementation(compose.desktop.currentOs) implementation(compose.desktop.currentOs)
} }
compose.desktop.application {
mainClass = "PreviewKt"
}

38
idea-plugin/examples/simple-preview-example/pure-jvm/src/main/kotlin/preview.kt

@ -1,16 +1,38 @@
import androidx.compose.material.Text
import androidx.compose.material.Button
import androidx.compose.runtime.*
import androidx.compose.desktop.ui.tooling.preview.Preview import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication
@Preview @Preview
@Composable @Composable
fun ExamplePreview() { fun ExamplePreview() {
var text by remember { mutableStateOf("Hello, World!") } var text by remember { mutableStateOf("Hello, World!") }
val buttonColors = ButtonDefaults.buttonColors(
Button(onClick = { backgroundColor = Color.Blue,
text = "Hello, Desktop!" contentColor = Color.White
}) { )
Text(text) Button(
colors = buttonColors,
modifier = Modifier.padding(5.dp),
onClick = { text = "Hello, Desktop!" }
) {
Row {
Image(
painterResource("compose-logo-white.png"),
"compose-logo",
modifier = Modifier.height(32.dp).width(32.dp),
)
Text(text, modifier = Modifier.padding(top = 8.dp, start = 5.dp))
} }
} }
}
fun main() = singleWindowApplication {
ExamplePreview()
}

BIN
idea-plugin/examples/simple-preview-example/pure-jvm/src/main/resources/compose-logo-white.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Loading…
Cancel
Save