Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Make Manual JSON Decoder fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
SerVB committed Jul 14, 2020
1 parent d3384fe commit 42ce1b2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ actual object ParamsProvider {

private val DEFAULT_HOST = getCurrentHostname()
private const val DEFAULT_PORT = "8887"
private val DEFAULT_TO_CLIENT_FORMAT: ToClientFormat = ToClientFormat.KOTLINX_PROTOBUF
private val DEFAULT_TO_CLIENT_FORMAT: ToClientFormat = ToClientFormat.KOTLINX_JSON_MANUAL
private const val DEFAULT_IMAGE_TTL = 60_000.0 // in ms
private const val DEFAULT_FLUSH_DELAY = 1
private const val DEFAULT_BACKGROUND_COLOR = "2A2"
Expand Down
2 changes: 1 addition & 1 deletion projector-client-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Name | Type | Default value | Description
`logUnsupportedEvents` | Presence | Not present | Log unsupported events received from server to browser console.
`doubleBuffering` | Presence | Not present | Enable double buffering for every single message from server.
`enableCompression` | Presence | Not present | Use compression for sending and receiving WebSocket messages.
`toClientFormat` | String | `protoBuf` | Sets format of data from server to client: `json` or `protoBuf`.
`toClientFormat` | String | `jsonManual` | Sets format of data from server to client: `json`, `jsonManual`, `protoBuf`.
`imageTtl` | Double | `60_000.0` | Set caching time of unused images in ms.
`flushDelay` | Int? | `1` | Set buffering time of events from client in ms. If the value is not integer, unbuffered mode is used: every client event is sent to the server immediately.
`showTextWidth` | Presence | Not present | Show near-text lines of browser width and desired width.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,22 @@ object ManualJsonToClientMessageDecoder : ToClientMessageDecoder {
"m" -> ServerMarkdownEvent.ServerMarkdownSetCssEvent(content["a"] as Int, content["b"] as String)
"n" -> ServerMarkdownEvent.ServerMarkdownScrollEvent(content["a"] as Int, content["b"] as Int)
"o" -> ServerMarkdownEvent.ServerMarkdownBrowseUriEvent(content["a"] as String)
"p" -> ServerWindowColorsEvent() // todo: decode map
"p" -> ServerWindowColorsEvent(content["a"].unsafeCast<Json>().toColorsStorage())
else -> throw IllegalArgumentException("Unsupported event type: ${JSON.stringify(this)}")
}
}

private fun Json.toColorsStorage(): ServerWindowColorsEvent.ColorsStorage {
return ServerWindowColorsEvent.ColorsStorage(
this["a"].unsafeCast<Json>().toColor(),
this["b"].unsafeCast<Json>().toColor(),
this["c"].unsafeCast<Json>().toColor(),
this["d"].unsafeCast<Json>().toColor(),
this["e"].unsafeCast<Json>().toColor(),
this["f"].unsafeCast<Json>().toColor()
)
}

private fun Array<Any>.toCaretInfoChange(): ServerCaretInfoChangedEvent.CaretInfoChange {
val type = this[0] as String
val content = this[1].unsafeCast<Json>()
Expand Down Expand Up @@ -288,12 +299,16 @@ object ManualJsonToClientMessageDecoder : ToClientMessageDecoder {
}
}

private fun Json.toColor(): PaintValue.Color {
return PaintValue.Color(this["a"] as Int)
}

