Skip to content

Commit

Permalink
Merge pull request #1991 from casperstorm/feat/comobox-cycle
Browse files Browse the repository at this point in the history
cycle `combo_box` with keybinds
  • Loading branch information
hecrj authored Aug 2, 2023
2 parents f7ca420 + 983764d commit 1b355f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Nix instructions to `DEPENDENCIES.md`. [#1859](https://github.com/iced-rs/iced/pull/1859)
- Loading spinners example. [#1902](https://github.com/iced-rs/iced/pull/1902)
- Workflow that verifies `CHANGELOG` is always up-to-date. [#1970](https://github.com/iced-rs/iced/pull/1970)
- Keybinds to cycle `ComboBox` options. [#1991](https://github.com/iced-rs/iced/pull/1991)

### Changed
- Updated `wgpu` to `0.16`. [#1807](https://github.com/iced-rs/iced/pull/1807)
Expand Down
43 changes: 34 additions & 9 deletions widget/src/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,13 @@ where

if let Event::Keyboard(keyboard::Event::KeyPressed {
key_code,
modifiers,
..
}) = event
{
match key_code {
keyboard::KeyCode::Enter => {
let shift_modifer = modifiers.shift();
match (key_code, shift_modifer) {
(keyboard::KeyCode::Enter, _) => {
if let Some(index) = &menu.hovered_option {
if let Some(option) =
state.filtered_options.options.get(*index)
Expand All @@ -483,9 +485,19 @@ where

event_status = event::Status::Captured;
}
keyboard::KeyCode::Up => {

(keyboard::KeyCode::Up, _)
| (keyboard::KeyCode::Tab, true) => {
if let Some(index) = &mut menu.hovered_option {
*index = index.saturating_sub(1);
if *index == 0 {
*index = state
.filtered_options
.options
.len()
.saturating_sub(1);
} else {
*index = index.saturating_sub(1);
}
} else {
menu.hovered_option = Some(0);
}
Expand All @@ -511,15 +523,28 @@ where

event_status = event::Status::Captured;
}
keyboard::KeyCode::Down => {
(keyboard::KeyCode::Down, _)
| (keyboard::KeyCode::Tab, false)
if !modifiers.shift() =>
{
if let Some(index) = &mut menu.hovered_option {
*index = index.saturating_add(1).min(
state
if *index
>= state
.filtered_options
.options
.len()
.saturating_sub(1),
);
.saturating_sub(1)
{
*index = 0;
} else {
*index = index.saturating_add(1).min(
state
.filtered_options
.options
.len()
.saturating_sub(1),
);
}
} else {
menu.hovered_option = Some(0);
}
Expand Down

0 comments on commit 1b355f5

Please sign in to comment.