dima-avdeev-jb
3 years ago
committed by
GitHub
7 changed files with 130 additions and 21 deletions
@ -0,0 +1,28 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jetbrains.compose |
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize |
||||||
|
import androidx.compose.material.Surface |
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.ui.Modifier |
||||||
|
import com.intellij.openapi.project.Project |
||||||
|
import com.jetbrains.compose.theme.WidgetTheme |
||||||
|
import org.intellij.datavis.r.inlays.components.GraphicsManager |
||||||
|
|
||||||
|
@Composable |
||||||
|
fun IntellijTheme(project: Project, content: @Composable () -> Unit) { |
||||||
|
val isDarkMode = try { |
||||||
|
GraphicsManager.getInstance(project)?.isDarkModeEnabled ?: false |
||||||
|
} catch (t: Throwable) { |
||||||
|
false |
||||||
|
} |
||||||
|
WidgetTheme(darkTheme = isDarkMode) { |
||||||
|
Surface(modifier = Modifier.fillMaxSize()) { |
||||||
|
content() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jetbrains.compose.panel |
||||||
|
|
||||||
|
import androidx.compose.runtime.mutableStateOf |
||||||
|
import androidx.compose.ui.awt.ComposePanel |
||||||
|
import com.intellij.openapi.application.ApplicationManager |
||||||
|
import com.intellij.openapi.project.DumbAware |
||||||
|
import com.intellij.openapi.project.Project |
||||||
|
import com.intellij.openapi.wm.ToolWindow |
||||||
|
import com.intellij.openapi.wm.ToolWindowFactory |
||||||
|
import com.intellij.ui.content.ContentFactory |
||||||
|
import com.jetbrains.compose.IntellijTheme |
||||||
|
import java.awt.Dimension |
||||||
|
|
||||||
|
class ComposeToolWindow : ToolWindowFactory, DumbAware { |
||||||
|
|
||||||
|
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { |
||||||
|
ApplicationManager.getApplication().invokeLater { |
||||||
|
toolWindow.contentManager.addContent( |
||||||
|
ContentFactory.SERVICE.getInstance().createContent( |
||||||
|
ComposePanel().apply { |
||||||
|
size = Dimension(300, 300) |
||||||
|
setContent { |
||||||
|
IntellijTheme(project) { |
||||||
|
CounterPanel(stateWithIdeLifecycle) |
||||||
|
} |
||||||
|
} |
||||||
|
}, |
||||||
|
"Compose tool window", |
||||||
|
false |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
val stateWithIdeLifecycle = mutableStateOf(CounterState()) |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jetbrains.compose.panel |
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Column |
||||||
|
import androidx.compose.material.Button |
||||||
|
import androidx.compose.material.Text |
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.runtime.MutableState |
||||||
|
import androidx.compose.runtime.* |
||||||
|
|
||||||
|
@Composable |
||||||
|
fun CounterPanel(stateWithIdeLifecycle: MutableState<CounterState>) { |
||||||
|
var stateInline by remember { mutableStateOf(CounterState()) } |
||||||
|
Column { |
||||||
|
Text("Counter with IDE lifecycle: ${stateWithIdeLifecycle.value.counter}") |
||||||
|
Button(onClick = { |
||||||
|
stateWithIdeLifecycle.value = stateWithIdeLifecycle.value.copy( |
||||||
|
counter = stateWithIdeLifecycle.value.counter + 1 |
||||||
|
) |
||||||
|
}) { |
||||||
|
Text("Increment state with IDE lifecycle") |
||||||
|
} |
||||||
|
Text("Counter with @Composable lifecycle: ${stateInline.counter}") |
||||||
|
Button(onClick = { |
||||||
|
stateInline = stateInline.copy( |
||||||
|
counter = stateInline.counter + 1 |
||||||
|
) |
||||||
|
}) { |
||||||
|
Text("Increment state with @Composable lifecycle") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jetbrains.compose.panel |
||||||
|
|
||||||
|
data class CounterState( |
||||||
|
val counter: Int = 0 |
||||||
|
) |
Loading…
Reference in new issue