From 0a6ebfa3ee084b272b44fa0f6e3b66e8371afbe0 Mon Sep 17 00:00:00 2001 From: James Moughan Date: Wed, 24 Dec 2014 00:03:42 +0000 Subject: Allow selection of all text in a text control using the ctrl-a/cmd-a shortcut. Fixes #4411. --- components/script/textinput.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'components/script/textinput.rs') diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 8e984fe60d6..2b357b793a2 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -64,6 +64,19 @@ enum DeleteDir { Backward } + +/// Was the keyboard event accompanied by the standard control modifier, +/// i.e. cmd on Mac OS or ctrl on other platforms. +#[cfg(target_os="macos")] +fn is_control_key(event: JSRef) -> bool { + event.MetaKey() && !event.CtrlKey() && !event.AltKey() +} + +#[cfg(not(target_os="macos"))] +fn is_control_key(event: JSRef) -> bool { + event.CtrlKey() && !event.MetaKey() && !event.AltKey() +} + impl TextInput { /// Instantiate a new text input control pub fn new(lines: Lines, initial: DOMString) -> TextInput { @@ -231,9 +244,24 @@ impl TextInput { return KeyReaction::DispatchInput; } + /// Select all text in the input control. + fn select_all(&mut self) { + self.selection_begin = Some(TextPoint { + line: 0, + index: 0, + }); + let last_line = self.lines.len() - 1; + self.edit_point.line = last_line; + self.edit_point.index = self.lines[last_line].char_len(); + } + /// Process a given `KeyboardEvent` and return an action for the caller to execute. pub fn handle_keydown(&mut self, event: JSRef) -> KeyReaction { match event.Key().as_slice() { + "a" if is_control_key(event) => { + self.select_all(); + KeyReaction::Nothing + }, // printable characters have single-character key values c if c.len() == 1 => { self.insert_char(c.char_at(0)); -- cgit v1.2.3