-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multiplatform
: Add desktop support (WIP)
Co-authored-by: Patryk Goworowski <[email protected]>
- Loading branch information
1 parent
fda4d2a
commit 6db5838
Showing
14 changed files
with
239 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
sample/app/src/desktopMain/kotlin/com/patrykandpatrick/vico/sample/Charts.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.sample | ||
|
||
actual val Charts.overridden: LinkedHashMap<UIFramework, List<Chart>>? | ||
get() = null |
24 changes: 24 additions & 0 deletions
24
sample/app/src/desktopMain/kotlin/com/patrykandpatrick/vico/sample/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.sample | ||
|
||
import androidx.compose.ui.window.Window | ||
import androidx.compose.ui.window.application | ||
|
||
fun main() { | ||
application { Window(onCloseRequest = ::exitApplication, title = "Vico") { SampleApp() } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../androidMain/kotlin/com/patrykandpatrick/vico/multiplatform/cartesian/Modifier.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.multiplatform.cartesian | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
|
||
@Composable internal actual fun Modifier.extraPointerInput(scrollState: VicoScrollState) = this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...tform/src/commonMain/kotlin/com/patrykandpatrick/vico/multiplatform/cartesian/Modifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.multiplatform.cartesian | ||
|
||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.gestures.scrollable | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.pointer.PointerEventType | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import com.patrykandpatrick.vico.multiplatform.common.Point | ||
import com.patrykandpatrick.vico.multiplatform.common.detectZoomGestures | ||
|
||
private const val BASE_SCROLL_ZOOM_DELTA = 0.1f | ||
|
||
private fun Offset.toPoint() = Point(x, y) | ||
|
||
@Composable internal expect fun Modifier.extraPointerInput(scrollState: VicoScrollState): Modifier | ||
|
||
@Composable | ||
internal fun Modifier.pointerInput( | ||
scrollState: VicoScrollState, | ||
onPointerPositionChange: ((Point?) -> Unit)?, | ||
onZoom: ((Float, Offset) -> Unit)?, | ||
) = | ||
scrollable( | ||
state = scrollState.scrollableState, | ||
orientation = Orientation.Horizontal, | ||
enabled = scrollState.scrollEnabled, | ||
reverseDirection = true, | ||
) | ||
.pointerInput(onPointerPositionChange, onZoom) { | ||
awaitPointerEventScope { | ||
while (true) { | ||
val event = awaitPointerEvent() | ||
when { | ||
event.type == PointerEventType.Scroll && scrollState.scrollEnabled && onZoom != null -> | ||
onZoom( | ||
1 - event.changes[0].scrollDelta.y * BASE_SCROLL_ZOOM_DELTA, | ||
event.changes[0].position, | ||
) | ||
onPointerPositionChange == null -> continue | ||
event.type == PointerEventType.Press -> | ||
onPointerPositionChange(event.changes[0].position.toPoint()) | ||
event.type == PointerEventType.Release -> onPointerPositionChange(null) | ||
event.type == PointerEventType.Move && !scrollState.scrollEnabled -> | ||
onPointerPositionChange(event.changes[0].position.toPoint()) | ||
} | ||
} | ||
} | ||
if (onZoom == null) return@pointerInput | ||
detectZoomGestures { centroid, zoom -> | ||
onPointerPositionChange?.invoke(null) | ||
onZoom(zoom, centroid) | ||
} | ||
} | ||
.extraPointerInput(scrollState) |
73 changes: 0 additions & 73 deletions
73
...orm/src/commonMain/kotlin/com/patrykandpatrick/vico/multiplatform/cartesian/TouchEvent.kt
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
.../desktopMain/kotlin/com/patrykandpatrick/vico/multiplatform/cartesian/Modifier.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.multiplatform.cartesian | ||
|
||
import androidx.compose.animation.core.AnimationState | ||
import androidx.compose.animation.core.animateDecay | ||
import androidx.compose.animation.rememberSplineBasedDecay | ||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.gestures.draggable | ||
import androidx.compose.foundation.gestures.rememberDraggableState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Modifier | ||
import com.patrykandpatrick.vico.multiplatform.common.getValue | ||
import com.patrykandpatrick.vico.multiplatform.common.rememberWrappedValue | ||
import com.patrykandpatrick.vico.multiplatform.common.setValue | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
|
||
@Composable | ||
internal actual fun Modifier.extraPointerInput(scrollState: VicoScrollState): Modifier { | ||
var scrollJob by rememberWrappedValue<Job?>(null) | ||
val coroutineScope = rememberCoroutineScope() | ||
val animationSpec = rememberSplineBasedDecay<Float>() | ||
return draggable( | ||
state = | ||
rememberDraggableState { delta -> | ||
scrollJob?.cancel() | ||
coroutineScope.launch { scrollState.scroll(Scroll.Relative.pixels(delta)) } | ||
}, | ||
orientation = Orientation.Horizontal, | ||
onDragStopped = { velocity -> | ||
scrollJob = | ||
coroutineScope.launch { | ||
AnimationState(scrollState.value, velocity).animateDecay(animationSpec) { | ||
runBlocking { scrollState.scroll(Scroll.Absolute.pixels(value)) } | ||
} | ||
} | ||
}, | ||
reverseDirection = true, | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
...form/src/iosMain/kotlin/com/patrykandpatrick/vico/multiplatform/cartesian/Modifier.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2025 by Patryk Goworowski and Patrick Michalik. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.patrykandpatrick.vico.multiplatform.cartesian | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
|
||
@Composable internal actual fun Modifier.extraPointerInput(scrollState: VicoScrollState) = this |