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 17import {UiRect} from 'viewers/components/rects/types2d'; 18import {RectShowState} from './rect_show_state'; 19 20export class RectFilter { 21 private forcedStates = new Map<string, RectShowState>(); 22 23 filterRects( 24 rects: UiRect[], 25 isOnlyVisibleMode: boolean, 26 isIgnoreNonHiddenMode: boolean, 27 ): UiRect[] { 28 if (!(isOnlyVisibleMode || !isIgnoreNonHiddenMode)) { 29 return rects; 30 } 31 return rects.filter((rect) => { 32 const satisfiesOnlyVisible = rect.isDisplay || rect.isVisible; 33 const forceHidden = this.forcedStates.get(rect.id) === RectShowState.HIDE; 34 const forceShow = this.forcedStates.get(rect.id) === RectShowState.SHOW; 35 36 if (isOnlyVisibleMode && !isIgnoreNonHiddenMode) { 37 return forceShow || (satisfiesOnlyVisible && !forceHidden); 38 } 39 if (isOnlyVisibleMode) { 40 return satisfiesOnlyVisible; 41 } 42 return !forceHidden; 43 }); 44 } 45 46 getRectIdToShowState( 47 allRects: UiRect[], 48 shownRects: UiRect[], 49 ): Map<string, RectShowState> { 50 const rectIdToShowState = new Map<string, RectShowState>(); 51 allRects.forEach((rect) => { 52 const forcedState = this.forcedStates.get(rect.id); 53 if (forcedState !== undefined) { 54 rectIdToShowState.set(rect.id, forcedState); 55 return; 56 } 57 const isShown = shownRects.some((other) => other.id === rect.id); 58 const showState = isShown ? RectShowState.SHOW : RectShowState.HIDE; 59 rectIdToShowState.set(rect.id, showState); 60 }); 61 return rectIdToShowState; 62 } 63 64 updateRectShowState(id: string, newShowState: RectShowState) { 65 this.forcedStates.set(id, newShowState); 66 } 67} 68