private fun Array<Any>.toPaintValue(): PaintValue {
val type = this[0] as String
val content = this[1].unsafeCast<Json>()

return when (type) {
"a" -> PaintValue.Color(content["a"] as Int)
"a" -> content.toColor()
"b" -> PaintValue.Gradient(
content["a"].unsafeCast<Json>().toPoint(), content["b"].unsafeCast<Json>().toPoint(),
content["c"] as Int, content["d"] as Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ sealed class ClientState {
}

is ToClientHandshakeSuccessEvent -> {

ProjectorUI.setColors(command.colors)
command.colors?.let { ProjectorUI.setColors(it) }

OnScreenMessenger.showText(
"Loading fonts...",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package org.jetbrains.projector.client.web.state

import org.jetbrains.projector.client.common.canvas.Extensions.argbIntToRgbaString
import org.jetbrains.projector.common.protocol.data.PaintValue
import org.jetbrains.projector.common.protocol.toClient.ServerWindowColorsEvent

interface LafListener {
fun lookAndFeelChanged()
Expand Down Expand Up @@ -58,19 +58,13 @@ object ProjectorUI {
const val borderRadius = 8
const val borderThickness = 8.0

fun setColors(colors: Map<String, PaintValue.Color>) {
colors.forEach {
when (it.key) {
"windowHeaderActiveBackground" -> windowHeaderActiveBackgroundArgb = it.value.argb.toLong()
"windowHeaderInactiveBackground" -> windowHeaderInactiveBackgroundArgb = it.value.argb.toLong()
"windowActiveBorder" -> {
windowActiveBorderArgb = it.value.argb.toLong()
borderStyle = "1px solid ${windowActiveBorderArgb.argbIntToRgbaString()}"
}
"windowInactiveBorder" -> windowInactiveBorderArgb = it.value.argb.toLong()
"windowHeaderActiveText" -> windowHeaderActiveTextArgb = it.value.argb.toLong()
"windowHeaderInactiveText" -> windowHeaderInactiveTextArgb = it.value.argb.toLong()
}
}
fun setColors(colors: ServerWindowColorsEvent.ColorsStorage) {
windowHeaderActiveBackgroundArgb = colors.windowHeaderActiveBackground.argb.toLong()
windowHeaderInactiveBackgroundArgb = colors.windowHeaderInactiveBackground.argb.toLong()
windowActiveBorderArgb = colors.windowActiveBorder.argb.toLong()
borderStyle = "1px solid ${windowActiveBorderArgb.argbIntToRgbaString()}"
windowInactiveBorderArgb = colors.windowInactiveBorder.argb.toLong()
windowHeaderActiveTextArgb = colors.windowHeaderActiveText.argb.toLong()
windowHeaderInactiveTextArgb = colors.windowHeaderInactiveText.argb.toLong()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ val COMMON_VERSION = listOf(ServerEvent.serializer(), ClientEvent.serializer())

// Don't change order here: it's used to obtain readable "human id"
val commonVersionList = listOf(
-1663032476
-1663032476,
615706807
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package org.jetbrains.projector.common.protocol.handshake

import kotlinx.serialization.Serializable
import org.jetbrains.projector.common.protocol.data.FontDataHolder
import org.jetbrains.projector.common.protocol.data.PaintValue
import org.jetbrains.projector.common.protocol.toClient.ServerWindowColorsEvent

@Serializable
sealed class ToClientHandshakeEvent
Expand All @@ -37,7 +37,7 @@ data class ToClientHandshakeSuccessEvent(
val toServerCompression: CompressionType,
val toServerProtocol: ProtocolType,
val fontDataHolders: List<FontDataHolder>,
val colors: Map<String, PaintValue.Color>
val colors: ServerWindowColorsEvent.ColorsStorage? = null
) : ToClientHandshakeEvent()

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,22 @@ sealed class ServerMarkdownEvent : ServerEvent() {
@SerialName("p")
data class ServerWindowColorsEvent(
@SerialName("a")
val colors: Map<String, PaintValue.Color> = emptyMap()
) : ServerEvent()
val colors: ColorsStorage
) : ServerEvent() {

@Serializable
data class ColorsStorage(
@SerialName("a")
val windowHeaderActiveBackground: PaintValue.Color,
@SerialName("b")
val windowHeaderInactiveBackground: PaintValue.Color,
@SerialName("c")
val windowActiveBorder: PaintValue.Color,
@SerialName("d")
val windowInactiveBorder: PaintValue.Color,
@SerialName("e")
val windowHeaderActiveText: PaintValue.Color,
@SerialName("f")
val windowHeaderInactiveText: PaintValue.Color
)
}

0 comments on commit 42ce1b2

Please sign in to comment.