diff --git a/crates/story/src/lib.rs b/crates/story/src/lib.rs index 73226156..c51e4590 100644 --- a/crates/story/src/lib.rs +++ b/crates/story/src/lib.rs @@ -45,8 +45,8 @@ pub use webview_story::WebViewStory; use gpui::{ actions, div, prelude::FluentBuilder as _, px, AnyElement, AnyView, AppContext, Context as _, Div, EventEmitter, FocusableView, Global, Hsla, InteractiveElement, IntoElement, Model, - ParentElement, Render, SharedString, Styled as _, View, ViewContext, VisualContext, - WindowContext, + ParentElement, Render, SharedString, StatefulInteractiveElement, Styled as _, View, + ViewContext, VisualContext, WindowContext, }; use ui::{ @@ -388,6 +388,7 @@ impl Render for StoryContainer { v_flex() .id("story-container") .size_full() + .overflow_y_scroll() .track_focus(&self.focus_handle) .on_action(cx.listener(Self::on_action_panel_info)) .when(self.description.len() > 0, |this| { @@ -402,7 +403,14 @@ impl Render for StoryContainer { ) }) .when_some(self.story.clone(), |this, story| { - this.child(v_flex().id("story-children").size_full().p_4().child(story)) + this.child( + v_flex() + .id("story-children") + .w_full() + .flex_1() + .p_4() + .child(story), + ) }) } } diff --git a/crates/story/src/text_story.rs b/crates/story/src/text_story.rs index b09ab7be..295e2042 100644 --- a/crates/story/src/text_story.rs +++ b/crates/story/src/text_story.rs @@ -1,5 +1,5 @@ use gpui::{ - div, px, rems, IntoElement, ParentElement, Render, Styled, View, ViewContext, + div, px, rems, IntoElement, ParentElement, Render, SharedString, Styled, View, ViewContext, VisualContext as _, WindowContext, }; @@ -235,7 +235,12 @@ impl Render for TextStory { .child( Clipboard::new("clipboard1") .content(|_| Label::new("Click icon to copy")) - .value("Copied!") + .value_fn({ + let view = cx.view().clone(); + move |cx| { + SharedString::from(format!("Check1 :{}", view.read(cx).check1)) + } + }) .on_copied(|value, _| println!("Copied value: {}", value)), ) .child( diff --git a/crates/ui/src/clipboard.rs b/crates/ui/src/clipboard.rs index e6055467..25a0a732 100644 --- a/crates/ui/src/clipboard.rs +++ b/crates/ui/src/clipboard.rs @@ -13,6 +13,7 @@ use crate::{ pub struct Clipboard { id: ElementId, value: SharedString, + value_fn: Option SharedString>>, content_builder: Option AnyElement>>, copied_callback: Option>, } @@ -21,7 +22,8 @@ impl Clipboard { pub fn new(id: impl Into) -> Self { Self { id: id.into(), - value: "".into(), + value: SharedString::default(), + value_fn: None, content_builder: None, copied_callback: None, } @@ -32,12 +34,14 @@ impl Clipboard { self } - pub fn content(mut self, element_builder: F) -> Self - where - E: IntoElement, - F: Fn(&mut WindowContext) -> E + 'static, - { - self.content_builder = Some(Box::new(move |cx| element_builder(cx).into_any_element())); + /// Set the value of the clipboard to the result of the given function. Default is None. + /// + /// When used this, the copy value will use the result of the function. + pub fn value_fn( + mut self, + value: impl Fn(&mut WindowContext) -> SharedString + 'static, + ) -> Self { + self.value_fn = Some(Rc::new(value)); self } @@ -48,6 +52,15 @@ impl Clipboard { self.copied_callback = Some(Rc::new(handler)); self } + + pub fn content(mut self, builder: F) -> Self + where + E: IntoElement, + F: Fn(&mut WindowContext) -> E + 'static, + { + self.content_builder = Some(Box::new(move |cx| builder(cx).into_any_element())); + self + } } impl IntoElement for Clipboard { @@ -89,6 +102,7 @@ impl Element for Clipboard { let copied_callback = self.copied_callback.as_ref().map(|c| c.clone()); let copied = state.copied.clone(); let copide_value = *copied.borrow(); + let value_fn = self.value_fn.clone(); let mut element = h_flex() .gap_1() @@ -106,6 +120,10 @@ impl Element for Clipboard { .when(!copide_value, |this| { this.on_click(move |_, cx| { cx.stop_propagation(); + let value = value_fn + .as_ref() + .map(|f| f(cx)) + .unwrap_or_else(|| value.clone()); cx.write_to_clipboard(ClipboardItem::new_string(value.to_string())); *copied.borrow_mut() = true;