| 1 | use gpui::{Axis, InteractiveElement as _, ParentElement as _, Styled as _, blue, green, px, red}; |
| 2 | use gpui_component::{ |
| 3 | Disableable as _, Icon, IconName, Selectable as _, Sizable as _, Size, |
| 4 | button::{ |
| 5 | Button, ButtonCustomVariant, ButtonGroup, ButtonRounded, ButtonVariant, |
| 6 | ButtonVariants as _, DropdownButton, Toggle, ToggleGroup, ToggleVariant, |
| 7 | ToggleVariants as _, |
| 8 | }, |
| 9 | input::Enter, |
| 10 | }; |
| 11 | |
| 12 | #[test] |
| 13 | fn legacy_button_path_and_builder_surface_remain_available() { |
| 14 | let button = Button::new("legacy-button") |
| 15 | .label("Save") |
| 16 | .icon(IconName::Check) |
| 17 | .tooltip("Save changes") |
| 18 | .tooltip_with_action( |
| 19 | "Save changes", |
| 20 | &Enter { |
| 21 | secondary: false, |
| 22 | shift: false, |
| 23 | }, |
| 24 | Some("Button"), |
| 25 | ) |
| 26 | .accessibility_id("settings.save") |
| 27 | .primary() |
| 28 | .with_variant(ButtonVariant::Secondary) |
| 29 | .outline() |
| 30 | .rounded(ButtonRounded::Large) |
| 31 | .rounded(px(6.)) |
| 32 | .compact() |
| 33 | .loading(false) |
| 34 | .loading_icon(Icon::new(IconName::LoaderCircle)) |
| 35 | .dropdown_caret(true) |
| 36 | .toggled(false) |
| 37 | .disabled(false) |
| 38 | .selected(true) |
| 39 | .with_size(Size::Small) |
| 40 | .tab_index(3) |
| 41 | .tab_stop(true) |
| 42 | .on_hover(|hovered, _, _| { |
| 43 | let _: &bool = hovered; |
| 44 | }) |
| 45 | .on_click(|event, _, _| { |
| 46 | let _: &gpui::ClickEvent = event; |
| 47 | }) |
| 48 | .child("application child") |
| 49 | .bg(red()) |
| 50 | .hover(|style| style.bg(green())); |
| 51 | |
| 52 | assert!(button.is_selected()); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn legacy_button_variants_and_rounding_types_remain_public() { |
| 57 | let variants = [ |
| 58 | ButtonVariant::Default, |
| 59 | ButtonVariant::Primary, |
| 60 | ButtonVariant::Secondary, |
| 61 | ButtonVariant::Danger, |
| 62 | ButtonVariant::Info, |
| 63 | ButtonVariant::Success, |
| 64 | ButtonVariant::Warning, |
| 65 | ButtonVariant::Ghost, |
| 66 | ButtonVariant::Link, |
| 67 | ButtonVariant::Text, |
| 68 | ]; |
| 69 | assert!(variants[7].is_ghost()); |
| 70 | assert!(variants[8].is_link()); |
| 71 | assert!(variants[9].is_text()); |
| 72 | |
| 73 | let _rounding = [ |
| 74 | ButtonRounded::None, |
| 75 | ButtonRounded::Small, |
| 76 | ButtonRounded::Medium, |
| 77 | ButtonRounded::Large, |
| 78 | ButtonRounded::Size(px(8.)), |
| 79 | ]; |
| 80 | |
| 81 | let _custom_constructor: fn(&gpui::App) -> ButtonCustomVariant = ButtonCustomVariant::new; |
| 82 | let _custom_builders = ( |
| 83 | ButtonCustomVariant::color, |
| 84 | ButtonCustomVariant::foreground, |
| 85 | ButtonCustomVariant::hover, |
| 86 | ButtonCustomVariant::active, |
| 87 | ButtonCustomVariant::shadow, |
| 88 | ); |
| 89 | |
| 90 | let _variant_helpers = Button::new("variant-helpers") |
| 91 | .primary() |
| 92 | .secondary() |
| 93 | .danger() |
| 94 | .warning() |
| 95 | .success() |
| 96 | .info() |
| 97 | .ghost() |
| 98 | .link() |
| 99 | .text(); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn legacy_button_group_and_dropdown_paths_remain_available() { |
| 104 | let _group = ButtonGroup::new("legacy-button-group") |
| 105 | .children([ |
| 106 | Button::new("one").label("One"), |
| 107 | Button::new("two").label("Two"), |
| 108 | ]) |
| 109 | .child(Button::new("three").label("Three")) |
| 110 | .multiple(true) |
| 111 | .layout(Axis::Horizontal) |
| 112 | .compact() |
| 113 | .outline() |
| 114 | .danger() |
| 115 | .disabled(false) |
| 116 | .with_size(Size::Medium) |
| 117 | .on_click(|indices, _, _| { |
| 118 | let _: &Vec<usize> = indices; |
| 119 | }) |
| 120 | .bg(red()); |
| 121 | |
| 122 | let dropdown = DropdownButton::new("legacy-dropdown-button") |
| 123 | .button(Button::new("dropdown-primary").label("Export")) |
| 124 | .dropdown_menu(|menu, _, _| menu) |
| 125 | .dropdown_menu_with_anchor(gpui::Anchor::BottomLeft, |menu, _, _| menu) |
| 126 | .outline() |
| 127 | .success() |
| 128 | .disabled(false) |
| 129 | .selected(true) |
| 130 | .with_size(Size::Large) |
| 131 | .bg(blue()); |
| 132 | assert!(dropdown.is_selected()); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn legacy_toggle_types_under_button_module_remain_available() { |
| 137 | let toggle = Toggle::new("legacy-toggle") |
| 138 | .label("Bold") |
| 139 | .icon(IconName::Check) |
| 140 | .tooltip("Toggle bold") |
| 141 | .checked(true) |
| 142 | .with_variant(ToggleVariant::Outline) |
| 143 | .ghost() |
| 144 | .outline() |
| 145 | .disabled(false) |
| 146 | .with_size(Size::Small) |
| 147 | .on_click(|checked, _, _| { |
| 148 | let _: &bool = checked; |
| 149 | }) |
| 150 | .child("application child") |
| 151 | .bg(red()); |
| 152 | |
| 153 | let _group = ToggleGroup::new("legacy-toggle-group") |
| 154 | .child(toggle) |
| 155 | .children([Toggle::new("left"), Toggle::new("right")]) |
| 156 | .segmented() |
| 157 | .disabled(false) |
| 158 | .with_size(Size::Medium) |
| 159 | .on_click(|indices, _, _| { |
| 160 | let _: &Vec<bool> = indices; |
| 161 | }) |
| 162 | .bg(green()); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn base_button_does_not_replace_legacy_button_module_path() { |
| 167 | fn legacy(_: Button) {} |
| 168 | fn base(_: gpui_base::Button) {} |
| 169 | |
| 170 | legacy(Button::new("legacy")); |
| 171 | base(gpui_base::Button::new("base")); |
| 172 | } |