Skip to content
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

Draft: Allow Squoosh to be enabled at runtime, add DesignDocSettings #920

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class BuilderProcessor(private val codeGenerator: CodeGenerator, val logger: KSP
file += "import com.android.designcompose.CustomizationContext\n"
file += "import com.android.designcompose.DesignDoc\n"
file += "import com.android.designcompose.DesignComposeCallbacks\n"
file += "import com.android.designcompose.DesignDocSettings\n"
file += "import com.android.designcompose.DesignSwitcherPolicy\n"
file += "import com.android.designcompose.OpenLinkCallback\n"
file += "import com.android.designcompose.DesignNodeData\n"
Expand Down Expand Up @@ -770,6 +771,8 @@ class BuilderProcessor(private val codeGenerator: CodeGenerator, val logger: KSP
// Add optional callbacks to be called on certain document events
val defaultCallbacks = if (override) "" else " = null"
args.add(Pair("designComposeCallbacks", "DesignComposeCallbacks?$defaultCallbacks"))
val defaultDesignDocSettings = if (override) "" else " = null"
args.add(Pair("designDocSettings", "DesignDocSettings?$defaultDesignDocSettings"))

// Add optional key that can be used to uniquely identify this particular instance
val keyDefault = if (override) "" else " = null"
Expand Down Expand Up @@ -989,6 +992,7 @@ class BuilderProcessor(private val codeGenerator: CodeGenerator, val logger: KSP
out.appendText(
" modifier = modifier.semantics { sDocClass = className},\n"
)
out.appendText(" squoosh = designDocSettings?.enableSquoosh?: false,\n")
out.appendText(
" serverParams = DocumentServerParams(queries, ignoredImages()),\n"
)
Expand All @@ -997,7 +1001,9 @@ class BuilderProcessor(private val codeGenerator: CodeGenerator, val logger: KSP
if (hideDesignSwitcher) "DesignSwitcherPolicy.HIDE"
else "DesignSwitcherPolicy.SHOW_IF_ROOT"
out.appendText(" designSwitcherPolicy = $switchPolicy,\n")
out.appendText(" designComposeCallbacks = designComposeCallbacks,\n")
out.appendText(
" designComposeCallbacks = designDocSettings?.designComposeCallbacks,\n"
)
out.appendText(" )\n")
out.appendText(" }\n")
out.appendText(" }\n\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.android.designcompose

class DesignDocSettings(
val designComposeCallbacks: DesignComposeCallbacks = DesignComposeCallbacks(),
val enableSquoosh: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ fun DesignDoc(
docId: String,
rootNodeQuery: NodeQuery,
modifier: Modifier = Modifier,
squoosh: Boolean = false,
placeholder: (@Composable () -> Unit)? = null,
customizations: CustomizationContext = CustomizationContext(),
serverParams: DocumentServerParams = DocumentServerParams(),
Expand All @@ -952,6 +953,7 @@ fun DesignDoc(
docId,
rootNodeQuery,
modifier = modifier,
squoosh = squoosh,
placeholder = placeholder,
customizations = customizations,
serverParams = serverParams,
Expand All @@ -968,14 +970,14 @@ fun DesignDoc(
// scrolling, no lists, no transformed input), but it does add animations and is likely the
// direction that DesignCompose will move in to be lighter weight and better integrate with
// external layout.
private const val USE_SQUOOSH = false

@Composable
internal fun DesignDocInternal(
docName: String,
incomingDocId: String,
rootNodeQuery: NodeQuery,
modifier: Modifier = Modifier,
squoosh: Boolean = false,
placeholder: (@Composable () -> Unit)? = null,
customizations: CustomizationContext = CustomizationContext(),
serverParams: DocumentServerParams = DocumentServerParams(),
Expand All @@ -985,7 +987,7 @@ internal fun DesignDocInternal(
designComposeCallbacks: DesignComposeCallbacks? = null,
parentComponents: List<ParentComponentInfo> = listOf(),
) {
if (USE_SQUOOSH) {
if (squoosh) {
SquooshRoot(
docName = docName,
incomingDocId = incomingDocId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.layout.ParentDataModifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFontLoader
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Density
Expand All @@ -49,6 +50,7 @@ import com.android.designcompose.CustomizationContext
import com.android.designcompose.DesignComposeCallbacks
import com.android.designcompose.DesignSettings
import com.android.designcompose.DesignSwitcherPolicy
import com.android.designcompose.DocRenderStatus
import com.android.designcompose.DocServer
import com.android.designcompose.DocumentSwitcher
import com.android.designcompose.InteractionState
Expand All @@ -60,6 +62,7 @@ import com.android.designcompose.common.DocumentServerParams
import com.android.designcompose.doc
import com.android.designcompose.rootNode
import com.android.designcompose.rootOverlays
import com.android.designcompose.sDocRenderStatus
import com.android.designcompose.serdegen.Dimension
import com.android.designcompose.serdegen.Layout
import com.android.designcompose.serdegen.NodeQuery
Expand Down Expand Up @@ -252,6 +255,8 @@ fun SquooshRoot(
liveUpdateMode == LiveUpdateMode.OFFLINE
)

LaunchedEffect(docName) { Log.i(TAG, "Squooshing $docName") }

// Design Switcher support
val showDesignSwitcher =
isRoot &&
Expand Down Expand Up @@ -430,16 +435,18 @@ fun SquooshRoot(
CompositionLocalProvider(LocalSquooshIsRootContext provides SquooshIsRoot(false)) {
androidx.compose.ui.layout.Layout(
modifier =
modifier.squooshRender(
transitionRoot ?: root,
doc,
docName,
customizationContext,
childRenderSelector,
// Is there a nicer way of passing these two?
currentAnimations,
animationValues,
),
modifier
.squooshRender(
transitionRoot ?: root,
doc,
docName,
customizationContext,
childRenderSelector,
// Is there a nicer way of passing these two?
currentAnimations,
animationValues,
)
.semantics { sDocRenderStatus = DocRenderStatus.Rendered },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good add - had missed this!

measurePolicy = { measurables, constraints ->
// Update the root node style to have the incoming width/height from our parent
// Composable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ val EXAMPLES: ArrayList<Triple<String, @Composable () -> Unit, String?>> =
arrayListOf(
Triple("Hello", { HelloWorld() }, HelloWorldDoc.javaClass.name),
Triple("HelloBye", { HelloBye() }, HelloByeDoc.javaClass.name),
Triple("HelloSquoosh", { HelloSquoosh() }, HelloWorldDoc.javaClass.name),
Triple("Image Update", { ImageUpdateTest() }, ImageUpdateTestDoc.javaClass.name),
Triple("Telltales", { TelltaleTest() }, TelltaleTestDoc.javaClass.name),
Triple("OpenLink", { OpenLinkTest() }, OpenLinkTestDoc.javaClass.name),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 Google LLC
*
* 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.android.designcompose.testapp.validation.examples

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import com.android.designcompose.DesignComposeCallbacks
import com.android.designcompose.DesignDocSettings

@Composable
fun HelloSquoosh() {

val designDocSettings = remember {
DesignDocSettings(
designComposeCallbacks =
DesignComposeCallbacks(
docReadyCallback = { id ->
Log.i("DesignCompose", "HelloWorld Ready: doc ID = $id")
},
newDocDataCallback = { docId, data ->
Log.i(
"DesignCompose",
"HelloWorld Updated doc ID $docId: ${data?.size ?: 0} bytes"
)
},
),
enableSquoosh = true
)
}
HelloWorldDoc.Main(
name = "Squoosh",
designDocSettings = designDocSettings,
)
}
10 changes: 6 additions & 4 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ if (unbundledAAOSDir.isNullOrBlank()) {
val unbundledRepo = File(unbundledAAOSDir, "out/aaos-apps-gradle-build/unbundled_m2repo")
if (unbundledRepo.exists()) {

include(":media-lib")
project(":media-lib").projectDir = File("reference-apps/aaos-unbundled/media")
include(":mediacompose-app")
project(":mediacompose-app").projectDir = File("reference-apps/aaos-unbundled/mediacompose")
// DO NOT SUBMIT Disabled for now
// include(":media-lib")
// project(":media-lib").projectDir = File("reference-apps/aaos-unbundled/media")
// include(":mediacompose-app")
// project(":mediacompose-app").projectDir =
// File("reference-apps/aaos-unbundled/mediacompose")

dependencyResolutionManagement { repositories { maven(uri(unbundledRepo)) } }
} else {
Expand Down
Loading