Skip to content

Commit

Permalink
Extract ImageVectorPreviewPanel as reusable component
Browse files Browse the repository at this point in the history
  • Loading branch information
egorikftp committed Nov 5, 2024
1 parent 42717ea commit b4cd720
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 262 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.git.luolix.topposegears.valkyrie.ir

val IR_STUB = IrImageVector(
name = "",
defaultWidth = 24.0f,
defaultHeight = 24.0f,
viewportWidth = 18.0f,
viewportHeight = 18.0f,
nodes = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.git.luolix.topposegears.valkyrie.editor

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorState
Expand All @@ -10,7 +12,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.ui.JBUI
import io.git.luolix.topposegears.valkyrie.editor.ui.ImageVectorPreviewPanel
import io.git.luolix.topposegears.valkyrie.editor.ui.VirtualFileImageVector
import io.git.luolix.topposegears.valkyrie.ui.foundation.theme.ValkyrieTheme
import java.awt.Dimension
import javax.swing.JComponent
Expand Down Expand Up @@ -48,7 +50,10 @@ private class ImageVectorPreviewEditor(
private val composePanel = ComposePanel().apply {
setContent {
ValkyrieTheme(project, this) {
ImageVectorPreviewPanel(file)
VirtualFileImageVector(
modifier = Modifier.fillMaxSize(),
file = file,
)
}
}
preferredSize = JBUI.size(Dimension(800, 800))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@file:Suppress("NAME_SHADOWING")

package io.git.luolix.topposegears.valkyrie.editor.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.intellij.openapi.application.readAction
import com.intellij.openapi.vfs.VirtualFile
import io.git.luolix.topposegears.valkyrie.editor.toKtFile
import io.git.luolix.topposegears.valkyrie.ir.IrImageVector
import io.git.luolix.topposegears.valkyrie.psi.imagevector.ImageVectorPsiParser
import io.git.luolix.topposegears.valkyrie.ui.foundation.components.previewer.ImageVectorPreviewPanel
import io.git.luolix.topposegears.valkyrie.ui.foundation.rememberMutableState
import io.git.luolix.topposegears.valkyrie.ui.foundation.theme.LocalProject
import org.jetbrains.kotlin.psi.KtFile

@Composable
fun VirtualFileImageVector(
file: VirtualFile,
modifier: Modifier = Modifier,
) {
val project = LocalProject.current

var irImageVector by rememberMutableState<IrImageVector?> { null }
var isPreparing by rememberMutableState { true }

val ktFile by produceState<KtFile?>(null) {
value = readAction {
file.toKtFile(project)
}
}

LaunchedEffect(ktFile) {
val ktFile = ktFile ?: return@LaunchedEffect

readAction {
isPreparing = true
irImageVector = ImageVectorPsiParser.parseToIrImageVector(ktFile)
isPreparing = false
}
}

if (!isPreparing) {
ImageVectorPreviewPanel(
modifier = modifier.fillMaxSize(),
irImageVector = irImageVector,
)
}
}

This file was deleted.

Loading

0 comments on commit b4cd720

Please sign in to comment.