Skip to content

Commit

Permalink
create bookmark widget
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Sep 24, 2024
1 parent ff2af9f commit 63ccc5a
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 16 deletions.
6 changes: 5 additions & 1 deletion examples/basic_browser.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Simple browser with familiar browser widgets and the ultralight(webkit) webengine as a backend

use iced::{Settings, Task, Theme};
use icy_browser::{get_fonts, BasicBrowser, Message};
use icy_browser::{get_fonts, BasicBrowser, Bookmark, Message};

fn run() -> (BasicBrowser, Task<Message>) {
(
BasicBrowser::new_basic()
.with_tab_bar()
.with_nav_bar()
.with_bookmark_bar(vec![Bookmark::new(
"https://www.rust-lang.org",
"rust-lang.org",
)])
.build(),
Task::none(),
)
Expand Down
12 changes: 10 additions & 2 deletions examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iced::{Element, Settings, Subscription, Task};
use std::time::Duration;

use icy_browser::{
get_fonts, widgets, BasicBrowser, BrowserWidget, KeyType, Message as WidgetMessage,
get_fonts, widgets, BasicBrowser, Bookmark, BrowserWidget, KeyType, Message as WidgetMessage,
ShortcutBuilder, ShortcutModifier,
};

Expand All @@ -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 palatte");

iced::application("Keyboard Driven Browser", Browser::update, Browser::view)
.subscription(Browser::subscription)
Expand Down Expand Up @@ -50,6 +50,14 @@ impl Default for Browser {
let widgets = BrowserWidget::new_basic()
.with_custom_shortcuts(shortcuts)
.with_tab_bar()
.with_bookmark_bar(vec![
Bookmark::new("https://www.rust-lang.org", "rust-lang.org"),
Bookmark::new(
"https://github.com/LegitCamper/icy_browser",
"icy_browser github",
),
Bookmark::new("https://docs.rs/iced/latest/iced/", "iced docs"),
])
.build();

Self { widgets }
Expand Down
26 changes: 24 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use iced::widget::image::{Handle, Image};
use iced::widget::{
button,
image::{Handle, Image},
Button,
};
pub use iced_fonts::BOOTSTRAP_FONT_BYTES;
pub use iced_on_focus_widget::hoverable;
use std::borrow::Cow;
use std::{borrow::Cow, str::FromStr};
use url::{ParseError, Url};

mod engines;
Expand Down Expand Up @@ -93,3 +97,21 @@ fn to_url(url: &str) -> Option<Url> {
}
}
}

pub struct Bookmark {
url: Url,
name: String,
// icon: Optional<>
}
impl Bookmark {
pub fn new(url: &str, name: &str) -> Self {
Bookmark {
url: Url::from_str(url).expect("Failed to parse url from bookmark url"),
name: name.to_string(),
}
}

pub fn as_button(&self) -> Button<Message> {
button(self.name.as_str()).on_press(Message::GoToUrl(self.url.to_string()))
}
}
17 changes: 17 additions & 0 deletions src/widgets/bookmark_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use iced::{widget::Row, Element};

use super::Message;
use crate::Bookmark;

/// Creates bookmark bar widget
pub fn bookmark_bar(bookmarks: &Vec<Bookmark>) -> Element<Message> {
Row::from_vec(
bookmarks
.iter()
.map(|bookmark| bookmark.as_button().into())
.collect(),
)
.padding(5)
.spacing(5)
.into()
}
32 changes: 23 additions & 9 deletions src/widgets/command_window.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use iced::widget::{center, column, container, mouse_area, opaque, stack, text_input};
use iced::{border, Color, Element, Length, Theme};
use iced::{border, Color, Element, Font, Length, Theme};
use iced_aw::SelectionList;
use strum::IntoEnumIterator;

use super::Message;

pub enum ResultType {
Command(Message),
// Bookmark,
}

