Make the channel argument to TextInput::new be optional, to support the signature expected by the unit tests.

This commit is contained in:
Avi Weinstock 2015-04-21 15:18:23 -04:00
parent 2d110e73b5
commit cf6aef5d51
4 changed files with 25 additions and 20 deletions

View file

@ -120,7 +120,7 @@ impl HTMLInputElement {
checked_changed: Cell::new(false), checked_changed: Cell::new(false),
value_changed: Cell::new(false), value_changed: Cell::new(false),
size: Cell::new(DEFAULT_INPUT_SIZE), size: Cell::new(DEFAULT_INPUT_SIZE),
textinput: DOMRefCell::new(TextInput::new(Single, "".to_owned(), chan)), textinput: DOMRefCell::new(TextInput::new(Single, "".to_owned(), Some(chan))),
activation_state: DOMRefCell::new(InputActivationState::new()) activation_state: DOMRefCell::new(InputActivationState::new())
} }
} }

View file

@ -94,7 +94,7 @@ impl HTMLTextAreaElement {
let chan = document.window().root().r().constellation_chan(); let chan = document.window().root().r().constellation_chan();
HTMLTextAreaElement { HTMLTextAreaElement {
htmlelement: HTMLElement::new_inherited(HTMLElementTypeId::HTMLTextAreaElement, localName, prefix, document), htmlelement: HTMLElement::new_inherited(HTMLElementTypeId::HTMLTextAreaElement, localName, prefix, document),
textinput: DOMRefCell::new(TextInput::new(Lines::Multiple, "".to_owned(), chan)), textinput: DOMRefCell::new(TextInput::new(Lines::Multiple, "".to_owned(), Some(chan))),
cols: Cell::new(DEFAULT_COLS), cols: Cell::new(DEFAULT_COLS),
rows: Cell::new(DEFAULT_ROWS), rows: Cell::new(DEFAULT_ROWS),
value_changed: Cell::new(false), value_changed: Cell::new(false),

View file

@ -43,7 +43,7 @@ pub struct TextInput {
selection_begin: Option<TextPoint>, selection_begin: Option<TextPoint>,
/// Is this a multiline input? /// Is this a multiline input?
multiline: bool, multiline: bool,
constellation_channel: ConstellationChan constellation_channel: Option<ConstellationChan>
} }
/// Resulting action to be taken by the owner of a text input that is handling an event. /// Resulting action to be taken by the owner of a text input that is handling an event.
@ -91,7 +91,7 @@ fn is_control_key(event: JSRef<KeyboardEvent>) -> bool {
impl TextInput { impl TextInput {
/// Instantiate a new text input control /// Instantiate a new text input control
pub fn new(lines: Lines, initial: DOMString, cc: ConstellationChan) -> TextInput { pub fn new(lines: Lines, initial: DOMString, cc: Option<ConstellationChan>) -> TextInput {
let mut i = TextInput { let mut i = TextInput {
lines: vec!(), lines: vec!(),
edit_point: Default::default(), edit_point: Default::default(),
@ -298,9 +298,14 @@ impl TextInput {
}, },
"v" if is_control_key(event) => { "v" if is_control_key(event) => {
let (tx, rx) = channel(); let (tx, rx) = channel();
self.constellation_channel.0.send(ConstellationMsg::GetClipboardContents(tx)).unwrap(); let mut contents = None;
let contents = rx.recv().unwrap(); if let Some(ref cc) = self.constellation_channel {
self.insert_string(contents.as_slice()); cc.0.send(ConstellationMsg::GetClipboardContents(tx)).unwrap();
contents = Some(rx.recv().unwrap());
}
if let Some(contents) = contents {
self.insert_string(contents.as_slice());
}
KeyReaction::DispatchInput KeyReaction::DispatchInput
}, },
// printable characters have single-character key values // printable characters have single-character key values

View file

@ -12,7 +12,7 @@ use std::borrow::ToOwned;
#[test] #[test]
fn test_textinput_delete_char() { fn test_textinput_delete_char() {
let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned(), None);
textinput.adjust_horizontal(2, Selection::NotSelected); textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.delete_char(DeleteDir::Backward); textinput.delete_char(DeleteDir::Backward);
assert_eq!(textinput.get_content(), "acdefg"); assert_eq!(textinput.get_content(), "acdefg");
@ -27,7 +27,7 @@ fn test_textinput_delete_char() {
#[test] #[test]
fn test_textinput_insert_char() { fn test_textinput_insert_char() {
let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned(), None);
textinput.adjust_horizontal(2, Selection::NotSelected); textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.insert_char('a'); textinput.insert_char('a');
assert_eq!(textinput.get_content(), "abacdefg"); assert_eq!(textinput.get_content(), "abacdefg");
@ -39,7 +39,7 @@ fn test_textinput_insert_char() {
#[test] #[test]
fn test_textinput_get_sorted_selection() { fn test_textinput_get_sorted_selection() {
let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned(), None);
textinput.adjust_horizontal(2, Selection::NotSelected); textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(2, Selection::Selected); textinput.adjust_horizontal(2, Selection::Selected);
let (begin, end) = textinput.get_sorted_selection(); let (begin, end) = textinput.get_sorted_selection();
@ -56,7 +56,7 @@ fn test_textinput_get_sorted_selection() {
#[test] #[test]
fn test_textinput_replace_selection() { fn test_textinput_replace_selection() {
let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned(), None);
textinput.adjust_horizontal(2, Selection::NotSelected); textinput.adjust_horizontal(2, Selection::NotSelected);
textinput.adjust_horizontal(2, Selection::Selected); textinput.adjust_horizontal(2, Selection::Selected);
@ -66,7 +66,7 @@ fn test_textinput_replace_selection() {
#[test] #[test]
fn test_textinput_current_line_length() { fn test_textinput_current_line_length() {
let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
assert_eq!(textinput.current_line_length(), 3); assert_eq!(textinput.current_line_length(), 3);
textinput.adjust_vertical(1, Selection::NotSelected); textinput.adjust_vertical(1, Selection::NotSelected);
@ -78,7 +78,7 @@ fn test_textinput_current_line_length() {
#[test] #[test]
fn test_textinput_adjust_vertical() { fn test_textinput_adjust_vertical() {
let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
textinput.adjust_horizontal(3, Selection::NotSelected); textinput.adjust_horizontal(3, Selection::NotSelected);
textinput.adjust_vertical(1, Selection::NotSelected); textinput.adjust_vertical(1, Selection::NotSelected);
assert_eq!(textinput.edit_point.line, 1); assert_eq!(textinput.edit_point.line, 1);
@ -95,7 +95,7 @@ fn test_textinput_adjust_vertical() {
#[test] #[test]
fn test_textinput_adjust_horizontal() { fn test_textinput_adjust_horizontal() {
let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
textinput.adjust_horizontal(4, Selection::NotSelected); textinput.adjust_horizontal(4, Selection::NotSelected);
assert_eq!(textinput.edit_point.line, 1); assert_eq!(textinput.edit_point.line, 1);
assert_eq!(textinput.edit_point.index, 0); assert_eq!(textinput.edit_point.index, 0);
@ -115,12 +115,12 @@ fn test_textinput_adjust_horizontal() {
#[test] #[test]
fn test_textinput_handle_return() { fn test_textinput_handle_return() {
let mut single_line_textinput = TextInput::new(Lines::Single, "abcdef".to_owned()); let mut single_line_textinput = TextInput::new(Lines::Single, "abcdef".to_owned(), None);
single_line_textinput.adjust_horizontal(3, Selection::NotSelected); single_line_textinput.adjust_horizontal(3, Selection::NotSelected);
single_line_textinput.handle_return(); single_line_textinput.handle_return();
assert_eq!(single_line_textinput.get_content(), "abcdef"); assert_eq!(single_line_textinput.get_content(), "abcdef");
let mut multi_line_textinput = TextInput::new(Lines::Multiple, "abcdef".to_owned()); let mut multi_line_textinput = TextInput::new(Lines::Multiple, "abcdef".to_owned(), None);
multi_line_textinput.adjust_horizontal(3, Selection::NotSelected); multi_line_textinput.adjust_horizontal(3, Selection::NotSelected);
multi_line_textinput.handle_return(); multi_line_textinput.handle_return();
assert_eq!(multi_line_textinput.get_content(), "abc\ndef"); assert_eq!(multi_line_textinput.get_content(), "abc\ndef");
@ -128,7 +128,7 @@ fn test_textinput_handle_return() {
#[test] #[test]
fn test_textinput_select_all() { fn test_textinput_select_all() {
let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
assert_eq!(textinput.edit_point.line, 0); assert_eq!(textinput.edit_point.line, 0);
assert_eq!(textinput.edit_point.index, 0); assert_eq!(textinput.edit_point.index, 0);
@ -139,16 +139,16 @@ fn test_textinput_select_all() {
#[test] #[test]
fn test_textinput_get_content() { fn test_textinput_get_content() {
let single_line_textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); let single_line_textinput = TextInput::new(Lines::Single, "abcdefg".to_owned(), None);
assert_eq!(single_line_textinput.get_content(), "abcdefg"); assert_eq!(single_line_textinput.get_content(), "abcdefg");
let multi_line_textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let multi_line_textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
assert_eq!(multi_line_textinput.get_content(), "abc\nde\nf"); assert_eq!(multi_line_textinput.get_content(), "abc\nde\nf");
} }
#[test] #[test]
fn test_textinput_set_content() { fn test_textinput_set_content() {
let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned(), None);
assert_eq!(textinput.get_content(), "abc\nde\nf"); assert_eq!(textinput.get_content(), "abc\nde\nf");
textinput.set_content("abc\nf".to_owned()); textinput.set_content("abc\nf".to_owned());