
@function gtkalpha($c,$a) {
  @return unquote("alpha(#{$c},#{$a})");
}

@function gtkmix($c1,$c2,$r) {
  $ratio: 1 -  $r / 100%; // match SCSS mix()
  @return unquote("mix(#{$c1},#{$c2},#{$ratio})");
}

@function gtkshade($c,$s) {
  @return unquote("shade(#{$c},#{$s})");
}

@function gtkcolor($c) {
  @return unquote("@#{$c}");
}

// Animations
@keyframes header_ripple_effect {
  from {
    background-image: radial-gradient(circle farthest-corner at center,
                                      $header_bg 0%,
                                      transparent 0%);
  }

  to {
    background-image: radial-gradient(circle farthest-corner at center,
                                      $header_bg 100%,
                                      transparent 0%);
  }
}

@keyframes ripple_effect {
  from {
    background-image: radial-gradient(circle farthest-corner at center,
                                      gtkalpha(currentColor, 0.1) 0%,
                                      transparent 0%);
  }

  to {
    background-image: radial-gradient(circle farthest-corner at center,
                                      gtkalpha(currentColor, 0.1) 100%,
                                      transparent 0%);
  }
}

// Drawing mixins

// generic drawing of more complex things

//
// Helper mixin for drawing visible focus rings
//
// If $target is specified, the focus ring is applied to the specified child element.
// If $outer is true, the focus ring extends outward. Otherwise, it extends inward.
// If $within is true, use focus-within instead of focus:focus-visible

@mixin focus-ring($target: null, $width: 2px, $offset: -$width, $outer: false, $focus-state: 'focus:focus-visible', $transition: null, $fc: $focus_border_color) {
  & #{$target} {
    outline: 0 solid transparent;
    outline-offset: if($outer, $offset + 4px, $offset + $width + 4px);
    transition: $focus_transition, $transition;
  }

  &:#{$focus-state} #{$target} {
    outline-color: $fc;
    outline-width: $width;
    outline-offset: $offset;
  }
}

@mixin _shadows($list...) {
//
// Helper mixin to stack up to box-shadows;
//
  $shadows: null;

  @each $shadow in $list {
    @if $shadow!=none { $shadows: $shadows, $shadow; }
  }

  box-shadow: $shadows;
}


// Solid color image
@function _solid($c) {
  @return linear-gradient(to bottom, $c, $c);
}

// Entries

@mixin entry($t) {
//
// Entries drawing function
//
// $t: entry type
//

  @if $t==normal {
  //
  // normal entry
  //
    color: $text_color;
    border-color: $entry_border;
    background-color: $entry_bg;
    outline: none;
  }

  @if $t==focus {
  //
  // focused entry
  //
    color: $text_color;
    border-color: $selected_bg_color;
    background-color: $entry_bg;
    outline: none;
  }

  @if $t==insensitive {
  //
  // insensitive entry
  //
    color: $insensitive_fg_color;
    border-color: $disable_borders_color;
    background-color: rgba($entry_bg, 0.55);
  }

  @if $t==header-normal {
  //
  // normal header-bar entry
  //

    color: $header_fg;
    border-color: $header_entry_border;
    background-color: $header_entry_bg;

    image, image:hover { color: inherit; }
  }

  @if $t==header-focus {
  //
  // focused header-bar entry
  //
    color: lighten($header_fg, 20%);
    border-color: $selected_bg_color;
    background-color: if($header == 'light', $header_entry_bg, darken($header_entry_bg, 6%));
  }

  @if $t==header-insensitive {
  //
  // insensitive header-bar entry
  //
    color: rgba($header_fg, 0.55);
    border-color: $header_entry_border;
    background-color: rgba($header_entry_bg, 0.85);
  }

  @else if $t==osd {
  //
  // normal osd entry
  //
    color: $osd_fg_color;
    border-color: $osd_entry_border;
    background-color: $osd_entry_bg;

    image, image:hover { color: inherit; }
  }

  @else if $t==osd-focus {
  //
  // active osd entry
  //
    color: $selected_fg_color;
    border-color: $osd_entry_border;
    background-color: $selected_bg_color;
  }

  @else if $t==osd-insensitive {
  //
  // insensitive osd entry
  //
    color: rgba($osd_fg_color, 0.55);
    background-color: rgba($osd_entry_bg, 0.85);
  }
}

