| 1 | use gpui::{ |
| 2 | AnyElement, App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement, |
| 3 | LayoutId, Pixels, Window, |
| 4 | }; |
| 5 | |
| 6 | use super::InputGroupButton; |
| 7 | |
| 8 | // Preserve this explicit part until its addon applies inherited state. Only |
| 9 | // immediate InputGroupButton children are inspected, never arbitrary descendants. |
| 10 | pub(super) struct InputGroupButtonElement { |
| 11 | button: Option<InputGroupButton>, |
| 12 | disabled: bool, |
| 13 | } |
| 14 | |
| 15 | impl InputGroupButtonElement { |
| 16 | pub(super) fn new(button: InputGroupButton) -> Self { |
| 17 | Self { |
| 18 | button: Some(button), |
| 19 | disabled: false, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub(super) fn disable(&mut self, disabled: bool) { |
| 24 | self.disabled |= disabled; |
| 25 | } |
| 26 | |
| 27 | pub(super) fn from_element(mut element: &mut AnyElement) -> Option<&mut Self> { |
| 28 | // ParentElement::child may erase an already-erased IntoElement again. |
| 29 | // Unwrap only those type-erasure layers, never an element's children. |
| 30 | element = unwrapped_element(element); |
| 31 | element.downcast_mut::<Self>() |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub(super) fn unwrapped_element(mut element: &mut AnyElement) -> &mut AnyElement { |
| 36 | while element.downcast_mut::<AnyElement>().is_some() { |
| 37 | element = element.downcast_mut::<AnyElement>().unwrap(); |
| 38 | } |
| 39 | element |
| 40 | } |
| 41 | |
| 42 | impl IntoElement for InputGroupButtonElement { |
| 43 | type Element = Self; |
| 44 | |
| 45 | fn into_element(self) -> Self { |
| 46 | self |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Element for InputGroupButtonElement { |
| 51 | type RequestLayoutState = AnyElement; |
| 52 | type PrepaintState = (); |
| 53 | |
| 54 | fn id(&self) -> Option<ElementId> { |
| 55 | None |
| 56 | } |
| 57 | |
| 58 | fn source_location(&self) -> Option<&'static std::panic::Location<'static>> { |
| 59 | None |
| 60 | } |
| 61 | |
| 62 | fn request_layout( |
| 63 | &mut self, |
| 64 | _: Option<&GlobalElementId>, |
| 65 | _: Option<&InspectorElementId>, |
| 66 | window: &mut Window, |
| 67 | cx: &mut App, |
| 68 | ) -> (LayoutId, AnyElement) { |
| 69 | let mut element = self |
| 70 | .button |
| 71 | .take() |
| 72 | .expect("input group button already rendered") |
| 73 | .render_in_group(self.disabled, window, cx); |
| 74 | (element.request_layout(window, cx), element) |
| 75 | } |
| 76 | |
| 77 | fn prepaint( |
| 78 | &mut self, |
| 79 | _: Option<&GlobalElementId>, |
| 80 | _: Option<&InspectorElementId>, |
| 81 | _: Bounds<Pixels>, |
| 82 | element: &mut AnyElement, |
| 83 | window: &mut Window, |
| 84 | cx: &mut App, |
| 85 | ) { |
| 86 | element.prepaint(window, cx); |
| 87 | } |
| 88 | |
| 89 | fn paint( |
| 90 | &mut self, |
| 91 | _: Option<&GlobalElementId>, |
| 92 | _: Option<&InspectorElementId>, |
| 93 | _: Bounds<Pixels>, |
| 94 | element: &mut AnyElement, |
| 95 | _: &mut (), |
| 96 | window: &mut Window, |
| 97 | cx: &mut App, |
| 98 | ) { |
| 99 | element.paint(window, cx); |
| 100 | } |
| 101 | } |