Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting numberpad input for the NumberInput #262

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 90 additions & 77 deletions src/widgets/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@
.bounds();
let dec_bounds = mod_children
.next()
.expect("fail to get decreate mod layout")
.expect("fail to get decrease mod layout")
.bounds();

if self.min == self.max {
Expand Down Expand Up @@ -471,95 +471,108 @@
if !text_input.is_focused() {
return event::Status::Ignored;
}
let (key, modifiers) = match ke {
keyboard::Event::KeyPressed { key, modifiers, .. } => (key, modifiers),
let (key, text) = match ke {
keyboard::Event::KeyPressed { key, text, .. } => (key, text),
keyboard::Event::ModifiersChanged(_) => {
return forward_to_text(event, shell, child, clipboard)
}
keyboard::Event::KeyReleased { .. } => return event::Status::Ignored,
};

match key {
keyboard::Key::Named(keyboard::key::Named::ArrowDown) => {
self.decrease_value(shell);
event::Status::Captured
}
keyboard::Key::Named(keyboard::key::Named::ArrowUp) => {
self.increase_value(shell);
event::Status::Captured
}
keyboard::Key::Named(
keyboard::key::Named::ArrowLeft | keyboard::key::Named::ArrowRight,
) => forward_to_text(event, shell, child, clipboard),
keyboard::Key::Named(keyboard::key::Named::Backspace)
if !T::zero().eq(&self.value) =>
{
let mut new_val = self.value.to_string();
match text_input.cursor().state(&Value::new(&new_val)) {
cursor::State::Index(idx) if idx >= 1 && idx <= new_val.len() => {
_ = new_val.remove(idx - 1);
}
cursor::State::Selection { start, end }
if start <= new_val.len() && end <= new_val.len() =>
{
new_val.replace_range(start.min(end)..start.max(end), "");
}
_ => return event::Status::Ignored,
}

if new_val.is_empty() {
new_val = T::zero().to_string();
}

match T::from_str(&new_val) {
Ok(val) if (self.min..self.max).contains(&val) && val != self.value => {
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
}
keyboard::Key::Named(_) | keyboard::Key::Unidentified => event::Status::Ignored,
keyboard::Key::Character(c) => {
if modifiers.command() && matches!(c.as_ref(), "a" | "c") {
match text {
Some(text) => {
if text == "\u{1}" || text == "\u{3}" {
// CTRL + a and CTRL + c
return forward_to_text(event, shell, child, clipboard);

Check warning on line 485 in src/widgets/number_input.rs

View workflow job for this annotation

GitHub Actions / all

unneeded `return` statement
}

let input = if modifiers.command() && c.as_ref() == "v" {
match clipboard.read(iced::advanced::clipboard::Kind::Standard) {
Some(paste) => paste,
None => return event::Status::Ignored,
} else if text == "\u{8}" {
// Backspace
if !T::zero().eq(&self.value) {

Check warning on line 488 in src/widgets/number_input.rs

View workflow job for this annotation

GitHub Actions / all

unnecessary boolean `not` operation
let mut new_val = self.value.to_string();
match text_input.cursor().state(&Value::new(&new_val)) {
cursor::State::Index(idx)
if idx >= 1 && idx <= new_val.len() =>
{
_ = new_val.remove(idx - 1);
}
cursor::State::Selection { start, end }
if start <= new_val.len() && end <= new_val.len() =>
{
new_val.replace_range(start.min(end)..start.max(end), "");
}
_ => return event::Status::Ignored,
}

if new_val.is_empty() {
new_val = T::zero().to_string();
}

match T::from_str(&new_val) {
Ok(val)
if (self.min..self.max).contains(&val)
&& val != self.value =>
{
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
} else {
event::Status::Ignored
}
} else if c.parse::<i64>().is_err() && c != "-" {
return event::Status::Ignored;
} else {
c.to_string()
};
let input = input.trim();

let mut new_val = self.value.to_string();
match text_input.cursor().state(&Value::new(&new_val)) {
cursor::State::Index(idx) if idx <= new_val.len() => {
new_val.insert_str(idx, input);
}
cursor::State::Selection { start, end }
if start <= new_val.len() && end <= new_val.len() =>
{
new_val.replace_range(start.min(end)..end.max(start), input);
let input = if text == "\u{16}" {
// CTRL + v
match clipboard.read(iced::advanced::clipboard::Kind::Standard) {
Some(paste) => paste,
None => return event::Status::Ignored,
}
} else if text.parse::<i64>().is_err() && text != "-" {
return event::Status::Ignored;
} else {
text.to_string()
};

let input = input.trim();

let mut new_val = self.value.to_string();
match text_input.cursor().state(&Value::new(&new_val)) {
cursor::State::Index(idx) if idx <= new_val.len() => {
new_val.insert_str(idx, input);
}
cursor::State::Selection { start, end }
if start <= new_val.len() && end <= new_val.len() =>
{
new_val.replace_range(start.min(end)..end.max(start), input);
}
_ => return event::Status::Ignored,
}
_ => return event::Status::Ignored,
}

match T::from_str(&new_val) {
Ok(val) if (self.min..self.max).contains(&val) && val != self.value => {
self.value = val;
forward_to_text(event, shell, child, clipboard)
match T::from_str(&new_val) {
Ok(val)
if (self.min..self.max).contains(&val) && val != self.value =>
{
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
Ok(_) => event::Status::Captured,
_ => event::Status::Ignored,
}
}
None => match key {
keyboard::Key::Named(keyboard::key::Named::ArrowDown) => {
self.decrease_value(shell);
event::Status::Captured
}
keyboard::Key::Named(keyboard::key::Named::ArrowUp) => {
self.increase_value(shell);
event::Status::Captured
}
keyboard::Key::Named(
keyboard::key::Named::ArrowLeft | keyboard::key::Named::ArrowRight,
) => forward_to_text(event, shell, child, clipboard),
_ => event::Status::Ignored,
},
}
}
Event::Mouse(mouse::Event::WheelScrolled { delta })
Expand Down