// Buttons

@mixin button($t, $actionb_bg:red, $actionb_fg: green) {
//
// Button drawing function
//
// $t:    button type,
// $actionb_bg, $actionb_fg: used for destructive and suggested action buttons

  @if $t==normal {
  //
  // normal button
  //
    color: $fg_color;
    border-color: $button_border;
    background-color: $button_bg;
  }

  @else if $t==hover {
  //
  // hovered button
  //
    color: darken($fg_color, 15%);
    border-color: if($variant=='light', rgba(black, 0.25), $button_border);
    background-color: lighten($button_bg, 8%);
  }

  @else if $t==active {
  //
  // pushed button
  //
    color: $selected_fg_color;
    border-color: if($variant=='light', $selected_bg_color, $button_border);
    background-color: $selected_bg_color;
  }

  @else if $t==checked {
    //
    // pushed button
    //
    color: $selected_fg_color;
    border-color: if($variant=='light', $selected_bg_color, $button_border);
    background-color: $selected_bg_color;
  }

  @else if $t==insensitive {
  //
  // insensitive button
  //
    label, & { color: $insensitive_fg_color; }

    border-color: $disable_borders_color;
    background-color: rgba($button_bg, 0.55);
  }

  @else if $t==insensitive-active {
  //
  // insensitive pushed button
  //
    label, & { color: rgba($selected_fg_color, 0.8); }

    border-color: rgba($selected_bg_color, 0.75);
    background-color: rgba($selected_bg_color, 0.75);

    opacity: 0.6;
  }

  @if $t==header-normal {
  //
  // normal header-bar button
  //
    color: $header_fg;
    outline-offset: -3px;
    background-color: transparent;
    border-color: transparent;
  }

  @else if $t==header-hover {
  //
  // hovered header-bar button
  //
    color: $header_fg;
    border-color: $borders_color;
    background-color: rgba($header_fg, if($variant=='light', 0.12, 0.18));
  }

  @else if $t==header-active {
  //
  // pushed header-bar button
  //
    color: $selected_fg_color;
    border-color: if($variant=='light', $selected_bg_color, transparent);
    background-color: $selected_bg_color;
  }

  @else if $t==header-insensitive {
  //
  // insensitive header-bar button
  //
    color: rgba($header_fg, 0.45);
    background-color: transparent;
    border-color: transparent;
  }

  @else if $t==header-insensitive-active {
  //
  // header-bar insensitive pushed button
  //
    color: rgba($selected_fg_color, 0.5);
    border-color: rgba($selected_bg_color, 0.75);
    background-color: rgba($selected_bg_color, 0.75);
  }

  @else if $t==osd {
  //
  // normal osd button
  //
    color: $osd_fg_color;
    border-color: $osd_button_border;
    background-color: $osd_button_bg;
  }

  @else if $t==osd-hover {
  //
  // active osd button
  //
    color: $osd_fg_color;
    border-color: $osd_button_border;
    background-color: rgba($osd_fg_color, 0.1);
  }

  @else if $t==osd-active {
  //
  // active osd button
  //
    color: $selected_fg_color;
    border-color: if($header == 'light', $selected_bg_color, $osd_button_border);
    background-color: $selected_bg_color;
  }

  @else if $t==osd-insensitive {
  //
  // insensitive osd button
  //
    color: $osd_insensitive_fg_color;
    border-color: $osd_button_border;
    background-color: rgba($osd_button_bg, 0.85);
  }

  @else if $t==suggested_destructive {
  //
  // suggested or destructive action buttons
  //
    background-clip: border-box;

    color: $actionb_fg;
    background-color: $actionb_bg;
    border-color: $actionb_bg;
  }

  @else if $t==undecorated {
  //
  // reset
  //
    border-color: transparent;
    background-color: transparent;
    background-image: none;
  }
}

