Skip to content

Commit

Permalink
works almost like expected
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 10, 2024
1 parent 26f54e8 commit b486deb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
28 changes: 17 additions & 11 deletions src/widgets/command_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub struct CommandWindowState {

impl CommandWindowState {
pub fn new(bookmarks: Option<Vec<Bookmark>>) -> Self {
// This may need to be extended in the future
let mut results: Vec<ResultType> = Vec::new();
// This may need to be extended in the future
results.extend(
vec![
Message::GoBackward,
Expand Down Expand Up @@ -68,12 +68,15 @@ impl CommandWindowState {
Some(selected_item) => {
if let Some(last) = self.filtered_results.last() {
if *selected_item != last.inner_name() {
let pos = self
if let Some(pos) = self
.filtered_results
.iter()
.position(|res| res.inner_name() == *selected_item)
.unwrap();
self.selected_item = Some(self.filtered_results[pos + 1].inner_name());
{
self.selected_item = Some(self.filtered_results[pos + 1].inner_name());
} else {
self.selected_item = None
}
}
}
}
Expand All @@ -92,12 +95,15 @@ impl CommandWindowState {
Some(selected_item) => {
if let Some(first) = self.filtered_results.first() {
if *selected_item != first.inner_name() {
let pos = self
if let Some(pos) = self
.filtered_results
.iter()
.position(|res| res.inner_name() == *selected_item)
.unwrap();
self.selected_item = Some(self.filtered_results[pos - 1].inner_name());
{
self.selected_item = Some(self.filtered_results[pos - 1].inner_name());
} else {
self.selected_item = None;
}
}
}
}
Expand Down Expand Up @@ -168,12 +174,12 @@ fn results_list<'a>(results: &[ResultType], selected_item: Option<String>) -> El
list.push(text(result.to_string()).size(20).into())
}

let mut text = text(format!(" {}", result.inner_name())).size(15);
let mut text = container(text(format!(" {}", result.inner_name())).size(16));
if let Some(selected_item) = selected_item.as_ref() {
if result.inner_name() == *selected_item {
// highlight currently selected element
// text = text.color(iced::Color::new(50., 138., 176., 100.));
text = text.size(30)
text = text.style(|theme: &Theme| {
container::Style::default().background(theme.palette().primary)
})
}
}
list.push(text.into())
Expand Down
70 changes: 68 additions & 2 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}) = event
{
match key {
key::Key::Named(key::Named::Escape) => {
self.command_window_state.query = String::new();
self.command_window_state.filtered_results =
self.command_window_state.possible_results.clone();
self.command_window_state.selected_item = None;

Task::done(Message::HideOverlay)
}
key::Key::Named(key::Named::ArrowDown) => {
self.command_window_state.next_item();
Task::none()
Expand All @@ -355,8 +363,66 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
self.command_window_state.previous_item();
Task::none()
}
// key::Key::Character(_) => todo!(),
// key::Key::Unidentified => todo!(),
key::Key::Named(key::Named::Backspace) => {
self.command_window_state.next_item();
if self.command_window_state.query.is_empty() {
Task::none()
} else {
Task::done(Message::CommandPalatteQueryChanged(
self.command_window_state.query
[..self.command_window_state.query.len() - 1]
.to_string(),
))
}
}
key::Key::Named(key::Named::Space) => {
self.command_window_state.next_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{} ",
self.command_window_state.query
)))
}
key::Key::Character(char) => {
self.command_window_state.next_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{}{}",
self.command_window_state.query, char
)))
}
key::Key::Named(key::Named::Enter) => {
for result in &self.command_window_state.filtered_results {
if let Some(selected_item) =
&self.command_window_state.selected_item
{
if result.inner_name() == *selected_item {
let task = match result {
ResultType::Commands(message) => message.clone(),
ResultType::Bookmarks(bookmark) => {
Message::GoToUrl(bookmark.url().to_string())
}
};

self.command_window_state.query = String::new();
self.command_window_state.filtered_results =
self.command_window_state.possible_results.clone();
self.command_window_state.selected_item = None;

return Task::batch([
Task::done(task),
Task::done(Message::HideOverlay),
]);
}
}
}
// TODO: maybe make red to show none was selected
self.command_window_state.query = String::new();
self.command_window_state.filtered_results =
self.command_window_state.possible_results.clone();
self.command_window_state.selected_item = None;

Task::done(Message::HideOverlay)
}

_ => Task::none(),
}
} else {
Expand Down

0 comments on commit b486deb

Please sign in to comment.