Skip to content

Commit

Permalink
🔀 Fix icon don't be scaled after density changed (#239)
Browse files Browse the repository at this point in the history
* Fix icon don't be scaled after density changed

* Fix comments
  • Loading branch information
devkanro authored Nov 3, 2023
1 parent 5e0ac3a commit d73420a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 66 deletions.
6 changes: 5 additions & 1 deletion ide-laf-bridge/api/ide-laf-bridge.api
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ public final class org/jetbrains/jewel/bridge/JewelBridgeException$KeysNotFoundE
public fun <init> (Ljava/util/List;Ljava/lang/String;)V
}

public final class org/jetbrains/jewel/bridge/JewelComposePanelKt {
public static final fun JewelComposePanel (Lkotlin/jvm/functions/Function2;)Ljavax/swing/JComponent;
public static final fun getLocalComponent ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class org/jetbrains/jewel/bridge/ToolWindowExtensionsKt {
public static final fun addComposeTab (Lcom/intellij/openapi/wm/ToolWindow;Ljava/lang/String;ZZLkotlin/jvm/functions/Function3;)V
public static synthetic fun addComposeTab$default (Lcom/intellij/openapi/wm/ToolWindow;Ljava/lang/String;ZZLkotlin/jvm/functions/Function3;ILjava/lang/Object;)V
}

public abstract interface class org/jetbrains/jewel/bridge/ToolWindowScope {
public abstract fun getPanel ()Landroidx/compose/ui/awt/ComposePanel;
public abstract fun getToolWindow ()Lcom/intellij/openapi/wm/ToolWindow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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 @@ -222,13 +221,9 @@ internal operator fun TextUnit.plus(delta: Float): TextUnit =
else -> this
}

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

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

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.awt.ComposePanel
import org.jetbrains.jewel.bridge.actionSystem.ComponentDataProviderBridge
import org.jetbrains.jewel.bridge.theme.SwingBridgeTheme
import org.jetbrains.jewel.foundation.ExperimentalJewelApi
import javax.swing.JComponent

public fun JewelComposePanel(
content: @Composable () -> Unit,
): JComponent {
return ComposePanel().apply {
setContent {
SwingBridgeTheme {
CompositionLocalProvider(LocalComponent provides this@apply) {
ComponentDataProviderBridge(this@apply, content = content)
}
}
}
}
}

@ExperimentalJewelApi
public val LocalComponent: ProvidableCompositionLocal<JComponent> = staticCompositionLocalOf {
error("CompositionLocal LocalComponent not provided")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.jewel.bridge

import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Density
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.Service.Level
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -42,14 +41,12 @@ internal class SwingBridgeService(scope: CoroutineScope) {
return BridgeThemeData(
themeDefinition = createBridgeThemeDefinition(),
componentStyling = createBridgeComponentStyling(themeDefinition),
density = retrieveDensity(),
)
}

internal data class BridgeThemeData(
val themeDefinition: ThemeDefinition,
val componentStyling: ComponentStyling,
val density: Density,
) {

public companion object {
Expand All @@ -67,7 +64,6 @@ internal class SwingBridgeService(scope: CoroutineScope) {
dropdownTextStyle = TextStyle.Default,
linkTextStyle = TextStyle.Default,
),
density = retrieveDensity(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.jewel.bridge

import androidx.compose.runtime.Composable
import androidx.compose.ui.awt.ComposePanel
import com.intellij.openapi.wm.ToolWindow
import org.jetbrains.jewel.foundation.enableNewSwingCompositing

Expand All @@ -15,22 +14,22 @@ public fun ToolWindow.addComposeTab(
// The operation is idempotent, so we can safely do it every time.
enableNewSwingCompositing()

val composePanel = ComposePanel()

val scope = object : ToolWindowScope {
override val toolWindow: ToolWindow = this@addComposeTab
override val panel: ComposePanel = composePanel
}

composePanel.setContent { scope.content() }
val tabContent = contentManager.factory.createContent(composePanel, tabDisplayName, isLockable)
val tabContent = contentManager.factory.createContent(
JewelComposePanel {
val scope = object : ToolWindowScope {
override val toolWindow: ToolWindow
get() = this@addComposeTab
}
scope.content()
},
tabDisplayName,
isLockable,
)
tabContent.isCloseable = isCloseable
contentManager.addContent(tabContent)
}

public interface ToolWindowScope {

public val toolWindow: ToolWindow

public val panel: ComposePanel
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.platform.LocalDensity
import com.intellij.openapi.components.service
import org.jetbrains.jewel.bridge.BridgePainterHintsProvider
import org.jetbrains.jewel.bridge.SwingBridgeService
import org.jetbrains.jewel.bridge.retrieveIdeaDensity
import org.jetbrains.jewel.foundation.ExperimentalJewelApi
import org.jetbrains.jewel.ui.ComponentStyling
import org.jetbrains.jewel.ui.painter.LocalPainterHintsProvider
Expand All @@ -28,7 +29,7 @@ public fun SwingBridgeTheme(content: @Composable () -> Unit) {
) {
CompositionLocalProvider(
LocalPainterHintsProvider provides BridgePainterHintsProvider(themeData.themeDefinition.isDark),
LocalDensity provides themeData.density,
LocalDensity provides retrieveIdeaDensity(LocalDensity.current),
) {
content()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import androidx.compose.ui.unit.dp
import com.intellij.icons.AllIcons
import com.intellij.ui.JBColor
import icons.JewelIcons
import org.jetbrains.jewel.bridge.LocalComponent
import org.jetbrains.jewel.bridge.ToolWindowScope
import org.jetbrains.jewel.bridge.theme.SwingBridgeTheme
import org.jetbrains.jewel.bridge.toComposeColor
import org.jetbrains.jewel.foundation.lazy.tree.buildTree
import org.jetbrains.jewel.foundation.modifier.onActivated
Expand All @@ -49,21 +49,19 @@ import org.jetbrains.jewel.ui.component.Tooltip

@Composable
internal fun ToolWindowScope.ComponentShowcaseTab() {
SwingBridgeTheme {
val bgColor by remember(JewelTheme.isDark) { mutableStateOf(JBColor.PanelBackground.toComposeColor()) }

val scrollState = rememberScrollState()
Row(
modifier = Modifier.trackComponentActivation(panel)
.fillMaxSize()
.background(bgColor)
.verticalScroll(scrollState)
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
) {
ColumnOne()
ColumnTwo()
}
val bgColor by remember(JewelTheme.isDark) { mutableStateOf(JBColor.PanelBackground.toComposeColor()) }

val scrollState = rememberScrollState()
Row(
modifier = Modifier.trackComponentActivation(LocalComponent.current)
.fillMaxSize()
.background(bgColor)
.verticalScroll(scrollState)
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
) {
ColumnOne()
ColumnTwo()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import kotlinx.coroutines.runBlocking
import kotlinx.datetime.toJavaLocalDate
import org.jetbrains.jewel.bridge.retrieveColorOrUnspecified
import org.jetbrains.jewel.bridge.retrieveTextStyle
import org.jetbrains.jewel.bridge.theme.SwingBridgeTheme
import org.jetbrains.jewel.bridge.toComposeColor
import org.jetbrains.jewel.bridge.toFontFamily
import org.jetbrains.jewel.foundation.lazy.SelectableLazyColumn
Expand Down Expand Up @@ -103,28 +102,26 @@ import kotlin.time.Duration.Companion.seconds
@OptIn(DependsOnJBR::class)
@Composable
fun ReleasesSampleCompose(project: Project) {
SwingBridgeTheme {
var selectedItem: ContentItem? by remember { mutableStateOf(null) }
HorizontalSplitLayout(
first = { modifier ->
LeftColumn(
project = project,
modifier = modifier.fillMaxSize(),
onSelectedItemChange = { selectedItem = it },
)
},
second = { modifier ->
RightColumn(
selectedItem = selectedItem,
modifier = modifier.fillMaxSize(),
)
},
Modifier.fillMaxSize(),
initialDividerPosition = 400.dp,
minRatio = .15f,
maxRatio = .7f,
)
}
var selectedItem: ContentItem? by remember { mutableStateOf(null) }
HorizontalSplitLayout(
first = { modifier ->
LeftColumn(
project = project,
modifier = modifier.fillMaxSize(),
onSelectedItemChange = { selectedItem = it },
)
},
second = { modifier ->
RightColumn(
selectedItem = selectedItem,
modifier = modifier.fillMaxSize(),
)
},
Modifier.fillMaxSize(),
initialDividerPosition = 400.dp,
minRatio = .15f,
maxRatio = .7f,
)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ResourcePainterProvider(
currentHintsProvider.hints(basePath)
.forEach { scope.resolveHint(it) }

val cacheKey = scope.acceptedHints.hashCode()
val cacheKey = scope.acceptedHints.hashCode() * 31 + LocalDensity.current.hashCode()

if (inDebugMode && cache[cacheKey] != null) {
println("Cache hit for $basePath(${scope.acceptedHints.joinToString()})")
Expand Down

0 comments on commit d73420a

Please sign in to comment.