Skip to content

Commit

Permalink
Fixed IntelliJ resource loading for different classpath configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
lamba92 committed Sep 7, 2023
1 parent 11e0013 commit 1a123c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.awt.ComposePanel
import com.intellij.openapi.wm.ToolWindow

fun ToolWindow.addComposePanel(
fun ToolWindow.addComposeTab(
tabDisplayName: String,
isLockable: Boolean = true,
content: @Composable ComposePanel.() -> Unit,
) = ComposePanel {
content()
}.also { contentManager.addContent(contentManager.factory.createContent(it, tabDisplayName, isLockable)) }
content: @Composable () -> Unit,
) = ComposePanel()
.apply { setContent(content) }
.also { contentManager.addContent(contentManager.factory.createContent(it, tabDisplayName, isLockable)) }

internal fun ComposePanel(
height: Int = 800,
width: Int = 800,
y: Int = 0,
x: Int = 0,
content: @Composable ComposePanel.() -> Unit,
): ComposePanel {
val panel = ComposePanel()
panel.setBounds(x, y, width, height)
panel.setContent {
panel.content()
}
return panel
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.jetbrains.jewel.bridge

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.ResourceLoader
import com.intellij.openapi.components.service
import com.intellij.util.ui.DirProvider
import org.jetbrains.jewel.ExperimentalJewelApi
import org.jetbrains.jewel.LocalResourceLoader
import org.jetbrains.jewel.themes.intui.standalone.IntUiDefaultResourceLoader
import org.jetbrains.jewel.themes.intui.standalone.IntUiTheme

private val bridgeService = service<SwingBridgeService>()
Expand All @@ -15,5 +20,19 @@ fun SwingBridgeTheme(content: @Composable () -> Unit) {
val themeData by bridgeService.currentBridgeThemeData.collectAsState()

// TODO handle non-Int UI themes, too
IntUiTheme(themeData.themeDefinition, themeData.componentStyling, swingCompatMode = true, content)
IntUiTheme(themeData.themeDefinition, themeData.componentStyling, swingCompatMode = true) {
CompositionLocalProvider(LocalResourceLoader provides IntelliJResourceLoader) {
content()
}
}
}

object IntelliJResourceLoader : ResourceLoader {

private val dirProvider = DirProvider()

override fun load(resourcePath: String) =
IntUiDefaultResourceLoader.loadOrNull(resourcePath)
?: IntUiDefaultResourceLoader.load(resourcePath.removePrefix(dirProvider.dir()))

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.jetbrains.jewel.OutlinedButton
import org.jetbrains.jewel.Text
import org.jetbrains.jewel.TextField
import org.jetbrains.jewel.bridge.SwingBridgeTheme
import org.jetbrains.jewel.bridge.addComposePanel
import org.jetbrains.jewel.bridge.addComposeTab
import org.jetbrains.jewel.bridge.toComposeColor
import org.jetbrains.jewel.themes.intui.standalone.IntUiTheme

Expand All @@ -39,7 +39,7 @@ internal class JewelDemoToolWindow : ToolWindowFactory, DumbAware {

@OptIn(ExperimentalJewelApi::class)
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
toolWindow.addComposePanel("Jewel") {
toolWindow.addComposeTab("Jewel") {
SwingBridgeTheme {
val resourceLoader = LocalResourceLoader.current
val bgColor by remember(IntUiTheme.isDark) { mutableStateOf(JBColor.PanelBackground.toComposeColor()) }
Expand Down

0 comments on commit 1a123c3

Please sign in to comment.