Skip to content

Commit

Permalink
fix palette spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 12, 2024
1 parent 223ccf3 commit bd4e535
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> iced::Result {
..Default::default()
};

println!("Press 'Crtl + E' to open to Command palatte");
println!("Press 'Crtl + E' to open to Command palette");

iced::application("Keyboard Driven Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use engines::ultralight::Ultralight;

pub mod widgets;
pub use widgets::{
browser_view, command_palatte, nav_bar, tab_bar, HomepageType, IcyBrowser, Message,
browser_view, command_palette, nav_bar, tab_bar, HomepageType, IcyBrowser, Message,
};

mod helpers;
Expand Down
30 changes: 20 additions & 10 deletions src/widgets/command_palatte.rs → src/widgets/command_palette.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::widget::{center, column, container, mouse_area, opaque, stack, text_input};
use iced::widget::{center, column, container, mouse_area, opaque, stack};
use iced::widget::{scrollable, text, Column};
use iced::{border, Color, Element, Length, Shadow, Theme};
use iced_event_wrapper::wrapper;
Expand Down Expand Up @@ -29,15 +29,15 @@ impl ResultType {
}
}

pub struct CommandPalatteState {
pub struct CommandPaletteState {
pub query: String,
pub possible_results: Vec<ResultType>,
pub filtered_results: Vec<ResultType>,
pub selected_item: Option<String>,
pub has_error: bool,
}

impl CommandPalatteState {
impl CommandPaletteState {
pub fn new(bookmarks: Option<Vec<Bookmark>>) -> Self {
let mut results: Vec<ResultType> = Vec::new();
// This may need to be extended in the future
Expand Down Expand Up @@ -146,20 +146,30 @@ impl CommandPalatteState {
}
}

impl Default for CommandPalatteState {
impl Default for CommandPaletteState {
fn default() -> Self {
Self::new(None)
}
}

pub fn command_palatte<'a>(
pub fn command_palette<'a>(
base: impl Into<Element<'a, Message>>,
state: &'a CommandPalatteState,
state: &'a CommandPaletteState,
) -> Element<'a, Message> {
let search = container(
text(if state.query.is_empty() {
"Command Palette"
} else {
&state.query
})
.size(25),
)
.style(|theme: &Theme| container::bordered_box(theme))
.padding(5)
.width(Length::Fill);

let mut window = container(column![
text_input("Command Palatte", &state.query)
.on_input(Message::CommandPalatteQueryChanged)
.size(25),
search,
container(results_list(
state.filtered_results.as_slice(),
state.selected_item.clone(),
Expand Down Expand Up @@ -214,7 +224,7 @@ pub fn command_palatte<'a>(
];

wrapper(stack)
.on_keyboard_event(|event| Message::CommandPalatteKeyboardEvent(Some(event)))
.on_keyboard_event(|event| Message::CommandPaletteKeyboardEvent(Some(event)))
.into()
}

Expand Down
102 changes: 48 additions & 54 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub use tab_bar::tab_bar;
pub mod bookmark_bar;
pub use bookmark_bar::bookmark_bar;

pub mod command_palatte;
pub use command_palatte::{command_palatte, CommandPalatteState, ResultType};
pub mod command_palette;
pub use command_palette::{command_palette, CommandPaletteState, ResultType};

use crate::{
engines::BrowserEngine, shortcut_pressed, to_url, Bookmark, Bookmarks, ImageInfo, Shortcuts,
Expand All @@ -35,7 +35,7 @@ pub trait CustomWidget<Message, Info: TabInfo + Clone> {
fn view(
&self,
nav_bar_state: NavBarState,
command_window_state: CommandPalatteState,
command_window_state: CommandPaletteState,
bookmarks: Option<Vec<Bookmark>>,
shortcuts: Shortcuts,
);
Expand All @@ -45,7 +45,7 @@ pub trait CustomWidget<Message, Info: TabInfo + Clone> {
// Options exist only to have defaults for EnumIter
#[derive(Debug, Clone, PartialEq, Display, EnumIter)]
pub enum Message {
// Commands visible to user with shortcuts and command palatte
// Commands visible to user with shortcuts and command palette
#[strum(to_string = "Go Backward (Back)")]
GoBackward,
#[strum(to_string = "Go Forward (Forward)")]
Expand All @@ -63,11 +63,11 @@ pub enum Message {
CloseCurrentTab,
#[strum(to_string = "New Tab")]
CreateTab,
#[strum(to_string = "Toggle Command Palatte")]
#[strum(to_string = "Toggle Command Palette")]
ToggleOverlay,
#[strum(to_string = "Show Command Palatte")]
#[strum(to_string = "Show Command Palette")]
ShowOverlay,
#[strum(to_string = "Hide Command Palatte")]
#[strum(to_string = "Hide Command Palette")]
HideOverlay,
#[strum(to_string = "Toggle Tab Bar")]
ToggleTabBar,
Expand All @@ -92,8 +92,8 @@ pub enum Message {
Update,
UrlChanged(String),
UpdateUrl,
CommandPalatteQueryChanged(String),
CommandPalatteKeyboardEvent(Option<keyboard::Event>),
CommandPaletteQueryChanged,
CommandPaletteKeyboardEvent(Option<keyboard::Event>),
SendKeyboardEvent(Option<keyboard::Event>),
SendMouseEvent(Point, Option<mouse::Event>),
UpdateViewSize(Size<u32>),
Expand All @@ -111,7 +111,7 @@ pub struct IcyBrowser<Engine: BrowserEngine> {
engine: Engine,
home: Url,
nav_bar_state: NavBarState,
command_palatte_state: CommandPalatteState,
command_palette_state: CommandPaletteState,
with_tab_bar: bool,
with_nav_bar: bool,
with_bookmark_bar: bool,
Expand All @@ -128,7 +128,7 @@ impl<Engine: BrowserEngine> Default for IcyBrowser<Engine> {
engine: Engine::new(),
home,
nav_bar_state: NavBarState::new(),
command_palatte_state: CommandPalatteState::new(None),
command_palette_state: CommandPaletteState::new(None),
with_tab_bar: false,
with_nav_bar: false,
with_bookmark_bar: false,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {

pub fn bookmarks(mut self, bookmarks: &[Bookmark]) -> Self {
self.bookmarks = Some(bookmarks.to_vec());
self.command_palatte_state = CommandPalatteState::new(self.bookmarks.clone());
self.command_palette_state = CommandPaletteState::new(self.bookmarks.clone());
self
}

Expand Down Expand Up @@ -365,25 +365,25 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
self.with_bookmark_bar = false;
Task::none()
}
Message::CommandPalatteQueryChanged(query) => {
self.command_palatte_state.query = query.clone();
self.command_palatte_state.filtered_results =
self.command_palatte_state.possible_results.clone();
Message::CommandPaletteQueryChanged => {
self.command_palette_state.filtered_results =
self.command_palette_state.possible_results.clone();

for tab in self.engine().get_tabs().display_tabs() {
self.command_palatte_state
self.command_palette_state
.filtered_results
.push(ResultType::Tab(tab));
}

let query = &self.command_palette_state.query;
if let Some(url) = to_url(query.as_str()) {
self.command_palatte_state
self.command_palette_state
.filtered_results
.push(ResultType::Url(url.to_string()));
}

self.command_palatte_state.filtered_results = self
.command_palatte_state
self.command_palette_state.filtered_results = self
.command_palette_state
.filtered_results
.clone()
.into_iter()
Expand All @@ -398,10 +398,10 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
.contains(&query.to_lowercase())
})
.collect::<Vec<_>>();
self.command_palatte_state.first_item();
self.command_palette_state.first_item();
Task::none()
}
Message::CommandPalatteKeyboardEvent(event) => {
Message::CommandPaletteKeyboardEvent(event) => {
if let Some(keyboard::Event::KeyPressed {
key,
modified_key: _,
Expand All @@ -416,66 +416,60 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
shortcut.0 == Message::HideOverlay || shortcut.0 == Message::ToggleOverlay
}) {
if shortcut_pressed(shortcut, &key, &modifiers) {
self.command_palatte_state.reset();
self.command_palette_state.reset();
return Task::done(Message::HideOverlay);
}
}
match key {
key::Key::Named(key::Named::Escape) => {
self.command_palatte_state.reset();
self.command_palette_state.reset();
Task::done(Message::HideOverlay)
}
key::Key::Named(key::Named::ArrowDown) => {
self.command_palatte_state.next_item();
self.command_palette_state.next_item();
Task::none()
}
key::Key::Named(key::Named::ArrowUp) => {
self.command_palatte_state.previous_item();
self.command_palette_state.previous_item();
Task::none()
}
key::Key::Named(key::Named::Backspace) => {
self.command_palatte_state.has_error = false;
if !self.command_palatte_state.query.is_empty() {
Task::done(Message::CommandPalatteQueryChanged(
self.command_palatte_state.query
[..self.command_palatte_state.query.len() - 1]
.to_string(),
))
self.command_palette_state.has_error = false;
if !self.command_palette_state.query.is_empty() {
self.command_palette_state.query.pop();
Task::done(Message::CommandPaletteQueryChanged)
} else {
Task::none()
}
}
key::Key::Named(key::Named::Space) => {
self.command_palatte_state.has_error = false;
Task::done(Message::CommandPalatteQueryChanged(format!(
"{} ",
self.command_palatte_state.query
)))
self.command_palette_state.has_error = false;
self.command_palette_state.query.push(' ');
Task::done(Message::CommandPaletteQueryChanged)
}
key::Key::Character(char) => {
self.command_palatte_state.has_error = false;
self.command_palette_state.has_error = false;
// paste instead of registering char
if modifiers.control() && char.as_str() == "v" {
if let Ok(ctx) = clipboard_rs::ClipboardContext::new() {
Task::done(Message::CommandPalatteQueryChanged(format!(
"{}{}",
self.command_palatte_state.query,
ctx.get_text().unwrap_or("".to_string())
)))
if let Ok(text) = ctx.get_text() {
self.command_palette_state.query = text;
Task::done(Message::CommandPaletteQueryChanged)
} else {
Task::none()
}
} else {
Task::none()
}
} else {
Task::done(Message::CommandPalatteQueryChanged(format!(
"{}{}",
self.command_palatte_state.query, char
)))
self.command_palette_state.query.push_str(char.as_str());
Task::done(Message::CommandPaletteQueryChanged)
}
}
key::Key::Named(key::Named::Enter) => {
for result in &self.command_palatte_state.filtered_results {
for result in &self.command_palette_state.filtered_results {
if let Some(selected_item) =
&self.command_palatte_state.selected_item
&self.command_palette_state.selected_item
{
if result.inner_name() == *selected_item {
let task = match result {
Expand All @@ -491,7 +485,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
};

self.command_palatte_state.reset();
self.command_palette_state.reset();

return Task::batch([
Task::done(task),
Expand All @@ -501,7 +495,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
}

self.command_palatte_state.has_error = true;
self.command_palette_state.has_error = true;
Task::none()
}

Expand Down Expand Up @@ -539,7 +533,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
} = key
{
// Default behaviors
// escape to exit command palatte
// escape to exit command palette
if self.show_overlay && key == keyboard::Key::Named(key::Named::Escape)
{
return Task::done(Message::HideOverlay);
Expand Down Expand Up @@ -588,7 +582,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {

let browser_view = browser_view(self.engine.get_tabs().get_current().get_view());
if self.show_overlay {
column = column.push(command_palatte(browser_view, &self.command_palatte_state))
column = column.push(command_palette(browser_view, &self.command_palette_state))
} else {
column = column.push(browser_view);
}
Expand Down

0 comments on commit bd4e535

Please sign in to comment.