1
+ use clipboard_rs:: Clipboard ;
1
2
use iced:: keyboard:: { self , key} ;
2
3
use iced:: widget:: { self , column} ;
3
4
use iced:: { mouse, Element , Event , Point , Size , Subscription , Task } ;
@@ -21,7 +22,7 @@ pub mod bookmark_bar;
21
22
pub use bookmark_bar:: bookmark_bar;
22
23
23
24
pub mod command_palatte;
24
- pub use command_palatte:: { command_palatte, CommandWindowState , ResultType } ;
25
+ pub use command_palatte:: { command_palatte, CommandPalatteState , ResultType } ;
25
26
26
27
use crate :: {
27
28
engines:: BrowserEngine , shortcut_pressed, to_url, Bookmark , Bookmarks , ImageInfo , Shortcuts ,
@@ -33,7 +34,7 @@ pub trait CustomWidget<Message> {
33
34
fn view (
34
35
& self ,
35
36
nav_bar_state : NavBarState ,
36
- command_window_state : CommandWindowState ,
37
+ command_window_state : CommandPalatteState ,
37
38
bookmarks : Option < Vec < Bookmark > > ,
38
39
shortcuts : Shortcuts ,
39
40
) ;
@@ -121,7 +122,7 @@ pub struct IcyBrowser<Engine: BrowserEngine> {
121
122
engine : Engine ,
122
123
home : Url ,
123
124
nav_bar_state : NavBarState ,
124
- command_window_state : CommandWindowState ,
125
+ command_palatte_state : CommandPalatteState ,
125
126
with_tab_bar : bool ,
126
127
with_nav_bar : bool ,
127
128
with_bookmark_bar : bool ,
@@ -138,7 +139,7 @@ impl<Engine: BrowserEngine> Default for IcyBrowser<Engine> {
138
139
engine : Engine :: new ( ) ,
139
140
home,
140
141
nav_bar_state : NavBarState :: new ( ) ,
141
- command_window_state : CommandWindowState :: new ( None ) ,
142
+ command_palatte_state : CommandPalatteState :: new ( None ) ,
142
143
with_tab_bar : false ,
143
144
with_nav_bar : false ,
144
145
with_bookmark_bar : false ,
@@ -181,7 +182,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
181
182
182
183
pub fn bookmarks ( mut self , bookmarks : & [ Bookmark ] ) -> Self {
183
184
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 ( ) ) ;
185
186
self
186
187
}
187
188
@@ -249,6 +250,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
249
250
Task :: none ( )
250
251
}
251
252
253
+ /// the update method which is required by iced for widgets
252
254
pub fn update ( & mut self , event : Message ) -> Task < Message > {
253
255
let task = match event {
254
256
Message :: Update => self . force_update ( ) ,
@@ -375,18 +377,18 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
375
377
Task :: none ( )
376
378
}
377
379
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 ( ) ;
381
383
382
384
if let Some ( url) = to_url ( query. as_str ( ) ) {
383
- self . command_window_state
385
+ self . command_palatte_state
384
386
. filtered_results
385
387
. push ( ResultType :: Url ( url. to_string ( ) ) ) ;
386
388
}
387
389
388
- self . command_window_state . filtered_results = self
389
- . command_window_state
390
+ self . command_palatte_state . filtered_results = self
391
+ . command_palatte_state
390
392
. filtered_results
391
393
. clone ( )
392
394
. into_iter ( )
@@ -401,6 +403,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
401
403
. contains ( & query. to_lowercase ( ) )
402
404
} )
403
405
. collect :: < Vec < _ > > ( ) ;
406
+ self . command_palatte_state . first_item ( ) ;
404
407
Task :: none ( )
405
408
}
406
409
Message :: CommandPalatteKeyboardEvent ( event) => {
@@ -418,56 +421,66 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
418
421
shortcut. 0 == Message :: HideOverlay || shortcut. 0 == Message :: ToggleOverlay
419
422
} ) {
420
423
if shortcut_pressed ( shortcut, & key, & modifiers) {
424
+ self . command_palatte_state . reset ( ) ;
421
425
return Task :: done ( Message :: HideOverlay ) ;
422
426
}
423
427
}
424
428
match key {
425
429
key:: Key :: Named ( key:: Named :: Escape ) => {
426
- self . command_window_state =
427
- CommandWindowState :: new ( self . bookmarks . clone ( ) ) ;
430
+ self . command_palatte_state . reset ( ) ;
428
431
Task :: done ( Message :: HideOverlay )
429
432
}
430
433
key:: Key :: Named ( key:: Named :: ArrowDown ) => {
431
- self . command_window_state . next_item ( ) ;
434
+ self . command_palatte_state . next_item ( ) ;
432
435
Task :: none ( )
433
436
}
434
437
key:: Key :: Named ( key:: Named :: ArrowUp ) => {
435
- self . command_window_state . previous_item ( ) ;
438
+ self . command_palatte_state . previous_item ( ) ;
436
439
Task :: none ( )
437
440
}
438
441
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 ( ) {
444
444
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 ]
447
447
. to_string ( ) ,
448
448
) )
449
+ } else {
450
+ Task :: none ( )
449
451
}
450
452
}
451
453
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 ;
454
455
Task :: done ( Message :: CommandPalatteQueryChanged ( format ! (
455
456
"{} " ,
456
- self . command_window_state . query
457
+ self . command_palatte_state . query
457
458
) ) )
458
459
}
459
460
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
+ }
466
479
}
467
480
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 {
469
482
if let Some ( selected_item) =
470
- & self . command_window_state . selected_item
483
+ & self . command_palatte_state . selected_item
471
484
{
472
485
if result. inner_name ( ) == * selected_item {
473
486
let task = match result {
@@ -480,8 +493,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
480
493
}
481
494
} ;
482
495
483
- self . command_window_state =
484
- CommandWindowState :: new ( self . bookmarks . clone ( ) ) ;
496
+ self . command_palatte_state . reset ( ) ;
485
497
486
498
return Task :: batch ( [
487
499
Task :: done ( task) ,
@@ -491,7 +503,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
491
503
}
492
504
}
493
505
494
- self . command_window_state . has_error = true ;
506
+ self . command_palatte_state . has_error = true ;
495
507
Task :: none ( )
496
508
}
497
509
@@ -578,7 +590,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
578
590
579
591
let browser_view = browser_view ( self . engine . get_tabs ( ) . get_current ( ) . get_view ( ) ) ;
580
592
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 ) )
582
594
} else {
583
595
column = column. push ( browser_view) ;
584
596
}
0 commit comments