Skip to content

Commit

Permalink
Supporting ultra HDR images in the lightbox. Fixes #629
Browse files Browse the repository at this point in the history
  • Loading branch information
savvasdalkitsis committed Mar 18, 2024
1 parent 9604fd6 commit 39e56aa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2024 Savvas Dalkitsis
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.savvasdalkitsis.uhuruphotos.foundation.image.api.hdr

import android.content.Context
import android.content.ContextWrapper
import android.content.pm.ActivityInfo.COLOR_MODE_DEFAULT
import android.content.pm.ActivityInfo.COLOR_MODE_HDR
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE
import androidx.activity.ComponentActivity

fun Context.setHDR(enable: Boolean) {
if (SDK_INT >= UPSIDE_DOWN_CAKE) {
getActivity()?.window?.colorMode = when {
enable -> COLOR_MODE_HDR
else -> COLOR_MODE_DEFAULT
}
}
}

private tailrec fun Context.getActivity(): ComponentActivity? = when (this) {
is ComponentActivity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ limitations under the License.

package com.savvasdalkitsis.uhuruphotos.foundation.image.api.ui

import android.graphics.drawable.BitmapDrawable
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
Expand All @@ -24,6 +27,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.savvasdalkitsis.uhuruphotos.foundation.image.api.hdr.setHDR
import me.saket.telephoto.zoomable.ZoomableImage
import me.saket.telephoto.zoomable.ZoomableImageState

Expand All @@ -47,6 +51,7 @@ fun FullSizeImage(
.listener(remember {
object : ImageRequest.Listener {
override fun onSuccess(request: ImageRequest, result: SuccessResult) {
context.setHDR(result.isHDR)
onFullResImageLoaded()
}
}
Expand All @@ -57,4 +62,10 @@ fun FullSizeImage(
contentScale = contentScale,
contentDescription = contentDescription,
)
}
}

private val SuccessResult.isHDR: Boolean
get() = when {
SDK_INT >= UPSIDE_DOWN_CAKE -> (drawable as? BitmapDrawable)?.bitmap?.hasGainmap() == true
else -> false
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import com.savvasdalkitsis.uhuruphotos.foundation.image.api.hdr.setHDR
import com.savvasdalkitsis.uhuruphotos.foundation.image.api.model.LocalFullImageLoader
import com.savvasdalkitsis.uhuruphotos.foundation.image.api.model.LocalThumbnailImageLoader
import com.savvasdalkitsis.uhuruphotos.foundation.image.api.model.LocalThumbnailWithNetworkCacheImageLoader
Expand Down Expand Up @@ -113,11 +114,13 @@ fun AppTheme(
ContentTheme(theme) {
val isLight = MaterialTheme.colors.isLight
val systemUiController = LocalSystemUiController.current
val context = LocalContext.current
SideEffect {
systemUiController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = isLight
)
context.setHDR(false)
}
content()
}
Expand Down
2 changes: 1 addition & 1 deletion foundation/ui/api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
libs.capturable,
libs.scrollbar,
project(":foundation:compose:api"),
project(":foundation:image:api"),
project(":foundation:theme:api"),
)
implementation(
Expand All @@ -34,7 +35,6 @@ dependencies {
libs.remember,
project(":foundation:compose:api"),
project(":foundation:icons:api"),
project(":foundation:image:api"),
project(":foundation:navigation:api"),
project(":foundation:strings:api"),
)
Expand Down

0 comments on commit 39e56aa

Please sign in to comment.