gpui-component/crates/story/examples
Duane Bester 31bab9dcc3
text_view: Adding optional code block actions to the TextView (#1725)
## Description

This PR Adds the ability for users of the library to provide code block
actions for code blocks rendered in markdown (or anything that uses the
TextViewStyle).

I tried to match the existing Fns:
```rs
pub fn heading_font_size<F>(mut self, f: F) -> Self
where
    F: Fn(u8, Pixels) -> Pixels + Send + Sync + 'static,
{
    self.heading_font_size = Some(Arc::new(f));
    self
}
```

New code in text_view:
```rs
pub fn code_block_actions<F, E>(mut self, f: F) -> Self
where
    F: Fn(SharedString, Option<SharedString>, &mut Window, &mut App) -> E
        + Send
        + Sync
        + 'static,
    E: IntoElement,
{
    self.code_block_actions = Some(Arc::new(move |code, lang, window, cx| {
        f(code, lang, window, cx).into_any_element()
    }));
    self
}
```

Example on adding a simple copy button:

```rs
TextView::markdown("preview", content, window, cx)
    .code_block_actions(|code, _lang, _window, _cx| {
        Clipboard::new("copy").value(code)
    })
```


## Screenshot

| Before                       | After                       |
| ---------------------------- | --------------------------- |
| <img width="630" height="527" alt="Screenshot 2025-12-01 at 8 37
50 PM"
src="https://github.com/user-attachments/assets/7ac3b8d9-da85-42f8-9d36-cc74848890ea"
/> | <img width="627" height="535" alt="Screenshot 2025-12-01 at 8 38
22 PM"
src="https://github.com/user-attachments/assets/cae67b43-bba8-4834-a060-4bd63d34aefd"
/> |


## How to Test

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce.

## Checklist

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and
followed the guidelines.
- [x] Reviewed the changes in this PR and confirmed AI generated code
(If any) is accurate.
- [x] Passed `cargo run` for story tests related to the changes.
- [ ] Tested macOS, Windows and Linux platforms performance (if the
change is platform-specific)

---------

Co-authored-by: Jason Lee <huacnlee@gmail.com>
2025-12-03 10:21:47 +00:00
..
fixtures editor: Add document_colors LSP. (#1329) 2025-10-06 18:08:12 +08:00
brush.rs setting: Add Settings component. (#1632) 2025-11-20 13:51:44 +00:00
dock.rs dock: Simplify DockItem API. (#1732) 2025-12-03 17:53:37 +08:00
editor.rs theme: Use font_family for all elements. (#1618) 2025-11-17 03:18:17 +00:00
html.rs theme: Use font_family for all elements. (#1618) 2025-11-17 03:18:17 +00:00
large-text.rs input: Add to support CodeEditor as single line mode. (#1696) 2025-11-28 19:51:40 +08:00
markdown.rs text_view: Adding optional code block actions to the TextView (#1725) 2025-12-03 10:21:47 +00:00
tiles.rs dock: Improve Panel trait changed title to return impl IntoElement. (#1719) 2025-12-01 18:13:23 +08:00
webview.rs assets: Add gpui-component-assets crate. (#1601) 2025-11-14 15:03:26 +08:00