Fixes #4573 Replaces the boolean argument of TextInput::adjust[horizontal|vertical] with enum to self document the code

This commit is contained in:
Matt McCoy 2015-01-08 15:53:49 -05:00
parent 7800d98728
commit 64dda93242

View file

@ -13,6 +13,12 @@ use std::cmp::{min, max};
use std::default::Default; use std::default::Default;
use std::num::SignedInt; use std::num::SignedInt;
#[deriving(Copy, PartialEq)]
enum Selection {
Selected,
NotSelected
}
#[jstraceable] #[jstraceable]
#[deriving(Copy)] #[deriving(Copy)]
struct TextPoint { struct TextPoint {
@ -98,7 +104,7 @@ impl TextInput {
1 1
} else { } else {
-1 -1
}, true); }, Selection::Selected);
} }
self.replace_selection("".into_string()); 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 /// Adjust the editing point position by a given of lines. The resulting column is
/// as close to the original column position as possible. /// 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 { if !self.multiline {
return; return;
} }
if select { if select == Selection::Selected {
if self.selection_begin.is_none() { if self.selection_begin.is_none() {
self.selection_begin = Some(self.edit_point); 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 /// 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 /// 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. /// adjusted vertically and the process repeats with the remaining adjustment requested.
fn adjust_horizontal(&mut self, adjust: int, select: bool) { fn adjust_horizontal(&mut self, adjust: int, select: Selection) {
if select { if select == Selection::Selected {
if self.selection_begin.is_none() { if self.selection_begin.is_none() {
self.selection_begin = Some(self.edit_point); 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. /// Process a given `KeyboardEvent` and return an action for the caller to execute.
pub fn handle_keydown(&mut self, event: JSRef<KeyboardEvent>) -> KeyReaction { 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() { match event.Key().as_slice() {
"a" if is_control_key(event) => { "a" if is_control_key(event) => {
self.select_all(); self.select_all();
@ -281,19 +294,19 @@ impl TextInput {
KeyReaction::DispatchInput KeyReaction::DispatchInput
} }
"ArrowLeft" => { "ArrowLeft" => {
self.adjust_horizontal(-1, event.ShiftKey()); self.adjust_horizontal(-1, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"ArrowRight" => { "ArrowRight" => {
self.adjust_horizontal(1, event.ShiftKey()); self.adjust_horizontal(1, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"ArrowUp" => { "ArrowUp" => {
self.adjust_vertical(-1, event.ShiftKey()); self.adjust_vertical(-1, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"ArrowDown" => { "ArrowDown" => {
self.adjust_vertical(1, event.ShiftKey()); self.adjust_vertical(1, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"Enter" => self.handle_return(), "Enter" => self.handle_return(),
@ -306,11 +319,11 @@ impl TextInput {
KeyReaction::Nothing KeyReaction::Nothing
} }
"PageUp" => { "PageUp" => {
self.adjust_vertical(-28, event.ShiftKey()); self.adjust_vertical(-28, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"PageDown" => { "PageDown" => {
self.adjust_vertical(28, event.ShiftKey()); self.adjust_vertical(28, maybe_select(event));
KeyReaction::Nothing KeyReaction::Nothing
} }
"Tab" => KeyReaction::TriggerDefaultAction, "Tab" => KeyReaction::TriggerDefaultAction,