Skip to content

Commit

Permalink
add more command palatte commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Oct 11, 2024
1 parent 88039cb commit e670333
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn run() -> (IcyBrowser<Ultralight>, Task<Message>) {
IcyBrowser::new()
.with_tab_bar()
.with_nav_bar()
.with_bookmark_bar(&[Bookmark::new("https://www.rust-lang.org", "rust-lang.org")])
.bookmarks(&[Bookmark::new("https://www.rust-lang.org", "rust-lang.org")])
.with_bookmark_bar()
.build(),
Task::none(),
)
Expand Down
3 changes: 2 additions & 1 deletion examples/basic_browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ fn run() -> (IcyBrowser<Ultralight>, Task<Message>) {
IcyBrowser::new()
.with_tab_bar()
.with_nav_bar()
.with_bookmark_bar(&[Bookmark::new("https://www.rust-lang.org", "rust-lang.org")])
.bookmarks(&[Bookmark::new("https://www.rust-lang.org", "rust-lang.org")])
.with_bookmark_bar()
.build(),
Task::none(),
)
Expand Down
3 changes: 2 additions & 1 deletion examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ impl Default for Browser {
let widgets = IcyBrowser::new()
.with_custom_shortcuts(shortcuts)
.with_tab_bar()
.with_bookmark_bar(&[
.bookmarks(&[
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"),
])
.with_bookmark_bar()
.build();

Self {
Expand Down
2 changes: 1 addition & 1 deletion src/engines/ultralight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ fn iced_key_to_ultralight_key(
#[cfg(windows)]
189,
#[cfg(unix)]
12,
74,
),
"+" => (
VirtualKeyCode::OemPlus,
Expand Down
13 changes: 12 additions & 1 deletion src/widgets/command_palatte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl ResultType {
pub fn inner_name(&self) -> String {
match self {
ResultType::Commands(command) => command.to_string(),
ResultType::Bookmarks(bookmark) => format!("Go to: {}", bookmark.url()),
ResultType::Bookmarks(bookmark) => format!("{} -> {}", bookmark.name(), bookmark.url()),
}
}
}
Expand All @@ -40,6 +40,17 @@ impl CommandWindowState {
Message::Refresh,
Message::GoHome,
Message::CloseCurrentTab,
Message::CreateTab,
Message::HideOverlay,
Message::ToggleTabBar,
Message::ShowTabBar,
Message::HideTabBar,
Message::ToggleNavBar,
Message::ShowNavBar,
Message::HideNavBar,
Message::ToggleBookmarkBar,
Message::ShowBookmarkBar,
Message::HideBookmarkBar,
]
.into_iter()
.map(ResultType::Commands),
Expand Down
119 changes: 89 additions & 30 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ pub enum Message {
ShowOverlay,
#[strum(to_string = "Hide Command Palatte")]
HideOverlay,
#[strum(to_string = "Toggle Tab Bar")]
ToggleTabBar,
#[strum(to_string = "Show Tab Bar")]
ShowTabBar,
#[strum(to_string = "Hide Tab Bar")]
HideTabBar,
#[strum(to_string = "Toggle Nav Bar")]
ToggleNavBar,
#[strum(to_string = "Show Nav Bar")]
ShowNavBar,
#[strum(to_string = "Hide Nav Bar")]
HideNavBar,
#[strum(to_string = "Toggle Bookmark Bar")]
ToggleBookmarkBar,
#[strum(to_string = "Show Bookmark Bar")]
ShowBookmarkBar,
#[strum(to_string = "Hide Bookmark Bar")]
HideBookmarkBar,

// Internal only - for widgets
Update,
Expand Down Expand Up @@ -95,10 +113,11 @@ impl Default for TabSelectionType {
pub struct IcyBrowser<Engine: BrowserEngine> {
engine: Engine,
home: Url,
nav_bar_state: Option<NavBarState>,
nav_bar_state: NavBarState,
command_window_state: CommandWindowState,
with_tab_bar: bool,
with_nav_bar: bool,
with_bookmark_bar: bool,
bookmarks: Option<Bookmarks>,
show_overlay: bool,
shortcuts: Shortcuts,
Expand All @@ -111,10 +130,11 @@ impl<Engine: BrowserEngine> Default for IcyBrowser<Engine> {
Self {
engine: Engine::new(),
home,
nav_bar_state: None,
nav_bar_state: NavBarState::new(),
command_window_state: CommandWindowState::new(None),
with_tab_bar: false,
with_nav_bar: false,
with_bookmark_bar: false,
bookmarks: None,
show_overlay: false,
shortcuts: Shortcuts::default(),
Expand Down Expand Up @@ -142,16 +162,21 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {

pub fn with_nav_bar(mut self) -> Self {
self.with_nav_bar = true;
self.nav_bar_state = Some(NavBarState::new());
self.nav_bar_state = NavBarState::new();
self
}

pub fn with_bookmark_bar(mut self, bookmarks: &[Bookmark]) -> Self {
pub fn bookmarks(mut self, bookmarks: &[Bookmark]) -> Self {
self.bookmarks = Some(bookmarks.to_vec());
self.command_window_state = CommandWindowState::new(self.bookmarks.clone());
self
}

pub fn with_bookmark_bar(mut self) -> Self {
self.with_bookmark_bar = true;
self
}

pub fn with_custom_shortcuts(mut self, shortcuts: Shortcuts) -> Self {
self.shortcuts = shortcuts;
self
Expand Down Expand Up @@ -235,9 +260,7 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
TabSelectionType::Index(index) => self.engine.get_tabs().index_to_id(index),
};
self.engine.get_tabs_mut().set_current_id(id);
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.engine.get_tabs().get_current().url();
}
self.nav_bar_state.0 = self.engine.get_tabs().get_current().url();
Task::none()
}
Message::CloseCurrentTab => Task::done(Message::CloseTab(TabSelectionType::Id(
Expand All @@ -254,15 +277,11 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
TabSelectionType::Index(index) => self.engine.get_tabs().index_to_id(index),
};
self.engine.get_tabs_mut().remove(id);
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.engine.get_tabs().get_current().url();
}
self.nav_bar_state.0 = self.engine.get_tabs().get_current().url();
Task::none()
}
Message::CreateTab => {
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.home.to_string();
}
self.nav_bar_state.0 = self.home.to_string();
let home = self.home.clone();
let bounds = self.view_size;
let tab = self.engine.new_tab(
Expand All @@ -278,16 +297,12 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
}
Message::GoBackward => {
self.engine.go_back();
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.engine.get_tabs().get_current().url();
}
self.nav_bar_state.0 = self.engine.get_tabs().get_current().url();
Task::none()
}
Message::GoForward => {
self.engine.go_forward();
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.engine.get_tabs().get_current().url();
}
self.nav_bar_state.0 = self.engine.get_tabs().get_current().url();
Task::none()
}
Message::Refresh => {
Expand All @@ -303,17 +318,61 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
Task::none()
}
Message::UpdateUrl => {
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = self.engine.get_tabs().get_current().url();
}
self.nav_bar_state.0 = self.engine.get_tabs().get_current().url();
Task::none()
}
Message::UrlChanged(url) => {
if let Some(state) = self.nav_bar_state.as_mut() {
state.0 = url;
self.nav_bar_state.0 = url;
Task::none()
}
Message::ToggleTabBar => {
if self.with_tab_bar {
self.with_tab_bar = false;
} else {
self.with_tab_bar = true;
}
Task::none()
}
Message::ShowTabBar => {
self.with_tab_bar = true;
Task::none()
}
Message::HideTabBar => {
self.with_tab_bar = false;
Task::none()
}
Message::ToggleNavBar => {
if self.with_nav_bar {
self.with_nav_bar = false;
} else {
self.with_nav_bar = true;
}
Task::none()
}
Message::ShowNavBar => {
self.with_nav_bar = true;
Task::none()
}
Message::HideNavBar => {
self.with_nav_bar = false;
Task::none()
}
Message::ToggleBookmarkBar => {
if self.with_bookmark_bar {
self.with_bookmark_bar = false;
} else {
self.with_bookmark_bar = true;
}
Task::none()
}
Message::ShowBookmarkBar => {
self.with_bookmark_bar = true;
Task::none()
}
Message::HideBookmarkBar => {
self.with_bookmark_bar = false;
Task::none()
}
Message::CommandPalatteQueryChanged(query) => {
self.command_window_state.query = query.clone();
self.command_window_state.filtered_results = self
Expand Down Expand Up @@ -501,13 +560,13 @@ impl<Engine: BrowserEngine> IcyBrowser<Engine> {
column = column.push(tab_bar(self.engine.get_tabs()))
}
if self.with_nav_bar {
column = column.push(
hoverable(nav_bar(self.nav_bar_state.as_ref().unwrap()))
.on_focus_change(Message::UpdateUrl),
)
column = column
.push(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))
if self.with_bookmark_bar {
if let Some(bookmarks) = self.bookmarks.as_ref() {
column = column.push(bookmark_bar(bookmarks))
}
}

let browser_view = browser_view(self.engine.get_tabs().get_current().get_view());
Expand Down

0 comments on commit e670333

Please sign in to comment.