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/ui_rect'; 18import {RectShowState} from './rect_show_state'; 19 20export class RectFilter { 21 private forcedStates = new Map<string, RectShowState>(); 22 23 constructor(private convertToForcedStateKey: (id: string) => string) {} 24 25 filterRects( 26 rects: UiRect[], 27 isOnlyVisibleMode: boolean, 28 isIgnoreRectShowStateMode: boolean, 29 isOnlyWithContentMode: boolean, 30 ): UiRect[] { 31 if ( 32 !isOnlyVisibleMode && 33 isIgnoreRectShowStateMode && 34 !isOnlyWithContentMode 35 ) { 36 return rects; 37 } 38 return rects.filter((rect) => { 39 const satisfiesHasContent = rect.hasContent || rect.isDisplay; 40 if (isOnlyWithContentMode && !satisfiesHasContent) { 41 return false; 42 } 43 44 const satisfiesOnlyVisible = rect.isDisplay || rect.isVisible; 45 const key = this.convertToForcedStateKey(rect.id); 46 const forceHidden = this.forcedStates.get(key) === RectShowState.HIDE; 47 const forceShow = this.forcedStates.get(key) === RectShowState.SHOW; 48 49 if (isOnlyVisibleMode && !isIgnoreRectShowStateMode) { 50 return forceShow || (satisfiesOnlyVisible && !forceHidden); 51 } 52 if (isOnlyVisibleMode) { 53 return satisfiesOnlyVisible; 54 } 55 return !forceHidden; 56 }); 57 } 58 59 getRectIdToShowState( 60 allRects: UiRect[], 61 shownRects: UiRect[], 62 ): Map<string, RectShowState> { 63 const rectIdToShowState = new Map<string, RectShowState>(); 64 allRects.forEach((rect) => { 65 const key = this.convertToForcedStateKey(rect.id); 66 const forcedState = this.forcedStates.get(key); 67 if (forcedState !== undefined) { 68 rectIdToShowState.set(rect.id, forcedState); 69 return; 70 } 71 const isShown = shownRects.some((other) => other.id === rect.id); 72 const showState = isShown ? RectShowState.SHOW : RectShowState.HIDE; 73 rectIdToShowState.set(rect.id, showState); 74 }); 75 return rectIdToShowState; 76 } 77 78 updateRectShowState(id: string, newShowState: RectShowState) { 79 this.forcedStates.set(this.convertToForcedStateKey(id), newShowState); 80 } 81 82 clear() { 83 this.forcedStates.clear(); 84 } 85} 86