Skip to content

Commit

Permalink
Added choosing between crop/fit in Aspect ratio preset by #1149
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jul 14, 2024
1 parent f5034bc commit 5b337b4
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ internal class AndroidImageTransformer @Inject constructor(
val newWidth: Float
val newHeight: Float

if (preset.ratio > originalWidth / originalHeight) {
val condition = if (preset.isFit) preset.ratio > originalWidth / originalHeight
else preset.ratio < originalWidth / originalHeight

if (condition) {
newWidth = originalHeight * preset.ratio
newHeight = originalHeight
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ sealed class Preset {

data class Percentage(val value: Int) : Preset()

data class AspectRatio(val ratio: Float) : Preset()
data class AspectRatio(
val ratio: Float,
val isFit: Boolean
) : Preset()

fun isTelegram(): Boolean = this is Telegram

fun value(): Int? = (this as? Percentage)?.value

fun ratio(): Float? = (this as? AspectRatio)?.ratio

fun isEmpty(): Boolean = this is None

fun isAspectRatio(): Boolean = this is AspectRatio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ internal fun <T> FilterItemContent(
end = 12.dp,
bottom = 12.dp
),
applyHorPadding = false,
applyHorizontalPadding = false,
startContent = {},
resultModifier = Modifier.padding(
horizontal = 16.dp,
Expand Down
2 changes: 2 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,6 @@
<string name="sand_painting">Sand Painting</string>
<string name="image_splitting">Image Splitting</string>
<string name="image_splitting_sub">Split single image by rows or columns</string>
<string name="fit_to_bounds">Fit To Bounds</string>
<string name="fit_to_bounds_sub">Combine crop resize mode with this parameter to achieve desired behavior (Crop/Fit to aspect ratio)</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -203,45 +203,48 @@ fun ResizeTypeSelector(
enter = fadeIn() + expandVertically(),
exit = fadeOut() + shrinkVertically()
) {
UseBlurredBackgroundToggle(
modifier = Modifier.padding(top = 8.dp, start = 8.dp, end = 8.dp, bottom = 8.dp),
checked = useBlurredBgInsteadOfColor,
onCheckedChange = {
useBlurredBgInsteadOfColor = it
updateResizeType()
}
)
}
AnimatedVisibility(
visible = value is ResizeType.CenterCrop,
enter = fadeIn() + expandVertically(),
exit = fadeOut() + shrinkVertically()
) {
AnimatedContent(targetState = useBlurredBgInsteadOfColor) { showBlurRadius ->
if (!showBlurRadius) {
BackgroundColorSelector(
modifier = Modifier
.padding(bottom = 8.dp, end = 8.dp, start = 8.dp)
.container(
shape = RoundedCornerShape(16.dp),
color = MaterialTheme.colorScheme.surface
),
value = canvasColor,
onColorChange = {
canvasColor = it
updateResizeType()
}
)
} else {
BlurRadiusSelector(
modifier = Modifier.padding(bottom = 8.dp, end = 8.dp, start = 8.dp),
value = blurRadius,
color = MaterialTheme.colorScheme.surface,
onValueChange = {
blurRadius = it
updateResizeType()
}
)
Column {
UseBlurredBackgroundToggle(
modifier = Modifier.padding(
top = 8.dp,
start = 8.dp,
end = 8.dp,
bottom = 4.dp
),
checked = useBlurredBgInsteadOfColor,
onCheckedChange = {
useBlurredBgInsteadOfColor = it
updateResizeType()
},
shape = ContainerShapeDefaults.topShape
)
AnimatedContent(targetState = useBlurredBgInsteadOfColor) { showBlurRadius ->
if (showBlurRadius) {
BlurRadiusSelector(
modifier = Modifier.padding(bottom = 8.dp, end = 8.dp, start = 8.dp),
value = blurRadius,
color = MaterialTheme.colorScheme.surface,
onValueChange = {
blurRadius = it
updateResizeType()
},
shape = ContainerShapeDefaults.bottomShape
)
} else {
BackgroundColorSelector(
modifier = Modifier
.padding(bottom = 8.dp, end = 8.dp, start = 8.dp)
.container(
shape = ContainerShapeDefaults.bottomShape,
color = MaterialTheme.colorScheme.surface
),
value = canvasColor,
onColorChange = {
canvasColor = it
updateResizeType()
},
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.material.icons.rounded.BlurLinear
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import ru.tech.imageresizershrinker.core.resources.R
Expand All @@ -32,14 +33,15 @@ import ru.tech.imageresizershrinker.core.ui.widget.preferences.PreferenceRowSwit
fun UseBlurredBackgroundToggle(
modifier: Modifier = Modifier,
checked: Boolean,
onCheckedChange: (Boolean) -> Unit
onCheckedChange: (Boolean) -> Unit,
shape: Shape = RoundedCornerShape(16.dp)
) {
PreferenceRowSwitch(
modifier = modifier,
title = stringResource(R.string.blur_edges),
subtitle = stringResource(R.string.blur_edges_sub),
checked = checked,
shape = RoundedCornerShape(16.dp),
shape = shape,
color = MaterialTheme.colorScheme.surface,
onClick = onCheckedChange,
startIcon = Icons.Rounded.BlurLinear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.FitScreen
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.rounded.AspectRatio
import androidx.compose.material3.AlertDialog
Expand Down Expand Up @@ -75,13 +76,15 @@ import ru.tech.imageresizershrinker.core.ui.widget.buttons.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.buttons.SupportingButton
import ru.tech.imageresizershrinker.core.ui.widget.controls.OOMWarning
import ru.tech.imageresizershrinker.core.ui.widget.image.AspectRatioSelector
import ru.tech.imageresizershrinker.core.ui.widget.modifier.ContainerShapeDefaults
import ru.tech.imageresizershrinker.core.ui.widget.modifier.alertDialogBorder
import ru.tech.imageresizershrinker.core.ui.widget.modifier.container
import ru.tech.imageresizershrinker.core.ui.widget.modifier.fadingEdges
import ru.tech.imageresizershrinker.core.ui.widget.other.RevealDirection
import ru.tech.imageresizershrinker.core.ui.widget.other.RevealValue
import ru.tech.imageresizershrinker.core.ui.widget.other.SwipeToReveal
import ru.tech.imageresizershrinker.core.ui.widget.other.rememberRevealState
import ru.tech.imageresizershrinker.core.ui.widget.preferences.PreferenceRowSwitch
import ru.tech.imageresizershrinker.core.ui.widget.text.AutoSizeText
import ru.tech.imageresizershrinker.core.ui.widget.text.RoundedTextField
import ru.tech.imageresizershrinker.core.ui.widget.text.RoundedTextFieldColors
Expand Down Expand Up @@ -166,30 +169,53 @@ fun PresetSelector(
DomainAspectRatio.defaultList.drop(3)
}

AspectRatioSelector(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp)
.padding(bottom = 8.dp),
contentPadding = PaddingValues(4.dp),
selectedAspectRatio = remember(value, aspectRatios) {
derivedStateOf {
aspectRatios.firstOrNull {
it.value == value.ratio()
Column(
modifier = Modifier.padding(horizontal = 8.dp)
) {
AspectRatioSelector(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(8.dp),
selectedAspectRatio = remember(value, aspectRatios) {
derivedStateOf {
aspectRatios.firstOrNull {
it.value == (value as? Preset.AspectRatio)?.ratio
}
}
}
}.value,
onAspectRatioChange = { domainAspectRatio, _ ->
onValueChange(
Preset.AspectRatio(domainAspectRatio.value)
)
},
title = {},
aspectRatios = aspectRatios,
shape = RoundedCornerShape(18.dp),
color = MaterialTheme.colorScheme.surfaceContainerLowest,
unselectedCardColor = MaterialTheme.colorScheme.surface
)
}.value,
onAspectRatioChange = { domainAspectRatio, _ ->
onValueChange(
Preset.AspectRatio(
ratio = domainAspectRatio.value,
isFit = (value as? Preset.AspectRatio)?.isFit ?: false
)
)
},
title = {},
aspectRatios = aspectRatios,
shape = ContainerShapeDefaults.topShape,
color = MaterialTheme.colorScheme.surface,
unselectedCardColor = MaterialTheme.colorScheme.surfaceContainerHigh
)
Spacer(modifier = Modifier.height(4.dp))
PreferenceRowSwitch(
modifier = Modifier.fillMaxWidth(),
title = stringResource(R.string.fit_to_bounds),
subtitle = stringResource(R.string.fit_to_bounds_sub),
checked = (value as? Preset.AspectRatio)?.isFit ?: false,
onClick = {
onValueChange(
Preset.AspectRatio(
ratio = (value as? Preset.AspectRatio)?.ratio ?: 1f,
isFit = it
)
)
},
startIcon = Icons.Outlined.FitScreen,
shape = ContainerShapeDefaults.bottomShape,
color = MaterialTheme.colorScheme.surface
)
Spacer(modifier = Modifier.height(8.dp))
}
}

Box(
Expand Down Expand Up @@ -229,7 +255,12 @@ fun PresetSelector(
EnhancedChip(
selected = selected,
onClick = {
onValueChange(Preset.AspectRatio(1f))
onValueChange(
Preset.AspectRatio(
ratio = 1f,
isFit = false
)
)
},
selectedColor = MaterialTheme.colorScheme.primary,
shape = MaterialTheme.shapes.medium
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun PreferenceRow(
enabled: Boolean = true,
shape: Shape = RoundedCornerShape(16.dp),
contentColor: Color? = null,
applyHorPadding: Boolean = true,
applyHorizontalPadding: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
startContent: (@Composable () -> Unit)? = null,
endContent: (@Composable () -> Unit)? = null,
Expand Down Expand Up @@ -101,7 +101,7 @@ fun PreferenceRow(
Row(
modifier = modifier
.then(
if (applyHorPadding) {
if (applyHorizontalPadding) {
Modifier.padding(horizontal = 16.dp)
} else Modifier
)
Expand Down Expand Up @@ -253,7 +253,7 @@ fun PreferenceRow(
bottom = 8.dp
)
} else Modifier.padding(16.dp),
applyHorPadding = false,
applyHorizontalPadding = false,
onClick = onClick
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun PreferenceRowSwitch(
enabled: Boolean = true,
subtitle: String? = null,
autoShadowElevation: Dp = 1.dp,
applyHorPadding: Boolean = true,
applyHorizontalPadding: Boolean = true,
checked: Boolean,
color: Color = Color.Unspecified,
contentColor: Color? = null,
Expand All @@ -55,17 +55,17 @@ fun PreferenceRowSwitch(
vertical = 8.dp
),
drawStartIconContainer: Boolean = false,
changeAlphaWhenDisabled: Boolean = true,
onDisabledClick: (() -> Unit)? = null,
onClick: (Boolean) -> Unit
onClick: (Boolean) -> Unit,
changeAlphaWhenDisabled: Boolean = true,
) {
val interactionSource = remember { MutableInteractionSource() }
PreferenceRow(
autoShadowElevation = autoShadowElevation,
enabled = enabled,
modifier = modifier,
resultModifier = resultModifier,
applyHorPadding = applyHorPadding,
applyHorizontalPadding = applyHorizontalPadding,
title = title,
contentColor = contentColor,
shape = shape,
Expand Down Expand Up @@ -143,7 +143,7 @@ fun PreferenceRowSwitch(
}
},
resultModifier = Modifier.padding(16.dp),
applyHorPadding = false,
applyHorizontalPadding = false,
onClick = onClick
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fun DrawModeSelector(
.fillMaxWidth()
.padding(horizontal = 8.dp),
resultModifier = Modifier.padding(16.dp),
applyHorPadding = false
applyHorizontalPadding = false
)
Spacer(
modifier = Modifier.height(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fun DrawPathModeSelector(
.fillMaxWidth()
.padding(horizontal = 8.dp),
resultModifier = Modifier.padding(16.dp),
applyHorPadding = false
applyHorizontalPadding = false
)
Spacer(modifier = Modifier.height(8.dp))
}
Expand Down Expand Up @@ -287,7 +287,7 @@ fun DrawPathModeSelector(
.fillMaxWidth()
.padding(horizontal = 8.dp),
resultModifier = Modifier.padding(16.dp),
applyHorPadding = false
applyHorizontalPadding = false
)
Spacer(modifier = Modifier.height(8.dp))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ fun AddEditMaskSheet(
),
color = Color.Unspecified,
resultModifier = Modifier.padding(16.dp),
applyHorPadding = false,
applyHorizontalPadding = false,
shape = RoundedCornerShape(24.dp),
onClick = {
viewModel.toggleIsInverseFillType()
Expand Down

0 comments on commit 5b337b4

Please sign in to comment.