/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

/* ::::: menupopup ::::: */

:is(menupopup, panel):where(:not([type="arrow"])) {
  --panel-background-color: Menu;
  --panel-text-color: MenuText;
  --panel-padding: var(--panel-padding-block) 0;
  --panel-border-color: ThreeDShadow;

  /* Nova's design tokens already provide a larger --panel-border-radius, so
     only apply the platform default when Nova is disabled. */
  @media not -moz-pref("browser.nova.enabled") {
    /* This should match GetMenuCornerRadius() on macOS, or be overridden below */
    --panel-border-radius: 6px;
  }

  @media (-moz-platform: linux) {
    @media (prefers-contrast) {
      --panel-border-color: color-mix(in srgb, currentColor 60%, transparent);
    }
  }

  @media (-moz-platform: macos) {
    --panel-background-color: none;
    --panel-border-color: transparent;

    @media (-moz-mac-tahoe-theme) {
      /* GetMenuCornerRadius() returns a larger value on Tahoe */
      --panel-border-radius: 12px;
    }
  }
}

:is(menupopup, panel) {
  /* We can always render in the preferred color scheme (unless otherwise
   * overridden). */
  color-scheme: light dark;
  min-width: 1px;
  -moz-window-input-region-margin: var(--panel-box-shadow-margin);
  margin: calc(-1 * var(--panel-box-shadow-margin));

  /* Panel design token theming */
  --background-color-canvas: var(--panel-background-color);

  @media (-moz-platform: linux) and (not (-moz-gtk-csd-transparency-available)) {
    /* On some linux WMs we need to draw square and opaque menus because alpha
     * is not available. */
    opacity: 1 !important;
    --panel-box-shadow-margin: 0px !important;
    --panel-border-radius: 0px !important;
  }

  @media (-moz-platform: macos) {
    /* -moz-window-shadow does the trick on macOS */
    --panel-box-shadow: none;
    --panel-box-shadow-margin: 0px;
    appearance: auto;
    -moz-default-appearance: menupopup;
    /* We set the default background here, rather than on ::part(content),
     * because otherwise it'd interfere with the native look. Non-native-looking
     * popups should get their background via --panel-background-color */
    background-color: Menu;
  }

  &::part(content) {
    display: flex;
    box-sizing: border-box;

    padding: var(--panel-padding);
    color: var(--panel-text-color);
    background: var(--panel-background-color);
    /* The values chosen for the border radius should match OS values, not necessarily
     * design system values. */
    /* stylelint-disable-next-line stylelint-plugin-mozilla/use-design-tokens */
    border-radius: var(--panel-border-radius);
    border: 1px solid var(--panel-border-color);
    width: var(--panel-width);
    box-shadow: var(--panel-box-shadow);
    margin: var(--panel-box-shadow-margin);

    min-width: 0;
    min-height: 0;

    /* Makes popup constraints work. Round up to avoid subpixel rounding
     * causing overflow, see bug 1846050 */
    /* Our stylelint rules don't account for the round() function, see bug 2018789 */
    /* stylelint-disable-next-line stylelint-plugin-mozilla/use-design-tokens */
    max-height: round(up, 100% - 2 * var(--panel-box-shadow-margin), 1px);
    /* stylelint-disable-next-line stylelint-plugin-mozilla/use-design-tokens */
    max-width: round(up, 100% - 2 * var(--panel-box-shadow-margin), 1px);

    overflow: clip; /* Don't let panel content overflow the border */
  }

  &[orient="vertical"]::part(content) {
    flex-direction: column;
  }
}

/* ::::: arrow panel ::::: */

panel:where([type="arrow"]) {
  appearance: none;
  background-color: transparent;

  &.panel-no-padding::part(content) {
    padding: 0;
  }
}

/* ::::: panel animations ::::: */

.animatable-menupopup,
panel[type="arrow"] {
  transition-timing-function: var(--animation-easing-function), ease-out;

  &:not([animate="false"]) {
    transition-duration: 0.18s;
  }

  @media not (-moz-platform: macos) {
    transition-property: transform, opacity;
    will-change: transform, opacity;
    opacity: 0;
    @media not (prefers-reduced-motion) {
      transform: translateY(-70px);

      &:where([side="bottom"]) {
        transform: translateY(70px);
      }
      /* On some platforms we can't animate the panel position easily, see
       * bug 1385079, bug 1800368, bug 1800442 */
      @media not (-moz-panel-animations) {
        transform: none;
      }
    }
  }

  /* On Mac, use the properties "-moz-window-transform" and "-moz-window-opacity"
   * instead of "transform" and "opacity" for these animations.
   * The -moz-window* properties apply to the whole window including the
   * window's shadow, and they don't affect the window's "shape", so the
   * system doesn't have to recompute the shadow shape during the animation.
   * This makes them a lot faster. These properties are not implemented on
   * other platforms.
   */
  @media (-moz-platform: macos) {
    transition-property:
      -moz-window-transform,
      -moz-window-opacity;
    @media not (prefers-reduced-motion) {
      -moz-window-transform: translateY(-70px);

      &:where([side="bottom"]) {
        -moz-window-transform: translateY(70px);
      }
    }
    /* Only do the fade-in animation on pre-Big Sur to avoid missing shadows on
     * Big Sur+, see bug 1672091. */
    @media not (-moz-mac-big-sur-theme) {
      -moz-window-opacity: 0;
    }
    /* If the @media rule above is removed, then we can also remove this */
    &[animate="cancel"] {
      -moz-window-opacity: 0;
    }
  }

  &[animate="cancel"] {
    -moz-window-transform: none;
    transform: none;
  }

  &:is([animate="false"], [animate="open"]) {
    opacity: 1;
    transform: none;
    -moz-window-opacity: 1;
    -moz-window-transform: none;
    transition-timing-function: var(--animation-easing-function), ease-in-out;
  }

  &[animating] {
    pointer-events: none;
  }
}
