# Common Element Patterns Reusable patterns for implementing common element types in GPUI. ## Text Rendering Elements Elements that display and manipulate text content. ### Pattern Characteristics - Use `StyledText` for text layout and rendering - Handle text selection in `paint` phase with hitbox interaction - Create hitboxes for text interaction in `prepaint` - Support text highlighting and custom styling via runs ### Implementation Template ```rust pub struct TextElement { id: ElementId, text: SharedString, style: TextStyle, } impl Element for TextElement { type RequestLayoutState = StyledText; type PrepaintState = Hitbox; fn request_layout(&mut self, .., window: &mut Window, cx: &mut App) -> (LayoutId, StyledText) { let styled_text = StyledText::new(self.text.clone()) .with_style(self.style); let (layout_id, _) = styled_text.request_layout(None, None, window, cx); (layout_id, styled_text) } fn prepaint(&mut self, .., bounds: Bounds, styled_text: &mut StyledText, window: &mut Window, cx: &mut App) -> Hitbox { styled_text.prepaint(None, None, bounds, &mut (), window, cx); window.insert_hitbox(bounds, HitboxBehavior::Normal) } fn paint(&mut self, .., bounds: Bounds, styled_text: &mut StyledText, hitbox: &mut Hitbox, window: &mut Window, cx: &mut App) { styled_text.paint(None, None, bounds, &mut (), &mut (), window, cx); window.set_cursor_style(CursorStyle::IBeam, hitbox); } } ``` ### Use Cases - Code editors with syntax highlighting - Rich text displays - Labels with custom formatting - Selectable text areas ## Container Elements Elements that manage and layout child elements. ### Pattern Characteristics - Manage child element layouts and positions - Handle scrolling and clipping when needed - Implement flex/grid-like layouts - Coordinate child interactions and event delegation ### Implementation Template ```rust pub struct ContainerElement { id: ElementId, children: Vec, direction: FlexDirection, gap: Pixels, } impl Element for ContainerElement { type RequestLayoutState = Vec; type PrepaintState = Vec>; fn request_layout(&mut self, .., window: &mut Window, cx: &mut App) -> (LayoutId, Vec) { let child_layout_ids: Vec<_> = self.children .iter_mut() .map(|child| child.request_layout(window, cx).0) .collect(); let layout_id = window.request_layout( Style { flex_direction: self.direction, gap: self.gap, ..default() }, child_layout_ids.clone(), cx ); (layout_id, child_layout_ids) } fn prepaint(&mut self, .., bounds: Bounds, layout_ids: &mut Vec, window: &mut Window, cx: &mut App) -> Vec> { let mut child_bounds = Vec::new(); for (child, layout_id) in self.children.iter_mut().zip(layout_ids.iter()) { let child_bound = window.layout_bounds(*layout_id); child.prepaint(child_bound, window, cx); child_bounds.push(child_bound); } child_bounds } fn paint(&mut self, .., child_bounds: &mut Vec>, window: &mut Window, cx: &mut App) { for (child, bounds) in self.children.iter_mut().zip(child_bounds.iter()) { child.paint(*bounds, window, cx); } } } ``` ### Use Cases - Panels and split views - List containers - Grid layouts - Tab containers ## Interactive Elements Elements that respond to user input (mouse, keyboard, touch). ### Pattern Characteristics - Create appropriate hitboxes for interaction areas - Handle mouse/keyboard/touch events properly - Manage focus and cursor styles - Support hover, active, and disabled states ### Implementation Template ```rust pub struct InteractiveElement { id: ElementId, content: AnyElement, on_click: Option>, hover_style: Option