From 2b21c850df2bc04970a8bb5d91d014e8cc47349a Mon Sep 17 00:00:00 2001 From: Kanro Date: Wed, 1 Nov 2023 19:04:36 +0800 Subject: [PATCH] Support IDE zoom and presentation mode (#234) * Support zoom mode * Update api * Scale JBFont only * Make retrieveDensity internal --- .../org/jetbrains/jewel/bridge/BridgeUtils.kt | 16 +++++++++++++++- .../jetbrains/jewel/bridge/SwingBridgeService.kt | 4 ++++ .../jewel/bridge/theme/SwingBridgeTheme.kt | 6 +++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt index 5949bf3b9..d0ec88557 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/BridgeUtils.kt @@ -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 @@ -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 @@ -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)), @@ -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) +} diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt index 390f0af74..c4e3f35a0 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/SwingBridgeService.kt @@ -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 @@ -57,6 +58,7 @@ internal class SwingBridgeService : Disposable { return BridgeThemeData( themeDefinition = createBridgeThemeDefinition(), componentStyling = createBridgeComponentStyling(themeDefinition), + density = retrieveDensity(), ) } @@ -67,6 +69,7 @@ internal class SwingBridgeService : Disposable { internal data class BridgeThemeData( val themeDefinition: ThemeDefinition, val componentStyling: ComponentStyling, + val density: Density, ) { companion object { @@ -82,6 +85,7 @@ internal class SwingBridgeService : Disposable { dropdownTextStyle = TextStyle.Default, linkTextStyle = TextStyle.Default, ), + density = retrieveDensity(), ) } } diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/theme/SwingBridgeTheme.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/theme/SwingBridgeTheme.kt index 9b2fab4cd..484103d28 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/theme/SwingBridgeTheme.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/theme/SwingBridgeTheme.kt @@ -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 @@ -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() } }