diff options
author | Matt McCoy <mattnenterprise@yahoo.com> | 2015-01-08 15:53:49 -0500 |
---|---|---|
committer | Matt McCoy <mattnenterprise@yahoo.com> | 2015-01-08 18:13:52 -0500 |
commit | 64dda93242cea20bc0177df9d92f1615dd44daa6 (patch) | |
tree | 087f49307dcdca174209433502c70b07223c38ad /components/script | |
parent | 7800d98728bfa1375ad8b6a2dac7f2f35603b6d1 (diff) | |
download | servo-64dda93242cea20bc0177df9d92f1615dd44daa6.tar.gz servo-64dda93242cea20bc0177df9d92f1615dd44daa6.zip |
Fixes #4573 Replaces the boolean argument of TextInput::adjust[horizontal|vertical] with enum to self document the code
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/textinput.rs | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 066dbd10e39..03f8f95b435 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -13,6 +13,12 @@ use std::cmp::{min, max}; use std::default::Default; use std::num::SignedInt; +#[deriving(Copy, PartialEq)] +enum Selection { + Selected, + NotSelected +} + #[jstraceable] #[deriving(Copy)] struct TextPoint { @@ -98,7 +104,7 @@ impl TextInput { 1 } else { -1 - }, true); + }, Selection::Selected); } self.replace_selection("".into_string()); } @@ -165,12 +171,12 @@ impl TextInput { /// Adjust the editing point position by a given of lines. The resulting column is /// as close to the original column position as possible. - fn adjust_vertical(&mut self, adjust: int, select: bool) { + fn adjust_vertical(&mut self, adjust: int, select: Selection) { if !self.multiline { return; } - if select { + if select == Selection::Selected { if self.selection_begin.is_none() { self.selection_begin = Some(self.edit_point); } @@ -199,8 +205,8 @@ impl TextInput { /// Adjust the editing point position by a given number of columns. If the adjustment /// requested is larger than is available in the current line, the editing point is /// adjusted vertically and the process repeats with the remaining adjustment requested. - fn adjust_horizontal(&mut self, adjust: int, select: bool) { - if select { + fn adjust_horizontal(&mut self, adjust: int, select: Selection) { + if select == Selection::Selected { if self.selection_begin.is_none() { self.selection_begin = Some(self.edit_point); } @@ -258,6 +264,13 @@ impl TextInput { /// Process a given `KeyboardEvent` and return an action for the caller to execute. pub fn handle_keydown(&mut self, event: JSRef<KeyboardEvent>) -> KeyReaction { + //A simple way to convert an event to a selection + fn maybe_select(event: JSRef<KeyboardEvent>) -> Selection { + if event.ShiftKey() { + return Selection::Selected + } + return Selection::NotSelected + } match event.Key().as_slice() { "a" if is_control_key(event) => { self.select_all(); @@ -281,19 +294,19 @@ impl TextInput { KeyReaction::DispatchInput } "ArrowLeft" => { - self.adjust_horizontal(-1, event.ShiftKey()); + self.adjust_horizontal(-1, maybe_select(event)); KeyReaction::Nothing } "ArrowRight" => { - self.adjust_horizontal(1, event.ShiftKey()); + self.adjust_horizontal(1, maybe_select(event)); KeyReaction::Nothing } "ArrowUp" => { - self.adjust_vertical(-1, event.ShiftKey()); + self.adjust_vertical(-1, maybe_select(event)); KeyReaction::Nothing } "ArrowDown" => { - self.adjust_vertical(1, event.ShiftKey()); + self.adjust_vertical(1, maybe_select(event)); KeyReaction::Nothing } "Enter" => self.handle_return(), @@ -306,11 +319,11 @@ impl TextInput { KeyReaction::Nothing } "PageUp" => { - self.adjust_vertical(-28, event.ShiftKey()); + self.adjust_vertical(-28, maybe_select(event)); KeyReaction::Nothing } "PageDown" => { - self.adjust_vertical(28, event.ShiftKey()); + self.adjust_vertical(28, maybe_select(event)); KeyReaction::Nothing } "Tab" => KeyReaction::TriggerDefaultAction, |