Skip to content

Commit

Permalink
Allow Squoosh to be enabled at runtime, add DesignDocSettings (#955)
Browse files Browse the repository at this point in the history
This is implemented as a class that can be applied to a DesignCompose
composable via a localComposition.
  • Loading branch information
timothyfroehlich authored Apr 4, 2024
1 parent 8f43138 commit 9dd5d81
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 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

import androidx.compose.runtime.compositionLocalOf

/**
* When applied as a compositionLocal, these settings will alter features or add functionality to
* DesignCompose Composables
*
* @property useSquoosh: Enable use of the Squoosh renderer. Squoosh implements its own view tree,
* rather than using Compose, which brings some performance and flexibility benefits. Squoosh
* isn't feature complete (no 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.
*/
class DesignDocSettings(
val useSquoosh: Boolean = false,
)

val LocalDesignDocSettings = compositionLocalOf { DesignDocSettings() }
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,6 @@ fun DesignDoc(
endSection()
}

// Enable the "Squoosh" refactor. Squoosh implements its own view tree, rather than using Compose,
// which brings some performance and flexibility benefits. Squoosh isn't feature complete (no
// 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,
Expand All @@ -985,7 +978,7 @@ internal fun DesignDocInternal(
designComposeCallbacks: DesignComposeCallbacks? = null,
parentComponents: List<ParentComponentInfo> = listOf(),
) {
if (USE_SQUOOSH) {
if (LocalDesignDocSettings.current.useSquoosh) {
SquooshRoot(
docName = docName,
incomingDocId = incomingDocId,
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,31 @@
/*
* 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 androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import com.android.designcompose.DesignDocSettings
import com.android.designcompose.LocalDesignDocSettings

@Composable
fun HelloSquoosh() {
CompositionLocalProvider(LocalDesignDocSettings provides DesignDocSettings(useSquoosh = true)) {
HelloWorldDoc.Main(
name = "Squoosh",
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9dd5d81

Please sign in to comment.