From 7385067a0a93fb8f52c05ab6e17bcd6310624fb8 Mon Sep 17 00:00:00 2001 From: Zhiming Ma Date: Sat, 26 Oct 2024 00:11:37 +0800 Subject: [PATCH] fix(intellij): fix ui theme sync. (#3326) --- .../tabbyml/intellijtabby/chat/ChatBrowser.kt | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/chat/ChatBrowser.kt b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/chat/ChatBrowser.kt index b61b7ecb2199..26bd6af4b2d1 100644 --- a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/chat/ChatBrowser.kt +++ b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/chat/ChatBrowser.kt @@ -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 @@ -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() @@ -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( @@ -94,8 +111,6 @@ 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() @@ -103,9 +118,12 @@ class ChatBrowser(private val project: Project) : JBCefBrowser() { }) 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() + } }) } @@ -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()