//
// Overshoot
//
@mixin overshoot($p, $t:normal, $c:$fg_color) {
//
// overshoot
//
// $p: position
// $t: type
// $c: base color
//
// possible $p values:
// top, bottom, right, left
//
// possible $t values:
// normal, backdrop
//

  $_small_gradient_length: 3%;
  $_big_gradient_length: 50%;

  $_small_gradient_size: 100% $_small_gradient_length;
  $_big_gradient_size: 100% $_big_gradient_length;

  @if $p==right or $p==left {
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  $_small_gradient_color: $c;
  $_big_gradient_color: transparentize($c, 0.93);

  @if $c==$fg_color {
    $_small_gradient_color: darken($borders_color, 10%);
    $_big_gradient_color: transparentize($fg_color, 0.93);

    @if $t==backdrop { $_small_gradient_color: $backdrop_borders_color; }
  }

  $_small_gradient: radial-gradient(farthest-side at $p,
                                    $_small_gradient_color 85%,
                                    transparentize($_small_gradient_color, 1));

  $_big_gradient: radial-gradient(farthest-side at $p,
                                  $_big_gradient_color,
                                  transparentize($_big_gradient_color, 1));

  @if $t==normal {
    background-image: $_small_gradient, $_big_gradient;
    background-size: $_small_gradient_size, $_big_gradient_size;
  }

  @else if $t==backdrop {
    background-image: $_small_gradient;
    background-size: $_small_gradient_size;
  }

  background-repeat: no-repeat;
  background-position: $p;

  background-color: transparent; // reset some properties to be sure to not inherit them somehow
  border: none;                  //
  box-shadow: none;              //
}

//
// Undershoot
//
@mixin undershoot_dash($p) {
// $p: position
//
// possible $p values:
// top, bottom, right, left
//

  $_undershoot_color_dark: rgba(black, 0.2);
  $_undershoot_color_light: rgba(white, 0.2);

  $_gradient_dir: left;
  $_dash_bg_size: 10px 1px;
  $_gradient_repeat: repeat-x;
  $_bg_pos: center $p;

  background-color: transparent; // shouldn't be needed, but better to be sure;

  @if ($p == left) or ($p == right) {
    $_gradient_dir: top;
    $_dash_bg_size: 1px 10px;
    $_gradient_repeat: repeat-y;
    $_bg_pos: $p center;
  }

  background-image: linear-gradient(to $_gradient_dir, // this is the dashed line
                                    $_undershoot_color_light 50%,
                                    $_undershoot_color_dark 50%);

  padding-#{$p}: 1px;
  background-size: $_dash_bg_size;
  background-repeat: $_gradient_repeat;
  background-origin: content-box;
  background-position: $_bg_pos;
  border: none;
}

@mixin undershoot($p, $c: $borders_color, $neighbor: false, $style: default) {
//
// undershoot
//
// $p: position
// $c: shade color
// $neighbor: use ~ instead of >
//
// possible $p values:
// top, bottom, right, left
//

  $_border_pos: '';
  $_direction: '';
  $_selector: if($neighbor, '~', '>');

  @if $p==top {
    $_direction: bottom;
    $_border_pos: 0 1px;
  } @else if $p==bottom {
    $_direction: top;
    $_border_pos: 0 -1px;
  } @else if $p==left {
    $_direction: right;
    $_border_pos: 1px 0;
  } @else if $p==right {
    $_direction: left;
    $_border_pos: -1px 0;
  } @else {
    @error "Unknown position #{$p}"
  }

  #{$_selector} undershoot.#{$p} {
    box-shadow: if($style == 'default', none, inset $_border_pos $borders_color);
    background: linear-gradient(to $_direction, gtkalpha($c, .25), transparent 6px);
  }
}

@mixin background-shadow($direction, $color) {
  background-image:
  linear-gradient($direction,
                  gtkalpha($color, 0.7),
                  gtkalpha($color, 0.14) 40px,
                  gtkalpha($color, 0) 56px),
  linear-gradient($direction,
                  gtkalpha($color, 0.4),
                  gtkalpha($color, 0.14) 7px,
                  gtkalpha($color, 0) 24px);
}

@mixin transition-shadows($color) {
  > dimming {
    background: none;
  }
  
  > border {
    background: none;
  }
  
  > shadow {
    min-width: 56px;
    min-height: 56px;
  
    &.left  { @include background-shadow(to right,  $color); }
    &.right { @include background-shadow(to left,   $color); }
    &.up    { @include background-shadow(to bottom, $color); }
    &.down  { @include background-shadow(to top,    $color); }
  }
}
