Skip to content

Commit

Permalink
fix some issues in command palatte
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 11, 2024
1 parent 142b7cb commit 73a058a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ mod helpers;
pub use helpers::{get_fonts, to_url, Bookmark, Bookmarks, ImageInfo};

mod shortcut;
pub use shortcut::{KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts};
pub use shortcut::{
shortcut_pressed, KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts,
};
2 changes: 1 addition & 1 deletion src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub type Shortcut = (Message, Vec<KeyType>);
/// Configures Widget Keyboard Shortcuts
pub type Shortcuts = Vec<Shortcut>;

pub fn check_shortcut(shortcut: &Shortcut, key: &Key, modifiers: &Modifiers) -> bool {
pub fn shortcut_pressed(shortcut: &Shortcut, key: &Key, modifiers: &Modifiers) -> bool {
shortcut
.1
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl CommandWindowState {
}
}

pub fn first_item(&mut self) {
self.selected_item = self
.filtered_results
.first()
.map(|res| res.inner_name())
.or(None)
}

pub fn next_item(&mut self) {
match &self.selected_item {
None => {
Expand Down Expand Up @@ -122,7 +130,7 @@ pub fn command_palatte<'a>(
state: &'a CommandWindowState,
) -> Element<'a, Message> {
let window = container(column![
text_input("Command Menu", &state.query)
text_input("Command Palatte", &state.query)
.on_input(Message::CommandPalatteQueryChanged)
.size(25),
container(results_list(
Expand Down
26 changes: 16 additions & 10 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use command_window::CommandWindowState;
use iced::keyboard::{self, key};
use iced::widget::{self, column};
use iced::{mouse, Element, Event, Point, Size, Subscription, Task};
Expand All @@ -21,12 +20,11 @@ pub use tab_bar::tab_bar;
pub mod bookmark_bar;
pub use bookmark_bar::bookmark_bar;

pub mod command_window;
pub use command_window::{command_palatte, ResultType};
pub mod command_palatte;
pub use command_palatte::{command_palatte, CommandWindowState, ResultType};

use crate::{
engines::BrowserEngine, shortcut::check_shortcut, to_url, Bookmark, Bookmarks, ImageInfo,
Shortcuts,
engines::BrowserEngine, shortcut_pressed, to_url, Bookmark, Bookmarks, ImageInfo, Shortcuts,
};

/// Allows users to implement their own custom view view with custom widgets and configurations
Expand Down Expand Up @@ -342,10 +340,18 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
modified_key: _,
physical_key: _,
location: _,
modifiers: _,
modifiers,
text: _,
}) = event
{
// use user shortcut to close command window
for shortcut in self.shortcuts.iter().filter(|shortcut| {
shortcut.0 == Message::HideOverlay || shortcut.0 == Message::ToggleOverlay
}) {
if shortcut_pressed(shortcut, &key, &modifiers) {
return Task::done(Message::HideOverlay);
}
}
match key {
key::Key::Named(key::Named::Escape) => {
self.command_window_state.query = String::new();
Expand All @@ -364,7 +370,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
Task::none()
}
key::Key::Named(key::Named::Backspace) => {
self.command_window_state.next_item();
self.command_window_state.first_item();
if self.command_window_state.query.is_empty() {
Task::none()
} else {
Expand All @@ -376,14 +382,14 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
}
key::Key::Named(key::Named::Space) => {
self.command_window_state.next_item();
self.command_window_state.first_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{} ",
self.command_window_state.query
)))
}
key::Key::Character(char) => {
self.command_window_state.next_item();
self.command_window_state.first_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{}{}",
self.command_window_state.query, char
Expand Down Expand Up @@ -470,7 +476,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {

// Shortcut (Customizable) behaviors
for shortcut in self.shortcuts.iter() {
if check_shortcut(shortcut, &key, &modifiers) {
if shortcut_pressed(shortcut, &key, &modifiers) {
return Task::done(shortcut.0.clone());
}
}
Expand Down

0 comments on commit 73a058a

Please sign in to comment.