Skip to content

Commit

Permalink
Propagate font settings from JetBrains to webview (#2654)
Browse files Browse the repository at this point in the history
Fixes:
https://linear.app/sourcegraph/issue/BUGS-462
https://linear.app/sourcegraph/issue/BUGS-342
https://linear.app/sourcegraph/issue/BUGS-220

## Test plan

1. Run this branch with sourcegraph/cody#6134
2. Open Settings > Appearance
3. Tick `Use custom font` and change font size
4. Apply changes
5. You should notice that both editor and webview font size changed


![image](https://github.com/user-attachments/assets/d3411dcc-5b85-4e6b-a438-ad25dc099ea8)
  • Loading branch information
pkukielka authored Nov 18, 2024
1 parent 09eae3d commit dcfc2d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ org.gradle.jvmargs=-Xmx4g -Xms500m
nodeBinaries.commit=8755ae4c05fd476cd23f2972049111ba436c86d4
nodeBinaries.version=v20.12.2
cody.autocomplete.enableFormatting=true
cody.commit=6ac4a8c1831ad3945fc16f20b8f21947897d3d14
cody.commit=05cdd34e7dc7e7e7652e71140729dcbf2cb33a14
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)
}

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() {}
}

0 comments on commit dcfc2d3

Please sign in to comment.