-
Notifications
You must be signed in to change notification settings - Fork 220
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
Initial code for Paparazzi interceptors #589
Draft
swankjesse
wants to merge
2
commits into
master
Choose a base branch
from
jwilson.0930.interceptors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
38 changes: 38 additions & 0 deletions
38
paparazzi/paparazzi/src/main/java/app/cash/paparazzi/Interceptor.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,38 @@ | ||
/* | ||
* Copyright (C) 2022 Block, Inc. | ||
* | ||
* 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 app.cash.paparazzi | ||
|
||
import android.view.View | ||
import java.awt.image.BufferedImage | ||
|
||
/** | ||
* Participate in Paparazzi's process of transforming a view into an image. | ||
* | ||
* Implementations may operate on the input view, the returned bitmap, or both. | ||
*/ | ||
interface Interceptor { | ||
fun intercept(chain: Chain): BufferedImage | ||
|
||
interface Chain { | ||
val deviceConfig: DeviceConfig | ||
val view: View | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the timing of events with regard to creating the View? Can I set something like a Coil ImageLoader before the view gets rendered? |
||
val snapshot: Snapshot | ||
val frameIndex: Int | ||
val frameCount: Int | ||
val fps: Int | ||
fun proceed(view: View): BufferedImage | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
paparazzi/paparazzi/src/main/java/app/cash/paparazzi/RealChain.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,35 @@ | ||
/* | ||
* Copyright (C) 2022 Block, Inc. | ||
* | ||
* 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 app.cash.paparazzi | ||
|
||
import android.view.View | ||
import java.awt.image.BufferedImage | ||
|
||
internal data class RealChain( | ||
val nextIndex: Int, | ||
val interceptors: List<Interceptor>, | ||
override val deviceConfig: DeviceConfig, | ||
override val view: View, | ||
override val snapshot: Snapshot, | ||
override val frameIndex: Int, | ||
override val frameCount: Int, | ||
override val fps: Int | ||
) : Interceptor.Chain { | ||
override fun proceed(view: View): BufferedImage { | ||
val nextChain = copy(nextIndex = nextIndex + 1, view = view) | ||
return interceptors[nextIndex].intercept(nextChain) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
paparazzi/paparazzi/src/main/java/app/cash/paparazzi/ResizeInterceptor.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,66 @@ | ||
/* | ||
* Copyright (C) 2022 Block, Inc. | ||
* | ||
* 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 app.cash.paparazzi | ||
|
||
import android.util.Size | ||
import app.cash.paparazzi.internal.ImageUtils | ||
import java.awt.image.BufferedImage | ||
|
||
class ResizeInterceptor private constructor( | ||
val size: (Size) -> Size | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): BufferedImage { | ||
val rendered = chain.proceed(chain.view) | ||
val sourceSize = Size(rendered.width, rendered.height) | ||
val targetSize = size(sourceSize) | ||
return ImageUtils.scale(rendered, targetSize.width, targetSize.height) | ||
} | ||
|
||
companion object { | ||
/** Scales images up or down until the largest dimension is [size]. */ | ||
fun maxAnyDimension(size: Int): ResizeInterceptor { | ||
return ResizeInterceptor { sourceSize -> | ||
val maxDimension = maxOf(sourceSize.width, sourceSize.height) | ||
val scale = size.toDouble() / maxDimension | ||
Size( | ||
maxOf(1, (scale * sourceSize.width).toInt()), | ||
maxOf(1, (scale * sourceSize.height).toInt()) | ||
) | ||
} | ||
} | ||
|
||
fun fixedSize(width: Int, height: Int): ResizeInterceptor { | ||
require(width >= 1 && height >= 1) | ||
return ResizeInterceptor { Size(width, height) } | ||
} | ||
|
||
fun fixedWidth(width: Int): ResizeInterceptor { | ||
require(width >= 1) | ||
return ResizeInterceptor { sourceSize -> | ||
val scale = width.toDouble() / sourceSize.width | ||
Size(width, maxOf(1, (scale * sourceSize.height).toInt())) | ||
} | ||
} | ||
|
||
fun fixedHeight(height: Int): ResizeInterceptor { | ||
require(height >= 1) | ||
return ResizeInterceptor { sourceSize -> | ||
val scale = height.toDouble() / sourceSize.height | ||
Size(maxOf(1, (scale * sourceSize.width).toInt()), height) | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
paparazzi/paparazzi/src/main/java/app/cash/paparazzi/RoundFrameInterceptor.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,34 @@ | ||
/* | ||
* Copyright (C) 2022 Block, Inc. | ||
* | ||
* 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 app.cash.paparazzi | ||
|
||
import java.awt.geom.Ellipse2D | ||
import java.awt.image.BufferedImage | ||
|
||
internal object RoundFrameInterceptor : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): BufferedImage { | ||
val image = chain.proceed(chain.view) | ||
val result = BufferedImage(image.width, image.height, image.type) | ||
val g = result.createGraphics() | ||
try { | ||
g.clip = Ellipse2D.Float(0f, 0f, image.height.toFloat(), image.width.toFloat()) | ||
g.drawImage(image, 0, 0, image.width, image.height, null) | ||
return result | ||
} finally { | ||
g.dispose() | ||
} | ||
} | ||
} |
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.
I like this api as it works similar to retrofit interceptor. This also seems as a API replacement for
RenderExtension
which is used to render on top of the layout lib output.(edit noticed RenderExtension.toInterceptor method so you had the same thought :)
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.
The one issue I know I came across with RenderExtension returning
BufferedImage
is that there were issues findingjava.awt
classes for drawing when creating your own instance within your codebase.