Skip to content

Commit 4a9c74a

Browse files
committed
can use command palatte like nav bar
1 parent e670333 commit 4a9c74a

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed

src/widgets/command_palatte.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use iced::widget::{center, column, container, mouse_area, opaque, stack, text_input};
22
use iced::widget::{scrollable, text, Column};
3-
use iced::{border, Color, Element, Length, Theme};
3+
use iced::{border, Color, Element, Length, Shadow, Theme};
44
use iced_event_wrapper::wrapper;
55
use strum_macros::Display;
66

@@ -11,13 +11,15 @@ use crate::Bookmark;
1111
pub enum ResultType {
1212
Commands(Message),
1313
Bookmarks(Bookmark),
14+
Url(String),
1415
}
1516

1617
impl ResultType {
1718
pub fn inner_name(&self) -> String {
1819
match self {
1920
ResultType::Commands(command) => command.to_string(),
2021
ResultType::Bookmarks(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()),
22+
ResultType::Url(url) => url.to_string(),
2123
}
2224
}
2325
}
@@ -27,6 +29,7 @@ pub struct CommandWindowState {
2729
pub possible_results: Vec<ResultType>,
2830
pub filtered_results: Vec<ResultType>,
2931
pub selected_item: Option<String>,
32+
pub has_error: bool,
3033
}
3134

3235
impl CommandWindowState {
@@ -64,6 +67,7 @@ impl CommandWindowState {
6467
possible_results: results.clone(),
6568
filtered_results: results,
6669
selected_item: None,
70+
has_error: false,
6771
}
6872
}
6973

