Skip to content

Commit 223ccf3

Browse files
committed
tabs are shown
1 parent 636b42c commit 223ccf3

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

examples/keyboard_driven.rs

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ impl Default for Browser {
4646
KeyType::Key(iced::keyboard::Key::Character("e".into())),
4747
],
4848
)
49+
.add_shortcut(
50+
icy_browser::Message::CreateTab,
51+
vec![
52+
KeyType::Modifier(ShortcutModifier::Ctrl),
53+
KeyType::Key(iced::keyboard::Key::Character("t".into())),
54+
],
55+
)
4956
.build();
5057
let widgets = IcyBrowser::new()
5158
.with_custom_shortcuts(shortcuts)

src/engines/mod.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,25 @@ pub trait TabInfo {
5656
fn title(&self) -> String;
5757
}
5858

59+
/// Can be converted from Tab to hold information for ResultType
60+
#[derive(Clone, Debug, PartialEq)]
61+
pub struct DisplayTab {
62+
pub id: u32,
63+
pub url: String,
64+
pub title: String,
65+
}
66+
67+
impl<Info: TabInfo> From<Tab<Info>> for DisplayTab {
68+
fn from(tab: Tab<Info>) -> Self {
69+
DisplayTab {
70+
id: tab.id,
71+
url: tab.url(),
72+
title: tab.title(),
73+
}
74+
}
75+
}
76+
5977
/// Stores Tab info like url & title
60-
// Some browser engines take a closure to the url and title
61-
// to automatically update it when it changes
6278
pub struct Tab<Info: TabInfo> {
6379
id: u32,
6480
view: ImageInfo,
@@ -75,6 +91,14 @@ impl<Info: TabInfo> Tab<Info> {
7591
}
7692
}
7793

94+
pub fn to_display_tab(&self) -> DisplayTab {
95+
DisplayTab {
96+
id: self.id,
97+
url: self.url(),
98+
title: self.title(),
99+
}
100+
}
101+
78102
pub fn get_view(&self) -> &ImageInfo {
79103
&self.view
80104
}
@@ -146,6 +170,10 @@ impl<Info: TabInfo> Tabs<Info> {
146170
&self.tabs
147171
}
148172

173+
pub fn display_tabs(&self) -> Vec<DisplayTab> {
174+
self.tabs.iter().map(|tab| tab.to_display_tab()).collect()
175+
}
176+
149177
pub fn insert(&mut self, tab: Tab<Info>) -> u32 {
150178
let id = tab.id;
151179
self.tabs.push(tab);

src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn get_fonts() -> Vec<Cow<'static, [u8]>> {
1515
}
1616

1717
// Image details for passing the view around
18-
#[derive(Debug, Clone)]
18+
#[derive(Clone, Debug, PartialEq)]
1919
pub struct ImageInfo {
2020
pub pixels: Vec<u8>,
2121
pub width: u32,

src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ mod shortcut;
1919
pub use shortcut::{
2020
shortcut_pressed, KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts,
2121
};
22+
23+
/// Allows different widgets to interact in their native way
24+
#[derive(Debug, Clone, PartialEq)]
25+
pub enum TabSelectionType {
26+
Id(u32),
27+
Index(usize),
28+
}
29+
impl Default for TabSelectionType {
30+
fn default() -> Self {
31+
TabSelectionType::Index(0)
32+
}
33+
}

src/widgets/command_palatte.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ use iced::{border, Color, Element, Length, Shadow, Theme};
44
use iced_event_wrapper::wrapper;
55
use strum_macros::Display;
66

7-
use super::Message;
8-
use crate::Bookmark;
7+
use crate::engines::DisplayTab;
8+
use crate::{Bookmark, Message};
99

1010
#[derive(Clone, Debug, Display, PartialEq)]
1111
pub enum ResultType {
12-
Commands(Message),
13-
Bookmarks(Bookmark),
12+
#[strum(to_string = "Commands")]
13+
Command(Message),
14+
#[strum(to_string = "Bookmarks")]
15+
Bookmark(Bookmark),
16+
#[strum(to_string = "Tabs")]
17+
Tab(DisplayTab),
1418
Url(String),
1519
}
1620

1721
impl ResultType {
1822
pub fn inner_name(&self) -> String {
1923
match self {
20-
ResultType::Commands(command) => command.to_string(),
21-
ResultType::Bookmarks(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()),
24+
ResultType::Command(command) => command.to_string(),
25+
ResultType::Bookmark(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()),
2226
ResultType::Url(url) => url.to_string(),
27+
ResultType::Tab(tab) => format!("{} -> {}", tab.title, tab.url),
2328
}
2429
}
2530
}
@@ -56,10 +61,10 @@ impl CommandPalatteState {
5661
Message::HideBookmarkBar,
5762
]
5863
.into_iter()
59-
.map(ResultType::Commands),
64+
.map(ResultType::Command),
6065
);
6166
if let Some(bookmarks) = bookmarks {
62-
results.extend(bookmarks.into_iter().map(ResultType::Bookmarks));
67+
results.extend(bookmarks.into_iter().map(ResultType::Bookmark));
6368
};
6469

6570
Self {

src/widgets/mod.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ pub use command_palatte::{command_palatte, CommandPalatteState, ResultType};
2626

2727
use crate::{
2828
engines::BrowserEngine, shortcut_pressed, to_url, Bookmark, Bookmarks, ImageInfo, Shortcuts,
29+
TabInfo, TabSelectionType,
2930
};
3031

3132
/// Allows users to implement their own custom view view with custom widgets and configurations
32-
pub trait CustomWidget<Message> {
33+
pub trait CustomWidget<Message, Info: TabInfo + Clone> {
3334
fn update(&mut self, message: Message);
3435
fn view(
3536
&self,
@@ -99,18 +100,6 @@ pub enum Message {
99100
IcedEvent(Option<iced::Event>),
100101
}
101102

102-
/// Allows different widgets to interact in their native way
103-
#[derive(Debug, Clone, PartialEq)]
104-
pub enum TabSelectionType {
105-
Id(u32),
106-
Index(usize),
107-
}
108-
impl Default for TabSelectionType {
109-
fn default() -> Self {
110-
TabSelectionType::Index(0)
111-
}
112-
}
113-
114103
/// Allows the user to write a custom homepage
115104
pub enum HomepageType<'a> {
116105
Url(&'a str),
@@ -381,6 +370,12 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
381370
self.command_palatte_state.filtered_results =
382371
self.command_palatte_state.possible_results.clone();
383372

373+
for tab in self.engine().get_tabs().display_tabs() {
374+
self.command_palatte_state
375+
.filtered_results
376+
.push(ResultType::Tab(tab));
377+
}
378+
384379
if let Some(url) = to_url(query.as_str()) {
385380
self.command_palatte_state
386381
.filtered_results
@@ -484,13 +479,16 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
484479
{
485480
if result.inner_name() == *selected_item {
486481
let task = match result {
487-
ResultType::Commands(message) => message.clone(),
488-
ResultType::Bookmarks(bookmark) => {
482+
ResultType::Command(message) => message.clone(),
483+
ResultType::Bookmark(bookmark) => {
489484
Message::GoToUrl(bookmark.url().to_string())
490485
}
491486
ResultType::Url(url) => {
492487
Message::GoToUrl(url.to_string())
493488
}
489+
ResultType::Tab(tab) => {
490+
Message::ChangeTab(TabSelectionType::Id(tab.id))
491+
}
494492
};
495493

496494
self.command_palatte_state.reset();

0 commit comments

Comments
 (0)