Browse Source

Update widgets gallery example to 0.3.0-build154

pull/398/head
Alexey Tsvetkov 4 years ago committed by Alexey Tsvetkov
parent
commit
1c0fb3a1e5
  1. 7
      examples/widgetsgallery/android/build.gradle.kts
  2. 2
      examples/widgetsgallery/android/src/main/java/org/jetbrains/compose/demo/widgets/MainActivity.kt
  3. 4
      examples/widgetsgallery/build.gradle.kts
  4. 6
      examples/widgetsgallery/common/src/androidMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Resources.kt
  5. 35
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/MainView.kt
  6. 29
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt
  7. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/AppBars.kt
  8. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Chips.kt
  9. 2
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Toggles.kt
  10. 3
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/UICards.kt
  11. 13
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/ResizablePanel.kt
  12. 18
      examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/VerticalSplittable.kt
  13. 6
      examples/widgetsgallery/common/src/desktopMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Mouse.kt
  14. 2
      examples/widgetsgallery/desktop/src/jvmMain/kotlin/org/jetbrains/compose/demo/widgets/main.kt

7
examples/widgetsgallery/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")
}
}

2
examples/widgetsgallery/android/src/main/java/org/jetbrains/compose/demo/widgets/MainActivity.kt

@ -1,8 +1,8 @@
package org.jetbrains.compose.demo.widgets
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.platform.setContent
import org.jetbrains.compose.demo.widgets.ui.MainView
class MainActivity : AppCompatActivity() {

4
examples/widgetsgallery/build.gradle.kts

@ -9,10 +9,10 @@ buildscript {
dependencies {
// __LATEST_COMPOSE_RELEASE_VERSION__
classpath("org.jetbrains.compose:compose-gradle-plugin:0.3.0-build146")
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"))
}
}

6
examples/widgetsgallery/common/src/androidMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Resources.kt

@ -3,18 +3,20 @@ package org.jetbrains.compose.demo.widgets.platform
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.vectorResource
import org.jetbrains.compose.demo.widgets.R
@Composable
actual fun imageResource(res: String): ImageBitmap {
val id = drawableId(res)
return androidx.compose.ui.res.imageResource(id)
return ImageBitmap.imageResource(id)
}
@Composable
actual fun vectorResource(res: String): ImageVector {
val id = drawableId(res)
return androidx.compose.ui.res.vectorResource(id)
return ImageVector.vectorResource(id)
}
// TODO: improve resource loading

35
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/MainView.kt

@ -1,21 +1,22 @@
package org.jetbrains.compose.demo.widgets.ui
import androidx.compose.animation.animate
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec
import androidx.compose.animation.core.animateDpAsState
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.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.AmbientContentColor
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.material.LocalContentColor
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.platform.AmbientDensity
import androidx.compose.ui.selection.DisableSelection
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
@ -46,10 +47,10 @@ fun WidgetsPanel() {
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 = Spring.StiffnessLow)
)
).value
}
VerticalSplittable(
@ -82,24 +83,26 @@ fun WidgetsPanel() {
@Composable
private fun WidgetsListView(widgetsTypeState: MutableState<WidgetsType>) {
Box {
with(AmbientDensity.current) {
with(LocalDensity.current) {
val scrollState = rememberLazyListState()
val fontSize = 14.sp
val lineHeight = fontSize.toDp() * 1.5f
val items = WidgetsType.sortedValues
LazyColumnFor(
items,
val sortedItems = WidgetsType.sortedValues
LazyColumn(
modifier = Modifier.fillMaxSize().withoutWidthConstraints(),
state = scrollState,
itemContent = { WidgetsListItemViewImpl(it, widgetsTypeState, fontSize, lineHeight) }
)
state = scrollState
) {
items(sortedItems) {
WidgetsListItemViewImpl(it, widgetsTypeState, fontSize, lineHeight)
}
}
VerticalScrollbar(
Modifier.align(Alignment.CenterEnd),
scrollState,
items.size,
sortedItems.size,
lineHeight
)
}
@ -124,7 +127,7 @@ private fun WidgetsListItemViewImpl(
.padding(start = 16.dp)
) {
var inFocus by remember { mutableStateOf(false) }
val textColor = AmbientContentColor.current.let {
val textColor = LocalContentColor.current.let {
when {
isCurrent -> it
inFocus -> it.copy(alpha = 0.6f)

29
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt

@ -1,7 +1,8 @@
package org.jetbrains.compose.demo.widgets.ui
import androidx.compose.foundation.ScrollableColumn
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
@ -12,20 +13,18 @@ fun WidgetsView(
widgetsTypeState: MutableState<WidgetsType>,
modifier: Modifier
) {
ScrollableColumn(modifier = modifier) {
Column {
@Suppress("UNUSED_VARIABLE")
val exhaustive = when (widgetsTypeState.value) {
WidgetsType.APP_BARS -> AppBars()
WidgetsType.BUTTONS -> Buttons()
WidgetsType.CHIPS -> Chips()
WidgetsType.LOADERS -> Loaders()
WidgetsType.SNACK_BARS -> SnackBars()
WidgetsType.TEXT_VIEWS -> TextViews()
WidgetsType.TEXT_INPUTS -> TextInputs()
WidgetsType.TOGGLES -> Toggles()
WidgetsType.UI_CARDS -> UICards()
}
Column(modifier = modifier.verticalScroll(state = rememberScrollState())) {
@Suppress("UNUSED_VARIABLE")
val exhaustive = when (widgetsTypeState.value) {
WidgetsType.APP_BARS -> AppBars()
WidgetsType.BUTTONS -> Buttons()
WidgetsType.CHIPS -> Chips()
WidgetsType.LOADERS -> Loaders()
WidgetsType.SNACK_BARS -> SnackBars()
WidgetsType.TEXT_VIEWS -> TextViews()
WidgetsType.TEXT_INPUTS -> TextInputs()
WidgetsType.TOGGLES -> Toggles()
WidgetsType.UI_CARDS -> UICards()
}
}
}

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/AppBars.kt

@ -81,7 +81,7 @@ private fun TopAppBarsDemo() {
imageResource(Res.drawable.p6),
contentDescription = "",
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
.preferredSize(32.dp).clip(CircleShape)
.requiredSize(32.dp).clip(CircleShape)
)
},
actions = {

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Chips.kt

@ -87,7 +87,7 @@ private fun CustomImageChip(
Image(
imageResource(imageId),
contentDescription = null,
modifier = Modifier.padding(8.dp).preferredSize(20.dp).clip(CircleShape)
modifier = Modifier.padding(8.dp).requiredSize(20.dp).clip(CircleShape)
)
Text(
text = text,

2
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/Toggles.kt

@ -67,7 +67,7 @@ fun Toggles() {
Slider(value = sliderState2, modifier = Modifier.fillMaxWidth().padding(8.dp),
valueRange = 0f..100f,
steps = 5,
thumbColor = MaterialTheme.colors.secondary,
colors = SliderDefaults.colors(thumbColor = MaterialTheme.colors.secondary),
onValueChange = { newValue ->
sliderState2 = newValue
}

3
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/screens/UICards.kt

@ -15,6 +15,7 @@ import org.jetbrains.compose.demo.widgets.platform.Res
import org.jetbrains.compose.demo.widgets.platform.imageResource
import org.jetbrains.compose.demo.widgets.theme.typography
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun UICards() {
val item = remember { DemoDataProvider.item }
@ -54,7 +55,7 @@ fun UICards() {
Image(
imageResource(Res.drawable.p3),
contentDescription = null,
modifier = Modifier.preferredSize(60.dp)
modifier = Modifier.requiredSize(60.dp)
)
Text(text = item.title, modifier = Modifier.padding(16.dp))
}

13
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/ResizablePanel.kt

@ -1,13 +1,11 @@
package org.jetbrains.compose.demo.widgets.ui.utils
import androidx.compose.animation.animate
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
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.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
@ -38,7 +36,10 @@ fun ResizablePanel(
title: String,
content: @Composable () -> Unit,
) {
val alpha = animate(if (state.isExpanded) 1f else 0f, SpringSpec(stiffness = Spring.StiffnessLow),)
val alpha = animateFloatAsState(
if (state.isExpanded) 1f else 0f,
SpringSpec(stiffness = Spring.StiffnessLow),
).value
Box(modifier) {
Column {
@ -56,7 +57,7 @@ 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
.size(24.dp)
.padding(end = 8.dp)

18
examples/widgetsgallery/common/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/utils/VerticalSplittable.kt

@ -2,6 +2,8 @@ package org.jetbrains.compose.demo.widgets.ui.utils
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.Orientation
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
@ -32,7 +34,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))
@ -61,6 +63,7 @@ fun VerticalSplitter(
onResize: (delta: Dp) -> Unit,
color: Color = Color.DarkGray
) = Box {
val density = LocalDensity.current
Box(
Modifier
.width(8.dp)
@ -68,13 +71,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
}

6
examples/widgetsgallery/common/src/desktopMain/kotlin/org/jetbrains/compose/demo/widgets/platform/Mouse.kt

@ -1,6 +1,6 @@
package org.jetbrains.compose.demo.widgets.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(

2
examples/widgetsgallery/desktop/src/jvmMain/kotlin/org/jetbrains/compose/demo/widgets/main.kt

@ -1,14 +1,12 @@
package org.jetbrains.compose.demo.widgets
import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.ExperimentalLayout
import androidx.compose.ui.unit.IntSize
import org.jetbrains.compose.demo.widgets.ui.MainView
import java.awt.Dimension
import java.awt.Toolkit
import javax.swing.SwingUtilities.invokeLater
@OptIn(ExperimentalLayout::class)
fun main() {
invokeLater {
Window(

Loading…
Cancel
Save