Browse Source

Codeviewer: refactoring

pull/123/head
Igor Demin 4 years ago
parent
commit
6e1d965931
  1. 2
      examples/codeviewer/build.gradle.kts
  2. 16
      examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt
  3. 6
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt
  4. 3
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt
  5. 12
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt
  6. 4
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt
  7. 4
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt
  8. 14
      examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/State.kt
  9. 8
      examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt

2
examples/codeviewer/build.gradle.kts

@ -9,7 +9,7 @@ buildscript {
dependencies { dependencies {
// __LATEST_COMPOSE_RELEASE_VERSION__ // __LATEST_COMPOSE_RELEASE_VERSION__
classpath("org.jetbrains.compose:compose-gradle-plugin:0.1.0-build113") classpath("org.jetbrains.compose:compose-gradle-plugin:0.2.0-build124")
classpath("com.android.tools.build:gradle:4.0.1") classpath("com.android.tools.build:gradle:4.0.1")
classpath(kotlin("gradle-plugin", version = "1.4.0")) classpath(kotlin("gradle-plugin", version = "1.4.0"))
} }

16
examples/codeviewer/common/src/androidMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt

@ -2,9 +2,9 @@ package org.jetbrains.codeviewer.platform
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageAsset import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageAsset import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.ContextAmbient import androidx.compose.ui.platform.AmbientContext
import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
@ -12,20 +12,20 @@ import java.io.InputStream
import java.net.URL import java.net.URL
@Composable @Composable
actual fun imageResource(res: String): ImageAsset { actual fun imageResource(res: String): ImageBitmap {
val context = ContextAmbient.current val context = AmbientContext.current
val id = context.resources.getIdentifier(res, "drawable", context.packageName) val id = context.resources.getIdentifier(res, "drawable", context.packageName)
return androidx.compose.ui.res.imageResource(id) return androidx.compose.ui.res.imageResource(id)
} }
actual suspend fun imageFromUrl(url: String): ImageAsset { actual suspend fun imageFromUrl(url: String): ImageBitmap {
val bytes = URL(url).openStream().buffered().use(InputStream::readBytes) val bytes = URL(url).openStream().buffered().use(InputStream::readBytes)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size).asImageAsset() return BitmapFactory.decodeByteArray(bytes, 0, bytes.size).asImageBitmap()
} }
@Composable @Composable
actual fun font(name: String, res: String, weight: FontWeight, style: FontStyle): Font { actual fun font(name: String, res: String, weight: FontWeight, style: FontStyle): Font {
val context = ContextAmbient.current val context = AmbientContext.current
val id = context.resources.getIdentifier(res, "font", context.packageName) val id = context.resources.getIdentifier(res, "font", context.packageName)
return androidx.compose.ui.text.font.font(id, weight, style) return androidx.compose.ui.text.font.font(id, weight, style)
} }

6
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt

@ -1,15 +1,15 @@
package org.jetbrains.codeviewer.platform package org.jetbrains.codeviewer.platform
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageAsset import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
@Composable @Composable
expect fun imageResource(res: String): ImageAsset expect fun imageResource(res: String): ImageBitmap
expect suspend fun imageFromUrl(url: String): ImageAsset expect suspend fun imageFromUrl(url: String): ImageBitmap
@Composable @Composable
expect fun font(name: String, res: String, weight: FontWeight, style: FontStyle): Font expect fun font(name: String, res: String, weight: FontWeight, style: FontStyle): Font

3
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/Editor.kt

