Skip to content

Commit b486deb

Browse files
committed
works almost like expected
1 parent 26f54e8 commit b486deb

File tree

2 files changed

+85
-13
lines changed

2 files changed

+85
-13
lines changed

src/widgets/command_window.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub struct CommandWindowState {
3131

3232
impl CommandWindowState {
3333
pub fn new(bookmarks: Option<Vec<Bookmark>>) -> Self {
34-
// This may need to be extended in the future
3534
let mut results: Vec<ResultType> = Vec::new();
35+
// This may need to be extended in the future
3636
results.extend(
3737
vec![
3838
Message::GoBackward,
@@ -68,12 +68,15 @@ impl CommandWindowState {
6868
Some(selected_item) => {
6969
if let Some(last) = self.filtered_results.last() {
7070
if *selected_item != last.inner_name() {
71-
let pos = self
71+
if let Some(pos) = self
7272
.filtered_results
7373
.iter()
7474
.position(|res| res.inner_name() == *selected_item)
75-
.unwrap();
76-
self.selected_item = Some(self.filtered_results[pos + 1].inner_name());
75+
{
76+
self.selected_item = Some(self.filtered_results[pos + 1].inner_name());
77+
} else {
78+
self.selected_item = None
79+
}
7780
}
7881
}
7982
}
@@ -92,12 +95,15 @@ impl CommandWindowState {
9295
Some(selected_item) => {
9396
if let Some(first) = self.filtered_results.first() {
9497
if *selected_item != first.inner_name() {
95-
let pos = self
98+
if let Some(pos) = self
9699
.filtered_results
97100
.iter()
98101
.position(|res| res.inner_name() == *selected_item)
99-
.unwrap();
100-
self.selected_item = Some(self.filtered_results[pos - 1].inner_name());
102+
{
103+
self.selected_item = Some(self.filtered_results[pos - 1].inner_name());
104+
} else {
105+
self.selected_item = None;
106+
}
101107
}
102108
}
103109
}
@@ -168,12 +174,12 @@ fn results_list<'a>(results: &[ResultType], selected_item: Option<String>) -> El
168174
list.push(text(result.to_string()).size(20).into())
169175
}
170176

171-
let mut text = text(format!(" {}", result.inner_name())).size(15);
177+
let mut text = container(text(format!(" {}", result.inner_name())).size(16));
172178
if let Some(selected_item) = selected_item.as_ref() {
173179
if result.inner_name() == *selected_item {
174-
// highlight currently selected element
175-
// text = text.color(iced::Color::new(50., 138., 176., 100.));
176-
text = text.size(30)
180+
text = text.style(|theme: &Theme| {
181+
container::Style::default().background(theme.palette().primary)
182+
})
177183
}
178184
}
179185
list.push(text.into())

src/widgets/mod.rs

+68-2
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
347347
}) = event
348348
{
349349
match key {
350+
key::Key::Named(key::Named::Escape) => {
351+
self.command_window_state.query = String::new();
352+
self.command_window_state.filtered_results =
353+
self.command_window_state.possible_results.clone();
354+
self.command_window_state.selected_item = None;
355+
356+
Task::done(Message::HideOverlay)
357+
}
350358
key::Key::Named(key::Named::ArrowDown) => {
351359
self.command_window_state.next_item();
352360
Task::none()
@@ -355,8 +363,66 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
355363
self.command_window_state.previous_item();
356364
Task::none()
357365
}
358-
// key::Key::Character(_) => todo!(),
359-
// key::Key::Unidentified => todo!(),
366+
key::Key::Named(key::Named::Backspace) => {
367+
self.command_window_state.next_item();
368+
if self.command_window_state.query.is_empty() {
369+
Task::none()
370+
} else {
371+
Task::done(Message::CommandPalatteQueryChanged(
372+
self.command_window_state.query
373+
[..self.command_window_state.query.len() - 1]
374+
.to_string(),
375+
))
376+
}
377+
}
378+
key::Key::Named(key::Named::Space) => {
379+
self.command_window_state.next_item();
380+
Task::done(Message::CommandPalatteQueryChanged(format!(
381+
"{} ",
382+
self.command_window_state.query
383+
)))
384+
}
385+
key::Key::Character(char) => {
386+
self.command_window_state.next_item();
387+
Task::done(Message::CommandPalatteQueryChanged(format!(
388+
"{}{}",
389+
self.command_window_state.query, char
390+
)))
391+
}
392+
key::Key::Named(key::Named::Enter) => {
393+
for result in &self.command_window_state.filtered_results {
394+
if let Some(selected_item) =
395+
&self.command_window_state.selected_item
396+
{
397+
if result.inner_name() == *selected_item {
398+
let task = match result {
399+
ResultType::Commands(message) => message.clone(),
400+
ResultType::Bookmarks(bookmark) => {
401+
Message::GoToUrl(bookmark.url().to_string())
402+
}
403+
};
404+
405+
self.command_window_state.query = String::new();
406+
self.command_window_state.filtered_results =
407+
self.command_window_state.possible_results.clone();
408+
self.command_window_state.selected_item = None;
409+
410+
return Task::batch([
411+
Task::done(task),
412+
Task::done(Message::HideOverlay),
413+
]);
414+
}
415+
}
416+
}
417+
// TODO: maybe make red to show none was selected
418+
self.command_window_state.query = String::new();
419+
self.command_window_state.filtered_results =
420+
self.command_window_state.possible_results.clone();
421+
self.command_window_state.selected_item = None;
422+
423+
Task::done(Message::HideOverlay)
424+
}
425+
360426
_ => Task::none(),
361427
}
362428
} else {

0 commit comments

Comments
 (0)