From e25d74d4578f8bfdcc300956c778794e01896182 Mon Sep 17 00:00:00 2001 From: Igor Demin Date: Tue, 16 Feb 2021 20:07:44 +0300 Subject: [PATCH] Codeviewer. Migrate to build154 --- examples/codeviewer/android/build.gradle.kts | 7 ++++ .../org/jetbrains/codeviewer/MainActivity.kt | 2 +- examples/codeviewer/build.gradle.kts | 4 +-- .../codeviewer/platform/Resources.kt | 23 ++----------- .../codeviewer/platform/Selection.kt | 19 ----------- .../codeviewer/platform/Resources.kt | 6 ---- .../codeviewer/platform/Selection.kt | 6 ---- .../jetbrains/codeviewer/ui/CodeViewerView.kt | 13 ++++---- .../jetbrains/codeviewer/ui/editor/Editor.kt | 1 + .../codeviewer/ui/editor/EditorEmptyView.kt | 6 ++-- .../codeviewer/ui/editor/EditorTabsView.kt | 20 ++++++----- .../codeviewer/ui/editor/EditorView.kt | 22 +++++++------ .../codeviewer/ui/filetree/FileTreeView.kt | 28 ++++++++-------- .../codeviewer/ui/statusbar/StatusBar.kt | 10 +++--- .../codeviewer/util/LazyColumnFor.kt | 33 ------------------- .../codeviewer/util/VerticalSplittable.kt | 19 +++++++---- .../jetbrains/codeviewer/platform/Mouse.kt | 6 ++-- .../codeviewer/platform/Resources.kt | 15 --------- .../codeviewer/platform/Selection.kt | 9 ----- .../kotlin/org/jetbrains/codeviewer/main.kt | 19 ++++------- 20 files changed, 88 insertions(+), 180 deletions(-) delete mode 100644 examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt delete mode 100644 examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt delete mode 100644 examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/LazyColumnFor.kt delete mode 100644 examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt diff --git a/examples/codeviewer/android/build.gradle.kts b/examples/codeviewer/android/build.gradle.kts index 20040c60e6..f752a9ce13 100644 --- a/examples/codeviewer/android/build.gradle.kts +++ b/examples/codeviewer/android/build.gradle.kts @@ -22,4 +22,11 @@ android { dependencies { implementation(project(":common")) + implementation("androidx.activity:activity-compose:1.3.0-alpha02") { + exclude(group = "androidx.compose.animation") + exclude(group = "androidx.compose.foundation") + exclude(group = "androidx.compose.material") + exclude(group = "androidx.compose.runtime") + exclude(group = "androidx.compose.ui") + } } \ No newline at end of file diff --git a/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt b/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt index 0957a85d61..872c04c87a 100644 --- a/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt +++ b/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt @@ -1,8 +1,8 @@ package org.jetbrains.codeviewer import android.os.Bundle +import androidx.activity.compose.setContent import androidx.appcompat.app.AppCompatActivity -import androidx.compose.ui.platform.setContent import org.jetbrains.codeviewer.platform._HomeFolder import org.jetbrains.codeviewer.ui.MainView import java.io.File diff --git a/examples/codeviewer/build.gradle.kts b/examples/codeviewer/build.gradle.kts index 10a5097a0b..b706b69116 100644 --- a/examples/codeviewer/build.gradle.kts +++ b/examples/codeviewer/build.gradle.kts @@ -9,10 +9,10 @@ buildscript { dependencies { // __LATEST_COMPOSE_RELEASE_VERSION__ - classpath("org.jetbrains.compose:compose-gradle-plugin:0.3.0-build148") + classpath("org.jetbrains.compose:compose-gradle-plugin:0.3.0-build154") classpath("com.android.tools.build:gradle:4.0.1") // __KOTLIN_COMPOSE_VERSION__ - classpath(kotlin("gradle-plugin", version = "1.4.21-2")) + classpath(kotlin("gradle-plugin", version = "1.4.30")) } } diff --git a/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt b/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt index e96562587a..f08c93a6cc 100644 --- a/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt +++ b/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt @@ -1,31 +1,14 @@ package org.jetbrains.codeviewer.platform -import android.graphics.BitmapFactory import androidx.compose.runtime.Composable -import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.graphics.asImageBitmap -import androidx.compose.ui.platform.AmbientContext +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight -import java.io.InputStream -import java.net.URL - -@Composable -actual fun imageResource(res: String): ImageBitmap { - val context = AmbientContext.current - val id = context.resources.getIdentifier(res, "drawable", context.packageName) - return androidx.compose.ui.res.imageResource(id) -} - -actual suspend fun imageFromUrl(url: String): ImageBitmap { - val bytes = URL(url).openStream().buffered().use(InputStream::readBytes) - return BitmapFactory.decodeByteArray(bytes, 0, bytes.size).asImageBitmap() -} @Composable actual fun Font(name: String, res: String, weight: FontWeight, style: FontStyle): Font { - val context = AmbientContext.current + val context = LocalContext.current val id = context.resources.getIdentifier(res, "font", context.packageName) - return androidx.compose.ui.text.font.Font(id, weight, style) + return Font(id, weight, style) } \ No newline at end of file diff --git a/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt b/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt deleted file mode 100644 index 938ad87474..0000000000 --- a/examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.jetbrains.codeviewer.platform - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.ui.selection.Selection -import androidx.compose.ui.selection.SelectionContainer -import androidx.compose.ui.text.InternalTextApi - -@OptIn(InternalTextApi::class) -@Composable -actual fun SelectionContainer(children: @Composable () -> Unit) { - val selection = remember { mutableStateOf(null) } - SelectionContainer( - selection = selection.value, - onSelectionChange = { selection.value = it }, - children = children - ) -} \ No newline at end of file diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt index a7aa4956ed..00fa6102a2 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt @@ -1,15 +1,9 @@ package org.jetbrains.codeviewer.platform import androidx.compose.runtime.Composable -import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight -@Composable -expect fun imageResource(res: String): ImageBitmap - -expect suspend fun imageFromUrl(url: String): ImageBitmap - @Composable expect fun Font(name: String, res: String, weight: FontWeight, style: FontStyle): Font \ No newline at end of file diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt deleted file mode 100644 index f6c1c3a96d..0000000000 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt +++ /dev/null @@ -1,6 +0,0 @@ -package org.jetbrains.codeviewer.platform - -import androidx.compose.runtime.Composable - -@Composable -expect fun SelectionContainer(children: @Composable () -> Unit) \ No newline at end of file diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/CodeViewerView.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/CodeViewerView.kt index f5c6c3b29e..cfc8ef796d 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/CodeViewerView.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/CodeViewerView.kt @@ -1,12 +1,13 @@ package org.jetbrains.codeviewer.ui -import androidx.compose.animation.animate import androidx.compose.animation.core.Spring.StiffnessLow import androidx.compose.animation.core.SpringSpec +import androidx.compose.animation.core.animateDpAsState +import androidx.compose.animation.core.animateFloatAsState import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* -import androidx.compose.material.AmbientContentColor import androidx.compose.material.Icon +import androidx.compose.material.LocalContentColor import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowForward @@ -31,10 +32,10 @@ fun CodeViewerView(model: CodeViewer) { val animatedSize = if (panelState.splitter.isResizing) { if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize } else { - animate( + animateDpAsState( if (panelState.isExpanded) panelState.expandedSize else panelState.collapsedSize, SpringSpec(stiffness = StiffnessLow) - ) + ).value } VerticalSplittable( @@ -82,7 +83,7 @@ private fun ResizablePanel( state: PanelState, content: @Composable () -> Unit, ) { - val alpha = animate(if (state.isExpanded) 1f else 0f, SpringSpec(stiffness = StiffnessLow)) + val alpha by animateFloatAsState(if (state.isExpanded) 1f else 0f, SpringSpec(stiffness = StiffnessLow)) Box(modifier) { Box(Modifier.fillMaxSize().graphicsLayer(alpha = alpha)) { @@ -92,7 +93,7 @@ private fun ResizablePanel( Icon( if (state.isExpanded) Icons.Default.ArrowBack else Icons.Default.ArrowForward, contentDescription = if (state.isExpanded) "Collapse" else "Expand", - tint = AmbientContentColor.current, + tint = LocalContentColor.current, modifier = Modifier .padding(top = 4.dp) .width(24.dp) diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt index 30676f1ed6..7c561573d4 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt @@ -45,6 +45,7 @@ fun Editor(file: File) = Editor( fun content(index: Int): Editor.Content { val text = textLines.get(index) + .trim('\r', '\n') // fix for native crash in skia val state = mutableStateOf(text) return Editor.Content(state, isCode) } diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt index 29596032c5..6e62993275 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt @@ -4,8 +4,8 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding -import androidx.compose.material.AmbientContentColor import androidx.compose.material.Icon +import androidx.compose.material.LocalContentColor import androidx.compose.material.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Code @@ -21,13 +21,13 @@ fun EditorEmptyView() = Box(Modifier.fillMaxSize()) { Icon( Icons.Default.Code, contentDescription = null, - tint = AmbientContentColor.current.copy(alpha = 0.60f), + tint = LocalContentColor.current.copy(alpha = 0.60f), modifier = Modifier.align(Alignment.CenterHorizontally) ) Text( "To view file open it from the file tree", - color = AmbientContentColor.current.copy(alpha = 0.60f), + color = LocalContentColor.current.copy(alpha = 0.60f), fontSize = 20.sp, modifier = Modifier.align(Alignment.CenterHorizontally).padding(16.dp) ) diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorTabsView.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorTabsView.kt index 987299bec5..845c48a547 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorTabsView.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorTabsView.kt @@ -1,19 +1,21 @@ package org.jetbrains.codeviewer.ui.editor -import androidx.compose.animation.animate -import androidx.compose.foundation.ScrollableRow import androidx.compose.foundation.clickable +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.material.AmbientContentColor +import androidx.compose.foundation.rememberScrollState import androidx.compose.material.Icon +import androidx.compose.material.LocalContentColor import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Close import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -22,7 +24,7 @@ import androidx.compose.ui.unit.sp import org.jetbrains.codeviewer.ui.common.AppTheme @Composable -fun EditorTabsView(model: Editors) = ScrollableRow { +fun EditorTabsView(model: Editors) = Row(Modifier.horizontalScroll(rememberScrollState())) { for (editor in model.editors) { EditorTabView(editor) } @@ -30,15 +32,15 @@ fun EditorTabsView(model: Editors) = ScrollableRow { @Composable fun EditorTabView(model: Editor) = Surface( - color = animate(if (model.isActive) { + color = if (model.isActive) { AppTheme.colors.backgroundDark } else { Color.Transparent - }) + } ) { Row( Modifier - .clickable { + .clickable(remember(::MutableInteractionSource), indication = null) { model.activate() } .padding(4.dp), @@ -46,7 +48,7 @@ fun EditorTabView(model: Editor) = Surface( ) { Text( model.fileName, - color = AmbientContentColor.current, + color = LocalContentColor.current, fontSize = 12.sp, modifier = Modifier.padding(horizontal = 4.dp) ) @@ -56,7 +58,7 @@ fun EditorTabView(model: Editor) = Surface( if (close != null) { Icon( Icons.Default.Close, - tint = AmbientContentColor.current, + tint = LocalContentColor.current, contentDescription = "Close", modifier = Modifier .size(24.dp) diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt index 0b6c7151f0..5cc09e1bac 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt @@ -2,9 +2,14 @@ package org.jetbrains.codeviewer.ui.editor import androidx.compose.foundation.background import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.text.selection.DisableSelection -import androidx.compose.material.* +import androidx.compose.foundation.text.selection.SelectionContainer +import androidx.compose.material.CircularProgressIndicator +import androidx.compose.material.LocalContentColor +import androidx.compose.material.Surface +import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.key @@ -12,19 +17,16 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha -import androidx.compose.ui.platform.AmbientDensity import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp -import org.jetbrains.codeviewer.platform.SelectionContainer import org.jetbrains.codeviewer.platform.VerticalScrollbar import org.jetbrains.codeviewer.ui.common.AppTheme import org.jetbrains.codeviewer.ui.common.Fonts import org.jetbrains.codeviewer.ui.common.Settings -import org.jetbrains.codeviewer.util.LazyColumnFor import org.jetbrains.codeviewer.util.loadableScoped import org.jetbrains.codeviewer.util.withoutWidthConstraints import kotlin.text.Regex.Companion.fromLiteral @@ -65,7 +67,7 @@ fun EditorView(model: Editor, settings: Settings) = key(model) { } @Composable -private fun Lines(lines: Editor.Lines, settings: Settings) = with(AmbientDensity.current) { +private fun Lines(lines: Editor.Lines, settings: Settings) = with(LocalDensity.current) { val maxNum = remember(lines.lineNumberDigitCount) { (1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" } } @@ -74,16 +76,16 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(AmbientDensity val scrollState = rememberLazyListState() val lineHeight = settings.fontSize.toDp() * 1.6f - LazyColumnFor( - lines.size, + LazyColumn( modifier = Modifier.fillMaxSize(), - state = scrollState, - itemContent = { index -> + state = scrollState + ) { + items(lines.size) { index -> Box(Modifier.height(lineHeight)) { Line(Modifier.align(Alignment.CenterStart), maxNum, lines[index], settings) } } - ) + } VerticalScrollbar( Modifier.align(Alignment.CenterEnd), diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt index d17e5f9262..d5905df35d 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt @@ -2,10 +2,10 @@ package org.jetbrains.codeviewer.ui.filetree import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* -import androidx.compose.foundation.lazy.LazyColumnFor +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.rememberLazyListState -import androidx.compose.material.AmbientContentColor import androidx.compose.material.Icon +import androidx.compose.material.LocalContentColor import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.material.icons.Icons @@ -17,7 +17,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.AmbientDensity +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.TextUnit @@ -35,7 +35,7 @@ fun FileTreeViewTabView() = Surface { ) { Text( "Files", - color = AmbientContentColor.current.copy(alpha = 0.60f), + color = LocalContentColor.current.copy(alpha = 0.60f), fontSize = 12.sp, modifier = Modifier.padding(horizontal = 4.dp) ) @@ -46,18 +46,20 @@ fun FileTreeViewTabView() = Surface { fun FileTreeView(model: FileTree) = Surface( modifier = Modifier.fillMaxSize() ) { - with(AmbientDensity.current) { + with(LocalDensity.current) { Box { val scrollState = rememberLazyListState() val fontSize = 14.sp val lineHeight = fontSize.toDp() * 1.5f - LazyColumnFor( - model.items, + LazyColumn( modifier = Modifier.fillMaxSize().withoutWidthConstraints(), - state = scrollState, - itemContent = { FileTreeItemView(fontSize, lineHeight, it) } - ) + state = scrollState + ) { + items(model.items.size) { + FileTreeItemView(fontSize, lineHeight, model.items[it]) + } + } VerticalScrollbar( Modifier.align(Alignment.CenterEnd), @@ -83,7 +85,7 @@ private fun FileTreeItemView(fontSize: TextUnit, height: Dp, model: FileTree.Ite FileItemIcon(Modifier.align(Alignment.CenterVertically), model) Text( text = model.name, - color = if (active.value) AmbientContentColor.current.copy(alpha = 0.60f) else AmbientContentColor.current, + color = if (active.value) LocalContentColor.current.copy(alpha = 0.60f) else LocalContentColor.current, modifier = Modifier .align(Alignment.CenterVertically) .clipToBounds() @@ -110,10 +112,10 @@ private fun FileItemIcon(modifier: Modifier, model: FileTree.Item) = Box(modifie is FileTree.ItemType.Folder -> when { !type.canExpand -> Unit type.isExpanded -> Icon( - Icons.Default.KeyboardArrowDown, contentDescription = null, tint = AmbientContentColor.current + Icons.Default.KeyboardArrowDown, contentDescription = null, tint = LocalContentColor.current ) else -> Icon( - Icons.Default.KeyboardArrowRight, contentDescription = null, tint = AmbientContentColor.current + Icons.Default.KeyboardArrowRight, contentDescription = null, tint = LocalContentColor.current ) } is FileTree.ItemType.File -> when (type.ext) { diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt index c28293b3a9..d2e29c8d81 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt @@ -1,14 +1,14 @@ package org.jetbrains.codeviewer.ui.statusbar import androidx.compose.foundation.layout.* -import androidx.compose.material.AmbientContentColor +import androidx.compose.material.LocalContentColor import androidx.compose.material.Slider import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.Providers +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.AmbientDensity +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.* import org.jetbrains.codeviewer.ui.common.Settings @@ -26,13 +26,13 @@ fun StatusBar(settings: Settings) = Box( Text( text = "Text size", modifier = Modifier.align(Alignment.CenterVertically), - color = AmbientContentColor.current.copy(alpha = 0.60f), + color = LocalContentColor.current.copy(alpha = 0.60f), fontSize = 12.sp ) Spacer(Modifier.width(8.dp)) - Providers(AmbientDensity provides AmbientDensity.current.scale(0.5f)) { + CompositionLocalProvider(LocalDensity provides LocalDensity.current.scale(0.5f)) { Slider( (settings.fontSize - MinFontSize) / (MaxFontSize - MinFontSize), onValueChange = { settings.fontSize = lerp(MinFontSize, MaxFontSize, it) }, diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/LazyColumnFor.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/LazyColumnFor.kt deleted file mode 100644 index 6b7548b214..0000000000 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/LazyColumnFor.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.jetbrains.codeviewer.util - -import androidx.compose.foundation.layout.PaddingValues -import androidx.compose.foundation.lazy.LazyColumnForIndexed -import androidx.compose.foundation.lazy.LazyItemScope -import androidx.compose.foundation.lazy.LazyListState -import androidx.compose.foundation.lazy.rememberLazyListState -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.unit.dp - -@Composable -fun LazyColumnFor( - size: Int, - modifier: Modifier = Modifier, - state: LazyListState = rememberLazyListState(), - contentPadding: PaddingValues = PaddingValues(0.dp), - horizontalAlignment: Alignment.Horizontal = Alignment.Start, - itemContent: @Composable LazyItemScope.(index: Int) -> Unit -) = LazyColumnForIndexed( - items = UnitList(size), - modifier = modifier, - state = state, - contentPadding = contentPadding, - horizontalAlignment = horizontalAlignment, -) { index, _ -> - itemContent(index) -} - -private class UnitList(override val size: Int) : AbstractList() { - override fun get(index: Int) = Unit -} \ No newline at end of file diff --git a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/VerticalSplittable.kt b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/VerticalSplittable.kt index 76332b3a72..d9b2e0f896 100644 --- a/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/VerticalSplittable.kt +++ b/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/VerticalSplittable.kt @@ -1,7 +1,9 @@ package org.jetbrains.codeviewer.util import androidx.compose.foundation.background +import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.draggable +import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.width @@ -10,9 +12,9 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.gesture.scrollorientationlocking.Orientation import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.Layout +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp @@ -28,7 +30,7 @@ fun VerticalSplittable( ) = Layout({ children() VerticalSplitter(splitterState, onResize) -}, modifier, measureBlock = { measurables, constraints -> +}, modifier, measurePolicy = { measurables, constraints -> require(measurables.size == 3) val firstPlaceable = measurables[0].measure(constraints.copy(minWidth = 0)) @@ -60,6 +62,7 @@ fun VerticalSplitter( onResize: (delta: Dp) -> Unit, color: Color = AppTheme.colors.backgroundDark ) = Box { + val density = LocalDensity.current Box( Modifier .width(8.dp) @@ -67,14 +70,16 @@ fun VerticalSplitter( .run { if (splitterState.isResizeEnabled) { this.draggable( - Orientation.Horizontal, + state = rememberDraggableState { + with(density) { + onResize(it.toDp()) + } + }, + orientation = Orientation.Horizontal, startDragImmediately = true, onDragStarted = { splitterState.isResizing = true }, onDragStopped = { splitterState.isResizing = false } - ) { - onResize(it.toDp()) - } - .cursorForHorizontalResize() + ).cursorForHorizontalResize() } else { this } diff --git a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Mouse.kt b/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Mouse.kt index dbd4584121..32609e72fb 100644 --- a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Mouse.kt +++ b/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Mouse.kt @@ -1,6 +1,6 @@ package org.jetbrains.codeviewer.platform -import androidx.compose.desktop.AppWindowAmbient +import androidx.compose.desktop.LocalAppWindow import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -21,9 +21,9 @@ actual fun Modifier.cursorForHorizontalResize(): Modifier = composed { var isHover by remember { mutableStateOf(false) } if (isHover) { - AppWindowAmbient.current!!.window.cursor = Cursor(Cursor.E_RESIZE_CURSOR) + LocalAppWindow.current.window.cursor = Cursor(Cursor.E_RESIZE_CURSOR) } else { - AppWindowAmbient.current!!.window.cursor = Cursor.getDefaultCursor() + LocalAppWindow.current.window.cursor = Cursor.getDefaultCursor() } pointerMoveFilter( diff --git a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt b/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt index f793c593bb..89efa1ddd3 100644 --- a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt +++ b/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt @@ -1,24 +1,9 @@ package org.jetbrains.codeviewer.platform import androidx.compose.runtime.Composable -import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.jetbrains.skija.Image -import java.io.InputStream -import java.net.URL - -@Composable -actual fun imageResource(res: String) = androidx.compose.ui.res.imageResource("drawable/$res.png") - -actual suspend fun imageFromUrl(url: String): ImageBitmap = withContext(Dispatchers.IO) { - val bytes = URL(url).openStream().buffered().use(InputStream::readBytes) - Image.makeFromEncoded(bytes).asImageBitmap() -} @Composable actual fun Font(name: String, res: String, weight: FontWeight, style: FontStyle): Font = diff --git a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt b/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt deleted file mode 100644 index cc8c8406b0..0000000000 --- a/examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Selection.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.jetbrains.codeviewer.platform - -import androidx.compose.foundation.text.selection.DesktopSelectionContainer -import androidx.compose.runtime.Composable - -@Composable -actual fun SelectionContainer(children: @Composable () -> Unit) { - DesktopSelectionContainer(content = children) -} diff --git a/examples/codeviewer/desktop/src/jvmMain/kotlin/org/jetbrains/codeviewer/main.kt b/examples/codeviewer/desktop/src/jvmMain/kotlin/org/jetbrains/codeviewer/main.kt index 63033b9d96..f30818eb08 100644 --- a/examples/codeviewer/desktop/src/jvmMain/kotlin/org/jetbrains/codeviewer/main.kt +++ b/examples/codeviewer/desktop/src/jvmMain/kotlin/org/jetbrains/codeviewer/main.kt @@ -1,24 +1,17 @@ package org.jetbrains.codeviewer import androidx.compose.desktop.Window -import androidx.compose.foundation.layout.ExperimentalLayout import androidx.compose.ui.unit.IntSize import org.jetbrains.codeviewer.ui.MainView import java.awt.image.BufferedImage import javax.imageio.ImageIO -import javax.swing.SwingUtilities.invokeLater -@OptIn(ExperimentalLayout::class) -fun main() { - invokeLater { - Window( - title = "Code Viewer", - size = IntSize(1280, 768), - icon = loadImageResource("ic_launcher.png") - ) { - MainView() - } - } +fun main() = Window( + title = "Code Viewer", + size = IntSize(1280, 768), + icon = loadImageResource("ic_launcher.png"), +) { + MainView() }