-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add derived TextStyle APIs under Typography (#287)
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/Typography.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Typography.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} |