@import "entry";

$menu_borders_color: mix(
    $menu_bg_color,
    $menu_fg_color,
    if(lightness($menu_bg_color) < 50%, .18, .1)
);

@mixin menu_calendar($bg, $fg) {
    color: $fg;

    &.header {
        border-bottom: 1px solid shade($bg, ($contrast + .1));
        border-radius: 0;

        &:backdrop { border-color: shade($bg, ($contrast + .1)); }
    }

    &.button {
        @extend %undecorated_button;
        color: alpha($fg, .55);

        &:hover {
            @extend %undecorated_button;
            color: $fg;
        }
    }

    &:indeterminate,
    &:indeterminate:backdrop { color: mix($fg, $bg, .5); }
}


/*********
 ! Menubar
**********/

@include exports("menubar") {
    menubar, .menubar {
        -GtkWidget-window-dragging: true;

        padding: 0;
        border: 0;
        background-color: $menubar_bg_color;
        background-image: none;
        //box-shadow: inset 0 -1px shade($menubar_bg_color, .9);
        color: $menubar_fg_color;

        > menuitem {
            min-height: 10px + $spacing * 2;
            padding: ($spacing + 2px) ($spacing + 4px);
            border: 1px solid transparent;
            background-color: transparent;
            background-image: none;
            color: $menubar_fg_color;

            &:hover {
                border-color: mix($menubar_bg_color, $menubar_fg_color, .21);
                background-color: mix($menubar_bg_color, $menubar_fg_color, .21);
                background-image: none;
                color: shade($menubar_fg_color, 1.08);
            }

            *:hover { color: shade($menubar_fg_color, 1.08); }
        }
    }
}


/******
 ! Menu
*******/

@include exports("menu") {

    menu,
    .menu,
    .context-menu {
        border-radius: 0;
        padding: $spacing;
        background-color: $menu_bg_color;
        color: $menu_fg_color;
        border: 1px solid mix($menu_bg_color, $menu_fg_color, .21);


        .csd & { border: 0; }  // axes borders in a composited env

        &:selected {
            background-color: $selected_bg_color;
            color: $selected_fg_color;
        }

        // A little hack to get some extra space above/below menuitem separators
        separator,
        .csd & separator {
            background-color: $menu_borders_color;
            margin: ($spacing - 2px) 0;
        }

        // Firefox workaround
        .separator,
        .csd & .separator {
            color: $menu_borders_color;
        }

        menuitem {
            min-height: 10px + $spacing * 2;
            min-width: 40px;
            padding: ($spacing + 2px);
            border-radius: 0;

            &:active, &:hover {
                border: 0;
                background-color: $selected_bg_color;
                background-image: none;
                color: $selected_fg_color;

                accelerator {
                    color: alpha($selected_fg_color, .6);
                }
            }

            *:active, *:hover { color: $selected_fg_color; }

            &:disabled, *:disabled {
                color: mix($menu_fg_color, $menu_bg_color, .5);
            }

            // submenu indicators
            arrow {
                min-height: 16px;
                min-width: 16px;

                &:dir(ltr) {
                    -gtk-icon-source: -gtk-icontheme("pan-end-symbolic");
                    margin-left: 10px;
                }

                &:dir(rtl) {
                    -gtk-icon-source: -gtk-icontheme("pan-end-symbolic-rtl");
                    margin-right: 10px;
                }
            }

            &.button, &.button.flat {
                &, &:focus, &:active, &:disabled, &:active:disabled {
                    background-color: transparent;
                    background-image: none;
                    border: 0;
                    box-shadow: none;
                    color: currentColor;
                }

                &:hover, &:focus:hover, &:active:hover, &:selected {
                    background-image: none;
                    background-color: $selected_bg_color;
                    color: $selected_fg_color;
                }
            }

            calendar { @include menu_calendar($menu_bg_color, $menu_fg_color); }

            // avoids labels color being overridden, see
            // https://bugzilla.gnome.org/show_bug.cgi?id=767058
            label { &:dir(rtl), &:dir(ltr) { color: inherit; } }
        }

        // overflow arrows
        > arrow {
            //@include button(undecorated);

            min-height: 16px;
            min-width: 16px;
            padding: $spacing;
            background-color: $menu_bg_color;
            border-radius: 0;

            &.top {
                margin-top: -6px;
                border-bottom: 1px solid $menu_borders_color;
                -gtk-icon-source: -gtk-icontheme("pan-up-symbolic");
            }

            &.bottom {
                margin-bottom: -6px;
                border-top: 1px solid $menu_borders_color;
                -gtk-icon-source: -gtk-icontheme("pan-down-symbolic");
            }

            &:hover {
                background-color: mix($menu_bg_color, $menu_fg_color, .1);
            }

            &:backdrop { background-color: $backdrop_menu_color; }

            &:disabled {
                color: transparent;
                background-color: transparent;
                border-color: transparent;
            }
        }
    }

    .context-menu { font: initial; } // Decouple the font of context menus from their entry/textview

    .monospace { font-family: monospace; }

    menuitem {
        accelerator {
            color: alpha($menu_fg_color, .6);

            &:hover { color: alpha($selected_fg_color, .8); }

            &:disabled { color: alpha(mix($menu_fg_color, $menu_bg_color, .5), .4); }
        }

        check, radio {
            min-height: 16px;
            min-width: 16px;

            &:dir(ltr) { margin-right: 7px; }

            &:dir(rtl) { margin-left: 7px; }
        }

        window decoration {
            box-shadow: 0 2px 3px alpha($black, .2);
        }

        entry { @include entry($menu_bg_color, $menu_fg_color); }
    }
}


