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)]
struct TextPoint {
/// 0-based line number
line: uint,
line: usize,
/// 0-based column number
index: uint,
index: usize,
}
/// 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.
fn current_line_length(&self) -> uint {
fn current_line_length(&self) -> usize {
self.lines[self.edit_point.line].chars().count()
}
/// 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: Selection) {
fn adjust_vertical(&mut self, adjust: isize, select: Selection) {
if !self.multiline {
return;
}
@ -187,26 +187,26 @@ impl TextInput {
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 {
self.edit_point.index = 0;
self.edit_point.line = 0;
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.index = self.current_line_length();
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);
}
/// 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: Selection) {
fn adjust_horizontal(&mut self, adjust: isize, select: Selection) {
if select == Selection::Selected {
if self.selection_begin.is_none() {
self.selection_begin = Some(self.edit_point);
@ -222,23 +222,23 @@ impl TextInput {
if adjust < 0 {
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.edit_point.index = self.current_line_length();
self.adjust_horizontal(adjust + remaining as int + 1, select);
} 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 {
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.edit_point.index = 0;
// one shift is consumed by the change of line, hence the -1
self.adjust_horizontal(adjust - remaining as int - 1, select);
} else {
self.edit_point.index = min(self.current_line_length(),
self.edit_point.index + adjust as uint);
self.edit_point.index + adjust as usize);
}
}
}