pub struct CommandWindowState {
pub query: String,
actions: Vec<String>,
commands: Vec<String>,
pub selected_action: String,
pub selected_index: usize,
}
Expand All @@ -16,7 +21,7 @@ impl CommandWindowState {
pub fn new() -> Self {
Self {
query: String::new(),
actions: Message::iter().map(|e| e.clone().to_string()).collect(),
commands: Message::iter().map(|e| e.clone().to_string()).collect(),
selected_action: String::new(),
selected_index: 0,
}
Expand All @@ -34,15 +39,24 @@ pub fn command_window<'a>(
state: &'a CommandWindowState,
) -> Element<'a, Message> {
let window = container(column![
text_input("Command Menu", &state.query).on_input(Message::QueryChanged),
SelectionList::new(&state.actions, Message::CommandSelectionChanged)
.width(Length::Fill)
.height(Length::Fill)
.style(|theme: &Theme, _| iced_aw::style::selection_list::Style {
text_input("Command Menu", &state.query)
.on_input(Message::QueryChanged)
.size(25),
SelectionList::new_with(
&state.commands,
Message::CommandSelectionChanged,
15.,
5,
|theme: &Theme, _| iced_aw::style::selection_list::Style {
text_color: theme.palette().text,
background: theme.palette().background.into(),
..Default::default()
}),
},
None,
Font::DEFAULT
)
.width(Length::Fill)
.height(Length::Fill)
])
.padding(10)
.center(600)
Expand Down
26 changes: 25 additions & 1 deletion src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ pub use nav_bar::nav_bar;
mod tab_bar;
pub use tab_bar::tab_bar;

mod bookmark_bar;
pub use bookmark_bar::bookmark_bar;

mod command_window;
pub use command_window::command_window;

use crate::Bookmark;
use crate::{engines::BrowserEngine, shortcut::check_shortcut, to_url, ImageInfo, Shortcuts};

// Options exist only to have defaults for EnumIter
Expand Down Expand Up @@ -58,6 +62,7 @@ pub enum Message {
UpdateUrl,
QueryChanged(String),
CommandSelectionChanged(usize, String),
CommandSelectionSelected,
SendKeyboardEvent(Option<keyboard::Event>),
SendMouseEvent(Point, Option<mouse::Event>),
UpdateViewSize(Size<u32>),
Expand Down Expand Up @@ -97,6 +102,7 @@ pub struct BrowserWidget<Engine: BrowserEngine, CustomViewState: Clone> {
custom_view_state: Option<CustomViewState>,
pub with_tab_bar: bool,
pub with_nav_bar: bool,
pub bookmarks: Option<Vec<Bookmark>>,
pub show_overlay: bool,
pub shortcuts: Shortcuts,
pub view_size: Size<u32>,
Expand All @@ -117,6 +123,7 @@ where
custom_view_state: None,
with_tab_bar: false,
with_nav_bar: false,
bookmarks: None,
show_overlay: false,
shortcuts: Shortcuts::default(),
view_size: Size::new(800, 800),
Expand Down Expand Up @@ -165,6 +172,11 @@ where
self
}

pub fn with_bookmark_bar(mut self, bookmarks: Vec<Bookmark>) -> Self {
self.bookmarks = Some(bookmarks);
self
}

pub fn with_custom_shortcuts(mut self, shortcuts: Shortcuts) -> Self {
self.shortcuts = shortcuts;
self
Expand Down Expand Up @@ -349,6 +361,9 @@ where
self.command_window_state.selected_action = name;
Task::none()
}
Message::CommandSelectionSelected => {
unimplemented!()
}
Message::ToggleOverlay => {
if self.show_overlay {
Task::done(Message::HideOverlay)
Expand Down Expand Up @@ -377,10 +392,16 @@ where
} = key
{
// Default behaviors
if key == keyboard::Key::Named(key::Named::Escape) && self.show_overlay
// escape to exit command palatte
if self.show_overlay && key == keyboard::Key::Named(key::Named::Escape)
{
return Task::done(Message::HideOverlay);
}
// ctrl + R = refresh
else if modifiers.control() && key == key::Key::Character("r".into())
{
return Task::done(Message::Refresh);
}

// Shortcut (Customizable) behaviors
for shortcut in self.shortcuts.iter() {
Expand Down Expand Up @@ -424,6 +445,9 @@ where
hoverable(nav_bar(&self.nav_bar_state)).on_focus_change(Message::UpdateUrl),
)
}
if let Some(bookmarks) = self.bookmarks.as_ref() {
column = column.push(bookmark_bar(bookmarks))
}

let browser_view = browser_view(
self.view_size,
Expand Down
1 change: 1 addition & 0 deletions src/widgets/nav_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Default for NavBarState {
}
}

/// Creates Navigation bar widget
pub fn nav_bar(state: &NavBarState) -> Element<Message> {
let back = tooltip_helper(
Button::new(icon_to_text(Bootstrap::ChevronBarLeft))
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/tab_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iced_aw::{TabBar as TB, TabLabel};
use super::{Message, TabSelectionType};
use crate::engines::{TabInfo, Tabs};

// helper function to create navigation bar
/// Creates Tab bar widget
pub fn tab_bar<Info: TabInfo>(tabs: &Tabs<Info>) -> Element<Message> {
let current_id = tabs.get_current_id();
let active_tab = tabs
Expand Down

0 comments on commit 63ccc5a

Please sign in to comment.