1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.internal.widget.remotecompose.core.operations.layout.modifiers; 17 18 import static com.android.internal.widget.remotecompose.core.documentation.DocumentedOperation.FLOAT; 19 import static com.android.internal.widget.remotecompose.core.documentation.DocumentedOperation.INT; 20 21 import android.annotation.NonNull; 22 23 import com.android.internal.widget.remotecompose.core.Operation; 24 import com.android.internal.widget.remotecompose.core.Operations; 25 import com.android.internal.widget.remotecompose.core.WireBuffer; 26 import com.android.internal.widget.remotecompose.core.documentation.DocumentationBuilder; 27 import com.android.internal.widget.remotecompose.core.serialize.MapSerializer; 28 import com.android.internal.widget.remotecompose.core.serialize.SerializeTags; 29 30 import java.util.List; 31 32 /** Set the width dimension on a component */ 33 public class WidthModifierOperation extends DimensionModifierOperation { 34 private static final int OP_CODE = Operations.MODIFIER_WIDTH; 35 public static final String CLASS_NAME = "WidthModifierOperation"; 36 private WidthInModifierOperation mWidthIn = null; 37 38 /** 39 * The name of the class 40 * 41 * @return the name 42 */ 43 @NonNull name()44 public static String name() { 45 return CLASS_NAME; 46 } 47 48 /** 49 * The OP_CODE for this command 50 * 51 * @return the opcode 52 */ id()53 public static int id() { 54 return OP_CODE; 55 } 56 57 /** 58 * Write the operation to the buffer 59 * 60 * @param buffer a WireBuffer 61 * @param type the type of dimension rule (DimensionModifierOperation.Type) 62 * @param value the value of the dimension 63 */ apply(@onNull WireBuffer buffer, int type, float value)64 public static void apply(@NonNull WireBuffer buffer, int type, float value) { 65 buffer.start(OP_CODE); 66 buffer.writeInt(type); 67 buffer.writeFloat(value); 68 } 69 70 /** 71 * Read this operation and add it to the list of operations 72 * 73 * @param buffer the buffer to read 74 * @param operations the list of operations that will be added to 75 */ read(@onNull WireBuffer buffer, @NonNull List<Operation> operations)76 public static void read(@NonNull WireBuffer buffer, @NonNull List<Operation> operations) { 77 Type type = Type.fromInt(buffer.readInt()); 78 float value = buffer.readFloat(); 79 Operation op = new WidthModifierOperation(type, value); 80 operations.add(op); 81 } 82 WidthModifierOperation(@onNull Type type, float value)83 public WidthModifierOperation(@NonNull Type type, float value) { 84 super(type, value); 85 } 86 87 @Override write(@onNull WireBuffer buffer)88 public void write(@NonNull WireBuffer buffer) { 89 apply(buffer, mType.ordinal(), mValue); 90 } 91 WidthModifierOperation(@onNull Type type)92 public WidthModifierOperation(@NonNull Type type) { 93 super(type); 94 } 95 WidthModifierOperation(float value)96 public WidthModifierOperation(float value) { 97 super(value); 98 } 99 100 @NonNull 101 @Override toString()102 public String toString() { 103 return "Width(" + mType + ", " + mValue + ")"; 104 } 105 106 @NonNull 107 @Override serializedName()108 public String serializedName() { 109 return "WIDTH"; 110 } 111 112 /** 113 * Populate the documentation with a description of this operation 114 * 115 * @param doc to append the description to. 116 */ documentation(@onNull DocumentationBuilder doc)117 public static void documentation(@NonNull DocumentationBuilder doc) { 118 doc.operation("Modifier Operations", OP_CODE, CLASS_NAME) 119 .description("define the animation") 120 .field(INT, "type", "") 121 .field(FLOAT, "value", ""); 122 } 123 124 /** 125 * Set width in constraints 126 * 127 * @param widthInConstraints width constraints 128 */ setWidthIn(WidthInModifierOperation widthInConstraints)129 public void setWidthIn(WidthInModifierOperation widthInConstraints) { 130 mWidthIn = widthInConstraints; 131 } 132 133 /** 134 * Returns width in constraints 135 * 136 * @return width in constraints 137 */ getWidthIn()138 public WidthInModifierOperation getWidthIn() { 139 return mWidthIn; 140 } 141 142 @Override serialize(MapSerializer serializer)143 public void serialize(MapSerializer serializer) { 144 serializer 145 .addTags(SerializeTags.MODIFIER) 146 .addType("WidthModifierOperation") 147 .add("width", mValue, mOutValue) 148 .add("dimensionModifierType", mType); 149 } 150 } 151