Skip to content

Commit 636b42c

Browse files
committed
fix item selection in command palatte
1 parent 9b36b14 commit 636b42c

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

src/widgets/command_palatte.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ impl ResultType {
2424
}
2525
}
2626

27-
pub struct CommandWindowState {
27+
pub struct CommandPalatteState {
2828
pub query: String,
2929
pub possible_results: Vec<ResultType>,
3030
pub filtered_results: Vec<ResultType>,
3131
pub selected_item: Option<String>,
3232
pub has_error: bool,
3333
}
3434

35-
impl CommandWindowState {
35+
impl CommandPalatteState {
3636
pub fn new(bookmarks: Option<Vec<Bookmark>>) -> Self {
3737
let mut results: Vec<ResultType> = Vec::new();
3838
// This may need to be extended in the future
@@ -71,6 +71,13 @@ impl CommandWindowState {
7171
}
7272
}
7373

74+
pub fn reset(&mut self) {
75+
self.query = String::new();
76+
self.filtered_results = self.possible_results.clone();
77+
self.selected_item = None;
78+
self.has_error = false;
79+
}
80+
7481
pub fn first_item(&mut self) {
7582
self.selected_item = self
7683
.filtered_results
@@ -134,15 +141,15 @@ impl CommandWindowState {
134141
}
135142
}
136143

137-
impl Default for CommandWindowState {
144+
impl Default for CommandPalatteState {
138145
fn default() -> Self {
139146
Self::new(None)
140147
}
141148
}
142149

143150
pub fn command_palatte<'a>(
144151
base: impl Into<Element<'a, Message>>,
145-
state: &'a CommandWindowState,
152+
state: &'a CommandPalatteState,
146153
) -> Element<'a, Message> {
147154
let mut window = container(column![
148155
text_input("Command Palatte", &state.query)

src/widgets/mod.rs

+49-37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clipboard_rs::Clipboard;
12
use iced::keyboard::{self, key};
23
use iced::widget::{self, column};
34
use iced::{mouse, Element, Event, Point, Size, Subscription, Task};
@@ -21,7 +22,7 @@ pub mod bookmark_bar;
2122
pub use bookmark_bar::bookmark_bar;
2223

2324
pub mod command_palatte;
24-
pub use command_palatte::{command_palatte, CommandWindowState, ResultType};
25+
pub use command_palatte::{command_palatte, CommandPalatteState, ResultType};
2526

2627
use crate::{
2728
engines::BrowserEngine, shortcut_pressed, to_url, Bookmark, Bookmarks, ImageInfo, Shortcuts,
@@ -33,7 +34,7 @@ pub trait CustomWidget<Message> {
3334
fn view(
3435
&self,
3536
nav_bar_state: NavBarState,
36-
command_window_state: CommandWindowState,
37+
command_window_state: CommandPalatteState,
3738
bookmarks: Option<Vec<Bookmark>>,
3839
shortcuts: Shortcuts,
3940
);
@@ -121,7 +122,7 @@ pub struct IcyBrowser<Engine: BrowserEngine> {
121122
engine: Engine,
122123
home: Url,
123124
nav_bar_state: NavBarState,
124-
command_window_state: CommandWindowState,
125+
command_palatte_state: CommandPalatteState,
125126
with_tab_bar: bool,
126127
with_nav_bar: bool,
127128
with_bookmark_bar: bool,
@@ -138,7 +139,7 @@ impl<Engine: BrowserEngine> Default for IcyBrowser<Engine> {
138139
engine: Engine::new(),
139140
home,
140141
nav_bar_state: NavBarState::new(),
141-
command_window_state: CommandWindowState::new(None),
142+
command_palatte_state: CommandPalatteState::new(None),
142143
with_tab_bar: false,
143144
with_nav_bar: false,
144145
with_bookmark_bar: false,
@@ -181,7 +182,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
181182

182183
pub fn bookmarks(mut self, bookmarks: &[Bookmark]) -> Self {
183184
self.bookmarks = Some(bookmarks.to_vec());
184-
self.command_window_state = CommandWindowState::new(self.bookmarks.clone());
185+
self.command_palatte_state = CommandPalatteState::new(self.bookmarks.clone());
185186
self
186187
}
187188

@@ -249,6 +250,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
249250
Task::none()
250251
}
251252

253+
/// the update method which is required by iced for widgets
252254
pub fn update(&mut self, event: Message) -> Task<Message> {
253255
let task = match event {
254256
Message::Update => self.force_update(),
@@ -375,18 +377,18 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
375377
Task::none()
376378
}
377379
Message::CommandPalatteQueryChanged(query) => {
378-
self.command_window_state.query = query.clone();
379-
self.command_window_state.filtered_results =
380-
self.command_window_state.possible_results.clone();
380+
self.command_palatte_state.query = query.clone();
381+
self.command_palatte_state.filtered_results =
382+
self.command_palatte_state.possible_results.clone();
381383

382384
if let Some(url) = to_url(query.as_str()) {
383-
self.command_window_state
385+
self.command_palatte_state
384386
.filtered_results
385387
.push(ResultType::Url(url.to_string()));
386388
}
387389

388-
self.command_window_state.filtered_results = self
389-
.command_window_state
390+
self.command_palatte_state.filtered_results = self
391+
.command_palatte_state
390392
.filtered_results
391393
.clone()
392394
.into_iter()
@@ -401,6 +403,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
401403
.contains(&query.to_lowercase())
402404
})
403405
.collect::<Vec<_>>();
406+
self.command_palatte_state.first_item();
404407
Task::none()
405408
}
406409
Message::CommandPalatteKeyboardEvent(event) => {
@@ -418,56 +421,66 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
418421
shortcut.0 == Message::HideOverlay || shortcut.0 == Message::ToggleOverlay
419422
}) {
420423
if shortcut_pressed(shortcut, &key, &modifiers) {
424+
self.command_palatte_state.reset();
421425
return Task::done(Message::HideOverlay);
422426
}
423427
}
424428
match key {
425429
key::Key::Named(key::Named::Escape) => {
426-
self.command_window_state =
427-
CommandWindowState::new(self.bookmarks.clone());
430+
self.command_palatte_state.reset();
428431
Task::done(Message::HideOverlay)
429432
}
430433
key::Key::Named(key::Named::ArrowDown) => {
431-
self.command_window_state.next_item();
434+
self.command_palatte_state.next_item();
432435
Task::none()
433436
}
434437
key::Key::Named(key::Named::ArrowUp) => {
435-
self.command_window_state.previous_item();
438+
self.command_palatte_state.previous_item();
436439
Task::none()
437440
}
438441
key::Key::Named(key::Named::Backspace) => {
439-
self.command_window_state.has_error = false;
440-
self.command_window_state.first_item();
441-
if self.command_window_state.query.is_empty() {
442-
Task::none()
443-
} else {
442+
self.command_palatte_state.has_error = false;
443+
if !self.command_palatte_state.query.is_empty() {
444444
Task::done(Message::CommandPalatteQueryChanged(
445-
self.command_window_state.query
446-
[..self.command_window_state.query.len() - 1]
445+
self.command_palatte_state.query
446+
[..self.command_palatte_state.query.len() - 1]
447447
.to_string(),
448448
))
449+
} else {
450+
Task::none()
449451
}
450452
}
451453
key::Key::Named(key::Named::Space) => {
452-
self.command_window_state.has_error = false;
453-
self.command_window_state.first_item();
454+
self.command_palatte_state.has_error = false;
454455
Task::done(Message::CommandPalatteQueryChanged(format!(
455456
"{} ",
456-
self.command_window_state.query
457+
self.command_palatte_state.query
457458
)))
458459
}
459460
key::Key::Character(char) => {
460-
self.command_window_state.has_error = false;
461-
self.command_window_state.first_item();
462-
Task::done(Message::CommandPalatteQueryChanged(format!(
463-
"{}{}",
464-
self.command_window_state.query, char
465-
)))
461+
self.command_palatte_state.has_error = false;
462+
// paste instead of registering char
463+
if modifiers.control() && char.as_str() == "v" {
464+
if let Ok(ctx) = clipboard_rs::ClipboardContext::new() {
465+
Task::done(Message::CommandPalatteQueryChanged(format!(
466+
"{}{}",
467+
self.command_palatte_state.query,
468+
ctx.get_text().unwrap_or("".to_string())
469+
)))
470+
} else {
471+
Task::none()
472+
}
473+
} else {
474+
Task::done(Message::CommandPalatteQueryChanged(format!(
475+
"{}{}",
476+
self.command_palatte_state.query, char
477+
)))
478+
}
466479
}
467480
key::Key::Named(key::Named::Enter) => {
468-
for result in &self.command_window_state.filtered_results {
481+
for result in &self.command_palatte_state.filtered_results {
469482
if let Some(selected_item) =
470-
&self.command_window_state.selected_item
483+
&self.command_palatte_state.selected_item
471484
{
472485
if result.inner_name() == *selected_item {
473486
let task = match result {
@@ -480,8 +493,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
480493
}
481494
};
482495

483-
self.command_window_state =
484-
CommandWindowState::new(self.bookmarks.clone());
496+
self.command_palatte_state.reset();
485497

486498
return Task::batch([
487499
Task::done(task),
@@ -491,7 +503,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
491503
}
492504
}
493505

494-
self.command_window_state.has_error = true;
506+
self.command_palatte_state.has_error = true;
495507
Task::none()
496508
}
497509

@@ -578,7 +590,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
578590

579591
let browser_view = browser_view(self.engine.get_tabs().get_current().get_view());
580592
if self.show_overlay {
581-
column = column.push(command_palatte(browser_view, &self.command_window_state))
593+
column = column.push(command_palatte(browser_view, &self.command_palatte_state))
582594
} else {
583595
column = column.push(browser_view);
584596
}

0 commit comments

Comments
 (0)