|
| 1 | +// Copyright 2023 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import { |
| 6 | + createElement, |
| 7 | + Overlay, |
| 8 | +} from './common.js'; |
| 9 | +import {generateLegibleTextColor} from './css_grid_label_helpers.js'; |
| 10 | + |
| 11 | +interface WindowControlsOverlayConfig { |
| 12 | + selectedPlatform: string; |
| 13 | + themeColor: string; |
| 14 | +} |
| 15 | + |
| 16 | +export class WindowControlsOverlay extends Overlay { |
| 17 | + private windowsToolBar!: HTMLElement; |
| 18 | + private linuxToolBar!: HTMLElement; |
| 19 | + private macToolbarRight!: HTMLElement; |
| 20 | + private macToolbarLeft!: HTMLElement; |
| 21 | + |
| 22 | + constructor(window: Window, style: CSSStyleSheet[] = []) { |
| 23 | + super(window, style); |
| 24 | + } |
| 25 | + |
| 26 | + override install() { |
| 27 | + const windowLinuxToolbarIcons = ['chevron', 'ellipsis', 'minimize', 'maximize', 'close']; |
| 28 | + const macLeftToolbarIcons = ['mac-close', 'mac-minimize', 'mac-maximize']; |
| 29 | + const macRightToolbarIcons = ['mac-chevron', 'mac-ellipsis']; |
| 30 | + |
| 31 | + this.windowsToolBar = createHiddenToolBarRow('windows', 'right', windowLinuxToolbarIcons); |
| 32 | + this.linuxToolBar = createHiddenToolBarRow('linux', 'right', windowLinuxToolbarIcons); |
| 33 | + this.macToolbarRight = createHiddenToolBarRow('mac', 'right', macRightToolbarIcons); |
| 34 | + this.macToolbarLeft = createHiddenToolBarRow('mac', 'left', macLeftToolbarIcons); |
| 35 | + |
| 36 | + this.document.body.append(this.windowsToolBar, this.linuxToolBar, this.macToolbarLeft, this.macToolbarRight); |
| 37 | + super.install(); |
| 38 | + } |
| 39 | + |
| 40 | + override uninstall() { |
| 41 | + this.document.body.innerHTML = ''; |
| 42 | + super.uninstall(); |
| 43 | + } |
| 44 | + |
| 45 | + drawWindowControlsOverlay(config: WindowControlsOverlayConfig) { |
| 46 | + // Clear all overlays |
| 47 | + this.clearOverlays(); |
| 48 | + |
| 49 | + // Display the Window Controls Overlay |
| 50 | + if (config.selectedPlatform === 'Windows') { |
| 51 | + revealElement(this.windowsToolBar); |
| 52 | + } else if (config.selectedPlatform === 'Linux') { |
| 53 | + revealElement(this.linuxToolBar); |
| 54 | + } else if (config.selectedPlatform === 'Mac') { |
| 55 | + revealElement(this.macToolbarLeft); |
| 56 | + revealElement(this.macToolbarRight); |
| 57 | + } |
| 58 | + |
| 59 | + // Set the theme Color |
| 60 | + this.document.documentElement.style.setProperty('--wco-theme-color', config.themeColor); |
| 61 | + this.document.documentElement.style.setProperty('--wco-icon-color', generateLegibleTextColor(config.themeColor)); |
| 62 | + } |
| 63 | + |
| 64 | + clearOverlays() { |
| 65 | + hideElement(this.linuxToolBar); |
| 66 | + hideElement(this.windowsToolBar); |
| 67 | + hideElement(this.macToolbarLeft); |
| 68 | + hideElement(this.macToolbarRight); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function hideElement(element: HTMLElement): void { |
| 73 | + element.classList.add('hidden'); |
| 74 | +} |
| 75 | + |
| 76 | +function revealElement(element: HTMLElement): void { |
| 77 | + element.classList.remove('hidden'); |
| 78 | +} |
| 79 | + |
| 80 | +function createDivOfIcons(icons: string[]): HTMLElement { |
| 81 | + const toolbar = createElement('div'); |
| 82 | + |
| 83 | + for (const iconName of icons) { |
| 84 | + const icon = createElement('div'); |
| 85 | + icon.id = iconName; |
| 86 | + icon.classList.add('image'); |
| 87 | + toolbar.append(icon); |
| 88 | + } |
| 89 | + |
| 90 | + return toolbar; |
| 91 | +} |
| 92 | + |
| 93 | +function createHiddenToolBarRow(osType: string, location: string, icons: string[]): HTMLElement { |
| 94 | + const toolbar = createDivOfIcons(icons); |
| 95 | + |
| 96 | + toolbar.classList.add('image-group'); |
| 97 | + toolbar.classList.add(`image-group-${location}`); |
| 98 | + toolbar.classList.add(`${osType}-${location}-image-group`); |
| 99 | + toolbar.classList.add('hidden'); |
| 100 | + |
| 101 | + return toolbar; |
| 102 | +} |
0 commit comments