clipboard: Add value_fn to get copy value with callback to Clipboard. (#557)

This commit is contained in:
Jason Lee 2025-01-20 15:54:27 +08:00 committed by GitHub
parent 355dfa5556
commit ad04b699fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 12 deletions

View file

@ -45,8 +45,8 @@ pub use webview_story::WebViewStory;
use gpui::{ use gpui::{
actions, div, prelude::FluentBuilder as _, px, AnyElement, AnyView, AppContext, Context as _, actions, div, prelude::FluentBuilder as _, px, AnyElement, AnyView, AppContext, Context as _,
Div, EventEmitter, FocusableView, Global, Hsla, InteractiveElement, IntoElement, Model, Div, EventEmitter, FocusableView, Global, Hsla, InteractiveElement, IntoElement, Model,
ParentElement, Render, SharedString, Styled as _, View, ViewContext, VisualContext, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled as _, View,
WindowContext, ViewContext, VisualContext, WindowContext,
}; };
use ui::{ use ui::{
@ -388,6 +388,7 @@ impl Render for StoryContainer {
v_flex() v_flex()
.id("story-container") .id("story-container")
.size_full() .size_full()
.overflow_y_scroll()
.track_focus(&self.focus_handle) .track_focus(&self.focus_handle)
.on_action(cx.listener(Self::on_action_panel_info)) .on_action(cx.listener(Self::on_action_panel_info))
.when(self.description.len() > 0, |this| { .when(self.description.len() > 0, |this| {
@ -402,7 +403,14 @@ impl Render for StoryContainer {
) )
}) })
.when_some(self.story.clone(), |this, story| { .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),
)
}) })
} }
} }

View file

@ -1,5 +1,5 @@
use gpui::{ use gpui::{
div, px, rems, IntoElement, ParentElement, Render, Styled, View, ViewContext, div, px, rems, IntoElement, ParentElement, Render, SharedString, Styled, View, ViewContext,
VisualContext as _, WindowContext, VisualContext as _, WindowContext,
}; };
@ -235,7 +235,12 @@ impl Render for TextStory {
.child( .child(
Clipboard::new("clipboard1") Clipboard::new("clipboard1")
.content(|_| Label::new("Click icon to copy")) .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)), .on_copied(|value, _| println!("Copied value: {}", value)),
) )
.child( .child(

View file

@ -13,6 +13,7 @@ use crate::{
pub struct Clipboard { pub struct Clipboard {
id: ElementId, id: ElementId,
value: SharedString, value: SharedString,
value_fn: Option<Rc<dyn Fn(&mut WindowContext) -> SharedString>>,
content_builder: Option<Box<dyn Fn(&mut WindowContext) -> AnyElement>>, content_builder: Option<Box<dyn Fn(&mut WindowContext) -> AnyElement>>,
copied_callback: Option<Rc<dyn Fn(SharedString, &mut WindowContext)>>, copied_callback: Option<Rc<dyn Fn(SharedString, &mut WindowContext)>>,
} }
@ -21,7 +22,8 @@ impl Clipboard {
pub fn new(id: impl Into<ElementId>) -> Self { pub fn new(id: impl Into<ElementId>) -> Self {
Self { Self {
id: id.into(), id: id.into(),
value: "".into(), value: SharedString::default(),
value_fn: None,
content_builder: None, content_builder: None,
copied_callback: None, copied_callback: None,
} }
@ -32,12 +34,14 @@ impl Clipboard {
self self
} }
pub fn content<E, F>(mut self, element_builder: F) -> Self /// Set the value of the clipboard to the result of the given function. Default is None.
where ///
E: IntoElement, /// When used this, the copy value will use the result of the function.
F: Fn(&mut WindowContext) -> E + 'static, pub fn value_fn(
{ mut self,
self.content_builder = Some(Box::new(move |cx| element_builder(cx).into_any_element())); value: impl Fn(&mut WindowContext) -> SharedString + 'static,
) -> Self {
self.value_fn = Some(Rc::new(value));
self self
} }
@ -48,6 +52,15 @@ impl Clipboard {
self.copied_callback = Some(Rc::new(handler)); self.copied_callback = Some(Rc::new(handler));
self self
} }
pub fn content<E, F>(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 { 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_callback = self.copied_callback.as_ref().map(|c| c.clone());
let copied = state.copied.clone(); let copied = state.copied.clone();
let copide_value = *copied.borrow(); let copide_value = *copied.borrow();
let value_fn = self.value_fn.clone();
let mut element = h_flex() let mut element = h_flex()
.gap_1() .gap_1()
@ -106,6 +120,10 @@ impl Element for Clipboard {
.when(!copide_value, |this| { .when(!copide_value, |this| {
this.on_click(move |_, cx| { this.on_click(move |_, cx| {
cx.stop_propagation(); 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())); cx.write_to_clipboard(ClipboardItem::new_string(value.to_string()));
*copied.borrow_mut() = true; *copied.borrow_mut() = true;