diff options
author | Avi Weinstock <aweinstock314@gmail.com> | 2015-03-26 12:06:30 -0400 |
---|---|---|
committer | Avi Weinstock <aweinstock314@gmail.com> | 2015-04-21 08:36:15 -0400 |
commit | ba4c45543889a09cccdd12aba39f4106b8a9346c (patch) | |
tree | b9b12c7e81881cdaacf2d283ef275c4673b9f8a8 /components/script/textinput.rs | |
parent | e4b620ea54c94e03095e4108bce94ec750416bba (diff) | |
download | servo-ba4c45543889a09cccdd12aba39f4106b8a9346c.tar.gz servo-ba4c45543889a09cccdd12aba39f4106b8a9346c.zip |
Implement X11 clipboard integration (Issue #5376).
Diffstat (limited to 'components/script/textinput.rs')
-rw-r--r-- | components/script/textinput.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/components/script/textinput.rs b/components/script/textinput.rs index c26549bff73..85bedec22d8 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -14,6 +14,8 @@ use std::cmp::{min, max}; use std::default::Default; use std::num::SignedInt; +use clipboard::ClipboardContext; + #[derive(Copy, PartialEq)] pub enum Selection { Selected, @@ -29,6 +31,8 @@ pub struct TextPoint { pub index: usize, } +no_jsmanaged_fields!(ClipboardContext); + /// Encapsulated state for handling keyboard input in a single or multiline text input control. #[jstraceable] pub struct TextInput { @@ -40,6 +44,8 @@ pub struct TextInput { selection_begin: Option<TextPoint>, /// Is this a multiline input? multiline: bool, + /// Means of accessing the clipboard + clipboard_ctx: ClipboardContext, } /// Resulting action to be taken by the owner of a text input that is handling an event. @@ -93,6 +99,7 @@ impl TextInput { edit_point: Default::default(), selection_begin: None, multiline: lines == Lines::Multiple, + clipboard_ctx: ClipboardContext::new().unwrap(), }; i.set_content(initial); i @@ -118,6 +125,15 @@ impl TextInput { self.replace_selection(ch.to_string()); } + /// Insert a string at the current editing point + fn insert_string(&mut self, s: &str) { + // it looks like this could be made performant by avoiding some redundant + // selection-related checks, but use the simple implementation for now + for ch in s.chars() { + self.insert_char(ch); + } + } + pub fn get_sorted_selection(&self) -> (TextPoint, TextPoint) { let begin = self.selection_begin.unwrap(); let end = self.edit_point; @@ -282,10 +298,15 @@ impl TextInput { self.select_all(); KeyReaction::Nothing }, + "v" if is_control_key(event) => { + let contents = self.clipboard_ctx.get_contents().unwrap(); + self.insert_string(contents.as_slice()); + KeyReaction::DispatchInput + }, // printable characters have single-character key values c if c.len() == 1 => { self.insert_char(c.char_at(0)); - return KeyReaction::DispatchInput; + KeyReaction::DispatchInput } "Space" => { self.insert_char(' '); |