Skip to content

Commit

Permalink
Loading of rasterisation settings from IntelliJ UI settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-senohrabek-jb committed Jul 4, 2024
1 parent 135f009 commit a5d4562
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.jewel.bridge

import com.intellij.ide.ui.LafManagerListener
import com.intellij.ide.ui.UISettingsListener
import com.intellij.openapi.application.Application
import com.intellij.openapi.application.ApplicationManager
import com.intellij.util.messages.MessageBus
Expand All @@ -12,6 +13,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.shareIn

Expand All @@ -21,11 +23,16 @@ internal val IntelliJApplication: Application
private fun Application.lookAndFeelFlow(scope: CoroutineScope): Flow<Unit> =
messageBus.flow(LafManagerListener.TOPIC, scope) { LafManagerListener { trySend(Unit) } }

private fun Application.uiSettingsFlow(scope: CoroutineScope): Flow<Unit> =
messageBus.flow(UISettingsListener.TOPIC, scope) {
UISettingsListener { trySend(Unit) }
}

internal fun Application.lookAndFeelChangedFlow(
scope: CoroutineScope,
sharingStarted: SharingStarted = SharingStarted.Eagerly,
): Flow<Unit> =
lookAndFeelFlow(scope).onStart { emit(Unit) }
merge(lookAndFeelFlow(scope), uiSettingsFlow(scope)).onStart { emit(Unit) }
.shareIn(scope, sharingStarted, replay = 1)

internal fun <L : Any, K> MessageBus.flow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.FontRasterizationSettings
import androidx.compose.ui.text.FontSmoothing
import androidx.compose.ui.text.PlatformParagraphStyle
import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -19,7 +23,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.isUnspecified
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.takeOrElse
import com.intellij.ide.ui.AntialiasingType
import com.intellij.ide.ui.LafManager
import com.intellij.ide.ui.UISettings
import com.intellij.ide.ui.UISettingsUtils
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.colors.EditorColorsManager
Expand Down Expand Up @@ -175,6 +181,9 @@ public fun retrieveTextStyle(
): TextStyle {
val lafFont = UIManager.getFont(key) ?: keyNotFound(key, "Font")
val jbFont = JBFont.create(lafFont, false)
val uiSettings = UISettings.instanceOrNull
val aa = uiSettings?.ideAAType ?: AntialiasingType.GREYSCALE
val platformDefaultFontRasterization = FontRasterizationSettings.PlatformDefault

val derivedFont = jbFont.let { if (bold) it.asBold() else it.asPlain() }
.let { if (fontStyle == FontStyle.Italic) it.asItalic() else it }
Expand All @@ -187,9 +196,27 @@ public fun retrieveTextStyle(
fontFamily = derivedFont.asComposeFontFamily(),
// TODO textDecoration might be defined in the AWT theme
lineHeight = lineHeight,
platformStyle = PlatformTextStyle(
null,
paragraphStyle = PlatformParagraphStyle(
fontRasterizationSettings = FontRasterizationSettings(
smoothing = aa.asComposeFontSmoothing(),
hinting = platformDefaultFontRasterization.hinting,
subpixelPositioning = platformDefaultFontRasterization.subpixelPositioning,
platformDefaultFontRasterization.autoHintingForced
)
),
)
)
}

@OptIn(ExperimentalTextApi::class)
private fun AntialiasingType.asComposeFontSmoothing(): FontSmoothing = when(this) {
AntialiasingType.GREYSCALE -> FontSmoothing.AntiAlias
AntialiasingType.SUBPIXEL -> FontSmoothing.SubpixelAntiAlias
AntialiasingType.OFF -> FontSmoothing.None
}

public val JBValue.dp: Dp
get() = unscaled.dp

Expand Down

0 comments on commit a5d4562

Please sign in to comment.