gpui-component/crates/story/src/clipboard_story.rs
Jason Lee 5e504d10bb
chore: Refactor tab key to switch focus. (#991)
Wait https://github.com/zed-industries/zed/pull/33008,
https://github.com/zed-industries/zed/pull/34804 to merge.


https://github.com/user-attachments/assets/3c21ebc9-44a0-4311-bb0f-5f63fe8d4bb3

- Improved `focus_ring` to render like an outline.
- Added to support press `Enter`, `Space` key to trigger Checkbox,
Button, Radio.

## Break Changes

- The `FocusableExt` has been changed to only for crate internal.
- The `FocusableCycle` has been removed, now GPUI has it own tab stop
implementation.
2025-09-29 16:48:24 +08:00

90 lines
2.8 KiB
Rust

use gpui::{
App, AppContext, Context, Entity, Focusable, IntoElement, ParentElement, Render, SharedString,
Styled, Window,
};
use gpui_component::{
clipboard::Clipboard,
input::{InputState, TextInput},
label::Label,
v_flex, ContextModal,
};
use crate::section;
pub struct ClipboardStory {
focus_handle: gpui::FocusHandle,
url_state: Entity<InputState>,
masked: bool,
}
impl super::Story for ClipboardStory {
fn title() -> &'static str {
"Clipboard"
}
fn description() -> &'static str {
"A button that helps you copy text or other content to your clipboard."
}
fn new_view(window: &mut Window, cx: &mut App) -> Entity<impl Render> {
Self::view(window, cx)
}
}
impl ClipboardStory {
pub(crate) fn new(window: &mut Window, cx: &mut App) -> Self {
let url_state =
cx.new(|cx| InputState::new(window, cx).default_value("https://github.com"));
Self {
url_state,
focus_handle: cx.focus_handle(),
masked: false,
}
}
pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(window, cx))
}
}
impl Focusable for ClipboardStory {
fn focus_handle(&self, _: &gpui::App) -> gpui::FocusHandle {
self.focus_handle.clone()
}
}
impl Render for ClipboardStory {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.gap_6()
.child(
section("Clipboard").max_w_md().child(
Clipboard::new("clipboard1")
.content(|_, _| Label::new("A clipboard button"))
.value_fn({
let view = cx.entity().clone();
move |_, cx| {
SharedString::from(format!("masked :{}", view.read(cx).masked))
}
})
.on_copied(|value, window, cx| {
window.push_notification(format!("Copied value: {}", value), cx)
}),
),
)
.child(
section("With in an Input").max_w_md().child(
TextInput::new(&self.url_state).suffix(
Clipboard::new("clipboard2")
.value_fn({
let state = self.url_state.clone();
move |_, cx| state.read(cx).value()
})
.on_copied(|value, window, cx| {
window.push_notification(format!("Copied value: {}", value), cx)
}),
),
),
)
}
}