| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui::{ |
| 4 | App, DefiniteLength, Entity, IntoElement, Pixels, RenderOnce, SharedString, StyleRefinement, |
| 5 | Styled, Window, prelude::FluentBuilder as _, |
| 6 | }; |
| 7 | |
| 8 | use super::{Input, TextareaState}; |
| 9 | use crate::native_menu::NativeMenu; |
| 10 | use crate::{RoleOverride, Sizable, Size, StyledExt as _}; |
| 11 | |
| 12 | /// A styled ordinary multi-line text field. |
| 13 | #[derive(IntoElement)] |
| 14 | pub struct Textarea { |
| 15 | token_renderer: Option<gpui_base::input::InlineTokenRenderer>, |
| 16 | token_click_listener: Option<gpui_base::input::InlineTokenClickListener>, |
| 17 | token_hover_listener: Option<gpui_base::input::InlineTokenHoverListener>, |
| 18 | state: Entity<TextareaState>, |
| 19 | style: StyleRefinement, |
| 20 | size: Size, |
| 21 | height: Option<DefiniteLength>, |
| 22 | line_box_height: Option<Pixels>, |
| 23 | appearance: bool, |
| 24 | bordered: bool, |
| 25 | disabled: bool, |
| 26 | readonly: bool, |
| 27 | tab_index: isize, |
| 28 | role: RoleOverride, |
| 29 | accessibility_id: Option<SharedString>, |
| 30 | aria_label: Option<SharedString>, |
| 31 | |
| 32 | /// An optional context menu builder to allow a custom context menu. |
| 33 | /// |
| 34 | /// If set, this overrides the built-in context menu. |
| 35 | context_menu_builder: Option<Rc<dyn Fn(NativeMenu, &mut Window, &mut App) -> NativeMenu>>, |
| 36 | |
| 37 | paste_handler: Option<Rc<dyn Fn(&gpui::ClipboardItem, &mut Window, &mut App) -> bool>>, |
| 38 | } |
| 39 | |
| 40 | impl Textarea { |
| 41 | /// The element each atomic inline token renders as, in place of the default |
| 42 | /// [`InputToken`](super::InputToken); editing and history stay |
| 43 | /// with the input. |
| 44 | pub fn token<R: IntoElement>( |
| 45 | mut self, |
| 46 | render: impl Fn(&super::InlineTokenContext, &mut Window, &mut App) -> R + 'static, |
| 47 | ) -> Self { |
| 48 | self.token_renderer = Some(Rc::new(move |token, window, cx| { |
| 49 | render(token, window, cx).into_any_element() |
| 50 | })); |
| 51 | self |
| 52 | } |
| 53 | /// Open a reference after a completed, unconsumed token click. |
| 54 | pub fn on_token_click( |
| 55 | mut self, |
| 56 | listener: impl Fn(&super::InlineTokenClickEvent, &mut Window, &mut App) + 'static, |
| 57 | ) -> Self { |
| 58 | self.token_click_listener = Some(Rc::new(listener)); |
| 59 | self |
| 60 | } |
| 61 | /// Report pointer presence over a token so the application can show a |
| 62 | /// tooltip or run custom logic. Hover never selects or edits. |
| 63 | pub fn on_token_hover( |
| 64 | mut self, |
| 65 | listener: impl Fn(&super::InlineTokenHoverEvent, &mut Window, &mut App) + 'static, |
| 66 | ) -> Self { |
| 67 | self.token_hover_listener = Some(Rc::new(listener)); |
| 68 | self |
| 69 | } |
| 70 | |
| 71 | pub fn new(state: &Entity<TextareaState>) -> Self { |
| 72 | Self { |
| 73 | state: state.clone(), |
| 74 | style: StyleRefinement::default(), |
| 75 | size: Size::default(), |
| 76 | height: None, |
| 77 | line_box_height: None, |
| 78 | appearance: true, |
| 79 | bordered: true, |
| 80 | disabled: false, |
| 81 | readonly: false, |
| 82 | tab_index: 0, |
| 83 | role: RoleOverride::default(), |
| 84 | accessibility_id: None, |
| 85 | aria_label: None, |
| 86 | context_menu_builder: None, |
| 87 | paste_handler: None, |
| 88 | token_renderer: None, |
| 89 | token_click_listener: None, |
| 90 | token_hover_listener: None, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn h(mut self, height: impl Into<DefiniteLength>) -> Self { |
| 95 | self.height = Some(height.into()); |
| 96 | self |
| 97 | } |
| 98 | |
| 99 | /// See [`Input::line_box_height`]. |
| 100 | pub fn line_box_height(mut self, height: Pixels) -> Self { |
| 101 | self.line_box_height = Some(height); |
| 102 | self |
| 103 | } |
| 104 | |
| 105 | pub fn appearance(mut self, appearance: bool) -> Self { |
| 106 | self.appearance = appearance; |
| 107 | self |
| 108 | } |
| 109 | |
| 110 | pub fn bordered(mut self, bordered: bool) -> Self { |
| 111 | self.bordered = bordered; |
| 112 | self |
| 113 | } |
| 114 | |
| 115 | pub fn disabled(mut self, disabled: bool) -> Self { |
| 116 | self.disabled = disabled; |
| 117 | self |
| 118 | } |
| 119 | |
| 120 | /// Set the textarea to read-only, default is `false`. |
| 121 | /// |
| 122 | /// Unlike [`Self::disabled`], a read-only textarea keeps the normal appearance |
| 123 | /// and still can be focused, selected and copied, it only rejects the changes |
| 124 | /// made by the user. |
| 125 | pub fn readonly(mut self, readonly: bool) -> Self { |
| 126 | self.readonly = readonly; |
| 127 | self |
| 128 | } |
| 129 | |
| 130 | pub fn tab_index(mut self, index: isize) -> Self { |
| 131 | self.tab_index = index; |
| 132 | self |
| 133 | } |
| 134 | |
| 135 | pub fn role(mut self, role: impl Into<RoleOverride>) -> Self { |
| 136 | self.role = role.into(); |
| 137 | self |
| 138 | } |
| 139 | |
| 140 | /// Set the developer-assigned accessibility identifier. |
| 141 | pub fn accessibility_id(mut self, id: impl Into<SharedString>) -> Self { |
| 142 | self.accessibility_id = Some(id.into()); |
| 143 | self |
| 144 | } |
| 145 | |
| 146 | pub fn aria_label(mut self, label: impl Into<SharedString>) -> Self { |
| 147 | self.aria_label = Some(label.into()); |
| 148 | self |
| 149 | } |
| 150 | |
| 151 | /// Replace the built-in context menu shown on right-click. |
| 152 | /// |
| 153 | /// The closure receives an empty menu and returns the one to show, so it |
| 154 | /// decides entirely what appears — the default items are not added. It |
| 155 | /// shows only while the state's context menu is enabled, which is the |
| 156 | /// default. |
| 157 | pub fn context_menu( |
| 158 | mut self, |
| 159 | f: impl Fn(NativeMenu, &mut Window, &mut App) -> NativeMenu + 'static, |
| 160 | ) -> Self { |
| 161 | self.context_menu_builder = Some(Rc::new(f)); |
| 162 | self |
| 163 | } |
| 164 | |
| 165 | /// Intercept paste payloads (images, files) before the default text insertion. |
| 166 | /// |
| 167 | /// `true` consumes the paste so nothing is inserted, `false` falls through |
| 168 | /// to `clipboard.text()`. Copied files arrive as `ExternalPaths` through |
| 169 | /// the same hook. On web the clipboard reads `None`; image paste needs |
| 170 | /// async clipboard access and is out of scope. |
| 171 | pub fn on_paste( |
| 172 | mut self, |
| 173 | handler: impl Fn(&gpui::ClipboardItem, &mut Window, &mut App) -> bool + 'static, |
| 174 | ) -> Self { |
| 175 | self.paste_handler = Some(Rc::new(handler)); |
| 176 | self |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl Sizable for Textarea { |
| 181 | fn with_size(mut self, size: impl Into<Size>) -> Self { |
| 182 | self.size = size.into(); |
| 183 | self |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | impl Styled for Textarea { |
| 188 | fn style(&mut self) -> &mut StyleRefinement { |
| 189 | &mut self.style |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl Textarea { |
| 194 | /// The [`Input`] this textarea renders, for a compound control that frames |
| 195 | /// it. |
| 196 | pub(crate) fn into_input(self) -> Input { |
| 197 | Input::from_state(self.state.clone()) |
| 198 | .when_some(self.token_renderer, |this, render| { |
| 199 | this.token(move |token, window, cx| render(token, window, cx)) |
| 200 | }) |
| 201 | .when_some(self.token_click_listener, |this, listener| { |
| 202 | this.on_token_click(move |event, window, cx| listener(event, window, cx)) |
| 203 | }) |
| 204 | .when_some(self.token_hover_listener, |this, listener| { |
| 205 | this.on_token_hover(move |event, window, cx| listener(event, window, cx)) |
| 206 | }) |
| 207 | .appearance(self.appearance) |
| 208 | .bordered(self.bordered) |
| 209 | .disabled(self.disabled) |
| 210 | .readonly(self.readonly) |
| 211 | .tab_index(self.tab_index) |
| 212 | .role(self.role) |
| 213 | .with_size(self.size) |
| 214 | .when_some(self.height, |this, height| this.h(height)) |
| 215 | .when_some(self.line_box_height, |this, height| { |
| 216 | this.line_box_height(height) |
| 217 | }) |
| 218 | .when_some(self.accessibility_id, |this, id| this.accessibility_id(id)) |
| 219 | .when_some(self.aria_label, |this, label| this.aria_label(label)) |
| 220 | .when_some(self.context_menu_builder, |this, build| { |
| 221 | this.context_menu(move |menu, window, cx| build(menu, window, cx)) |
| 222 | }) |
| 223 | .when_some(self.paste_handler, |this, handler| { |
| 224 | this.on_paste(move |item, window, cx| handler(item, window, cx)) |
| 225 | }) |
| 226 | .refine_style(&self.style) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | impl RenderOnce for Textarea { |
| 231 | fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
| 232 | self.into_input() |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #[cfg(test)] |
| 237 | mod tests { |
| 238 | use super::*; |
| 239 | |
| 240 | #[gpui::test] |
| 241 | fn test_on_paste_builder(cx: &mut gpui::TestAppContext) { |
| 242 | use gpui::{AppContext as _, Render}; |
| 243 | |
| 244 | struct Probe; |
| 245 | impl Render for Probe { |
| 246 | fn render( |
| 247 | &mut self, |
| 248 | _: &mut Window, |
| 249 | _: &mut gpui::Context<Self>, |
| 250 | ) -> impl gpui::IntoElement { |
| 251 | gpui::div() |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | cx.update(crate::init); |
| 256 | let _ = cx.add_window_view(|window, cx| { |
| 257 | let state = cx.new(|cx| TextareaState::new(window, cx)); |
| 258 | assert!(Textarea::new(&state).paste_handler.is_none()); |
| 259 | let textarea = Textarea::new(&state).on_paste(|_, _, _| true); |
| 260 | assert!(textarea.paste_handler.is_some()); |
| 261 | Probe |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | #[gpui::test] |
| 266 | fn test_on_token_hover_builder(cx: &mut gpui::TestAppContext) { |
| 267 | use gpui::{AppContext as _, Render}; |
| 268 | |
| 269 | struct Probe; |
| 270 | impl Render for Probe { |
| 271 | fn render( |
| 272 | &mut self, |
| 273 | _: &mut Window, |
| 274 | _: &mut gpui::Context<Self>, |
| 275 | ) -> impl gpui::IntoElement { |
| 276 | gpui::div() |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | cx.update(crate::init); |
| 281 | let _ = cx.add_window_view(|window, cx| { |
| 282 | let state = cx.new(|cx| TextareaState::new(window, cx)); |
| 283 | assert!(Textarea::new(&state).token_hover_listener.is_none()); |
| 284 | let textarea = Textarea::new(&state).on_token_hover(|_, _, _| {}); |
| 285 | assert!(textarea.token_hover_listener.is_some()); |
| 286 | Probe |
| 287 | }); |
| 288 | } |
| 289 | } |