clipboard: Add value_fn to get copy value with callback to Clipboard. (#557)
This commit is contained in:
parent
355dfa5556
commit
ad04b699fc
3 changed files with 43 additions and 12 deletions
|
|
@ -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),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use crate::{
|
|||
pub struct Clipboard {
|
||||
id: ElementId,
|
||||
value: SharedString,
|
||||
value_fn: Option<Rc<dyn Fn(&mut WindowContext) -> SharedString>>,
|
||||
content_builder: Option<Box<dyn Fn(&mut WindowContext) -> AnyElement>>,
|
||||
copied_callback: Option<Rc<dyn Fn(SharedString, &mut WindowContext)>>,
|
||||
}
|
||||
|
|
@ -21,7 +22,8 @@ impl Clipboard {
|
|||
pub fn new(id: impl Into<ElementId>) -> 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<E, F>(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<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 {
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue