From 1a51b2f8517e5a06fe46de06ecaf2dec1ee7efbc Mon Sep 17 00:00:00 2001 From: SerVB Date: Tue, 14 Jul 2020 18:45:16 +0300 Subject: [PATCH] Switch from map to data class for clarity --- .../projector/server/ProjectorServer.kt | 4 +- .../projector/server/idea/IdeColors.kt | 48 +++++++++++-------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/projector-server/src/main/kotlin/org/jetbrains/projector/server/ProjectorServer.kt b/projector-server/src/main/kotlin/org/jetbrains/projector/server/ProjectorServer.kt index 3984ac16..7201876b 100644 --- a/projector-server/src/main/kotlin/org/jetbrains/projector/server/ProjectorServer.kt +++ b/projector-server/src/main/kotlin/org/jetbrains/projector/server/ProjectorServer.kt @@ -101,9 +101,7 @@ class ProjectorServer private constructor( private var windowColorsEvent: ServerWindowColorsEvent? = null private val ideaColors = IdeColors { colors -> - if (colors.isNotEmpty()) { - windowColorsEvent = ServerWindowColorsEvent(colors) - } + windowColorsEvent = ServerWindowColorsEvent(colors) } private val markdownPanelUpdater = MarkdownPanelUpdater( diff --git a/projector-server/src/main/kotlin/org/jetbrains/projector/server/idea/IdeColors.kt b/projector-server/src/main/kotlin/org/jetbrains/projector/server/idea/IdeColors.kt index 0ac4599e..34ee1588 100644 --- a/projector-server/src/main/kotlin/org/jetbrains/projector/server/idea/IdeColors.kt +++ b/projector-server/src/main/kotlin/org/jetbrains/projector/server/idea/IdeColors.kt @@ -19,6 +19,7 @@ package org.jetbrains.projector.server.idea import org.jetbrains.projector.common.protocol.data.PaintValue +import org.jetbrains.projector.common.protocol.toClient.ServerWindowColorsEvent import org.jetbrains.projector.server.log.Logger import java.awt.Color import java.lang.reflect.InvocationHandler @@ -29,31 +30,34 @@ import java.lang.reflect.Proxy * Subscribe on LafManager and update colors on LAF change. * Calls provided in constructor onColorsChanged action on LAF change. */ -class IdeColors(private val onColorsChanged: (Map) -> Unit) { +class IdeColors(private val onColorsChanged: (ServerWindowColorsEvent.ColorsStorage) -> Unit) { private val logger = Logger(IdeColors::class.simpleName!!) - var colors = emptyMap() + var colors: ServerWindowColorsEvent.ColorsStorage? = null private set init { invokeWhenIdeaIsInitialized("Getting IDE colors") { ideaClassLoader -> - colors = getColors(ideaClassLoader) - onColorsChanged(colors) + getColors(ideaClassLoader)?.let { + colors = it + onColorsChanged(it) + } subscribeToIdeLafManager(ideaClassLoader) } } private fun subscribeToIdeLafManager(ideaClassLoader: ClassLoader) { - try { val lafManagerClass = Class.forName("com.intellij.ide.ui.LafManager", false, ideaClassLoader) val lafManagerListenerClass = Class.forName("com.intellij.ide.ui.LafManagerListener", false, ideaClassLoader) val obj = InvocationHandler { _, method, _ -> if (method.declaringClass == lafManagerListenerClass && method.name == "lookAndFeelChanged") { - colors = getColors(ideaClassLoader) - onColorsChanged(colors) + getColors(ideaClassLoader)?.let { + colors = it + onColorsChanged(it) + } } null } @@ -68,32 +72,36 @@ class IdeColors(private val onColorsChanged: (Map) -> } } - private fun getColors(ideaClassLoader: ClassLoader): Map { - + private fun getColors(ideaClassLoader: ClassLoader): ServerWindowColorsEvent.ColorsStorage? { try { - val result = mutableMapOf() - val popupClass = Class.forName("com.intellij.util.ui.JBUI\$CurrentTheme\$Popup", false, ideaClassLoader) val headerBackgroundMethod = popupClass.getDeclaredMethod("headerBackground", Boolean::class.java) - result["windowHeaderActiveBackground"] = PaintValue.Color((headerBackgroundMethod.invoke(null, true) as Color).rgb) - result["windowHeaderInactiveBackground"] = PaintValue.Color((headerBackgroundMethod.invoke(null, false) as Color).rgb) + val windowHeaderActiveBackground = PaintValue.Color((headerBackgroundMethod.invoke(null, true) as Color).rgb) + val windowHeaderInactiveBackground = PaintValue.Color((headerBackgroundMethod.invoke(null, false) as Color).rgb) val borderColorMethod = popupClass.getDeclaredMethod("borderColor", Boolean::class.java) - result["windowActiveBorder"] = PaintValue.Color((borderColorMethod.invoke(null, true) as Color).rgb) - result["windowInactiveBorder"] = PaintValue.Color((borderColorMethod.invoke(null, false) as Color).rgb) + val windowActiveBorder = PaintValue.Color((borderColorMethod.invoke(null, true) as Color).rgb) + val windowInactiveBorder = PaintValue.Color((borderColorMethod.invoke(null, false) as Color).rgb) val labelClass = Class.forName("com.intellij.util.ui.JBUI\$CurrentTheme\$Label", false, ideaClassLoader) val labelForegroundMethod = labelClass.getDeclaredMethod("foreground") - result["windowHeaderActiveText"] = PaintValue.Color((labelForegroundMethod.invoke(null) as Color).rgb) - result["windowHeaderInactiveText"] = result["windowHeaderActiveText"]!! - - return result + val windowHeaderActiveText = PaintValue.Color((labelForegroundMethod.invoke(null) as Color).rgb) + val windowHeaderInactiveText = windowHeaderActiveText + + return ServerWindowColorsEvent.ColorsStorage( + windowHeaderActiveBackground = windowHeaderActiveBackground, + windowHeaderInactiveBackground = windowHeaderInactiveBackground, + windowActiveBorder = windowActiveBorder, + windowInactiveBorder = windowInactiveBorder, + windowHeaderActiveText = windowHeaderActiveText, + windowHeaderInactiveText = windowHeaderInactiveText + ) } catch (e: Exception) { logger.error(e) { "Failed to get IDE color scheme." } - return emptyMap() + return null } } }