1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7import dataclasses 8 9from typing_extensions import Self 10 11from crossbench.benchmarks.loading.point import Point 12 13 14@dataclasses.dataclass(frozen=False) 15# Represents a rectangular section of the device's display. 16class DisplayRectangle: 17 # The top left corner of the rectangle. 18 origin: Point 19 # The width in pixels of the rectangle. 20 width: int 21 # The height in pixels of the rectangle. 22 height: int 23 24 # Stretches or squishes the rectangle by |factor| 25 def __mul__(self, factor: float) -> DisplayRectangle: 26 return DisplayRectangle( 27 Point(round(self.origin.x * factor), round(self.origin.y * factor)), 28 round(self.width * factor), round(self.height * factor)) 29 30 __rmul__ = __mul__ 31 32 def __bool__(self) -> bool: 33 return self.width != 0 and self.height != 0 34 35 # Translates the rectangle into |other| 36 def shift_by(self, other: Self) -> DisplayRectangle: 37 return DisplayRectangle( 38 Point(self.origin.x + other.origin.x, self.origin.y + other.origin.y), 39 self.width, self.height) 40 41 @property 42 def left(self) -> int: 43 return self.origin.x 44 45 @property 46 def right(self) -> int: 47 return self.origin.x + self.width 48 49 @property 50 def top(self) -> int: 51 return self.origin.y 52 53 @property 54 def bottom(self) -> int: 55 return self.origin.y + self.height 56 57 @property 58 def mid_x(self) -> int: 59 return round(self.origin.x + (self.width / 2)) 60 61 @property 62 def mid_y(self) -> int: 63 return round(self.origin.y + (self.height / 2)) 64 65 @property 66 def middle(self) -> Point: 67 return Point(self.mid_x, self.mid_y) 68