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.semantics; 17 18 import android.annotation.Nullable; 19 20 import com.android.internal.widget.remotecompose.core.RemoteContext; 21 import com.android.internal.widget.remotecompose.core.operations.layout.Component; 22 23 /** 24 * Interface for components that support scrolling. 25 * 26 * <p>This interface defines the contract for components that can be scrolled, either horizontally, 27 * vertically. It provides methods for checking scroll support, performing scroll operations, and 28 * querying the scroll range and current position. 29 */ 30 public interface ScrollableComponent extends AccessibilitySemantics { 31 int SCROLL_NONE = 0; 32 int SCROLL_HORIZONTAL = 1; 33 int SCROLL_VERTICAL = 2; 34 35 /** Indicates whether this component supports scrolling by a specified offset. */ supportsScrollByOffset()36 default boolean supportsScrollByOffset() { 37 return true; 38 } 39 40 /** 41 * Scrolls the content by the specified offset. 42 * 43 * @param offset The amount to scroll by in pixels. Positive values indicate scrolling down or 44 * to the right, while negative values indicate scrolling up or to the left. 45 * @return The offset value that was consumed by this component scrolling. 46 */ scrollByOffset(RemoteContext context, int offset)47 default int scrollByOffset(RemoteContext context, int offset) { 48 return offset; 49 } 50 51 /** 52 * Scrolls the content in the specified direction. 53 * 54 * @param direction the direction to scroll 55 * @return whether a scroll was possible 56 */ scrollDirection(RemoteContext context, ScrollDirection direction)57 default boolean scrollDirection(RemoteContext context, ScrollDirection direction) { 58 return false; 59 } 60 61 /** 62 * Show a child with the given ID on the screen, typically scrolling so it's fully on screen. 63 * 64 * @param child The child (including nested) to check for visibility. 65 * @return {@code true} if the child with the given ID could be shown on screen; {@code false} 66 * otherwise. 67 */ showOnScreen(RemoteContext context, Component child)68 default boolean showOnScreen(RemoteContext context, Component child) { 69 return false; 70 } 71 72 /** Gets the current scroll direction. */ scrollDirection()73 int scrollDirection(); 74 75 /** Represents a range along a scroll axis. */ getScrollAxisRange()76 default @Nullable ScrollAxisRange getScrollAxisRange() { 77 return null; 78 } 79 80 /** 81 * Represents the state of a scrollable axis, including its current value, maximum value, and 82 * whether it can scroll forward or backward. 83 */ 84 class ScrollAxisRange { 85 private float mValue; 86 private float mMaxValue; 87 private boolean mCanScrollForward; 88 private boolean mCanScrollBackwards; 89 90 /** 91 * Represents the range and scroll capabilities of a single axis (e.g., horizontal or 92 * vertical) in a scrollable area. 93 */ ScrollAxisRange( float value, float maxValue, boolean canScrollForward, boolean canScrollBackwards)94 public ScrollAxisRange( 95 float value, float maxValue, boolean canScrollForward, boolean canScrollBackwards) { 96 this.mValue = value; 97 this.mMaxValue = maxValue; 98 this.mCanScrollForward = canScrollForward; 99 this.mCanScrollBackwards = canScrollBackwards; 100 } 101 102 /** Returns the current scroll offset held by this scrollable component. */ getmValue()103 public float getmValue() { 104 return mValue; 105 } 106 107 /** Returns the maximum scroll offset possible by this scrollable component. */ getMaxValue()108 public float getMaxValue() { 109 return mMaxValue; 110 } 111 112 /** Returns {@code true} if this scrollable component can scroll forward. */ canScrollForward()113 public boolean canScrollForward() { 114 return mCanScrollForward; 115 } 116 117 /** Returns {@code true} if this scrollable component can scroll backwards. */ canScrollBackwards()118 public boolean canScrollBackwards() { 119 return mCanScrollBackwards; 120 } 121 } 122 123 enum ScrollDirection { 124 FORWARD, 125 BACKWARD, 126 UP, 127 DOWN, 128 LEFT, 129 RIGHT, 130 } 131 } 132