diff --git a/designcompose/src/main/java/com/android/designcompose/proto/DimensionProto.kt b/designcompose/src/main/java/com/android/designcompose/proto/DimensionProto.kt deleted file mode 100644 index e52ebfe42..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/DimensionProto.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.definition.element.dimensionProto -import com.android.designcompose.serdegen.Dimension -import com.android.designcompose.serdegen.Dimension.Undefined -import com.android.designcompose.serdegen.DimensionProto -import com.google.protobuf.Empty -import com.novi.serde.Unit -import java.util.Optional - -/** @deprecated This function will be removed in the future. */ -@Deprecated("This function will be removed in the future.") -fun Optional.getDim(): Dimension = - this.orElseThrow { NoSuchFieldException("Malformed data: DimensionProto unset") } - .dimension - .orElseThrow { NoSuchFieldException("Malformed data: DimensionProto's dimension unset") } - -/** @deprecated This function will be removed in the future. */ -@Deprecated("This function will be removed in the future.") -fun newDimensionProtoPoints(value: Float = 0f) = - Optional.of(DimensionProto(Optional.of(Dimension.Points(value)))) - -/** @deprecated This function will be removed in the future. */ -@Deprecated("This function will be removed in the future.") -fun newDimensionProtoPercent(value: Float = 0f) = - Optional.of(DimensionProto(Optional.of(Dimension.Percent(value)))) - -@Deprecated("This function will be removed in the future.") -fun newDimensionProtoUndefined() = - Optional.of(DimensionProto(Optional.of(Dimension.Undefined(com.novi.serde.Unit())))) - -@Deprecated("This function will be removed in the future.") -fun Dimension.toOptDimProto() = Optional.of(DimensionProto(Optional.of(this))) - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.definition.element.DimensionProto.intoSerde(): Dimension = - when (dimensionCase) { - com.android.designcompose.definition.element.DimensionProto.DimensionCase.UNDEFINED -> - Dimension.Undefined(Unit()) - com.android.designcompose.definition.element.DimensionProto.DimensionCase.AUTO -> - Dimension.Auto(Unit()) - com.android.designcompose.definition.element.DimensionProto.DimensionCase.POINTS -> - Dimension.Points(points) - com.android.designcompose.definition.element.DimensionProto.DimensionCase.PERCENT -> - Dimension.Percent(percent) - else -> - throw IllegalArgumentException("Unknown DimensionProto: $this") // Should never happen. - } - -/** @deprecated This function will be removed in the future. */ -@Deprecated("This function will be removed in the future.") -fun com.android.designcompose.definition.element.DimensionProto.into(): DimensionProto = - com.android.designcompose.serdegen.DimensionProto(Optional.of(this.intoSerde())) - -/** @deprecated This function will be removed in the future. */ -@Deprecated("This function will be removed in the future.") -internal fun Dimension.intoProto(): com.android.designcompose.definition.element.DimensionProto = - dimensionProto { - when (val s = this@intoProto) { - // These are empty types so we need to set them to default instances - is Undefined -> undefined = Empty.getDefaultInstance() - is Dimension.Auto -> auto = Empty.getDefaultInstance() - is Dimension.Points -> points = s.value.toFloat() - is Dimension.Percent -> percent = s.value.toFloat() - } - } diff --git a/designcompose/src/main/java/com/android/designcompose/proto/DimensionRect.kt b/designcompose/src/main/java/com/android/designcompose/proto/DimensionRect.kt deleted file mode 100644 index 9a262bedc..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/DimensionRect.kt +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.definition.element.dimensionRect -import com.android.designcompose.serdegen.Dimension -import com.android.designcompose.serdegen.DimensionRect -import java.util.Optional - -@Deprecated("This API surface area is experimental and is likely to change in the future.") -val Optional.start: Dimension - get() = - this.orElseThrow { NoSuchFieldException("Malformed data: DimensionRect unset") } - .start - .getDim() - -val Optional.end: Dimension - @Deprecated("This API surface area is experimental and is likely to change in the future.") - get() = - this.orElseThrow { NoSuchFieldException("Malformed data: DimensionRect unset") } - .end - .getDim() - -val Optional.top: Dimension - @Deprecated("This API surface area is experimental and is likely to change in the future.") - get() = - this.orElseThrow { NoSuchFieldException("Malformed data: DimensionRect unset") } - .top - .getDim() - -val Optional.bottom: Dimension - @Deprecated("This API surface area is experimental and is likely to change in the future.") - get() = - this.orElseThrow { NoSuchFieldException("Malformed data: DimensionRect unset") } - .bottom - .getDim() - -@Deprecated("This API surface area is experimental and is likely to change in the future.") -fun Optional.isDefault(): Boolean { - return this.start is Dimension.Undefined && - this.end is Dimension.Undefined && - this.top is Dimension.Undefined && - this.bottom is Dimension.Undefined -} - -@Deprecated("This API surface area is experimental and is likely to change in the future.") -fun newDimensionRectPointsZero(): Optional = - Optional.of( - DimensionRect( - newDimensionProtoPoints(0f), - newDimensionProtoPoints(0f), - newDimensionProtoPoints(0f), - newDimensionProtoPoints(0f), - ) - ) - -@Deprecated("This API surface area is experimental and is likely to change in the future.") -internal fun com.android.designcompose.definition.element.DimensionRect.intoSerde() = - com.android.designcompose.serdegen.DimensionRect( - Optional.of( - com.android.designcompose.serdegen.DimensionProto(Optional.of(start.intoSerde())) - ), - Optional.of( - com.android.designcompose.serdegen.DimensionProto(Optional.of(end.intoSerde())) - ), - Optional.of( - com.android.designcompose.serdegen.DimensionProto(Optional.of(top.intoSerde())) - ), - Optional.of( - com.android.designcompose.serdegen.DimensionProto(Optional.of(bottom.intoSerde())) - ), - ) - -@Deprecated("This API surface area is experimental and is likely to change in the future.") -internal fun DimensionRect.intoProto() = dimensionRect { - val s = this@intoProto - start = s.start.getDim().intoProto() - end = s.end.getDim().intoProto() - top = s.top.getDim().intoProto() - bottom = s.bottom.getDim().intoProto() -} diff --git a/designcompose/src/main/java/com/android/designcompose/proto/IntoProto.kt b/designcompose/src/main/java/com/android/designcompose/proto/IntoProto.kt deleted file mode 100644 index 244d57107..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/IntoProto.kt +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.android_interface.layoutNodeList -import com.android.designcompose.definition.element.size -import com.android.designcompose.definition.layout.AlignContent as ProtoAlignContent -import com.android.designcompose.definition.layout.AlignItems as ProtoAlignItems -import com.android.designcompose.definition.layout.AlignSelf as ProtoAlignSelf -import com.android.designcompose.definition.layout.FlexDirection as ProtoFlexDirection -import com.android.designcompose.definition.layout.ItemSpacingKt.auto -import com.android.designcompose.definition.layout.JustifyContent as ProtoJustifyContent -import com.android.designcompose.definition.layout.PositionType as ProtoPositionType -import com.android.designcompose.definition.layout.itemSpacing -import com.android.designcompose.definition.layout.layoutNode -import com.android.designcompose.definition.layout.layoutParentChildren -import com.android.designcompose.definition.layout.layoutStyle -import com.android.designcompose.serdegen.AlignContent -import com.android.designcompose.serdegen.AlignItems -import com.android.designcompose.serdegen.AlignSelf -import com.android.designcompose.serdegen.FlexDirection -import com.android.designcompose.serdegen.ItemSpacing -import com.android.designcompose.serdegen.ItemSpacingType -import com.android.designcompose.serdegen.JustifyContent -import com.android.designcompose.serdegen.LayoutNode -import com.android.designcompose.serdegen.LayoutNodeList -import com.android.designcompose.serdegen.LayoutParentChildren -import com.android.designcompose.serdegen.LayoutStyle -import com.android.designcompose.serdegen.PositionType -import com.android.designcompose.serdegen.Size - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun LayoutNode.intoProto() = layoutNode { - val s = this@intoProto - layoutId = s.layout_id - parentLayoutId = s.parent_layout_id - childIndex = s.child_index - style = s.style.intoProto() - name = s.name - useMeasureFunc = s.use_measure_func - if (s.fixed_width.isPresent) fixedWidth = s.fixed_width.get() - if (s.fixed_height.isPresent) fixedHeight = s.fixed_height.get() -} - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun LayoutParentChildren.intoProto() = layoutParentChildren { - val s = this@intoProto - parentLayoutId = s.parent_layout_id - childLayoutIds.addAll(s.child_layout_ids) -} - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun LayoutNodeList.intoProto() = layoutNodeList { - layoutNodes.addAll(this@intoProto.layout_nodes.map { it.intoProto() }) - parentChildren.addAll(this@intoProto.parent_children.map { it.intoProto() }) -} - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun ItemSpacing.intoProto() = itemSpacing { // ktlint-disable indent - when (val s = this@intoProto.type()) { - is ItemSpacingType.Fixed -> fixed = s.value - is ItemSpacingType.Auto -> auto = auto { - width = s.value.width - height = s.value.height - } - else -> throw IllegalArgumentException("Unknown ItemSpacing: $this") // Should never happen. - } -} - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun Size.intoProto() = size { - val s = this@intoProto - width = s.width - height = s.height -} - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun AlignSelf.intoProto() = - when (this) { - is AlignSelf.Auto -> ProtoAlignSelf.ALIGN_SELF_AUTO - is AlignSelf.FlexStart -> ProtoAlignSelf.ALIGN_SELF_FLEX_START - is AlignSelf.FlexEnd -> ProtoAlignSelf.ALIGN_SELF_FLEX_END - is AlignSelf.Center -> ProtoAlignSelf.ALIGN_SELF_CENTER - is AlignSelf.Baseline -> ProtoAlignSelf.ALIGN_SELF_BASELINE - is AlignSelf.Stretch -> ProtoAlignSelf.ALIGN_SELF_STRETCH - else -> throw IllegalArgumentException("Unknown AlignSelf: $this") // Should never happen. - } - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun AlignContent.intoProto() = - when (this) { - is AlignContent.FlexStart -> ProtoAlignContent.ALIGN_CONTENT_FLEX_START - is AlignContent.FlexEnd -> ProtoAlignContent.ALIGN_CONTENT_FLEX_END - is AlignContent.Center -> ProtoAlignContent.ALIGN_CONTENT_CENTER - is AlignContent.Stretch -> ProtoAlignContent.ALIGN_CONTENT_STRETCH - is AlignContent.SpaceBetween -> ProtoAlignContent.ALIGN_CONTENT_SPACE_BETWEEN - is AlignContent.SpaceAround -> ProtoAlignContent.ALIGN_CONTENT_SPACE_AROUND - else -> - throw IllegalArgumentException("Unknown AlignContent: $this") // Should never happen. - } - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun AlignItems.intoProto() = - when (this) { - is AlignItems.FlexStart -> ProtoAlignItems.ALIGN_ITEMS_FLEX_START - is AlignItems.FlexEnd -> ProtoAlignItems.ALIGN_ITEMS_FLEX_END - is AlignItems.Center -> ProtoAlignItems.ALIGN_ITEMS_CENTER - is AlignItems.Baseline -> ProtoAlignItems.ALIGN_ITEMS_BASELINE - is AlignItems.Stretch -> ProtoAlignItems.ALIGN_ITEMS_STRETCH - else -> throw IllegalArgumentException("Unknown AlignItems: $this") // Should never happen. - } - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun FlexDirection.intoProto() = - when (this) { - is FlexDirection.Row -> ProtoFlexDirection.FLEX_DIRECTION_ROW - is FlexDirection.RowReverse -> ProtoFlexDirection.FLEX_DIRECTION_ROW_REVERSE - is FlexDirection.Column -> ProtoFlexDirection.FLEX_DIRECTION_COLUMN - is FlexDirection.ColumnReverse -> ProtoFlexDirection.FLEX_DIRECTION_COLUMN_REVERSE - is FlexDirection.None -> ProtoFlexDirection.FLEX_DIRECTION_NONE - else -> - throw IllegalArgumentException("Unknown FlexDirection: $this") // Should never happen. - } - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun JustifyContent.intoProto() = - when (this) { - is JustifyContent.FlexStart -> ProtoJustifyContent.JUSTIFY_CONTENT_FLEX_START - is JustifyContent.FlexEnd -> ProtoJustifyContent.JUSTIFY_CONTENT_FLEX_END - is JustifyContent.Center -> ProtoJustifyContent.JUSTIFY_CONTENT_CENTER - is JustifyContent.SpaceBetween -> ProtoJustifyContent.JUSTIFY_CONTENT_SPACE_BETWEEN - is JustifyContent.SpaceAround -> ProtoJustifyContent.JUSTIFY_CONTENT_SPACE_AROUND - is JustifyContent.SpaceEvenly -> ProtoJustifyContent.JUSTIFY_CONTENT_SPACE_EVENLY - else -> - throw IllegalArgumentException("Unknown JustifyContent: $this") // Should never happen. - } - -@Deprecated("This is for temporary use by DesignCompose.") -internal fun PositionType.intoProto() = - when (this) { - is PositionType.Relative -> ProtoPositionType.POSITION_TYPE_RELATIVE - is PositionType.Absolute -> ProtoPositionType.POSITION_TYPE_ABSOLUTE - else -> - throw IllegalArgumentException("Unknown PositionType: $this") // Should never happen. - } - -/** Temporary (I hope) conversion from the Serde layout style to the proto layout style. */ -@Deprecated("This is for temporary use by DesignCompose.") -internal fun LayoutStyle.intoProto() = layoutStyle { - val s = this@intoProto - margin = - s.margin.orElseThrow { NoSuchFieldException("Malformed data: margin unset") }.intoProto() - padding = - s.padding.orElseThrow { NoSuchFieldException("Malformed data: padding unset") }.intoProto() - itemSpacing = s.item_spacing.get().intoProto() - top = s.top.getDim().intoProto() - left = s.left.getDim().intoProto() - bottom = s.bottom.getDim().intoProto() - right = s.right.getDim().intoProto() - width = s.width.getDim().intoProto() - height = s.height.getDim().intoProto() - minWidth = s.min_width.getDim().intoProto() - maxWidth = s.max_width.getDim().intoProto() - minHeight = s.min_height.getDim().intoProto() - maxHeight = s.max_height.getDim().intoProto() - boundingBox = s.bounding_box.get().intoProto() - flexGrow = s.flex_grow - flexShrink = s.flex_shrink - flexBasis = s.flex_basis.getDim().intoProto() - alignSelf = alignSelfFromInt(s.align_self).intoProto() - alignContent = alignContentFromInt(s.align_content).intoProto() - alignItems = alignItemsFromInt(s.align_items).intoProto() - flexDirection = flexDirectionFromInt(s.flex_direction).intoProto() - justifyContent = justifyContentFromInt(s.justify_content).intoProto() - positionType = positionTypeFromInt(s.position_type).intoProto() -} diff --git a/designcompose/src/main/java/com/android/designcompose/proto/IntoSerde.kt b/designcompose/src/main/java/com/android/designcompose/proto/IntoSerde.kt deleted file mode 100644 index 1483b7bbe..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/IntoSerde.kt +++ /dev/null @@ -1,155 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.android_interface.LayoutChangedResponse -import com.android.designcompose.definition.layout.AlignContent -import com.android.designcompose.definition.layout.AlignItems -import com.android.designcompose.definition.layout.AlignSelf -import com.android.designcompose.definition.layout.FlexDirection -import com.android.designcompose.definition.layout.ItemSpacing -import com.android.designcompose.definition.layout.JustifyContent -import com.android.designcompose.definition.layout.PositionType -import com.android.designcompose.serdegen.Size -import java.util.Optional - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun LayoutChangedResponse.Layout.intoSerde() = - com.android.designcompose.serdegen.Layout( - order, - width, - height, - left, - top, - contentWidth, - contentHeight, - ) - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun ItemSpacing.intoSerde() = - when (itemSpacingTypeCase) { - ItemSpacing.ItemSpacingTypeCase.FIXED -> - com.android.designcompose.serdegen.ItemSpacing( - Optional.of(com.android.designcompose.serdegen.ItemSpacingType.Fixed(fixed)) - ) - ItemSpacing.ItemSpacingTypeCase.AUTO -> - com.android.designcompose.serdegen.ItemSpacing( - Optional.of( - com.android.designcompose.serdegen.ItemSpacingType.Auto( - com.android.designcompose.serdegen.Auto(auto.width, auto.height) - ) - ) - ) - else -> throw IllegalArgumentException("Unknown ItemSpacing: $this") // Should never happen. - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun com.android.designcompose.definition.element.Size.intoSerde() = Size(width, height) - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun AlignSelf.intoSerde() = - when (this) { - AlignSelf.ALIGN_SELF_AUTO -> com.android.designcompose.serdegen.AlignSelf.Auto() - AlignSelf.ALIGN_SELF_FLEX_START -> com.android.designcompose.serdegen.AlignSelf.FlexStart() - AlignSelf.ALIGN_SELF_FLEX_END -> com.android.designcompose.serdegen.AlignSelf.FlexEnd() - AlignSelf.ALIGN_SELF_CENTER -> com.android.designcompose.serdegen.AlignSelf.Center() - AlignSelf.ALIGN_SELF_BASELINE -> com.android.designcompose.serdegen.AlignSelf.Baseline() - AlignSelf.ALIGN_SELF_STRETCH -> com.android.designcompose.serdegen.AlignSelf.Stretch() - else -> throw IllegalArgumentException("Unknown AlignSelf: $this") // Should never happen - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun AlignContent.intoSerde() = - when (this) { - AlignContent.ALIGN_CONTENT_FLEX_START -> - com.android.designcompose.serdegen.AlignContent.FlexStart() - AlignContent.ALIGN_CONTENT_FLEX_END -> - com.android.designcompose.serdegen.AlignContent.FlexEnd() - AlignContent.ALIGN_CONTENT_CENTER -> - com.android.designcompose.serdegen.AlignContent.Center() - AlignContent.ALIGN_CONTENT_STRETCH -> - com.android.designcompose.serdegen.AlignContent.Stretch() - AlignContent.ALIGN_CONTENT_SPACE_BETWEEN -> - com.android.designcompose.serdegen.AlignContent.SpaceBetween() - AlignContent.ALIGN_CONTENT_SPACE_AROUND -> - com.android.designcompose.serdegen.AlignContent.SpaceAround() - else -> throw IllegalArgumentException("Unknown AlignContent: $this") // Should never happen - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun AlignItems.intoSerde() = - when (this) { - AlignItems.ALIGN_ITEMS_FLEX_START -> - com.android.designcompose.serdegen.AlignItems.FlexStart() - AlignItems.ALIGN_ITEMS_FLEX_END -> com.android.designcompose.serdegen.AlignItems.FlexEnd() - AlignItems.ALIGN_ITEMS_CENTER -> com.android.designcompose.serdegen.AlignItems.Center() - AlignItems.ALIGN_ITEMS_BASELINE -> com.android.designcompose.serdegen.AlignItems.Baseline() - AlignItems.ALIGN_ITEMS_STRETCH -> com.android.designcompose.serdegen.AlignItems.Stretch() - else -> throw IllegalArgumentException("Unknown AlignItems: $this") // Should never happen - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun FlexDirection.intoSerde() = - when (this) { - FlexDirection.FLEX_DIRECTION_ROW -> com.android.designcompose.serdegen.FlexDirection.Row() - FlexDirection.FLEX_DIRECTION_COLUMN -> - com.android.designcompose.serdegen.FlexDirection.Column() - FlexDirection.FLEX_DIRECTION_ROW_REVERSE -> - com.android.designcompose.serdegen.FlexDirection.RowReverse() - FlexDirection.FLEX_DIRECTION_COLUMN_REVERSE -> - com.android.designcompose.serdegen.FlexDirection.ColumnReverse() - else -> - throw IllegalArgumentException("Unknown FlexDirection: $this") // Should never happen - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun JustifyContent.intoSerde() = - when (this) { - JustifyContent.JUSTIFY_CONTENT_FLEX_START -> - com.android.designcompose.serdegen.JustifyContent.FlexStart() - JustifyContent.JUSTIFY_CONTENT_FLEX_END -> - com.android.designcompose.serdegen.JustifyContent.FlexEnd() - JustifyContent.JUSTIFY_CONTENT_CENTER -> - com.android.designcompose.serdegen.JustifyContent.Center() - JustifyContent.JUSTIFY_CONTENT_SPACE_BETWEEN -> - com.android.designcompose.serdegen.JustifyContent.SpaceBetween() - JustifyContent.JUSTIFY_CONTENT_SPACE_AROUND -> - com.android.designcompose.serdegen.JustifyContent.SpaceAround() - JustifyContent.JUSTIFY_CONTENT_SPACE_EVENLY -> - com.android.designcompose.serdegen.JustifyContent.SpaceEvenly() - else -> - throw IllegalArgumentException("Unknown JustifyContent: $this") // Should never happen - } - -/** @deprecated This is for the old Design Compose infra, please don't use it anymore. */ -@Deprecated("b/314820134: This is for the old Design Compose infra, please don't use it anymore.") -internal fun PositionType.intoSerde() = - when (this) { - PositionType.POSITION_TYPE_RELATIVE -> - com.android.designcompose.serdegen.PositionType.Relative() - PositionType.POSITION_TYPE_ABSOLUTE -> - com.android.designcompose.serdegen.PositionType.Absolute() - else -> throw IllegalArgumentException("Unknown PositionType: $this") // Should never happen - } diff --git a/designcompose/src/main/java/com/android/designcompose/proto/ProtoEnums.kt b/designcompose/src/main/java/com/android/designcompose/proto/ProtoEnums.kt deleted file mode 100644 index 3ca46979f..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/ProtoEnums.kt +++ /dev/null @@ -1,591 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.serdegen.AlignContent -import com.android.designcompose.serdegen.AlignItems -import com.android.designcompose.serdegen.AlignSelf -import com.android.designcompose.serdegen.BlendMode -import com.android.designcompose.serdegen.Display -import com.android.designcompose.serdegen.FlexDirection -import com.android.designcompose.serdegen.FontStyle -import com.android.designcompose.serdegen.JustifyContent -import com.android.designcompose.serdegen.Overflow -import com.android.designcompose.serdegen.PositionType -import com.android.designcompose.serdegen.RenderMethod -import com.android.designcompose.serdegen.ScaleMode -import com.android.designcompose.serdegen.StrokeCap -import com.android.designcompose.serdegen.TextAlign -import com.android.designcompose.serdegen.Trigger -import com.android.designcompose.serdegen.TriggerType -import java.util.Optional -import kotlin.jvm.optionals.getOrNull - -// We don't have enum values due to the java code being generated from Rust, which was -// generated from proto. This file contains functions to translate between integers and enum values. -// Restore use of enums once java is generated from proto. - -// StrokeAlign -// Unspecified = 0, -// Inside = 1, -// Center = 2, -// Outside = 3, -@Deprecated("This function will be removed in the future.") -enum class StrokeAlignType { - Unspecified, - Inside, - Center, - Outside, -} - -@Deprecated("This function will be removed in the future.") -internal fun strokeAlignFromInt(int: Int): StrokeAlignType { - return when (int) { - 1 -> StrokeAlignType.Inside - 2 -> StrokeAlignType.Center - 3 -> StrokeAlignType.Outside - else -> StrokeAlignType.Unspecified - } -} - -@Deprecated("This function will be removed in the future.") -internal fun strokeAlignTypeToInt(align: StrokeAlignType): Int { - return when (align) { - StrokeAlignType.Inside -> 1 - StrokeAlignType.Center -> 2 - StrokeAlignType.Outside -> 3 - StrokeAlignType.Unspecified -> 0 - } -} - -// Unspecified = 0, -// Fill = 1, -// Fit = 2, -// Tile = 3, -// Stretch = 4, -@Deprecated("This function will be removed in the future.") -internal fun scaleModeFromInt(value: Int): ScaleMode { - return when (value) { - 1 -> ScaleMode.Fill() - 2 -> ScaleMode.Fit() - 3 -> ScaleMode.Tile() - 4 -> ScaleMode.Stretch() - else -> ScaleMode.Unspecified() - } -} - -@Deprecated("This function will be removed in the future.") -enum class WindingRuleType { - NonZero, - EvenOdd, -} - -@Deprecated("This function will be removed in the future.") -// 1 -> NonZero, -// 2 -> EvenOdd -internal fun windingRuleFromInt(value: Int): WindingRuleType { - return when (value) { - 1 -> WindingRuleType.NonZero - else -> WindingRuleType.EvenOdd - } -} - -@Deprecated("This function will be removed in the future.") -enum class OverlayPositionEnum { - UNSPECIFIED, - CENTER, - TOP_LEFT, - TOP_CENTER, - TOP_RIGHT, - BOTTOM_LEFT, - BOTTOM_CENTER, - BOTTOM_RIGHT, - MANUAL, -} - -@Deprecated("This function will be removed in the future.") -internal fun overlayPositionEnumFromInt(value: Int): OverlayPositionEnum { - return when (value) { - 1 -> OverlayPositionEnum.CENTER - 2 -> OverlayPositionEnum.TOP_LEFT - 3 -> OverlayPositionEnum.TOP_CENTER - 4 -> OverlayPositionEnum.TOP_RIGHT - 5 -> OverlayPositionEnum.BOTTOM_LEFT - 6 -> OverlayPositionEnum.BOTTOM_CENTER - 7 -> OverlayPositionEnum.BOTTOM_RIGHT - 8 -> OverlayPositionEnum.MANUAL - else -> OverlayPositionEnum.UNSPECIFIED - } -} - -@Deprecated("This function will be removed in the future.") -enum class OverlayBackgroundInteractionEnum { - UNSPECIFIED, - NONE, - CLOSE_ON_CLICK_OUTSIDE, -} - -@Deprecated("This function will be removed in the future.") -internal fun overlayBackgroundInteractionFromInt(value: Int): OverlayBackgroundInteractionEnum { - return when (value) { - 1 -> OverlayBackgroundInteractionEnum.NONE - 2 -> OverlayBackgroundInteractionEnum.CLOSE_ON_CLICK_OUTSIDE - else -> OverlayBackgroundInteractionEnum.UNSPECIFIED - } -} - -@Deprecated("This function will be removed in the future.") -enum class NavigationType { - Unspecified, - Navigate, - Swap, - Overlay, - ScrollTo, - ChangeTo, -} - -@Deprecated("This function will be removed in the future.") -internal fun navigationTypeFromInt(value: Int): NavigationType { - return when (value) { - 1 -> NavigationType.Navigate - 2 -> NavigationType.Swap - 3 -> NavigationType.Overlay - 4 -> NavigationType.ScrollTo - 5 -> NavigationType.ChangeTo - else -> NavigationType.Unspecified - } -} - -@Deprecated("This function will be removed in the future.") -// Getter for Trigger which returns it's TriggerType if it's not null. -val Optional.type: TriggerType? - get() { - return this.getOrNull()?.trigger_type?.getOrNull() - } - -@Deprecated("This function will be removed in the future.") -internal fun strokeCapFromInt(value: Int): StrokeCap { - return when (value) { - 1 -> StrokeCap.None() - 2 -> StrokeCap.Round() - 3 -> StrokeCap.Square() - 4 -> StrokeCap.LineArrow() - 5 -> StrokeCap.TriangleArrow() - 6 -> StrokeCap.CircleFilled() - 7 -> StrokeCap.DiamondFilled() - else -> StrokeCap.Unspecified() - } -} - -@Deprecated("This function will be removed in the future.") -internal fun alignItemsFromInt(value: Int) = - when (value) { - 1 -> AlignItems.FlexStart() - 2 -> AlignItems.FlexEnd() - 3 -> AlignItems.Center() - 4 -> AlignItems.Baseline() - 5 -> AlignItems.Stretch() - else -> AlignItems.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun AlignItems.toInt() = - when (this) { - is AlignItems.FlexStart -> 1 - is AlignItems.FlexEnd -> 2 - is AlignItems.Center -> 3 - is AlignItems.Baseline -> 4 - is AlignItems.Stretch -> 5 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun alignSelfFromInt(value: Int) = - when (value) { - 1 -> AlignSelf.Auto() - 2 -> AlignSelf.FlexStart() - 3 -> AlignSelf.FlexEnd() - 4 -> AlignSelf.Center() - 5 -> AlignSelf.Baseline() - 6 -> AlignSelf.Stretch() - else -> AlignSelf.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun AlignSelf.toInt() = - when (this) { - is AlignSelf.Auto -> 1 - is AlignSelf.FlexStart -> 2 - is AlignSelf.FlexEnd -> 3 - is AlignSelf.Center -> 4 - is AlignSelf.Baseline -> 5 - is AlignSelf.Stretch -> 6 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun alignContentFromInt(value: Int) = - when (value) { - 1 -> AlignContent.FlexStart() - 2 -> AlignContent.FlexEnd() - 3 -> AlignContent.Center() - 4 -> AlignContent.Stretch() - 5 -> AlignContent.SpaceBetween() - 6 -> AlignContent.SpaceAround() - else -> AlignContent.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun AlignContent.toInt() = - when (this) { - is AlignContent.FlexStart -> 1 - is AlignContent.FlexEnd -> 2 - is AlignContent.Center -> 3 - is AlignContent.Stretch -> 4 - is AlignContent.SpaceBetween -> 5 - is AlignContent.SpaceAround -> 6 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun flexDirectionFromInt(value: Int) = - when (value) { - 1 -> FlexDirection.Row() - 2 -> FlexDirection.Column() - 3 -> FlexDirection.RowReverse() - 4 -> FlexDirection.ColumnReverse() - 5 -> FlexDirection.None() - else -> FlexDirection.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun FlexDirection.toInt() = - when (this) { - is FlexDirection.Row -> 1 - is FlexDirection.Column -> 2 - is FlexDirection.RowReverse -> 3 - is FlexDirection.ColumnReverse -> 4 - is FlexDirection.None -> 5 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun justifyContentFromInt(value: Int) = - when (value) { - 1 -> JustifyContent.FlexStart() - 2 -> JustifyContent.FlexEnd() - 3 -> JustifyContent.Center() - 4 -> JustifyContent.SpaceBetween() - 5 -> JustifyContent.SpaceAround() - 6 -> JustifyContent.SpaceEvenly() - else -> JustifyContent.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun JustifyContent.toInt() = - when (this) { - is JustifyContent.FlexStart -> 1 - is JustifyContent.FlexEnd -> 2 - is JustifyContent.Center -> 3 - is JustifyContent.SpaceBetween -> 4 - is JustifyContent.SpaceAround -> 5 - is JustifyContent.SpaceEvenly -> 6 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun positionTypeFromInt(value: Int) = - when (value) { - 1 -> PositionType.Relative() - 2 -> PositionType.Absolute() - else -> PositionType.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun PositionType.toInt() = - when (this) { - is PositionType.Relative -> 1 - is PositionType.Absolute -> 2 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun overflowDirectionFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.OverflowDirection.None() - 2 -> com.android.designcompose.serdegen.OverflowDirection.HorizontalScrolling() - 3 -> com.android.designcompose.serdegen.OverflowDirection.VerticalScrolling() - 4 -> com.android.designcompose.serdegen.OverflowDirection.HorizontalAndVerticalScrolling() - else -> com.android.designcompose.serdegen.OverflowDirection.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.OverflowDirection.toInt() = - when (this) { - is com.android.designcompose.serdegen.OverflowDirection.None -> 1 - is com.android.designcompose.serdegen.OverflowDirection.HorizontalScrolling -> 2 - is com.android.designcompose.serdegen.OverflowDirection.VerticalScrolling -> 3 - is com.android.designcompose.serdegen.OverflowDirection.HorizontalAndVerticalScrolling -> 4 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun layoutSizingFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.LayoutSizing.Fixed() - 2 -> com.android.designcompose.serdegen.LayoutSizing.Hug() - 3 -> com.android.designcompose.serdegen.LayoutSizing.Fill() - else -> com.android.designcompose.serdegen.LayoutSizing.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.LayoutSizing.toInt() = - when (this) { - is com.android.designcompose.serdegen.LayoutSizing.Fixed -> 1 - is com.android.designcompose.serdegen.LayoutSizing.Hug -> 2 - is com.android.designcompose.serdegen.LayoutSizing.Fill -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun fontStyleFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.FontStyle.Normal() - 2 -> com.android.designcompose.serdegen.FontStyle.Italic() - 3 -> com.android.designcompose.serdegen.FontStyle.Oblique() - else -> com.android.designcompose.serdegen.FontStyle.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun FontStyle.toInt() = - when (this) { - is FontStyle.Normal -> 1 - is FontStyle.Italic -> 2 - is FontStyle.Oblique -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun textDecorationFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.TextDecoration.None() - 2 -> com.android.designcompose.serdegen.TextDecoration.Underline() - 3 -> com.android.designcompose.serdegen.TextDecoration.Strikethrough() - else -> com.android.designcompose.serdegen.TextDecoration.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.TextDecoration.toInt() = - when (this) { - is com.android.designcompose.serdegen.TextDecoration.None -> 1 - is com.android.designcompose.serdegen.TextDecoration.Underline -> 2 - is com.android.designcompose.serdegen.TextDecoration.Strikethrough -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun blendModeFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.BlendMode.PassThrough() - 2 -> com.android.designcompose.serdegen.BlendMode.Normal() - 3 -> com.android.designcompose.serdegen.BlendMode.Darken() - 4 -> com.android.designcompose.serdegen.BlendMode.Multiply() - 5 -> com.android.designcompose.serdegen.BlendMode.LinearBurn() - 6 -> com.android.designcompose.serdegen.BlendMode.ColorBurn() - 7 -> com.android.designcompose.serdegen.BlendMode.Lighten() - 8 -> com.android.designcompose.serdegen.BlendMode.Screen() - 9 -> com.android.designcompose.serdegen.BlendMode.LinearDodge() - 10 -> com.android.designcompose.serdegen.BlendMode.ColorDodge() - 11 -> com.android.designcompose.serdegen.BlendMode.Overlay() - 12 -> com.android.designcompose.serdegen.BlendMode.SoftLight() - 13 -> com.android.designcompose.serdegen.BlendMode.HardLight() - 14 -> com.android.designcompose.serdegen.BlendMode.Difference() - 15 -> com.android.designcompose.serdegen.BlendMode.Exclusion() - 16 -> com.android.designcompose.serdegen.BlendMode.Hue() - 17 -> com.android.designcompose.serdegen.BlendMode.Saturation() - 18 -> com.android.designcompose.serdegen.BlendMode.Color() - 19 -> com.android.designcompose.serdegen.BlendMode.Luminosity() - else -> com.android.designcompose.serdegen.BlendMode.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun BlendMode.toInt() = - when (this) { - is BlendMode.PassThrough -> 1 - is BlendMode.Normal -> 2 - is BlendMode.Darken -> 3 - is BlendMode.Multiply -> 4 - is BlendMode.LinearBurn -> 5 - is BlendMode.ColorBurn -> 6 - is BlendMode.Lighten -> 7 - is BlendMode.Screen -> 8 - is BlendMode.LinearDodge -> 9 - is BlendMode.ColorDodge -> 10 - is BlendMode.Overlay -> 11 - is BlendMode.SoftLight -> 12 - is BlendMode.HardLight -> 13 - is BlendMode.Difference -> 14 - is BlendMode.Exclusion -> 15 - is BlendMode.Hue -> 16 - is BlendMode.Saturation -> 17 - is BlendMode.Color -> 18 - is BlendMode.Luminosity -> 19 - - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun overflowFromInt(value: Int) = - when (value) { - 1 -> Overflow.Visible() - 2 -> Overflow.Hidden() - 3 -> Overflow.Scroll() - else -> Overflow.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun Overflow.toInt() = - when (this) { - is Overflow.Visible -> 1 - is Overflow.Hidden -> 2 - is Overflow.Scroll -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun textAlignFromInt(value: Int) = - when (value) { - 1 -> TextAlign.Left() - 2 -> TextAlign.Center() - 3 -> TextAlign.Right() - else -> TextAlign.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun TextAlign.toInt() = - when (this) { - is TextAlign.Left -> 1 - is TextAlign.Center -> 2 - is TextAlign.Right -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun textAlignVerticalFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.TextAlignVertical.Top() - 2 -> com.android.designcompose.serdegen.TextAlignVertical.Center() - 3 -> com.android.designcompose.serdegen.TextAlignVertical.Bottom() - else -> com.android.designcompose.serdegen.TextAlignVertical.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.TextAlignVertical.toInt() = - when (this) { - is com.android.designcompose.serdegen.TextAlignVertical.Top -> 1 - is com.android.designcompose.serdegen.TextAlignVertical.Center -> 2 - is com.android.designcompose.serdegen.TextAlignVertical.Bottom -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun textOverflowFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.TextOverflow.Clip() - 2 -> com.android.designcompose.serdegen.TextOverflow.Ellipsis() - else -> com.android.designcompose.serdegen.TextOverflow.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.TextOverflow.toInt() = - when (this) { - is com.android.designcompose.serdegen.TextOverflow.Clip -> 1 - is com.android.designcompose.serdegen.TextOverflow.Ellipsis -> 2 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun pointerEventsFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.PointerEvents.Auto() - 2 -> com.android.designcompose.serdegen.PointerEvents.None() - 3 -> com.android.designcompose.serdegen.PointerEvents.Inherit() - else -> com.android.designcompose.serdegen.PointerEvents.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.PointerEvents.toInt() = - when (this) { - is com.android.designcompose.serdegen.PointerEvents.Auto -> 1 - is com.android.designcompose.serdegen.PointerEvents.None -> 2 - is com.android.designcompose.serdegen.PointerEvents.Inherit -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun displayFromInt(value: Int) = - when (value) { - 1 -> Display.Flex() - 2 -> Display.None() - else -> Display.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun Display.toInt() = - when (this) { - is Display.Flex -> 1 - is Display.None -> 2 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun flexWrapFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.FlexWrap.NoWrap() - 2 -> com.android.designcompose.serdegen.FlexWrap.Wrap() - 3 -> com.android.designcompose.serdegen.FlexWrap.WrapReverse() - else -> com.android.designcompose.serdegen.FlexWrap.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun com.android.designcompose.serdegen.FlexWrap.toInt() = - when (this) { - is com.android.designcompose.serdegen.FlexWrap.NoWrap -> 1 - is com.android.designcompose.serdegen.FlexWrap.Wrap -> 2 - is com.android.designcompose.serdegen.FlexWrap.WrapReverse -> 3 - else -> 0 - } - -@Deprecated("This function will be removed in the future.") -internal fun gridLayoutTypeFromInt(value: Int) = - when (value) { - 1 -> com.android.designcompose.serdegen.GridLayoutType.FixedColumns() - 2 -> com.android.designcompose.serdegen.GridLayoutType.FixedRows() - 3 -> com.android.designcompose.serdegen.GridLayoutType.AutoColumns() - 4 -> com.android.designcompose.serdegen.GridLayoutType.AutoRows() - 5 -> com.android.designcompose.serdegen.GridLayoutType.Horizontal() - 6 -> com.android.designcompose.serdegen.GridLayoutType.Vertical() - else -> com.android.designcompose.serdegen.GridLayoutType.Unspecified() - } - -@Deprecated("This function will be removed in the future.") -internal fun RenderMethod.toInt() = - when (this) { - is RenderMethod.None -> 1 - is RenderMethod.PixelPerfect -> 2 - else -> 0 - } diff --git a/designcompose/src/main/java/com/android/designcompose/proto/ProtoUtils.kt b/designcompose/src/main/java/com/android/designcompose/proto/ProtoUtils.kt deleted file mode 100644 index 0c2f8de16..000000000 --- a/designcompose/src/main/java/com/android/designcompose/proto/ProtoUtils.kt +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.proto - -import com.android.designcompose.definition.view.NodeStyle -import com.android.designcompose.definition.view.ViewStyle -import com.android.designcompose.serdegen.Background -import com.android.designcompose.serdegen.BackgroundType -import com.android.designcompose.serdegen.Box -import com.android.designcompose.serdegen.FontWeight -import com.android.designcompose.serdegen.ItemSpacing -import com.android.designcompose.serdegen.LayoutStyle -import com.android.designcompose.serdegen.NumOrVar -import com.android.designcompose.serdegen.NumOrVarType -import com.android.designcompose.serdegen.Shape -import com.android.designcompose.serdegen.Text -import com.android.designcompose.serdegen.Trigger -import com.android.designcompose.serdegen.TriggerType -import com.android.designcompose.serdegen.ViewData -import com.android.designcompose.serdegen.ViewDataType -import com.android.designcompose.serdegen.ViewShape -import java.util.Optional -import kotlin.jvm.optionals.getOrNull - -// -// Background Helper functions -// - -internal inline fun Background.isType(): Boolean { - val bgType = background_type.getOrNull() - bgType?.let { - return it is T - } - return false -} - -@Deprecated("This function will be removed in the future.") -internal fun Background.getType(): BackgroundType { - val bgType = background_type.getOrNull() - return bgType ?: BackgroundType.None(com.novi.serde.Unit()) -} - -@Deprecated("This function will be removed in the future.") -internal fun ViewShape.get(): Shape { - return this.shape.orElseThrow { - NoSuchFieldException("Malformed data: ViewShape has no shape field") - } -} - -@Deprecated("This function will be removed in the future.") -internal fun newViewShapeRect(isMask: Boolean) = ViewShape(Optional.of(Shape.Rect(Box(isMask)))) - -@Deprecated("This function will be removed in the future.") -internal fun newFontWeight(weight: Float) = - FontWeight(Optional.of(NumOrVar(Optional.of(NumOrVarType.Num(weight))))) - -@Deprecated("This function will be removed in the future.") -internal fun ItemSpacing.type() = item_spacing_type.getOrNull() - -@Deprecated("This function will be removed in the future.") -internal fun newNumOrVar(value: Float) = NumOrVar(Optional.of(NumOrVarType.Num(value))) - -@Deprecated("This function will be removed in the future.") -internal val ViewStyle.nodeStyle: NodeStyle - get() = - this.node_style.orElseThrow { - NoSuchFieldException("Malformed data: ViewStyle has no node_style field") - } - -@Deprecated("This function will be removed in the future.") -internal val ViewStyle.layoutStyle: LayoutStyle - get() = - this.layout_style.orElseThrow { - NoSuchFieldException("Malformed data: ViewStyle has no layout_style field") - } - -@Deprecated("This function will be removed in the future.") -internal fun Optional.getType() = this.get().view_data_type.get() - -@Deprecated("This function will be removed in the future.") -internal fun Optional.ifContainerGetShape(): Shape? { - return (this.get().view_data_type.get() as? ViewDataType.Container)?.value?.shape?.get()?.get() -} - -@Deprecated("This function will be removed in the future.") -internal fun Optional.ifTextGetText(): Text? { - return (this.get().view_data_type.get() as? ViewDataType.Text)?.value -} - -@Deprecated("This function will be removed in the future.") -internal fun Optional.isSupportedInteraction() = - this.type is TriggerType.AfterTimeout || - this.type is TriggerType.Click || - this.type is TriggerType.Press - -internal fun Optional.isPressOrClick() = - this.type is TriggerType.Press || this.type is TriggerType.Click - -internal fun Optional.isTimeout() = this.type is TriggerType.AfterTimeout - -internal fun T?.getOrThrow(field: String): T { - if (this == null) throw NoSuchFieldException("Malformed data: $field unset") - return this -} diff --git a/designcompose/src/main/java/com/android/designcompose/squoosh/SquooshText.kt b/designcompose/src/main/java/com/android/designcompose/squoosh/SquooshText.kt index 63ecc10bd..5f5a4f729 100644 --- a/designcompose/src/main/java/com/android/designcompose/squoosh/SquooshText.kt +++ b/designcompose/src/main/java/com/android/designcompose/squoosh/SquooshText.kt @@ -42,16 +42,12 @@ import com.android.designcompose.definition.element.LineHeight.LineHeightTypeCas import com.android.designcompose.definition.element.TextDecoration import com.android.designcompose.definition.element.weightOrNull import com.android.designcompose.definition.view.View -import com.android.designcompose.definition.view.fontSizeOrNull import com.android.designcompose.definition.view.fontWeightOrNull -import com.android.designcompose.definition.view.styleOrNull -import com.android.designcompose.definition.view.textColorOrNull import com.android.designcompose.definition.view.textShadowOrNull import com.android.designcompose.getText import com.android.designcompose.getTextState import com.android.designcompose.getTextStyle import com.android.designcompose.getValue -import com.android.designcompose.proto.getOrThrow import com.android.designcompose.utils.asBrush import com.android.designcompose.utils.blurFudgeFactor import com.android.designcompose.utils.getTextContent @@ -150,23 +146,19 @@ internal fun squooshComputeTextInfo( } else if (v.data.hasStyledText()) { val builder = AnnotatedString.Builder() for (run in getTextContent(appContext, v.data.styledText)) { - val style = run.styleOrNull.getOrThrow("StyledTextRun") + val style = run.style val textBrushAndOpacity = - style.textColorOrNull - .getOrThrow("textColor") + style.textColor .asBrush(appContext, document, density.density, variableState) val fontWeight = - style.fontWeightOrNull - .getOrThrow("fontWeight") - .weight + style.fontWeight.weight .getValue(variableState) builder.pushStyle( (SpanStyle( brush = textBrushAndOpacity?.first, alpha = textBrushAndOpacity?.second ?: 1.0f, fontSize = - style.fontSizeOrNull - .getOrThrow("fontSize") + style.fontSize .getValue(variableState) .sp, fontWeight = FontWeight(fontWeight.roundToInt()), @@ -174,6 +166,7 @@ internal fun squooshComputeTextInfo( when (style.fontStyle) { com.android.designcompose.definition.element.FontStyle .FONT_STYLE_ITALIC -> FontStyle.Italic + else -> FontStyle.Normal }, fontFamily = @@ -190,12 +183,14 @@ internal fun squooshComputeTextInfo( when (style.textDecoration) { TextDecoration.TEXT_DECORATION_UNDERLINE -> androidx.compose.ui.text.style.TextDecoration.Underline + TextDecoration.TEXT_DECORATION_STRIKETHROUGH -> androidx.compose.ui.text.style.TextDecoration.LineThrough + else -> androidx.compose.ui.text.style.TextDecoration.None }, // platformStyle = PlatformSpanStyle(includeFontPadding = false), - )) + )), ) builder.append(run.text) builder.pop()