use gpui::prelude::*;
use gpui::*;
use crate::theme::get_theme;
use crate::utils::darken;
use super::focus_navigation::{FocusNext, FocusPrev};
actions!(ccf_button, [ActivateButton]);
pub fn register_keybindings(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("enter", ActivateButton, Some("CcfButton")),
KeyBinding::new("space", ActivateButton, Some("CcfButton")),
]);
}
pub fn primary_button(
id: impl Into<ElementId>,
label: &str,
enabled: bool,
cx: &App,
) -> Stateful<Div> {
let theme = get_theme(cx);
div()
.id(id)
.key_context("CcfButton")
.focusable()
.tab_stop(enabled) .on_action(|_: &FocusNext, window, _cx| {
window.focus_next();
})
.on_action(|_: &FocusPrev, window, _cx| {
window.focus_prev();
})
.flex()
.items_center()
.justify_center()
.h(px(36.))
.px_4()
.rounded_md()
.cursor_pointer()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.border_2()
.border_color(rgba(0x00000000)) .when(enabled, |d| {
d.bg(rgb(theme.primary))
.text_color(rgb(theme.text_primary))
.hover(|d| d.bg(rgb(theme.primary_hover)))
.active(|d| d.bg(rgb(theme.primary_active)))
})
.when(!enabled, |d| {
d.bg(rgb(theme.disabled_bg))
.text_color(rgb(theme.disabled_text))
.cursor_default()
})
.focus(|d| d.border_color(rgb(theme.border_focus_on_color)))
.child(label.to_string())
}
pub fn secondary_button(
id: impl Into<ElementId>,
label: &str,
cx: &App,
) -> Stateful<Div> {
let theme = get_theme(cx);
div()
.id(id)
.key_context("CcfButton")
.focusable()
.tab_stop(true)
.on_action(|_: &FocusNext, window, _cx| {
window.focus_next();
})
.on_action(|_: &FocusPrev, window, _cx| {
window.focus_prev();
})
.flex()
.items_center()
.justify_center()
.h(px(36.))
.px_4()
.rounded_md()
.cursor_pointer()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.bg(rgb(theme.secondary_bg))
.text_color(rgb(theme.text_primary))
.border_2()
.border_color(rgb(theme.secondary_border))
.hover(|d| d.bg(rgb(theme.secondary_bg_hover)))
.active(|d| d.bg(rgb(theme.secondary_bg_active)))
.focus(|d| d.border_color(rgb(theme.border_focus)))
.child(label.to_string())
}
pub fn danger_button(
id: impl Into<ElementId>,
label: &str,
enabled: bool,
cx: &App,
) -> Stateful<Div> {
let theme = get_theme(cx);
let danger_hover = darken(theme.error, 0.15);
let danger_active = darken(theme.error, 0.25);
div()
.id(id)
.key_context("CcfButton")
.focusable()
.tab_stop(enabled) .on_action(|_: &FocusNext, window, _cx| {
window.focus_next();
})
.on_action(|_: &FocusPrev, window, _cx| {
window.focus_prev();
})
.flex()
.items_center()
.justify_center()
.h(px(36.))
.px_4()
.rounded_md()
.cursor_pointer()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.border_2()
.border_color(rgba(0x00000000)) .when(enabled, |d| {
d.bg(rgb(theme.error))
.text_color(rgb(theme.text_primary))
.hover(|d| d.bg(rgb(danger_hover)))
.active(|d| d.bg(rgb(danger_active)))
})
.when(!enabled, |d| {
d.bg(rgb(theme.disabled_bg))
.text_color(rgb(theme.disabled_text))
.cursor_default()
})
.focus(|d| d.border_color(rgb(theme.border_focus_on_color)))
.child(label.to_string())
}