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

Support IDE zoom and presentation mode #234

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions ide-laf-bridge/api/ide-laf-bridge.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class org/jetbrains/jewel/bridge/BridgeUtilsKt {
public static final fun retrieveColorOrNull (Ljava/lang/String;)Landroidx/compose/ui/graphics/Color;
public static final fun retrieveColorOrUnspecified (Ljava/lang/String;)J
public static final fun retrieveColorsOrUnspecified ([Ljava/lang/String;)Ljava/util/List;
public static final fun retrieveDensity ()Landroidx/compose/ui/unit/Density;
public static final fun retrieveInsetsAsPaddingValues (Ljava/lang/String;)Landroidx/compose/foundation/layout/PaddingValues;
public static final fun retrieveIntAsDp (Ljava/lang/String;)F
public static final fun retrieveIntAsDpOrUnspecified (Ljava/lang/String;)F
Expand Down
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
}

fun retrieveDensity(): Density {
devkanro marked this conversation as resolved.
Show resolved Hide resolved
val ideaScale = UISettingsUtils.getInstance().currentIdeScale
val scale = GraphicsEnvironment.getLocalGraphicsEnvironment()
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better to use com.intellij.ui.scale.JBUIScale#pixScale here using Component provided as LocalComponent, and not to use defaultScreenDevice, since there are can be issues with different monitors

.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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, in the future we need to subscribe on changes in user scale, idea scale and sysScale changes

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fwiw this already works following zoom changes; haven't checked what happens when changing screens/system scale

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also works with presentation mode

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When ide scale changed the look and feel changing event will be fired too. I'll check the system scale event tomorrow.

) {
content()
}
}
Expand Down