"""Stable roles for Chrome's otherwise indistinguishable top-level windows.""" from classroom_sway import matches CHROME_PATTERNS = ( r"google-chrome", r"chrome", r"chrome-.*", r"google-chrome-.*", ) def chrome_windows(current): """Chrome containers in Sway tree order, without using page titles.""" return [window for window in current if matches(window, CHROME_PATTERNS)] class ChromeRoles: """Remember the first Chrome container and classify every later one.""" def __init__(self): self.primary = None self.secondary = [] def update(self, current): identifiers = [window["id"] for window in chrome_windows(current)] previous_primary = self.primary previous_secondary = list(self.secondary) promoted = None if self.primary not in identifiers: self.primary = identifiers[0] if identifiers else None if self.primary is not None and previous_primary is not None: promoted = self.primary self.secondary = [ identifier for identifier in previous_secondary if identifier in identifiers and identifier != self.primary ] new_secondary = [] for identifier in identifiers: if identifier == self.primary or identifier in self.secondary: continue self.secondary.append(identifier) new_secondary.append(identifier) removed = [ identifier for identifier in previous_secondary if identifier not in identifiers ] return { "primary": self.primary, "promoted": promoted, "new_secondary": new_secondary, "removed": removed, "active_secondary": self.secondary[-1] if self.secondary else None, }