Skip to content

Commit

Permalink
Refactor SvgLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Oct 17, 2023
1 parent bfa6344 commit 09701e3
Show file tree
Hide file tree
Showing 108 changed files with 1,479 additions and 1,934 deletions.
39 changes: 23 additions & 16 deletions core/src/main/kotlin/org/jetbrains/jewel/Checkbox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.text.TextStyle
Expand All @@ -40,6 +39,10 @@ import org.jetbrains.jewel.CommonStateBitMask.Pressed
import org.jetbrains.jewel.CommonStateBitMask.Selected
import org.jetbrains.jewel.ToggleableComponentState.Companion.readToggleableState
import org.jetbrains.jewel.foundation.Stroke
import org.jetbrains.jewel.painter.PainterHint
import org.jetbrains.jewel.painter.PainterSuffixHint
import org.jetbrains.jewel.painter.hints.Selected
import org.jetbrains.jewel.painter.hints.Stateful
import org.jetbrains.jewel.styling.CheckboxColors
import org.jetbrains.jewel.styling.CheckboxIcons
import org.jetbrains.jewel.styling.CheckboxMetrics
Expand All @@ -48,7 +51,6 @@ import org.jetbrains.jewel.styling.LocalCheckboxStyle
@Composable
fun Checkbox(
checked: Boolean,
resourceLoader: ResourceLoader,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -71,15 +73,13 @@ fun Checkbox(
metrics = metrics,
icons = icons,
textStyle = textStyle,
resourceLoader = resourceLoader,
content = null,
)
}

