From 4a9c74acbe52cbb77f28e112f5567e8f12098602 Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Fri, 11 Oct 2024 17:00:29 -0600 Subject: [PATCH] can use command palatte like nav bar --- src/widgets/command_palatte.rs | 40 ++++++++++++++++++----- src/widgets/mod.rs | 58 +++++++++++++++------------------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/src/widgets/command_palatte.rs b/src/widgets/command_palatte.rs index d24aea5..3a0642b 100644 --- a/src/widgets/command_palatte.rs +++ b/src/widgets/command_palatte.rs @@ -1,6 +1,6 @@ use iced::widget::{center, column, container, mouse_area, opaque, stack, text_input}; use iced::widget::{scrollable, text, Column}; -use iced::{border, Color, Element, Length, Theme}; +use iced::{border, Color, Element, Length, Shadow, Theme}; use iced_event_wrapper::wrapper; use strum_macros::Display; @@ -11,6 +11,7 @@ use crate::Bookmark; pub enum ResultType { Commands(Message), Bookmarks(Bookmark), + Url(String), } impl ResultType { @@ -18,6 +19,7 @@ impl ResultType { match self { ResultType::Commands(command) => command.to_string(), ResultType::Bookmarks(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()), + ResultType::Url(url) => url.to_string(), } } } @@ -27,6 +29,7 @@ pub struct CommandWindowState { pub possible_results: Vec, pub filtered_results: Vec, pub selected_item: Option, + pub has_error: bool, } impl CommandWindowState { @@ -64,6 +67,7 @@ impl CommandWindowState { possible_results: results.clone(), filtered_results: results, selected_item: None, + has_error: false, } } @@ -140,7 +144,7 @@ pub fn command_palatte<'a>( base: impl Into>, state: &'a CommandWindowState, ) -> Element<'a, Message> { - let window = container(column![ + let mut window = container(column![ text_input("Command Palatte", &state.query) .on_input(Message::CommandPalatteQueryChanged) .size(25), @@ -152,12 +156,32 @@ pub fn command_palatte<'a>( .height(Length::Fill) ]) .padding(10) - .center(600) - .style(|theme: &Theme| container::Style { - background: Some(theme.palette().background.into()), - border: border::rounded(10), - ..container::Style::default() - }); + .center(600); + + println!("has error: {}", state.has_error); + if state.has_error { + window = window.style(|theme: &Theme| container::Style { + background: Some(theme.palette().background.into()), + border: border::rounded(10), + shadow: Shadow { + color: Color { + r: 255., + g: 0., + b: 0., + a: 0., + }, + blur_radius: 10., + ..Default::default() + }, + ..container::Style::default() + }); + } else { + window = window.style(|theme: &Theme| container::Style { + background: Some(theme.palette().background.into()), + border: border::rounded(10), + ..container::Style::default() + }); + } let stack = stack![ base.into(), diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 1a6f86d..5ca1c02 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -326,11 +326,7 @@ impl IcyBrowser { Task::none() } Message::ToggleTabBar => { - if self.with_tab_bar { - self.with_tab_bar = false; - } else { - self.with_tab_bar = true; - } + self.with_tab_bar = !self.with_tab_bar; Task::none() } Message::ShowTabBar => { @@ -342,11 +338,7 @@ impl IcyBrowser { Task::none() } Message::ToggleNavBar => { - if self.with_nav_bar { - self.with_nav_bar = false; - } else { - self.with_nav_bar = true; - } + self.with_nav_bar = !self.with_nav_bar; Task::none() } Message::ShowNavBar => { @@ -358,11 +350,7 @@ impl IcyBrowser { Task::none() } Message::ToggleBookmarkBar => { - if self.with_bookmark_bar { - self.with_bookmark_bar = false; - } else { - self.with_bookmark_bar = true; - } + self.with_bookmark_bar = !self.with_bookmark_bar; Task::none() } Message::ShowBookmarkBar => { @@ -375,9 +363,18 @@ impl IcyBrowser { } Message::CommandPalatteQueryChanged(query) => { self.command_window_state.query = query.clone(); + self.command_window_state.filtered_results = + self.command_window_state.possible_results.clone(); + + if let Some(url) = to_url(query.as_str()) { + self.command_window_state + .filtered_results + .push(ResultType::Url(url.to_string())); + } + self.command_window_state.filtered_results = self .command_window_state - .possible_results + .filtered_results .clone() .into_iter() .filter(|command| { @@ -390,7 +387,7 @@ impl IcyBrowser { .to_lowercase() .contains(&query.to_lowercase()) }) - .collect(); + .collect::>(); Task::none() } Message::CommandPalatteKeyboardEvent(event) => { @@ -413,11 +410,8 @@ impl IcyBrowser { } 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; - + self.command_window_state = + CommandWindowState::new(self.bookmarks.clone()); Task::done(Message::HideOverlay) } key::Key::Named(key::Named::ArrowDown) => { @@ -429,6 +423,7 @@ impl IcyBrowser { Task::none() } key::Key::Named(key::Named::Backspace) => { + self.command_window_state.has_error = false; self.command_window_state.first_item(); if self.command_window_state.query.is_empty() { Task::none() @@ -441,6 +436,7 @@ impl IcyBrowser { } } key::Key::Named(key::Named::Space) => { + self.command_window_state.has_error = false; self.command_window_state.first_item(); Task::done(Message::CommandPalatteQueryChanged(format!( "{} ", @@ -448,6 +444,7 @@ impl IcyBrowser { ))) } key::Key::Character(char) => { + self.command_window_state.has_error = false; self.command_window_state.first_item(); Task::done(Message::CommandPalatteQueryChanged(format!( "{}{}", @@ -465,12 +462,13 @@ impl IcyBrowser { ResultType::Bookmarks(bookmark) => { Message::GoToUrl(bookmark.url().to_string()) } + ResultType::Url(url) => { + Message::GoToUrl(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; + self.command_window_state = + CommandWindowState::new(self.bookmarks.clone()); return Task::batch([ Task::done(task), @@ -479,13 +477,9 @@ impl IcyBrowser { } } } - // 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) + self.command_window_state.has_error = true; + Task::none() } _ => Task::none(),