Skip to content

Commit

Permalink
TextBox can receive EditAction commands
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmyr committed Apr 8, 2020
1 parent 8d09e1a commit caa62a8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -237,7 +240,9 @@ impl Widget<String> 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);
Expand Down Expand Up @@ -289,6 +294,12 @@ impl Widget<String> 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::<EditAction>()
.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));
Expand Down

0 comments on commit caa62a8

Please sign in to comment.