Skip to content

Commit

Permalink
✨ Allow selection of plugins
Browse files Browse the repository at this point in the history
Allows the selection of the first entry of the previous, or next plugin.

By default the following actions:

- `select_next_plugin`
- `select_previous_plugin`

are bound to `^N` and `^P` respectively.
  • Loading branch information
a-kenji committed Feb 27, 2024
1 parent 3d10924 commit 59c26ea
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ impl Application for Centerpiece {
..
}) => self.select_next_entry(),

iced::Event::Keyboard(iced::keyboard::Event::KeyPressed {
key_code: iced::keyboard::KeyCode::N,
modifiers: iced::keyboard::Modifiers::CTRL,
}) => self.select_next_plugin(),

iced::Event::Keyboard(iced::keyboard::Event::KeyPressed {
key_code: iced::keyboard::KeyCode::P,
modifiers: iced::keyboard::Modifiers::CTRL,
}) => self.select_previous_plugin(),

iced::Event::Keyboard(iced::keyboard::Event::KeyPressed {
key_code: iced::keyboard::KeyCode::Enter,
..
Expand Down Expand Up @@ -294,6 +304,45 @@ impl Centerpiece {
)
}

fn select_next_plugin(&mut self) -> iced::Command<Message> {
let accumulated_entries = self
.plugins
.iter()
.map(|plugin| plugin.entries.len())
.scan(0, |acc, len| {
let prev = *acc;
*acc += len;
Some(prev)
})
.find(|&total| total > self.active_entry_index)
.unwrap_or(self.active_entry_index);

self.active_entry_index = accumulated_entries;
self.scroll_to_selected_entry()
}

fn select_previous_plugin(&mut self) -> iced::Command<Message> {
if self.plugins.is_empty() || self.active_entry_index == 0 {
return self.select_first_entry();
}

let accumulated_entries = self
.plugins
.iter()
.map(|plugin| plugin.entries.len())
.scan(0, |acc, len| {
let prev = *acc;
*acc += len;
Some(prev)
})
.take_while(|&total| total < self.active_entry_index)
.last()
.unwrap_or(0);

self.active_entry_index = accumulated_entries;
self.scroll_to_selected_entry()
}

fn register_plugin(&mut self, plugin: crate::model::Plugin) -> iced::Command<Message> {
self.plugins.push(plugin);
self.plugins.sort_by(|a, b| b.priority.cmp(&a.priority));
Expand Down

0 comments on commit 59c26ea

Please sign in to comment.