return;
    }

    const state = {
      active: false,
      fullscreen: false,
      customtitlebar: false,
      toolbarLuminanceCache: new Map(),
    };

    _windowStateMap.set(window, state);

    window.addEventListener("nativethemechange", this);
    window.addEventListener("activate", this);
    window.addEventListener("deactivate", this);
    window.addEventListener("toolbarvisibilitychange", this);
    window.addEventListener("windowlwthemeupdate", this);

    // If the window isn't active now, we assume that it has never been active
    // before and will soon become active such that inferFromText will be
    // called from the initial activate event.
    if (Services.focus.activeWindow == window) {
      this.inferFromText("activate", window);
    }
  },

  uninit(window) {
    const state = _windowStateMap.get(window);
    if (!state) {
      return;
    }

    window.removeEventListener("nativethemechange", this);
    window.removeEventListener("activate", this);
    window.removeEventListener("deactivate", this);
    window.removeEventListener("toolbarvisibilitychange", this);
    window.removeEventListener("windowlwthemeupdate", this);

    _windowStateMap.delete(window);
  },

  handleEvent(event) {
    const window = event.target.ownerGlobal;
    switch (event.type) {
      case "activate":
      case "deactivate":
      case "nativethemechange":
      case "windowlwthemeupdate":
        this.inferFromText(event.type, window);
        break;
      case "toolbarvisibilitychange":
        this.inferFromText(event.type, window, event.visible);
        break;
    }
  },

  inferFromText(reason, window, reasonValue) {
    const state = _windowStateMap.get(window);

    if (!state) {
      return;
    }

    switch (reason) {
      case "activate": // falls through
      case "deactivate":
        state.active = reason === "activate";
        break;
      case "fullscreen":
        state.fullscreen = reasonValue;
        break;
      case "nativethemechange":
      case "windowlwthemeupdate":
        // theme change, we'll need to recalculate all color values
        state.toolbarLuminanceCache.clear();
        break;
      case "toolbarvisibilitychange":
        // toolbar changes dont require reset of the cached color values
        break;
      case "customtitlebar":
        state.customtitlebar = reasonValue;
        break;
    }

    let toolbarSelector = ".browser-toolbar:not([collapsed])";
    if (Services.appinfo.nativeMenubar) {
      toolbarSelector += ":not([type=menubar])";
    }

    // The getComputedStyle calls and setting the brighttext are separated in
    // two loops to avoid flushing layout and making it dirty repeatedly.
    let cachedLuminances = state.toolbarLuminanceCache;
    let luminances = new Map();
    for (let toolbar of window.document.querySelectorAll(toolbarSelector)) {
      // toolbars *should* all have ids, but guard anyway to avoid blowing up
      let cacheKey = toolbar.id && toolbar.id + JSON.stringify(state);
      // lookup cached luminance value for this toolbar in this window state
      let luminance = cacheKey && cachedLuminances.get(cacheKey);
      if (isNaN(luminance)) {
        let { r, g, b } = InspectorUtils.colorToRGBA(
          window.getComputedStyle(toolbar).color
        );
        luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
        if (cacheKey) {
          cachedLuminances.set(cacheKey, luminance);
        }
      }
      luminances.set(toolbar, luminance);
    }

    const luminanceThreshold = 127; // In between 0 and 255
    for (let [toolbar, luminance] of luminances) {
      toolbar.toggleAttribute("brighttext", luminance > luminanceThreshold);
    }
  },
};
PK