@ -6,7 +6,6 @@ import kotlinx.coroutines.CoroutineScope
import org.jetbrains.codeviewer.platform.File import org.jetbrains.codeviewer.platform.File
import org.jetbrains.codeviewer.util.EmptyTextLines import org.jetbrains.codeviewer.util.EmptyTextLines
import org.jetbrains.codeviewer.util.SingleSelection import org.jetbrains.codeviewer.util.SingleSelection
import org.jetbrains.codeviewer.util.afterSet
class Editor( class Editor(
val fileName: String, val fileName: String,
@ -27,7 +26,7 @@ class Editor(
interface Lines { interface Lines {
val lineNumberDigitCount: Int get() = size.toString().length val lineNumberDigitCount: Int get() = size.toString().length
val size: Int val size: Int
fun get(index: Int): Line operator fun get(index: Int): Line
} }
class Content(val value: State<String>, val isCode: Boolean) class Content(val value: State<String>, val isCode: Boolean)

12
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorView.kt

@ -14,7 +14,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawOpacity import androidx.compose.ui.draw.drawOpacity
import androidx.compose.ui.platform.DensityAmbient import androidx.compose.ui.platform.AmbientDensity
import androidx.compose.ui.selection.DisableSelection import androidx.compose.ui.selection.DisableSelection
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.SpanStyle
@ -27,14 +27,13 @@ import org.jetbrains.codeviewer.ui.common.AppTheme
import org.jetbrains.codeviewer.ui.common.Fonts import org.jetbrains.codeviewer.ui.common.Fonts
import org.jetbrains.codeviewer.ui.common.Settings import org.jetbrains.codeviewer.ui.common.Settings
import org.jetbrains.codeviewer.util.LazyColumnFor import org.jetbrains.codeviewer.util.LazyColumnFor
import org.jetbrains.codeviewer.util.loadable
import org.jetbrains.codeviewer.util.loadableScoped import org.jetbrains.codeviewer.util.loadableScoped
import org.jetbrains.codeviewer.util.withoutWidthConstraints import org.jetbrains.codeviewer.util.withoutWidthConstraints
import kotlin.text.Regex.Companion.fromLiteral import kotlin.text.Regex.Companion.fromLiteral
@Composable @Composable
fun EditorView(model: Editor, settings: Settings) = key(model) { fun EditorView(model: Editor, settings: Settings) = key(model) {
with (DensityAmbient.current) { with (AmbientDensity.current) {
SelectionContainer { SelectionContainer {
Surface( Surface(
Modifier.fillMaxSize(), Modifier.fillMaxSize(),
@ -68,7 +67,7 @@ fun EditorView(model: Editor, settings: Settings) = key(model) {
} }
@Composable @Composable
private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient.current) { private fun Lines(lines: Editor.Lines, settings: Settings) = with(AmbientDensity.current) {
val maxNum = remember(lines.lineNumberDigitCount) { val maxNum = remember(lines.lineNumberDigitCount) {
(1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" } (1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" }
} }
@ -82,11 +81,8 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
state = scrollState, state = scrollState,
itemContent = { index -> itemContent = { index ->
val line: Editor.Line? by loadable { lines.get(index) }
Box(Modifier.height(lineHeight)) { Box(Modifier.height(lineHeight)) {
if (line != null) { Line(Modifier.align(Alignment.CenterStart), maxNum, lines[index], settings)
Line(Modifier.align(Alignment.CenterStart), maxNum, line!!, settings)
}
} }
} }
) )

4
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/filetree/FileTreeView.kt

@ -17,7 +17,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.DensityAmbient import androidx.compose.ui.platform.AmbientDensity
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.TextUnit
@ -46,7 +46,7 @@ fun FileTreeViewTabView() = Surface {
fun FileTreeView(model: FileTree) = Surface( fun FileTreeView(model: FileTree) = Surface(
modifier = Modifier.fillMaxSize() modifier = Modifier.fillMaxSize()
) { ) {
with(DensityAmbient.current) { with(AmbientDensity.current) {
Box { Box {
val scrollState = rememberLazyListState() val scrollState = rememberLazyListState()
val fontSize = 14.sp val fontSize = 14.sp

4
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/statusbar/StatusBar.kt

@ -8,7 +8,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.Providers import androidx.compose.runtime.Providers
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.DensityAmbient import androidx.compose.ui.platform.AmbientDensity
import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.lerp import androidx.compose.ui.unit.lerp
@ -35,7 +35,7 @@ fun StatusBar(settings: Settings) = Box(
Spacer(Modifier.width(8.dp)) Spacer(Modifier.width(8.dp))
Providers(DensityAmbient provides DensityAmbient.current.scale(0.5f)) { Providers(AmbientDensity provides AmbientDensity.current.scale(0.5f)) {
Slider( Slider(
(settings.fontSize - MinFontSize) / (MaxFontSize - MinFontSize), (settings.fontSize - MinFontSize) / (MaxFontSize - MinFontSize),
onValueChange = { settings.fontSize = lerp(MinFontSize, MaxFontSize, it) }, onValueChange = { settings.fontSize = lerp(MinFontSize, MaxFontSize, it) },

14
examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/util/State.kt

@ -1,14 +0,0 @@
package org.jetbrains.codeviewer.util
import androidx.compose.runtime.MutableState
fun <T> MutableState<T>.afterSet(
action: (T) -> Unit
) = object : MutableState<T> by this {
override var value: T
get() = this@afterSet.value
set(value) {
this@afterSet.value = value
action(value)
}
}

8
examples/codeviewer/common/src/desktopMain/kotlin/org/jetbrains/codeviewer/platform/Resources.kt

@ -1,8 +1,8 @@
package org.jetbrains.codeviewer.platform package org.jetbrains.codeviewer.platform
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageAsset import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageAsset import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
@ -15,9 +15,9 @@ import java.net.URL
@Composable @Composable
actual fun imageResource(res: String) = androidx.compose.ui.res.imageResource("drawable/$res.png") actual fun imageResource(res: String) = androidx.compose.ui.res.imageResource("drawable/$res.png")
actual suspend fun imageFromUrl(url: String): ImageAsset = withContext(Dispatchers.IO) { actual suspend fun imageFromUrl(url: String): ImageBitmap = withContext(Dispatchers.IO) {
val bytes = URL(url).openStream().buffered().use(InputStream::readBytes) val bytes = URL(url).openStream().buffered().use(InputStream::readBytes)
Image.makeFromEncoded(bytes).asImageAsset() Image.makeFromEncoded(bytes).asImageBitmap()
} }
@Composable @Composable

Loading…
Cancel
Save