From 498f8c2293e1c1abcaf5890747dfec7e9f651805 Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Wed, 8 Apr 2020 09:29:45 -0400 Subject: [PATCH] TextBox can receive EditAction commands This makes it possible for another widget to tell the TextBox to perform some edit action, such as modifying the selection or replacing the text. --- druid/src/widget/textbox.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/druid/src/widget/textbox.rs b/druid/src/widget/textbox.rs index 636a386f44..c180ca6774 100644 --- a/druid/src/widget/textbox.rs +++ b/druid/src/widget/textbox.rs @@ -51,6 +51,9 @@ pub struct TextBox { } impl TextBox { + /// Perform an `EditAction`. The payload *must* be an `EditAction`. + pub const PERFORM_EDIT: Selector = Selector::new("druid-builtin.textbox.perform-edit"); + /// Create a new TextBox widget pub fn new() -> TextBox { Self { @@ -237,7 +240,9 @@ impl Widget for TextBox { match event { Event::MouseDown(mouse) => { - ctx.request_focus(); + if !ctx.has_focus() { + ctx.request_focus(); + } ctx.set_active(true); let cursor_offset = self.offset_for_point(mouse.pos, &text_layout); @@ -289,6 +294,12 @@ impl Widget for TextBox { ctx.set_handled(); } Event::Command(cmd) if cmd.selector == RESET_BLINK => self.reset_cursor_blink(ctx), + Event::Command(cmd) if cmd.selector == TextBox::PERFORM_EDIT => { + let edit = cmd + .get_object::() + .expect("PERFORM_EDIT contained non-edit payload"); + self.do_edit_action(edit.to_owned(), data); + } Event::Paste(ref item) => { if let Some(string) = item.get_string() { edit_action = Some(EditAction::Paste(string));