Skip to content

Commit

Permalink
can use command palatte like nav bar
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 11, 2024
1 parent e670333 commit 4a9c74a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 40 deletions.
40 changes: 32 additions & 8 deletions src/widgets/command_palatte.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,13 +11,15 @@ use crate::Bookmark;
pub enum ResultType {
Commands(Message),
Bookmarks(Bookmark),
Url(String),
}

impl ResultType {
pub fn inner_name(&self) -> String {
match self {
ResultType::Commands(command) => command.to_string(),
ResultType::Bookmarks(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()),
ResultType::Url(url) => url.to_string(),
}
}
}
Expand All @@ -27,6 +29,7 @@ pub struct CommandWindowState {
pub possible_results: Vec<ResultType>,
pub filtered_results: Vec<ResultType>,
pub selected_item: Option<String>,
pub has_error: bool,
}

impl CommandWindowState {
Expand Down Expand Up @@ -64,6 +67,7 @@ impl CommandWindowState {
possible_results: results.clone(),
filtered_results: results,
selected_item: None,
has_error: false,
}
}

Expand Down Expand Up @@ -140,7 +144,7 @@ pub fn command_palatte<'a>(
base: impl Into<Element<'a, Message>>,
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),
Expand All @@ -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(),
Expand Down
58 changes: 26 additions & 32 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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 => {
Expand All @@ -342,11 +338,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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 => {
Expand All @@ -358,11 +350,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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 => {
Expand All @@ -375,9 +363,18 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
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| {
Expand All @@ -390,7 +387,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
.to_lowercase()
.contains(&query.to_lowercase())
})
.collect();
.collect::<Vec<_>>();
Task::none()
}
Message::CommandPalatteKeyboardEvent(event) => {
Expand All @@ -413,11 +410,8 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
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) => {
Expand All @@ -429,6 +423,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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()
Expand All @@ -441,13 +436,15 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
}
key::Key::Named(key::Named::Space) => {
self.command_window_state.has_error = false;
self.command_window_state.first_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{} ",
self.command_window_state.query
)))
}
key::Key::Character(char) => {
self.command_window_state.has_error = false;
self.command_window_state.first_item();
Task::done(Message::CommandPalatteQueryChanged(format!(
"{}{}",
Expand All @@ -465,12 +462,13 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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),
Expand All @@ -479,13 +477,9 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
}
}
// 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(),
Expand Down

0 comments on commit 4a9c74a

Please sign in to comment.