Skip to content

Commit

Permalink
Merge pull request #148 from PatilShreyas/version-2.1.0
Browse files Browse the repository at this point in the history
Release v2.1.0
  • Loading branch information
PatilShreyas committed Feb 25, 2024
2 parents 0d330f4 + 371caac commit 9e83492
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ In `build.gradle` of app module, include this dependency

```gradle
dependencies {
implementation "dev.shreyaspatil:capturable:2.0.0"
implementation "dev.shreyaspatil:capturable:2.1.0"
}
```

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ android {
dependencies {

// Capturable library
// implementation "dev.shreyaspatil:capturable:2.0.0"
// implementation "dev.shreyaspatil:capturable:2.1.0"
implementation(project(":capturable"))

// Android
Expand Down
2 changes: 1 addition & 1 deletion capturable/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ POM_PACKAGING=aar
POM_INCEPTION_YEAR=2022

GROUP=dev.shreyaspatil
VERSION_NAME=2.0.0
VERSION_NAME=2.1.0
VERSION_CODE=3

POM_URL=https://github.com/PatilShreyas/Capturable/
Expand Down
103 changes: 74 additions & 29 deletions capturable/src/main/java/dev/shreyaspatil/capturable/Capturable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ import androidx.compose.ui.node.DelegatableNode
import androidx.compose.ui.node.DelegatingNode
import androidx.compose.ui.node.ModifierNodeElement
import dev.shreyaspatil.capturable.controller.CaptureController
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand Down Expand Up @@ -157,51 +161,51 @@ private data class CapturableModifierNodeElement(
}

override fun update(node: CapturableModifierNode) {
node.controller = controller
node.updateController(controller)
}
}

/**
* Capturable Modifier node which delegates task to the [CacheDrawModifierNode] for drawing
* Composable UI to the Picture and then helping it to converting picture into a Bitmap.
* Capturable Modifier node which delegates task to the [CacheDrawModifierNode] for drawing in
* runtime when content capture is requested
* [CacheDrawModifierNode] is used for drawing Composable UI from Canvas to the Picture and then
* this node converts picture into a Bitmap.
*
* @param controller A [CaptureController] which gives control to capture the Composable content.
*/
@Suppress("unused")
private class CapturableModifierNode(
var controller: CaptureController
controller: CaptureController
) : DelegatingNode(), DelegatableNode {

val picture = Picture()

/**
* Delegates the drawing to [CacheDrawModifierNode] in order to draw content rendered on the
* canvas directly to the [picture].
* State to hold the current [CaptureController] instance.
* This can be updated via [updateController] method.
*/
val drawModifierNode = delegate(
CacheDrawModifierNode {
// Example that shows how to redirect rendering to an Android Picture and then
// draw the picture into the original destination
val width = this.size.width.toInt()
val height = this.size.height.toInt()

onDrawWithContent {
val pictureCanvas = Canvas(picture.beginRecording(width, height))

draw(this, this.layoutDirection, pictureCanvas, this.size) {
this@onDrawWithContent.drawContent()
}
picture.endRecording()

drawIntoCanvas { canvas -> canvas.nativeCanvas.drawPicture(picture) }
}
}
)
private val currentController = MutableStateFlow(controller)

override fun onAttach() {
super.onAttach()
coroutineScope.launch {
controller.captureRequests.collect { request ->
observeCaptureRequestsAndServe()
}
}

/**
* Sets new [CaptureController]
*/
fun updateController(newController: CaptureController) {
currentController.value = newController
}

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun observeCaptureRequestsAndServe() {
currentController
.flatMapLatest { it.captureRequests }
.collect { request ->
val completable = request.imageBitmapDeferred
try {
val picture = getCurrentContentAsPicture()
val bitmap = withContext(Dispatchers.Default) {
picture.asBitmap(request.config)
}
Expand All @@ -210,7 +214,48 @@ private class CapturableModifierNode(
completable.completeExceptionally(error)
}
}
}
}

private suspend fun getCurrentContentAsPicture(): Picture {
return Picture().apply { drawCanvasIntoPicture(this) }
}

/**
* Draws the current content into the provided [picture]
*/
private suspend fun drawCanvasIntoPicture(picture: Picture) {
// CompletableDeferred to wait until picture is drawn from the Canvas content
val pictureDrawn = CompletableDeferred<Unit>()

// Delegate the task to draw the content into the picture
val delegatedNode = delegate(
CacheDrawModifierNode {
val width = this.size.width.toInt()
val height = this.size.height.toInt()

onDrawWithContent {
val pictureCanvas = Canvas(picture.beginRecording(width, height))

draw(this, this.layoutDirection, pictureCanvas, this.size) {
this@onDrawWithContent.drawContent()
}
picture.endRecording()

drawIntoCanvas { canvas ->
canvas.nativeCanvas.drawPicture(picture)

// Notify that picture is drawn
pictureDrawn.complete(Unit)
}
}
}
)
// Wait until picture is drawn
pictureDrawn.await()

// As task is accomplished, remove the delegation of node to prevent draw operations on UI
// updates or recompositions.
undelegate(delegatedNode)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlinx.coroutines.flow.asSharedFlow
* Controller for capturing [Composable] content.
* @see dev.shreyaspatil.capturable.Capturable for implementation details.
*/
class CaptureController internal constructor() {
class CaptureController {

/**
* Medium for providing capture requests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html class="no-js">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" charset="UTF-8">
<title>CaptureController</title>
<link href="../../../images/logo-icon.svg" rel="icon" type="image/svg">
<script>var pathToRoot = "../../../";</script>
<script>document.documentElement.classList.replace("no-js","js");</script>
<script>const storage = localStorage.getItem("dokka-dark-mode")
if (storage == null) {
const osDarkSchemePreferred = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
if (osDarkSchemePreferred === true) {
document.getElementsByTagName("html")[0].classList.add("theme-dark")
}
} else {
const savedDarkMode = JSON.parse(storage)
if(savedDarkMode === true) {
document.getElementsByTagName("html")[0].classList.add("theme-dark")
}
}
</script>
<script type="text/javascript" src="https://unpkg.com/kotlin-playground@1/dist/playground.min.js" async="async"></script>
<script type="text/javascript" src="../../../scripts/sourceset_dependencies.js" async="async"></script>
<link href="../../../styles/style.css" rel="Stylesheet">
<link href="../../../styles/main.css" rel="Stylesheet">
<link href="../../../styles/prism.css" rel="Stylesheet">
<link href="../../../styles/logo-styles.css" rel="Stylesheet">
<link href="../../../styles/font-jb-sans-auto.css" rel="Stylesheet">
<script type="text/javascript" src="../../../scripts/clipboard.js" async="async"></script>
<script type="text/javascript" src="../../../scripts/navigation-loader.js" async="async"></script>
<script type="text/javascript" src="../../../scripts/platform-content-handler.js" async="async"></script>
<script type="text/javascript" src="../../../scripts/main.js" defer="defer"></script>
<script type="text/javascript" src="../../../scripts/prism.js" async="async"></script>
<script type="text/javascript" src="../../../scripts/symbol-parameters-wrapper_deferred.js" defer="defer"></script>
</head>
<body>
<div class="root">
<nav class="navigation" id="navigation-wrapper">
<div class="navigation--inner">
<div class="navigation-title">
<button class="menu-toggle" id="menu-toggle" type="button">toggle menu</button>
<div class="library-name">
<a class="library-name--link" href="../../../index.html">
capturable
</a>
</div>
<div class="library-version">
</div>
</div>
<div class="filter-section" id="filter-section">
<button class="platform-tag platform-selector jvm-like" data-active="" data-filter=":capturable:dokkaHtml/release">androidJvm</button>
</div>
</div>
<div class="navigation-controls">
<button class="navigation-controls--btn navigation-controls--theme" id="theme-toggle-button" type="button">switch theme</button>
<div class="navigation-controls--btn navigation-controls--search" id="searchBar" role="button">search in API</div>
</div>
</nav>
<div id="container">
<div class="sidebar" id="leftColumn">
<div class="sidebar--inner" id="sideMenu"></div>
</div>
<div id="main">
<div class="main-content" data-page-type="member" id="content" pageIds="capturable::dev.shreyaspatil.capturable.controller/CaptureController/CaptureController/#/PointingToDeclaration//936064587">
<div class="breadcrumbs"><a href="../../../index.html">capturable</a><span class="delimiter">/</span><a href="../index.html">dev.shreyaspatil.capturable.controller</a><span class="delimiter">/</span><a href="index.html">CaptureController</a><span class="delimiter">/</span><span class="current">CaptureController</span></div>
<div class="cover ">
<h1 class="cover"><span>Capture</span><wbr></wbr><span><span>Controller</span></span></h1>
</div>
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":capturable:dokkaHtml/release"><div class="symbol monospace"><span class="token keyword">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span></div></div></div>
</div>
<div class="footer">
<span class="go-to-top-icon"><a href="#content" id="go-to-top-link"></a></span><span>© 2024 Copyright</span><span
class="pull-right"><span>Generated by </span><a
href="https://github.com/Kotlin/dokka"><span>dokka</span><span class="padded-icon"></span></a></span>
</div>
</div>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ <h1 class="cover"><span>Capture</span><wbr></wbr><span><span>Controller</span></
<div class="tabbedcontent">
<div class="tabs-section" tabs-section="tabs-section"><button class="section-tab" data-active="" data-togglable="CONSTRUCTOR,TYPE,PROPERTY,FUNCTION">Members</button></div>
<div class="tabs-section-body">
<div data-togglable="CONSTRUCTOR">
<h2 class="">Constructors</h2>
<div class="table"><a data-name="-854098920%2FConstructors%2F936064587" anchor-label="CaptureController" id="-854098920%2FConstructors%2F936064587" data-filterable-set=":capturable:dokkaHtml/release"></a>
<div class="table-row" data-togglable="CONSTRUCTOR" data-filterable-current=":capturable:dokkaHtml/release" data-filterable-set=":capturable:dokkaHtml/release">
<div class="main-subrow keyValue ">
<div class=""><span class="inline-flex">
<div><a href="-capture-controller.html"><span>Capture</span><wbr></wbr><span><span>Controller</span></span></a></div>
<span class="anchor-wrapper"><span class="anchor-icon" pointing-to="-854098920%2FConstructors%2F936064587"></span>
<div class="copy-popup-wrapper "><span class="copy-popup-icon"></span><span>Link copied to clipboard</span></div>
</span></span></div>
<div>
<div class="title">
<div class="platform-hinted " data-platform-hinted="data-platform-hinted"><div class="content sourceset-dependent-content" data-active="" data-togglable=":capturable:dokkaHtml/release"><div class="symbol monospace"><span class="token keyword">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span></div></div></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div data-togglable="FUNCTION">
<h2 class="">Functions</h2>
<div class="table"><a data-name="158162935%2FFunctions%2F936064587" anchor-label="capture" id="158162935%2FFunctions%2F936064587" data-filterable-set=":capturable:dokkaHtml/release"></a>
Expand Down
1 change: 1 addition & 0 deletions docs/capturable/package-list
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ $dokka.linkExtension:html
$dokka.location:dev.shreyaspatil.capturable.controller////PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/index.html
$dokka.location:dev.shreyaspatil.capturable.controller//rememberCaptureController/#/PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/remember-capture-controller.html
$dokka.location:dev.shreyaspatil.capturable.controller/CaptureController///PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/index.html
$dokka.location:dev.shreyaspatil.capturable.controller/CaptureController/CaptureController/#/PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/-capture-controller.html
$dokka.location:dev.shreyaspatil.capturable.controller/CaptureController/capture/#android.graphics.Bitmap.Config/PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture.html
$dokka.location:dev.shreyaspatil.capturable.controller/CaptureController/captureAsync/#android.graphics.Bitmap.Config/PointingToDeclaration/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture-async.html
$dokka.location:dev.shreyaspatil.capturable////PointingToDeclaration/capturable/dev.shreyaspatil.capturable/index.html
Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/pages.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"class CaptureController","description":"dev.shreyaspatil.capturable.controller.CaptureController","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/index.html","searchKeys":["CaptureController","class CaptureController","dev.shreyaspatil.capturable.controller.CaptureController"]},{"name":"fun Capturable(controller: CaptureController, modifier: Modifier = Modifier, onCaptured: (ImageBitmap?, Throwable?) -> Unit, content: () -> Unit)","description":"dev.shreyaspatil.capturable.Capturable","location":"capturable/dev.shreyaspatil.capturable/-capturable.html","searchKeys":["Capturable","fun Capturable(controller: CaptureController, modifier: Modifier = Modifier, onCaptured: (ImageBitmap?, Throwable?) -> Unit, content: () -> Unit)","dev.shreyaspatil.capturable.Capturable"]},{"name":"fun Modifier.capturable(controller: CaptureController): Modifier","description":"dev.shreyaspatil.capturable.capturable","location":"capturable/dev.shreyaspatil.capturable/capturable.html","searchKeys":["capturable","fun Modifier.capturable(controller: CaptureController): Modifier","dev.shreyaspatil.capturable.capturable"]},{"name":"fun capture(config: Bitmap.Config = Bitmap.Config.ARGB_8888)","description":"dev.shreyaspatil.capturable.controller.CaptureController.capture","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture.html","searchKeys":["capture","fun capture(config: Bitmap.Config = Bitmap.Config.ARGB_8888)","dev.shreyaspatil.capturable.controller.CaptureController.capture"]},{"name":"fun captureAsync(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Deferred<ImageBitmap>","description":"dev.shreyaspatil.capturable.controller.CaptureController.captureAsync","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture-async.html","searchKeys":["captureAsync","fun captureAsync(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Deferred<ImageBitmap>","dev.shreyaspatil.capturable.controller.CaptureController.captureAsync"]},{"name":"fun rememberCaptureController(): CaptureController","description":"dev.shreyaspatil.capturable.controller.rememberCaptureController","location":"capturable/dev.shreyaspatil.capturable.controller/remember-capture-controller.html","searchKeys":["rememberCaptureController","fun rememberCaptureController(): CaptureController","dev.shreyaspatil.capturable.controller.rememberCaptureController"]}]
[{"name":"class CaptureController","description":"dev.shreyaspatil.capturable.controller.CaptureController","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/index.html","searchKeys":["CaptureController","class CaptureController","dev.shreyaspatil.capturable.controller.CaptureController"]},{"name":"constructor()","description":"dev.shreyaspatil.capturable.controller.CaptureController.CaptureController","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/-capture-controller.html","searchKeys":["CaptureController","constructor()","dev.shreyaspatil.capturable.controller.CaptureController.CaptureController"]},{"name":"fun Capturable(controller: CaptureController, modifier: Modifier = Modifier, onCaptured: (ImageBitmap?, Throwable?) -> Unit, content: () -> Unit)","description":"dev.shreyaspatil.capturable.Capturable","location":"capturable/dev.shreyaspatil.capturable/-capturable.html","searchKeys":["Capturable","fun Capturable(controller: CaptureController, modifier: Modifier = Modifier, onCaptured: (ImageBitmap?, Throwable?) -> Unit, content: () -> Unit)","dev.shreyaspatil.capturable.Capturable"]},{"name":"fun Modifier.capturable(controller: CaptureController): Modifier","description":"dev.shreyaspatil.capturable.capturable","location":"capturable/dev.shreyaspatil.capturable/capturable.html","searchKeys":["capturable","fun Modifier.capturable(controller: CaptureController): Modifier","dev.shreyaspatil.capturable.capturable"]},{"name":"fun capture(config: Bitmap.Config = Bitmap.Config.ARGB_8888)","description":"dev.shreyaspatil.capturable.controller.CaptureController.capture","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture.html","searchKeys":["capture","fun capture(config: Bitmap.Config = Bitmap.Config.ARGB_8888)","dev.shreyaspatil.capturable.controller.CaptureController.capture"]},{"name":"fun captureAsync(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Deferred<ImageBitmap>","description":"dev.shreyaspatil.capturable.controller.CaptureController.captureAsync","location":"capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture-async.html","searchKeys":["captureAsync","fun captureAsync(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Deferred<ImageBitmap>","dev.shreyaspatil.capturable.controller.CaptureController.captureAsync"]},{"name":"fun rememberCaptureController(): CaptureController","description":"dev.shreyaspatil.capturable.controller.rememberCaptureController","location":"capturable/dev.shreyaspatil.capturable.controller/remember-capture-controller.html","searchKeys":["rememberCaptureController","fun rememberCaptureController(): CaptureController","dev.shreyaspatil.capturable.controller.rememberCaptureController"]}]

0 comments on commit 9e83492

Please sign in to comment.