-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Add support for heightDp, widthDp, showBackground, backgroundColor #577
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fa0a375
Add support for heightDp, widthDp, showBackground, backgroundColor
sergio-sastre 9b5068d
fix build so it takes screenshots
sergio-sastre e254ec0
Adjust height to consider ActionBar in setDisplaySize
sergio-sastre b4ff8cf
Fix size issue
takahirom be5c99a
Refactor code in RoborazziComposePreview
sergio-sastre e514e45
clean up captureRoboImage method
sergio-sastre d1cac8c
Clean up
sergio-sastre e57d40f
Remove unnecessary Activity
sergio-sastre ea218db
Fix build problem due to method ambiguity
sergio-sastre a10f240
Add maxHeapSize
takahirom 936bbf9
Add the RoborazziComposeDecorator to make Compose's captureRoboImage …
takahirom d6d632e
Make launchRoborazziTransparentActivity private
takahirom 8f08a53
Rename and refactor
takahirom a7c6168
Fix warning
takahirom a899893
Migrate uiMode, locale, fontScale,device to RoborazziComposeApplier
takahirom f1a9c47
Merge branch 'main' into takahirom/refactor-plugable/2024-12-01
takahirom 9eabc9c
Add .gitignore
takahirom da8378f
Add .gitignore (#578)
takahirom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions
24
...zzi-compose/src/main/java/com/github/takahirom/roborazzi/RegisterActivityToRobolectric.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 @@ | ||
package com.github.takahirom.roborazzi | ||
|
||
import android.app.Application | ||
import android.content.ComponentName | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.robolectric.Shadows | ||
|
||
/** | ||
* Workaround for https://github.com/takahirom/roborazzi/issues/100 | ||
*/ | ||
internal fun registerActivityToRobolectricIfNeeded() { | ||
try { | ||
val appContext: Application = ApplicationProvider.getApplicationContext() | ||
Shadows.shadowOf(appContext.packageManager).addActivityIfNotPresent( | ||
ComponentName( | ||
appContext.packageName, | ||
RoborazziTransparentActivity::class.java.name, | ||
) | ||
) | ||
} catch (e: ClassNotFoundException) { | ||
// Configured to run even without Robolectric | ||
e.printStackTrace() | ||
} | ||
} |
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
146 changes: 146 additions & 0 deletions
146
roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeApplier.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,146 @@ | ||
package com.github.takahirom.roborazzi | ||
|
||
import android.app.Activity | ||
import android.graphics.Color | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.test.core.app.ActivityScenario | ||
import org.robolectric.Shadows.shadowOf | ||
import org.robolectric.shadows.ShadowDisplay.getDefaultDisplay | ||
import kotlin.math.roundToInt | ||
|
||
interface RoborazziComposeApplier | ||
|
||
@ExperimentalRoborazziApi | ||
interface RoborazziComposeActivityScenarioApplier : RoborazziComposeApplier { | ||
fun applyToActivityScenario(scenario: ActivityScenario<out Activity>) | ||
} | ||
|
||
@ExperimentalRoborazziApi | ||
interface RoborazziComposeComposableApplier : RoborazziComposeApplier { | ||
fun applyToComposable(content: @Composable () -> Unit): @Composable () -> Unit | ||
} | ||
|
||
|
||
@ExperimentalRoborazziApi | ||
class RoborazziComposeApplierBuilder { | ||
private val activityScenarioAppliers = | ||
mutableListOf<RoborazziComposeActivityScenarioApplier>() | ||
private val composableAppliers = mutableListOf<RoborazziComposeComposableApplier>() | ||
|
||
fun with(applier: RoborazziComposeApplier): RoborazziComposeApplierBuilder { | ||
if (applier is RoborazziComposeActivityScenarioApplier) { | ||
activityScenarioAppliers.add(applier) | ||
} | ||
if (applier is RoborazziComposeComposableApplier) { | ||
composableAppliers.add(applier) | ||
} | ||
return this | ||
} | ||
|
||
fun sized(widthDp: Int = 0, heightDp: Int = 0): RoborazziComposeApplierBuilder { | ||
return with(RoborazziComposeSizeApplier(widthDp, heightDp)) | ||
} | ||
|
||
fun colored( | ||
showBackground: Boolean, | ||
backgroundColor: Long = 0L | ||
): RoborazziComposeApplierBuilder { | ||
return with(RoborazziComposeBackgroundApplier(showBackground, backgroundColor)) | ||
} | ||
|
||
@InternalRoborazziApi | ||
fun apply( | ||
scenario: ActivityScenario<out Activity>, | ||
content: @Composable () -> Unit | ||
): @Composable () -> Unit { | ||
activityScenarioAppliers.forEach { it.applyToActivityScenario(scenario) } | ||
var appliedContent = content | ||
composableAppliers.forEach { applier -> | ||
appliedContent = applier.applyToComposable(appliedContent) | ||
} | ||
return { | ||
appliedContent() | ||
} | ||
} | ||
|
||
@ExperimentalRoborazziApi | ||
data class RoborazziComposeSizeApplier(val widthDp: Int, val heightDp: Int) : | ||
RoborazziComposeActivityScenarioApplier, | ||
RoborazziComposeComposableApplier { | ||
override fun applyToActivityScenario(scenario: ActivityScenario<out Activity>) { | ||
scenario.onActivity { activity -> | ||
activity.setDisplaySize(widthDp = widthDp, heightDp = heightDp) | ||
} | ||
} | ||
|
||
private fun Activity.setDisplaySize( | ||
widthDp: Int, | ||
heightDp: Int | ||
) { | ||
if (widthDp <= 0 && heightDp <= 0) return | ||
|
||
val display = shadowOf(getDefaultDisplay()) | ||
val density = resources.displayMetrics.density | ||
if (widthDp > 0) { | ||
val widthPx = (widthDp * density).roundToInt() | ||
display.setWidth(widthPx) | ||
} | ||
if (heightDp > 0) { | ||
val heightPx = (heightDp * density).roundToInt() | ||
display.setHeight(heightPx) | ||
} | ||
recreate() | ||
} | ||
|
||
override fun applyToComposable(content: @Composable () -> Unit): @Composable () -> Unit { | ||
/** | ||
* WARNING: | ||
* For this to work, it requires that the Display is within the widthDp and heightDp dimensions | ||
* You can ensure that by calling [Activity.setDisplaySize] before | ||
*/ | ||
val modifier = when { | ||
widthDp > 0 && heightDp > 0 -> Modifier.size(widthDp.dp, heightDp.dp) | ||
widthDp > 0 -> Modifier.width(widthDp.dp) | ||
heightDp > 0 -> Modifier.height(heightDp.dp) | ||
else -> Modifier | ||
} | ||
return { | ||
Box(modifier = modifier) { | ||
content() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ExperimentalRoborazziApi | ||
data class RoborazziComposeBackgroundApplier( | ||
val showBackground: Boolean, | ||
val backgroundColor: Long | ||
) : RoborazziComposeActivityScenarioApplier { | ||
override fun applyToActivityScenario(scenario: ActivityScenario<out Activity>) { | ||
when (showBackground) { | ||
false -> { | ||
scenario.onActivity { activity -> | ||
activity.window.decorView.setBackgroundColor(Color.TRANSPARENT) | ||
} | ||
} | ||
|
||
true -> { | ||
val color = when (backgroundColor != 0L) { | ||
true -> backgroundColor.toInt() | ||
false -> Color.WHITE | ||
} | ||
scenario.onActivity { activity -> | ||
activity.window.decorView.setBackgroundColor(color) | ||
} | ||
} | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
roborazzi/src/main/java/com/github/takahirom/roborazzi/RoborazziTransparentActivity.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package com.github.takahirom.roborazzi | ||
|
||
import android.os.Bundle | ||
import android.os.PersistableBundle | ||
import androidx.activity.ComponentActivity | ||
|
||
class RoborazziTransparentActivity: ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { | ||
super.onCreate(savedInstanceState, persistentState) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen) | ||
super.onCreate(savedInstanceState) | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is true that it adds some complexity, but brings the benefit of making the api more flexible.
Therefore, I have a couple of points:
I think it would become much more intuitive if all of them would be handled the same way, e.g.
I think symmetry in code also makes it more understandable
previewOptions
andRoborazziPreviewOptionsBuilder
orcomposableConfig
andRoborazziComposableConfigBuilder
is more understandableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review. That makes sense.
I've migrated the settings such as local to the applier and renamed the applier to config.
a899893
https://github.com/takahirom/roborazzi/pull/579/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
The only improvements I’d propose are input validations (e.g. fontScale should not be lower than 0 and the like) and logging messages for invalid inputs.
apart from that feel free to merge 😊