@Composable
fun TriStateCheckbox(
state: ToggleableState,
resourceLoader: ResourceLoader,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -101,7 +101,6 @@ fun TriStateCheckbox(
metrics = metrics,
icons = icons,
textStyle = textStyle,
resourceLoader = resourceLoader,
content = null,
)
}
Expand All @@ -110,7 +109,6 @@ fun TriStateCheckbox(
fun TriStateCheckboxRow(
text: String,
state: ToggleableState,
resourceLoader: ResourceLoader,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -131,7 +129,6 @@ fun TriStateCheckboxRow(
colors = colors,
metrics = metrics,
icons = icons,
resourceLoader = resourceLoader,
textStyle = textStyle,
) {
Text(text)
Expand All @@ -142,7 +139,6 @@ fun TriStateCheckboxRow(
fun CheckboxRow(
text: String,
checked: Boolean,
resourceLoader: ResourceLoader,
onCheckedChange: ((Boolean) -> Unit)?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -165,7 +161,6 @@ fun CheckboxRow(
colors = colors,
metrics = metrics,
icons = icons,
resourceLoader = resourceLoader,
textStyle = textStyle,
) {
Text(text)
Expand All @@ -175,7 +170,6 @@ fun CheckboxRow(
@Composable
fun CheckboxRow(
checked: Boolean,
resourceLoader: ResourceLoader,
onCheckedChange: ((Boolean) -> Unit)?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -199,7 +193,6 @@ fun CheckboxRow(
colors = colors,
metrics = metrics,
icons = icons,
resourceLoader = resourceLoader,
textStyle = textStyle,
content = content,
)
Expand All @@ -208,7 +201,6 @@ fun CheckboxRow(
@Composable
fun TriStateCheckboxRow(
state: ToggleableState,
resourceLoader: ResourceLoader,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
Expand All @@ -230,7 +222,6 @@ fun TriStateCheckboxRow(
colors = colors,
metrics = metrics,
icons = icons,
resourceLoader = resourceLoader,
textStyle = textStyle,
content = content,
)
Expand All @@ -244,7 +235,6 @@ private fun CheckboxImpl(
colors: CheckboxColors,
metrics: CheckboxMetrics,
icons: CheckboxIcons,
resourceLoader: ResourceLoader,
modifier: Modifier,
enabled: Boolean,
outline: Outline,
Expand All @@ -268,13 +258,17 @@ private fun CheckboxImpl(
checkboxState.copy(pressed = false)

is HoverInteraction.Enter -> checkboxState = checkboxState.copy(hovered = true)
is HoverInteraction.Exit -> checkboxState = checkboxState.copy(hovered = true)
is HoverInteraction.Exit -> checkboxState = checkboxState.copy(hovered = false)
is FocusInteraction.Focus -> checkboxState = checkboxState.copy(focused = true)
is FocusInteraction.Unfocus -> checkboxState = checkboxState.copy(focused = false)
}
}
}

if (LocalSwingCompatMode.current) {
checkboxState = checkboxState.copy(hovered = false, pressed = false)
}

val wrapperModifier = modifier.triStateToggleable(
state = state,
onClick = onClick,
Expand All @@ -294,7 +288,15 @@ private fun CheckboxImpl(
alignment = Stroke.Alignment.Center,
)

val checkboxPainter by icons.checkbox.getPainter(resourceLoader, checkboxState)
val checkboxPainter by icons.checkbox.getPainter(
if (checkboxState.toggleableState == ToggleableState.Indeterminate) {
CheckBoxIndeterminate
} else {
PainterHint.None
},
Selected(checkboxState),
Stateful(checkboxState),
)

if (content == null) {
Box(contentAlignment = Alignment.TopStart) {
Expand Down Expand Up @@ -323,6 +325,11 @@ private fun CheckboxImpl(
}
}

private object CheckBoxIndeterminate : PainterSuffixHint() {

override fun suffix(): String = "Indeterminate"
}

@Composable
private fun CheckBoxImage(outerModifier: Modifier, checkboxPainter: Painter, checkBoxModifier: Modifier) {
Box(outerModifier, contentAlignment = Alignment.Center) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,73 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.loadSvgPainter
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import org.jetbrains.jewel.styling.CircularProgressStyle
import org.jetbrains.jewel.util.toHexString
import org.jetbrains.jewel.util.toRgbaHexString

@Composable
fun CircularProgressIndicator(
svgLoader: SvgLoader,
modifier: Modifier = Modifier,
style: CircularProgressStyle = IntelliJTheme.circularProgressStyle,
) {
CircularProgressIndicatorImpl(
modifier = modifier,
svgLoader = svgLoader,
iconSize = DpSize(16.dp, 16.dp),
style = style,
frameRetriever = { color -> SpinnerProgressIconGenerator.Small.generateSvgFrames(color.toHexString()) },
frameRetriever = { color -> SpinnerProgressIconGenerator.Small.generateSvgFrames(color.toRgbaHexString()) },
)
}

@Composable
fun CircularProgressIndicatorBig(
svgLoader: SvgLoader,
modifier: Modifier = Modifier,
style: CircularProgressStyle = IntelliJTheme.circularProgressStyle,
) {
CircularProgressIndicatorImpl(
modifier = modifier,
svgLoader = svgLoader,
iconSize = DpSize(32.dp, 32.dp),
style = style,
frameRetriever = { color -> SpinnerProgressIconGenerator.Big.generateSvgFrames(color.toHexString()) },
frameRetriever = { color -> SpinnerProgressIconGenerator.Big.generateSvgFrames(color.toRgbaHexString()) },
)
}

@Composable
private fun CircularProgressIndicatorImpl(
modifier: Modifier = Modifier,
svgLoader: SvgLoader,
iconSize: DpSize,
style: CircularProgressStyle,
frameRetriever: (Color) -> List<String>,
) {
val defaultColor = if (IntelliJTheme.isDark) Color(0xFF6F737A) else Color(0xFFA8ADBD)
var isFrameReady by remember { mutableStateOf(false) }
var currentFrame: Pair<String, Int> by remember { mutableStateOf("" to 0) }
var currentFrame: Painter? by remember { mutableStateOf(null) }
val currentPainter = currentFrame

if (!isFrameReady) {
if (currentPainter == null) {
Box(modifier.size(iconSize))
} else {
Icon(
modifier = modifier.size(iconSize),
painter = svgLoader.loadRawSvg(
currentFrame.first,
"circularProgressIndicator_frame_${currentFrame.second}",
),
painter = currentPainter,
contentDescription = null,
)
}

LaunchedEffect(style.color) {
val density = LocalDensity.current
LaunchedEffect(density, style.color) {
val frames = frameRetriever(style.color.takeOrElse { defaultColor })
.map {
loadSvgPainter(it.byteInputStream(), density)
}
while (true) {
for (i in frames.indices) {
currentFrame = frames[i] to i
currentFrame = frames[i]
isFrameReady = true
delay(style.frameTime.inWholeMilliseconds)
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/kotlin/org/jetbrains/jewel/ContextMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.ui.input.InputMode
import androidx.compose.ui.input.InputModeManager
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import androidx.compose.ui.window.rememberCursorPositionProvider
Expand All @@ -34,7 +33,6 @@ object IntelliJContextMenuRepresentation : ContextMenuRepresentation {
true
},
style = IntelliJTheme.menuStyle,
resourceLoader = LocalResourceLoader.current,
) {
contextItems(items)
}
Expand All @@ -45,7 +43,6 @@ object IntelliJContextMenuRepresentation : ContextMenuRepresentation {
@Composable
internal fun ContextMenu(
onDismissRequest: (InputMode) -> Boolean,
resourceLoader: ResourceLoader,
modifier: Modifier = Modifier,
focusable: Boolean = true,
style: MenuStyle = IntelliJTheme.menuStyle,
Expand Down Expand Up @@ -81,7 +78,6 @@ internal fun ContextMenu(
MenuContent(
modifier = modifier,
content = content,
resourceLoader = resourceLoader,
)
}
}
Expand Down
8 changes: 2 additions & 6 deletions core/src/main/kotlin/org/jetbrains/jewel/Dropdown.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import androidx.compose.ui.input.InputModeManager
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
Expand All @@ -44,14 +43,14 @@ import org.jetbrains.jewel.CommonStateBitMask.Hovered
import org.jetbrains.jewel.CommonStateBitMask.Pressed
import org.jetbrains.jewel.foundation.Stroke
import org.jetbrains.jewel.foundation.border
import org.jetbrains.jewel.painter.hints.Stateful
import org.jetbrains.jewel.styling.DropdownStyle
import org.jetbrains.jewel.styling.LocalMenuStyle
import org.jetbrains.jewel.styling.MenuStyle
import org.jetbrains.jewel.util.appendIf

@Composable
fun Dropdown(
resourceLoader: ResourceLoader,
modifier: Modifier = Modifier,
enabled: Boolean = true,
menuModifier: Modifier = Modifier,
Expand Down Expand Up @@ -135,7 +134,7 @@ fun Dropdown(
.align(Alignment.CenterEnd),
contentAlignment = Alignment.Center,
) {
val chevronIcon by style.icons.chevronDown.getPainter(resourceLoader, dropdownState)
val chevronIcon by style.icons.chevronDown.getPainter(Stateful(dropdownState))
Icon(
painter = chevronIcon,
contentDescription = null,
Expand All @@ -157,7 +156,6 @@ fun Dropdown(
style = style.menuStyle,
horizontalAlignment = Alignment.Start,
content = menuContent,
resourceLoader = resourceLoader,
)
}
}
Expand All @@ -167,7 +165,6 @@ fun Dropdown(
internal fun DropdownMenu(
onDismissRequest: (InputMode) -> Boolean,
horizontalAlignment: Alignment.Horizontal,
resourceLoader: ResourceLoader,
modifier: Modifier = Modifier,
style: MenuStyle,
content: MenuScope.() -> Unit,
Expand Down Expand Up @@ -208,7 +205,6 @@ internal fun DropdownMenu(
MenuContent(
modifier = modifier,
content = content,
resourceLoader = resourceLoader,
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/org/jetbrains/jewel/GlobalColors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface GlobalColors {
val outlines: OutlineColors

val infoContent: Color

val paneBackground: Color
}

@Immutable
Expand Down
Loading

0 comments on commit 09701e3

Please sign in to comment.