@@ -140,7 +144,7 @@ pub fn command_palatte<'a>(
140144
base: impl Into<Element<'a, Message>>,
141145
state: &'a CommandWindowState,
142146
) -> Element<'a, Message> {
143-
let window = container(column![
147+
let mut window = container(column![
144148
text_input("Command Palatte", &state.query)
145149
.on_input(Message::CommandPalatteQueryChanged)
146150
.size(25),
@@ -152,12 +156,32 @@ pub fn command_palatte<'a>(
152156
.height(Length::Fill)
153157
])
154158
.padding(10)
155-
.center(600)
156-
.style(|theme: &Theme| container::Style {
157-
background: Some(theme.palette().background.into()),
158-
border: border::rounded(10),
159-
..container::Style::default()
160-
});
159+
.center(600);
160+
161+
println!("has error: {}", state.has_error);
162+
if state.has_error {
163+
window = window.style(|theme: &Theme| container::Style {
164+
background: Some(theme.palette().background.into()),
165+
border: border::rounded(10),
166+
shadow: Shadow {
167+
color: Color {
168+
r: 255.,
169+
g: 0.,
170+
b: 0.,
171+
a: 0.,
172+
},
173+
blur_radius: 10.,
174+
..Default::default()
175+
},
176+
..container::Style::default()
177+
});
178+
} else {
179+
window = window.style(|theme: &Theme| container::Style {
180+
background: Some(theme.palette().background.into()),
181+
border: border::rounded(10),
182+
..container::Style::default()
183+
});
184+
}
161185

162186
let stack = stack![
163187
base.into(),

src/widgets/mod.rs

+26-32
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
326326
Task::none()
327327
}
328328
Message::ToggleTabBar => {
329-
if self.with_tab_bar {
330-
self.with_tab_bar = false;
331-
} else {
332-
self.with_tab_bar = true;
333-
}
329+
self.with_tab_bar = !self.with_tab_bar;
334330
Task::none()
335331
}
336332
Message::ShowTabBar => {
@@ -342,11 +338,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
342338
Task::none()
343339
}
344340
Message::ToggleNavBar => {
345-
if self.with_nav_bar {
346-
self.with_nav_bar = false;
347-
} else {
348-
self.with_nav_bar = true;
349-
}
341+
self.with_nav_bar = !self.with_nav_bar;
350342
Task::none()
351343
}
352344
Message::ShowNavBar => {
@@ -358,11 +350,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
358350
Task::none()
359351
}
360352
Message::ToggleBookmarkBar => {
361-
if self.with_bookmark_bar {
362-
self.with_bookmark_bar = false;
363-
} else {
364-
self.with_bookmark_bar = true;
365-
}
353+
self.with_bookmark_bar = !self.with_bookmark_bar;
366354
Task::none()
367355
}
368356
Message::ShowBookmarkBar => {
@@ -375,9 +363,18 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
375363
}
376364
Message::CommandPalatteQueryChanged(query) => {
377365
self.command_window_state.query = query.clone();
366+
self.command_window_state.filtered_results =
367+
self.command_window_state.possible_results.clone();
368+
369+
if let Some(url) = to_url(query.as_str()) {
370+
self.command_window_state
371+
.filtered_results
372+
.push(ResultType::Url(url.to_string()));
373+
}
374+
378375
self.command_window_state.filtered_results = self
379376
.command_window_state
380-
.possible_results
377+
.filtered_results
381378
.clone()
382379
.into_iter()
383380
.filter(|command| {
@@ -390,7 +387,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
390387
.to_lowercase()
391388
.contains(&query.to_lowercase())
392389
})
393-
.collect();
390+
.collect::<Vec<_>>();
394391
Task::none()
395392
}
396393
Message::CommandPalatteKeyboardEvent(event) => {
@@ -413,11 +410,8 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
413410
}
414411
match key {
415412
key::Key::Named(key::Named::Escape) => {
416-
self.command_window_state.query = String::new();
417-
self.command_window_state.filtered_results =
418-
self.command_window_state.possible_results.clone();
419-
self.command_window_state.selected_item = None;
420-
413+
self.command_window_state =
414+
CommandWindowState::new(self.bookmarks.clone());
421415
Task::done(Message::HideOverlay)
422416
}
423417
key::Key::Named(key::Named::ArrowDown) => {
@@ -429,6 +423,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
429423
Task::none()
430424
}
431425
key::Key::Named(key::Named::Backspace) => {
426+
self.command_window_state.has_error = false;
432427
self.command_window_state.first_item();
433428
if self.command_window_state.query.is_empty() {
434429
Task::none()
@@ -441,13 +436,15 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
441436
}
442437
}
443438
key::Key::Named(key::Named::Space) => {
439+
self.command_window_state.has_error = false;
444440
self.command_window_state.first_item();
445441
Task::done(Message::CommandPalatteQueryChanged(format!(
446442
"{} ",
447443
self.command_window_state.query
448444
)))
449445
}
450446
key::Key::Character(char) => {
447+
self.command_window_state.has_error = false;
451448
self.command_window_state.first_item();
452449
Task::done(Message::CommandPalatteQueryChanged(format!(
453450
"{}{}",
@@ -465,12 +462,13 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
465462
ResultType::Bookmarks(bookmark) => {
466463
Message::GoToUrl(bookmark.url().to_string())
467464
}
465+
ResultType::Url(url) => {
466+
Message::GoToUrl(url.to_string())
467+
}
468468
};
469469

470-
self.command_window_state.query = String::new();
471-
self.command_window_state.filtered_results =
472-
self.command_window_state.possible_results.clone();
473-
self.command_window_state.selected_item = None;
470+
self.command_window_state =
471+
CommandWindowState::new(self.bookmarks.clone());
474472

475473
return Task::batch([
476474
Task::done(task),
@@ -479,13 +477,9 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
479477
}
480478
}
481479
}
482-
// TODO: maybe make red to show none was selected
483-
self.command_window_state.query = String::new();
484-
self.command_window_state.filtered_results =
485-
self.command_window_state.possible_results.clone();
486-
self.command_window_state.selected_item = None;
487480

488-
Task::done(Message::HideOverlay)
481+
self.command_window_state.has_error = true;
482+
Task::none()
489483
}
490484

491485
_ => Task::none(),

0 commit comments

Comments
 (0)