Use platform-sized integers for textinput.rs

This commit is contained in:
Josh Matthews 2015-02-20 16:12:58 -05:00
parent 172db80703
commit 837be27347

View file

@ -24,9 +24,9 @@ enum Selection {
#[derive(Copy)] #[derive(Copy)]
struct TextPoint { struct TextPoint {
/// 0-based line number /// 0-based line number
line: uint, line: usize,
/// 0-based column number /// 0-based column number
index: uint, index: usize,
} }
/// Encapsulated state for handling keyboard input in a single or multiline text input control. /// Encapsulated state for handling keyboard input in a single or multiline text input control.
@ -166,13 +166,13 @@ impl TextInput {
} }
/// Return the length of the current line under the editing point. /// Return the length of the current line under the editing point.
fn current_line_length(&self) -> uint { fn current_line_length(&self) -> usize {
self.lines[self.edit_point.line].chars().count() self.lines[self.edit_point.line].chars().count()
} }
/// 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: Selection) { fn adjust_vertical(&mut self, adjust: isize, select: Selection) {
if !self.multiline { if !self.multiline {
return; return;
} }
@ -187,26 +187,26 @@ impl TextInput {
assert!(self.edit_point.line < self.lines.len()); assert!(self.edit_point.line < self.lines.len());
let target_line: int = self.edit_point.line as int + adjust; let target_line: isize = self.edit_point.line as isize + adjust;
if target_line < 0 { if target_line < 0 {
self.edit_point.index = 0; self.edit_point.index = 0;
self.edit_point.line = 0; self.edit_point.line = 0;
return; return;
} else if target_line as uint >= self.lines.len() { } else if target_line as usize >= self.lines.len() {
self.edit_point.line = self.lines.len() - 1; self.edit_point.line = self.lines.len() - 1;
self.edit_point.index = self.current_line_length(); self.edit_point.index = self.current_line_length();
return; return;
} }
self.edit_point.line = target_line as uint; self.edit_point.line = target_line as usize;
self.edit_point.index = min(self.current_line_length(), self.edit_point.index); self.edit_point.index = min(self.current_line_length(), self.edit_point.index);
} }
/// 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: Selection) { fn adjust_horizontal(&mut self, adjust: isize, select: Selection) {
if select == Selection::Selected { 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);
@ -222,23 +222,23 @@ impl TextInput {
if adjust < 0 { if adjust < 0 {
let remaining = self.edit_point.index; let remaining = self.edit_point.index;
if adjust.abs() as uint > remaining && self.edit_point.line > 0 { if adjust.abs() as usize > remaining && self.edit_point.line > 0 {
self.adjust_vertical(-1, select); self.adjust_vertical(-1, select);
self.edit_point.index = self.current_line_length(); self.edit_point.index = self.current_line_length();
self.adjust_horizontal(adjust + remaining as int + 1, select); self.adjust_horizontal(adjust + remaining as int + 1, select);
} else { } else {
self.edit_point.index = max(0, self.edit_point.index as int + adjust) as uint; self.edit_point.index = max(0, self.edit_point.index as int + adjust) as usize;
} }
} else { } else {
let remaining = self.current_line_length() - self.edit_point.index; let remaining = self.current_line_length() - self.edit_point.index;
if adjust as uint > remaining && self.lines.len() > self.edit_point.line + 1 { if adjust as usize > remaining && self.lines.len() > self.edit_point.line + 1 {
self.adjust_vertical(1, select); self.adjust_vertical(1, select);
self.edit_point.index = 0; self.edit_point.index = 0;
// one shift is consumed by the change of line, hence the -1 // one shift is consumed by the change of line, hence the -1
self.adjust_horizontal(adjust - remaining as int - 1, select); self.adjust_horizontal(adjust - remaining as int - 1, select);
} else { } else {
self.edit_point.index = min(self.current_line_length(), self.edit_point.index = min(self.current_line_length(),
self.edit_point.index + adjust as uint); self.edit_point.index + adjust as usize);
} }
} }
} }