Skip to content

Commit

Permalink
tabs are shown
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 12, 2024
1 parent 636b42c commit 223ccf3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
7 changes: 7 additions & 0 deletions examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 30 additions & 2 deletions src/engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Info: TabInfo> From<Tab<Info>> for DisplayTab {
fn from(tab: Tab<Info>) -> 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<Info: TabInfo> {
id: u32,
view: ImageInfo,
Expand All @@ -75,6 +91,14 @@ impl<Info: TabInfo> Tab<Info> {
}
}

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
}
Expand Down Expand Up @@ -146,6 +170,10 @@ impl<Info: TabInfo> Tabs<Info> {
&self.tabs
}

pub fn display_tabs(&self) -> Vec<DisplayTab> {
self.tabs.iter().map(|tab| tab.to_display_tab()).collect()
}

pub fn insert(&mut self, tab: Tab<Info>) -> u32 {
let id = tab.id;
self.tabs.push(tab);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn get_fonts() -> Vec<Cow<'static, [u8]>> {
}

// Image details for passing the view around
#[derive(Debug, Clone)]
#[derive(Clone, Debug, PartialEq)]
pub struct ImageInfo {
pub pixels: Vec<u8>,
pub width: u32,
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
21 changes: 13 additions & 8 deletions src/widgets/command_palatte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 13 additions & 15 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> {
pub trait CustomWidget<Message, Info: TabInfo + Clone> {
fn update(&mut self, message: Message);
fn view(
&self,
Expand Down Expand Up @@ -99,18 +100,6 @@ pub enum Message {
IcedEvent(Option<iced::Event>),
}

/// 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),
Expand Down Expand Up @@ -381,6 +370,12 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
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
Expand Down Expand Up @@ -484,13 +479,16 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
{
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();
Expand Down

0 comments on commit 223ccf3

Please sign in to comment.