Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate font settings from JetBrains to webview #2654

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/kotlin/com/sourcegraph/cody/sidebar/WebThemeController.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.sourcegraph.cody.sidebar

import com.intellij.ide.ui.UISettings
import com.intellij.ide.ui.UISettingsListener
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
import com.sourcegraph.config.ThemeUtil
import java.awt.Color
import javax.swing.UIManager

class WebThemeController {
class WebThemeController(parentDisposable: Disposable) {
private var themeChangeListener: ((WebTheme) -> Unit)? = null

init {
Expand All @@ -14,19 +18,30 @@ class WebThemeController {
invokeLater { themeChangeListener?.invoke(getTheme()) }
}
}

ApplicationManager.getApplication()
.messageBus
.connect(parentDisposable)
.subscribe(
UISettingsListener.TOPIC,
UISettingsListener { _ -> invokeLater { themeChangeListener?.invoke(getTheme()) } })
}

fun setThemeChangeListener(listener: (WebTheme) -> Unit) {
themeChangeListener = listener
}

fun getTheme(): WebTheme {
val themeVariables =
val colorVariables =
UIManager.getDefaults()
.filterValues { it is Color }
.mapKeys { toCSSVariableName(it.key.toString()) }
.mapValues { toCSSColor(it.value as Color) }
return WebTheme(ThemeUtil.isDarkTheme(), themeVariables)

val fontSizeVariable =
(toCSSVariableName("font-size") to "${UISettings.getInstance().fontSize}px")

return WebTheme(ThemeUtil.isDarkTheme(), colorVariables + fontSizeVariable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these variables? Strings of css properties?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private fun toCSSColor(value: Color) =
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/com/sourcegraph/cody/ui/web/WebUIService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sourcegraph.cody.ui.web

import com.google.gson.JsonParser
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
Expand Down Expand Up @@ -31,7 +32,7 @@ internal data class WebUIProxyCreationGate(
// - Pushes theme updates into Webviews.
// - Routes postMessage from host to Webviews.
@Service(Service.Level.PROJECT)
class WebUIService(private val project: Project) {
class WebUIService(private val project: Project) : Disposable {
companion object {
@JvmStatic fun getInstance(project: Project): WebUIService = project.service<WebUIService>()
}
Expand Down Expand Up @@ -78,8 +79,8 @@ class WebUIService(private val project: Project) {
}
}

private var themeController =
WebThemeController().apply { setThemeChangeListener { updateTheme(it) } }
private val themeController =
WebThemeController(this).apply { setThemeChangeListener { updateTheme(it) } }

private fun updateTheme(theme: WebTheme) {
synchronized(proxies) {
Expand Down Expand Up @@ -164,4 +165,6 @@ class WebUIService(private val project: Project) {
internal fun setTitle(handle: String, title: String) {
withProxy(handle) { it.title = title }
}

override fun dispose() {}
}
Loading