From 223ccf3f04e749c42d685a6f4611a4a56caefb77 Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Fri, 11 Oct 2024 19:57:29 -0600 Subject: [PATCH] tabs are shown --- examples/keyboard_driven.rs | 7 +++++++ src/engines/mod.rs | 32 ++++++++++++++++++++++++++++++-- src/helpers.rs | 2 +- src/lib.rs | 12 ++++++++++++ src/widgets/command_palatte.rs | 21 +++++++++++++-------- src/widgets/mod.rs | 28 +++++++++++++--------------- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/examples/keyboard_driven.rs b/examples/keyboard_driven.rs index 3efe10a..c7120f2 100644 --- a/examples/keyboard_driven.rs +++ b/examples/keyboard_driven.rs @@ -46,6 +46,13 @@ impl Default for Browser { KeyType::Key(iced::keyboard::Key::Character("e".into())), ], ) + .add_shortcut( + icy_browser::Message::CreateTab, + vec![ + KeyType::Modifier(ShortcutModifier::Ctrl), + KeyType::Key(iced::keyboard::Key::Character("t".into())), + ], + ) .build(); let widgets = IcyBrowser::new() .with_custom_shortcuts(shortcuts) diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 330610d..c4f809d 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -56,9 +56,25 @@ pub trait TabInfo { fn title(&self) -> String; } +/// Can be converted from Tab to hold information for ResultType +#[derive(Clone, Debug, PartialEq)] +pub struct DisplayTab { + pub id: u32, + pub url: String, + pub title: String, +} + +impl From> for DisplayTab { + fn from(tab: Tab) -> Self { + DisplayTab { + id: tab.id, + url: tab.url(), + title: tab.title(), + } + } +} + /// Stores Tab info like url & title -// Some browser engines take a closure to the url and title -// to automatically update it when it changes pub struct Tab { id: u32, view: ImageInfo, @@ -75,6 +91,14 @@ impl Tab { } } + pub fn to_display_tab(&self) -> DisplayTab { + DisplayTab { + id: self.id, + url: self.url(), + title: self.title(), + } + } + pub fn get_view(&self) -> &ImageInfo { &self.view } @@ -146,6 +170,10 @@ impl Tabs { &self.tabs } + pub fn display_tabs(&self) -> Vec { + self.tabs.iter().map(|tab| tab.to_display_tab()).collect() + } + pub fn insert(&mut self, tab: Tab) -> u32 { let id = tab.id; self.tabs.push(tab); diff --git a/src/helpers.rs b/src/helpers.rs index 4bb6f32..e6c5691 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -15,7 +15,7 @@ pub fn get_fonts() -> Vec> { } // Image details for passing the view around -#[derive(Debug, Clone)] +#[derive(Clone, Debug, PartialEq)] pub struct ImageInfo { pub pixels: Vec, pub width: u32, diff --git a/src/lib.rs b/src/lib.rs index 6c7e6be..f28dc29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,3 +19,15 @@ mod shortcut; pub use shortcut::{ shortcut_pressed, KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts, }; + +/// Allows different widgets to interact in their native way +#[derive(Debug, Clone, PartialEq)] +pub enum TabSelectionType { + Id(u32), + Index(usize), +} +impl Default for TabSelectionType { + fn default() -> Self { + TabSelectionType::Index(0) + } +} diff --git a/src/widgets/command_palatte.rs b/src/widgets/command_palatte.rs index 4c9ec4b..975003a 100644 --- a/src/widgets/command_palatte.rs +++ b/src/widgets/command_palatte.rs @@ -4,22 +4,27 @@ use iced::{border, Color, Element, Length, Shadow, Theme}; use iced_event_wrapper::wrapper; use strum_macros::Display; -use super::Message; -use crate::Bookmark; +use crate::engines::DisplayTab; +use crate::{Bookmark, Message}; #[derive(Clone, Debug, Display, PartialEq)] pub enum ResultType { - Commands(Message), - Bookmarks(Bookmark), + #[strum(to_string = "Commands")] + Command(Message), + #[strum(to_string = "Bookmarks")] + Bookmark(Bookmark), + #[strum(to_string = "Tabs")] + Tab(DisplayTab), 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::Command(command) => command.to_string(), + ResultType::Bookmark(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()), ResultType::Url(url) => url.to_string(), + ResultType::Tab(tab) => format!("{} -> {}", tab.title, tab.url), } } } @@ -56,10 +61,10 @@ impl CommandPalatteState { Message::HideBookmarkBar, ] .into_iter() - .map(ResultType::Commands), + .map(ResultType::Command), ); if let Some(bookmarks) = bookmarks { - results.extend(bookmarks.into_iter().map(ResultType::Bookmarks)); + results.extend(bookmarks.into_iter().map(ResultType::Bookmark)); }; Self { diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index a5d3608..0c413d3 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -26,10 +26,11 @@ pub use command_palatte::{command_palatte, CommandPalatteState, ResultType}; use crate::{ engines::BrowserEngine, shortcut_pressed, to_url, Bookmark, Bookmarks, ImageInfo, Shortcuts, + TabInfo, TabSelectionType, }; /// Allows users to implement their own custom view view with custom widgets and configurations -pub trait CustomWidget { +pub trait CustomWidget { fn update(&mut self, message: Message); fn view( &self, @@ -99,18 +100,6 @@ pub enum Message { IcedEvent(Option), } -/// Allows different widgets to interact in their native way -#[derive(Debug, Clone, PartialEq)] -pub enum TabSelectionType { - Id(u32), - Index(usize), -} -impl Default for TabSelectionType { - fn default() -> Self { - TabSelectionType::Index(0) - } -} - /// Allows the user to write a custom homepage pub enum HomepageType<'a> { Url(&'a str), @@ -381,6 +370,12 @@ impl IcyBrowser { self.command_palatte_state.filtered_results = self.command_palatte_state.possible_results.clone(); + for tab in self.engine().get_tabs().display_tabs() { + self.command_palatte_state + .filtered_results + .push(ResultType::Tab(tab)); + } + if let Some(url) = to_url(query.as_str()) { self.command_palatte_state .filtered_results @@ -484,13 +479,16 @@ impl IcyBrowser { { if result.inner_name() == *selected_item { let task = match result { - ResultType::Commands(message) => message.clone(), - ResultType::Bookmarks(bookmark) => { + ResultType::Command(message) => message.clone(), + ResultType::Bookmark(bookmark) => { Message::GoToUrl(bookmark.url().to_string()) } ResultType::Url(url) => { Message::GoToUrl(url.to_string()) } + ResultType::Tab(tab) => { + Message::ChangeTab(TabSelectionType::Id(tab.id)) + } }; self.command_palatte_state.reset();