Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract ImageVectorPreviewPanel as reusable component #273

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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