forked from Gurupreet/ComposeCookBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifierExtensions.kt
More file actions
159 lines (152 loc) · 4.73 KB
/
ModifierExtensions.kt
File metadata and controls
159 lines (152 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.guru.composecookbook.theme.modifiers
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TileMode
fun Modifier.horizontalGradientBackground(
colors: List<Color>
) = gradientBackground(colors) { gradientColors, size ->
Brush.horizontalGradient(
colors = gradientColors,
startX = 0f,
endX = size.width
)
}
fun Modifier.verticalGradientBackground(
colors: List<Color>
) = gradientBackground(colors) { gradientColors, size ->
Brush.verticalGradient(
colors = gradientColors,
startY = 0f,
endY = size.width
)
}
fun Modifier.diagonalGradientTint(
colors: List<Color>,
blendMode: BlendMode
) = gradientTint(colors, blendMode) { gradientColors, size ->
Brush.linearGradient(
colors = gradientColors,
start = Offset(
x = 0f,
y = 0f
),
end = Offset(
x = size.width,
y = size.height
),
tileMode = TileMode.Clamp
)
}
fun Modifier.gradientBackground(
colors: List<Color>,
brushProvider: (List<Color>, Size) -> Brush
): Modifier = composed {
var size by remember { mutableStateOf(Size.Zero) }
val gradient = remember(colors, size) { brushProvider(colors, size) }
drawWithContent {
size = this.size
drawRect(brush = gradient)
drawContent()
}
}
fun Modifier.gradientTint(
colors: List<Color>,
blendMode: BlendMode,
brushProvider: (List<Color>, Size) -> Brush
) = composed {
var size by remember { mutableStateOf(Size.Zero) }
val gradient = remember(colors, size) { brushProvider(colors, size) }
drawWithContent {
drawContent()
size = this.size
drawRect(
brush = gradient,
blendMode = blendMode
)
}
}
//TODO fix drag obervers
//fun Modifier.swipeGesture(
// swipeValue: AnimatedFloat,
// swipeDirection: Direction = Direction.LEFT,
// maxSwipe: Float,
// onItemSwiped: () -> Unit
//): Modifier = composed {
// (this then dragGestureFilter(
// canDrag = { it == swipeDirection },
// dragObserver = dragObserver(
// swipeValue = swipeValue,
// maxSwipe = maxSwipe,
// onItemSwiped = onItemSwiped
// )
// )).then(object : LayoutModifier {
// override fun MeasureScope.measure(
// measurable: Measurable,
// constraints: Constraints
// ): MeasureResult {
// val children = measurable.measure(constraints)
// // swipeValue.setBounds(-children.width.toFloat()-100f, children.width.toFloat()+100f)
// return layout(children.width, children.height) {
// children.place(swipeValue.value.toInt(), 0)
// }
// }
// })
//}
//
//@Composable
//fun dragObserver(
// swipeValue: AnimatedFloat,
// maxSwipe: Float,
// onItemSwiped: () -> Unit
//): DragObserver {
//
// return object : DragObserver {
// override fun onStart(downPosition: Offset) {
// // swipeValue.setBounds(-maxSwipe, maxSwipe)
// }
//
// private fun reset() {
// swipeValue.animateTo(
// 0f,
// anim = SpringSpec(
// dampingRatio = 0.8f, stiffness = 300f
// )
// )
// }
//
// override fun onDrag(dragDistance: Offset): Offset {
// swipeValue.snapTo(swipeValue.targetValue + dragDistance.x)
// return dragDistance
// }
//
// override fun onStop(velocity: Offset) {
// if (abs(swipeValue.targetValue) < 400f) {
// reset()
// } else {
// val animateTo = if (swipeValue.value > 0) maxSwipe else -maxSwipe
// swipeValue.animateTo(
// animateTo,
// anim = SpringSpec<Float>(
// dampingRatio = 0.8f, stiffness = 300f
// ),
// onEnd = { _, _ ->
// // On swiped do something
// // onItemSwiped.invoke()
// }
// )
// // actually it should be in animation end but it's bit slow animation I put it out.
// onItemSwiped.invoke()
// }
// }
// }
//}