Skip to content

Commit

Permalink
Support IDE zoom and presentation mode (#234)
Browse files Browse the repository at this point in the history
* Support zoom mode

* Update api

* Scale JBFont only

* Make retrieveDensity internal
  • Loading branch information
devkanro authored Nov 1, 2023
1 parent b614595 commit 2b21c85
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.platform.Typeface
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.takeOrElse
import com.intellij.ide.ui.UISettingsUtils
import com.intellij.openapi.diagnostic.Logger
import com.intellij.ui.JBColor
import com.intellij.ui.JBColor.marker
Expand All @@ -32,6 +34,7 @@ import org.jetbrains.skiko.awt.font.AwtFontManager
import org.jetbrains.skiko.toSkikoTypefaceOrNull
import java.awt.Dimension
import java.awt.Font
import java.awt.GraphicsEnvironment
import java.awt.Insets
import javax.swing.UIManager

Expand Down Expand Up @@ -189,7 +192,7 @@ suspend fun retrieveTextStyle(

return TextStyle(
color = color,
fontSize = size.takeOrElse { derivedFont.size.sp },
fontSize = size.takeOrElse { derivedFont.size.sp / UISettingsUtils.getInstance().currentIdeScale },
fontWeight = if (bold) FontWeight.Bold else FontWeight.Normal,
fontStyle = fontStyle,
fontFamily = FontFamily(Typeface(typeface)),
Expand Down Expand Up @@ -217,3 +220,14 @@ internal operator fun TextUnit.plus(delta: Float) =
isEm -> TextUnit(value + delta, type)
else -> this
}

internal fun retrieveDensity(): Density {
val ideaScale = UISettingsUtils.getInstance().currentIdeScale
val scale = GraphicsEnvironment.getLocalGraphicsEnvironment()
.defaultScreenDevice
.defaultConfiguration
.defaultTransform
.scaleX * ideaScale

return Density(scale.toFloat(), 1f)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.jewel.bridge

import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Density
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.Service.Level
Expand Down Expand Up @@ -57,6 +58,7 @@ internal class SwingBridgeService : Disposable {
return BridgeThemeData(
themeDefinition = createBridgeThemeDefinition(),
componentStyling = createBridgeComponentStyling(themeDefinition),
density = retrieveDensity(),
)
}

Expand All @@ -67,6 +69,7 @@ internal class SwingBridgeService : Disposable {
internal data class BridgeThemeData(
val themeDefinition: ThemeDefinition,
val componentStyling: ComponentStyling,
val density: Density,
) {

companion object {
Expand All @@ -82,6 +85,7 @@ internal class SwingBridgeService : Disposable {
dropdownTextStyle = TextStyle.Default,
linkTextStyle = TextStyle.Default,
),
density = retrieveDensity(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalDensity
import com.intellij.openapi.components.service
import org.jetbrains.jewel.bridge.BridgePainterHintsProvider
import org.jetbrains.jewel.bridge.SwingBridgeService
Expand All @@ -25,7 +26,10 @@ fun SwingBridgeTheme(content: @Composable () -> Unit) {
ComponentStyling.with(themeData.componentStyling),
swingCompatMode = true,
) {
CompositionLocalProvider(LocalPainterHintsProvider provides BridgePainterHintsProvider(themeData.themeDefinition.isDark)) {
CompositionLocalProvider(
LocalPainterHintsProvider provides BridgePainterHintsProvider(themeData.themeDefinition.isDark),
LocalDensity provides themeData.density,
) {
content()
}
}
Expand Down

0 comments on commit 2b21c85

Please sign in to comment.