/*********
 ! Popover
**********/

@include exports("popover") {
    popover.background {
        padding: $spacing - 3px;
        border-radius: $roundness;
        background-clip: border-box;
        background-color: $menu_bg_color;
        background-image: none;
        color: $menu_fg_color;
        box-shadow: 0 3px 6px alpha($black, .16);

        .csd &, & {
            /*@include border($menu_bg_color);*/
            @include border(alpha($menu_fg_color, .5));
            border-width: 1px;
            border-style: solid;
        }

        &:backdrop { box-shadow: none; }

        treeview.view {
            &:hover, &:selected, &:selected:focus, &:backdrop:selected, &:backdrop:selected:focus { border-top-color: $selected_bg_color; }

            &, &:backdrop { border-top-color: shade($menu_bg_color, ($contrast + .4)); }
        }

        view, .view, list {
            &:hover {
                background-image: none;
                background-color: $selected_bg_color;
                color: $selected_fg_color;
            }

            &, &:backdrop {
                background-color: shade($menu_bg_color, ($contrast + .5));
                background-image: none;
                color: $menu_fg_color;
                border-color: border_normal($menu_bg_color);
            }
        }

        list row {
            &, .button {
                background-color: transparent;
                background-image: none;
                color: $menu_fg_color;

                &:focus, &:hover, &:active {
                    background-image: none;
                    background-color: $selected_bg_color;
                    color: $selected_fg_color;
                }
            }
        }

        .frame {
            border-color: border_normal($menu_bg_color);
            border-radius: $roundness;
        }

        entry { @include entry($base_color, $text_color);  }

        button { @include button($header_button_bg_color, $header_button_fg_color);  }

        .linked > button { @include linked_button($header_button_bg_color, $header_button_fg_color); }

        > list, > .view, > toolbar {
            border-style: none;
            background-color: transparent;
        }
    }

    modelbutton.flat,
    menuitem.button.flat {
        @extend %undecorated_button;

        padding: ($spacing + 2px);
        outline-color: transparent;
        transition: none;

        &:hover {
            background-color: $selected_bg_color;
            color: $selected_fg_color;
        }

        &:active, &:selected { &, arrow { @extend %selected_items; } }

        &:checked { color: $fg_color; }

        arrow {
            &.left { -gtk-icon-source: -gtk-icontheme("pan-start-symbolic"); }

            &.right { -gtk-icon-source: -gtk-icontheme("pan-end-symbolic"); }
        }

        // FIXME: temporary workaround
        check:last-child,
        radio:last-child { margin-left: 8px; }

        check:first-child,
        radio:first-child { margin-right: 8px; }
    }
}
