Skip to content

Commit

Permalink
fix(intellij): fix ui theme sync. (#3326)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored Oct 25, 2024
1 parent 7c5a13d commit 7385067
Showing 1 changed file with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.progress.util.BackgroundTaskUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.SystemInfo
import com.intellij.ui.jcef.JBCefBrowser
import com.intellij.ui.jcef.JBCefBrowserBase
import com.intellij.ui.jcef.JBCefJSQuery
Expand All @@ -34,7 +35,18 @@ import java.awt.datatransfer.StringSelection
import java.io.File


class ChatBrowser(private val project: Project) : JBCefBrowser() {
class ChatBrowser(private val project: Project) : JBCefBrowser(
createBuilder()
.setOffScreenRendering(
when {
SystemInfo.isWindows -> false
SystemInfo.isMac -> false
SystemInfo.isLinux -> true
else -> false
}
)
.setEnableOpenDevToolsMenuItem(true)
) {
private val logger = Logger.getInstance(ChatBrowser::class.java)
private val gson = Gson()
private val combinedState = project.service<CombinedState>()
Expand Down Expand Up @@ -69,6 +81,11 @@ class ChatBrowser(private val project: Project) : JBCefBrowser() {

init {
component.isVisible = false
val bgColor = calcComponentBgColor()
component.background = bgColor
setPageBackgroundColor("hsl(${bgColor.toHsl()})")

loadHTML(HTML_CONTENT)

jbCefClient.addLoadHandler(object : CefLoadHandlerAdapter() {
override fun onLoadingStateChange(
Expand All @@ -94,18 +111,19 @@ class ChatBrowser(private val project: Project) : JBCefBrowser() {
return@addHandler JBCefJSQuery.Response("")
}

loadHTML(HTML_CONTENT)

messageBusConnection.subscribe(CombinedState.Listener.TOPIC, object : CombinedState.Listener {
override fun stateChanged(state: CombinedState.State) {
reloadContent()
}
})

messageBusConnection.subscribe(EditorColorsManager.TOPIC, EditorColorsListener {
logger.debug("EditorColorsManager globalSchemeChange received, updating style.")
jsApplyStyle()
chatPanelUpdateTheme()
BackgroundTaskUtil.executeOnPooledThread(this) {
logger.debug("EditorColorsManager globalSchemeChange received, updating style.")
Thread.sleep(100)
jsApplyStyle()
chatPanelUpdateTheme()
}
})
}

Expand Down Expand Up @@ -198,11 +216,16 @@ class ChatBrowser(private val project: Project) : JBCefBrowser() {

private val isDarkTheme get() = EditorColorsManager.getInstance().isDarkEditor

private fun calcComponentBgColor(): Color {
val editorColorsScheme = EditorColorsManager.getInstance().schemeForCurrentUITheme
return editorColorsScheme.getColor(EditorColors.CARET_ROW_COLOR)
?: if (isDarkTheme) editorColorsScheme.defaultBackground.brighter() else editorColorsScheme.defaultBackground.darker()
}

private fun buildCss(): String {
val editorColorsScheme = EditorColorsManager.getInstance().schemeForCurrentUITheme
val bgColor = editorColorsScheme.defaultBackground
val bgActiveColor = editorColorsScheme.getColor(EditorColors.CARET_ROW_COLOR)
?: if (isDarkTheme) editorColorsScheme.defaultBackground.brighter() else editorColorsScheme.defaultBackground.darker()
val bgActiveColor = calcComponentBgColor()
val fgColor = editorColorsScheme.defaultForeground
val borderColor = editorColorsScheme.getColor(EditorColors.BORDER_LINES_COLOR)
?: if (isDarkTheme) editorColorsScheme.defaultForeground.brighter() else editorColorsScheme.defaultForeground.darker()
Expand Down

0 comments on commit 7385067

Please sign in to comment.