All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.me.saket.telephoto.zoomable.internal.dimens.kt Maven / Gradle / Ivy

There is a newer version: 0.14.0
Show newest version
package me.saket.telephoto.zoomable.internal

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.withTransform
import androidx.compose.ui.layout.ScaleFactor
import androidx.compose.ui.unit.IntSize
import kotlin.math.roundToInt

internal fun Size.roundToIntSize() =
  IntSize(width.roundToInt(), height.roundToInt())

internal operator fun Size.times(scale: ScaleFactor) =
  Size(
    width = width * scale.scaleX,
    height = height * scale.scaleY,
  )

internal fun Size.discardFractionalParts(): IntSize {
  return IntSize(width = width.toInt(), height = height.toInt())
}

internal val ScaleFactor.maxScale: Float
  get() = maxOf(scaleX, scaleY)

internal operator fun ScaleFactor.unaryMinus(): ScaleFactor =
  this * -1f

internal val ScaleFactor.Companion.Zero
  get() = ScaleFactor(0f, 0f)

internal val TransformOrigin.Companion.Zero
  get() = TransformOrigin(0f, 0f)

internal operator fun Offset.times(factor: ScaleFactor) =
  Offset(x = x * factor.scaleX, y = y * factor.scaleY)

internal operator fun Offset.div(factor: ScaleFactor) =
  Offset(x = x / factor.scaleX, y = y / factor.scaleY)

/**
 * Call [action] with [zoom] and [translate] applied to this offset. The value
 * generated by [action] is returned by applying the inverse of [translate] of [zoom].
 *
 * The name of this function was inspired from [DrawScope.withTransform].
 */
internal fun Offset.withZoomAndTranslate(
  zoom: ScaleFactor,
  translate: Offset,
  action: (Offset) -> Offset,
): Offset {
  return (action((this * zoom) + translate) - translate) / zoom
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy