aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/textinput.rs
diff options
context:
space:
mode:
authorJames Moughan <jamougha@gmail.com>2014-12-24 00:03:42 +0000
committerJames Moughan <jamougha@gmail.com>2014-12-24 00:14:17 +0000
commit0a6ebfa3ee084b272b44fa0f6e3b66e8371afbe0 (patch)
treef6bba8db94dfa85a1da5f0539a0e73cd74d08882 /components/script/textinput.rs
parent7b7fe964d328362126a8a56ff463d48f3285391f (diff)
downloadservo-0a6ebfa3ee084b272b44fa0f6e3b66e8371afbe0.tar.gz
servo-0a6ebfa3ee084b272b44fa0f6e3b66e8371afbe0.zip
Allow selection of all text in a text control using the ctrl-a/cmd-a shortcut.
Fixes #4411.
Diffstat (limited to 'components/script/textinput.rs')
-rw-r--r--components/script/textinput.rs28
1 files changed, 28 insertions, 0 deletions
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<KeyboardEvent>) -> bool {
+ event.MetaKey() && !event.CtrlKey() && !event.AltKey()
+}
+
+#[cfg(not(target_os="macos"))]
+fn is_control_key(event: JSRef<KeyboardEvent>) -> 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<KeyboardEvent>) -> 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));