Skip to content

Commit

Permalink
Add derived TextStyle APIs under Typography (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r authored Jan 15, 2024
1 parent 6dca337 commit 36df5e5
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ide-laf-bridge/api/ide-laf-bridge.api
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public abstract interface class org/jetbrains/jewel/bridge/ToolWindowScope {
public abstract fun getToolWindow ()Lcom/intellij/openapi/wm/ToolWindow;
}

public final class org/jetbrains/jewel/bridge/TypographyKt {
public static final fun medium (Lorg/jetbrains/jewel/ui/component/Typography;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public static final fun regular (Lorg/jetbrains/jewel/ui/component/Typography;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public static final fun small (Lorg/jetbrains/jewel/ui/component/Typography;Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
}

public final class org/jetbrains/jewel/bridge/actionSystem/ProvideDataKt {
public static final fun ComponentDataProviderBridge (Ljavax/swing/JComponent;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun provideData (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;)Landroidx/compose/ui/Modifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.jetbrains.jewel.bridge

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp
import com.intellij.openapi.util.SystemInfo
import com.intellij.ui.NewUiValue
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.ui.component.Typography
import org.jetbrains.jewel.ui.component.minus

/**
* The text style to use for regular text. Identical to
* [JewelTheme.textStyle].
*
* Only available when running in the IntelliJ Platform.
*/
@Composable
public fun Typography.regular(): TextStyle = labelTextStyle()

/**
* The text style to use for medium text. Smaller than [regular].
*
* Only available when running in the IntelliJ Platform.
*
* **Note:** when using the Classic UI on Windows, this returns the same
* value as [regular] (the default TextStyle from the theme). This is the
* same behavior implemented by [com.intellij.util.ui.JBFont].
*/
@Composable
public fun Typography.medium(): TextStyle =
if (mediumAndSmallFontsAsRegular()) {
labelTextStyle()
} else {
labelTextStyle().copy(fontSize = labelTextSize() - 1.sp)
}

/**
* The text style to use for small text. Smaller than [medium]. Should be
* avoided when running in the New UI, in favor of [medium], unless it's
* absolutely necessary.
*
* Only available when running in the IntelliJ Platform.
*
* **Note:** when using the Classic UI on Windows, this returns the same
* value as [regular] (the default TextStyle from the theme). This is the
* same behavior implemented by [com.intellij.util.ui.JBFont].
*/
@Composable
public fun Typography.small(): TextStyle =
if (mediumAndSmallFontsAsRegular()) {
labelTextStyle()
} else {
labelTextStyle().copy(fontSize = labelTextSize() - 2.sp)
}

// Copied from JBFont — current as of IJP 233.
private fun mediumAndSmallFontsAsRegular(): Boolean =
SystemInfo.isWindows && !NewUiValue.isEnabled()
17 changes: 17 additions & 0 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,23 @@ public final class org/jetbrains/jewel/ui/component/TooltipPlacement : androidx/
public fun positionProvider-9KIMszo (JLandroidx/compose/runtime/Composer;I)Landroidx/compose/ui/window/PopupPositionProvider;
}

public final class org/jetbrains/jewel/ui/component/Typography {
public static final field $stable I
public static final field INSTANCE Lorg/jetbrains/jewel/ui/component/Typography;
public final fun h0TextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public final fun h1TextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public final fun h2TextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public final fun h3TextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public final fun h4TextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
public final fun labelTextSize-5XXgJZs (Landroidx/compose/runtime/Composer;I)J
public final fun labelTextStyle (Landroidx/compose/runtime/Composer;I)Landroidx/compose/ui/text/TextStyle;
}

public final class org/jetbrains/jewel/ui/component/TypographyKt {
public static final fun minus-NB67dxo (JJ)J
public static final fun plus-NB67dxo (JJ)J
}

public final class org/jetbrains/jewel/ui/component/styling/ButtonColors {
public static final field $stable I
public static final field Companion Lorg/jetbrains/jewel/ui/component/styling/ButtonColors$Companion;
Expand Down
85 changes: 85 additions & 0 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.jetbrains.jewel.ui.component

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.isUnspecified
import androidx.compose.ui.unit.sp
import org.jetbrains.jewel.foundation.theme.JewelTheme

/**
* A quick way to obtain text styles derived from
* [the default `TextStyle`][JewelTheme.textStyle]. These match the
* functionality provided by `JBFont` in the IntelliJ Platform.
*/
public object Typography {

/** The text style to use for labels. Identical to [JewelTheme.textStyle]. */
@Composable
public fun labelTextStyle(): TextStyle = JewelTheme.textStyle

/**
* The text size to use for labels. Identical to the size set in
* [JewelTheme.textStyle].
*/
@Composable
public fun labelTextSize(): TextUnit = JewelTheme.textStyle.fontSize

/**
* The text style to use for h0 titles. Derived from
* [JewelTheme.textStyle].
*/
@Composable
public fun h0TextStyle(): TextStyle =
JewelTheme.textStyle.copy(fontSize = labelTextSize() + 12.sp, fontWeight = FontWeight.Bold)

/**
* The text style to use for h1 titles. Derived from
* [JewelTheme.textStyle].
*/
@Composable
public fun h1TextStyle(): TextStyle =
JewelTheme.textStyle.copy(fontSize = labelTextSize() + 9.sp, fontWeight = FontWeight.Bold)

/**
* The text style to use for h2 titles. Derived from
* [JewelTheme.textStyle].
*/
@Composable
public fun h2TextStyle(): TextStyle =
JewelTheme.textStyle.copy(fontSize = labelTextSize() + 5.sp)

/**
* The text style to use for h3 titles. Derived from
* [JewelTheme.textStyle].
*/
@Composable
public fun h3TextStyle(): TextStyle =
JewelTheme.textStyle.copy(fontSize = labelTextSize() + 3.sp)

/**
* The text style to use for h4 titles. Derived from
* [JewelTheme.textStyle].
*/
@Composable
public fun h4TextStyle(): TextStyle =
JewelTheme.textStyle.copy(fontSize = labelTextSize() + 1.sp, fontWeight = FontWeight.Bold)
}

public operator fun TextUnit.plus(other: TextUnit): TextUnit =
when {
isSp && other.isSp -> TextUnit(value + other.value, TextUnitType.Sp)
isEm && other.isEm -> TextUnit(value + other.value, TextUnitType.Em)
isUnspecified && other.isUnspecified -> TextUnit(value + other.value, TextUnitType.Unspecified)
else -> error("Can't add together different TextUnits. Got $type and ${other.type}")
}

public operator fun TextUnit.minus(other: TextUnit): TextUnit =
when {
isSp && other.isSp -> TextUnit(value - other.value, TextUnitType.Sp)
isEm && other.isEm -> TextUnit(value - other.value, TextUnitType.Em)
isUnspecified && other.isUnspecified -> TextUnit(value - other.value, TextUnitType.Unspecified)
else -> error("Can't subtract different TextUnits. Got $type and ${other.type}")
}

0 comments on commit 36df5e5

Please sign in to comment.