Skip to content

Commit

Permalink
fix color theme updating
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Feb 1, 2024
1 parent c6face8 commit b5f2b55
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.nx.console.graph

import NxGraphServer
import com.intellij.ide.ui.UISettingsListener
import com.intellij.notification.NotificationType
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
Expand Down Expand Up @@ -37,7 +38,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
protected val browser: JBCefBrowser = JBCefBrowser()
protected val graphServer: NxGraphServer = StandardNxGraphServer.getInstance(project)

protected val backgroundColor = getHexColor(UIUtil.getPanelBackground())
protected var backgroundColor = getHexColor(UIUtil.getPanelBackground())
protected val queryMessenger = JBCefJSQuery.create(browser as JBCefBrowserBase)
protected val browserLoadedState: MutableStateFlow<Boolean> = MutableStateFlow(false)

Expand All @@ -51,9 +52,11 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
OpenDevToolsContextMenuHandler(),
browser.cefBrowser
)
browser.setPageBackgroundColor(backgroundColor)
browser.setOpenLinksInExternalBrowser(true)

registerThemeListener()
setColors()

queryMessenger.addHandler { msg ->
when (msg) {
"ready" -> browserLoadedState.value = true
Expand Down Expand Up @@ -269,6 +272,38 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
}
}

private fun registerThemeListener() {
val connection = ApplicationManager.getApplication().messageBus.connect()
connection.subscribe(UISettingsListener.TOPIC, UISettingsListener { setColors() })
}

private fun setColors() {
backgroundColor = getHexColor(UIUtil.getPanelBackground())
browser.setPageBackgroundColor(backgroundColor)
executeWhenLoaded {
browser.executeJavaScript(
"""
const isDark = ${!JBColor.isBright()};
const body = document.body;
const darkClass = 'vscode-dark';
const lightClass = 'vscode-light';
body.classList.remove(darkClass, lightClass);
if (isDark) {
body.classList.add(darkClass);
} else {
body.classList.add(lightClass);
}
console.log("$backgroundColor")
body.style.setProperty('background-color', '$backgroundColor', 'important');
"""
.trimIndent()
)
}
}

override fun dispose() {
browser.dispose()
}
Expand Down

0 comments on commit b5f2b55

Please sign in to comment.