From 9d6403502d3584cd1e68f9557c80ae01bae30b56 Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Sat, 17 Aug 2024 20:34:48 -0600 Subject: [PATCH] fix trait errors with assosiated type --- src/engines/mod.rs | 22 ++++++++++++++++++---- src/engines/ultralight.rs | 13 ++++++++++--- src/widgets/browser_widgets.rs | 27 +++++++++++++++++---------- src/widgets/tab_bar.rs | 9 +++++---- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 7da5554..e4504b1 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -16,7 +16,9 @@ pub enum PixelFormat { } #[allow(unused)] -pub trait BrowserEngine { +pub trait BrowserEngine { + type TabInfo: TabInfo; + fn new() -> Self; fn do_work(&self); @@ -32,10 +34,10 @@ pub trait BrowserEngine { fn get_url(&self) -> Option; fn goto_url(&self, url: &Url); fn has_loaded(&self) -> bool; - fn new_tab(&mut self, url: &Url); + fn new_tab(&mut self, url: &Url) -> u32; fn goto_tab(&mut self, id: u32); - fn get_tabs(&self) -> &Tabs; - fn get_tabs_mut(&mut self) -> &mut Tabs; + fn get_tabs(&self) -> &Tabs; + fn get_tabs_mut(&mut self) -> &mut Tabs; fn refresh(&self); fn go_forward(&self); @@ -98,6 +100,18 @@ impl Tabs { } } + pub fn get_current_id(&self) -> u32 { + self.current + } + + pub fn set_current_id(&mut self, id: u32) { + self.current = id + } + + pub fn tabs(&self) -> &Vec> { + &self.tabs + } + pub fn insert(&mut self, tab: Tab) -> u32 { let id = tab.id; self.tabs.push(tab); diff --git a/src/engines/ultralight.rs b/src/engines/ultralight.rs index 70512e6..6a20536 100644 --- a/src/engines/ultralight.rs +++ b/src/engines/ultralight.rs @@ -86,7 +86,9 @@ impl Ultralight { } } -impl BrowserEngine for Ultralight { +impl BrowserEngine for Ultralight { + type TabInfo = UltalightTabInfo; + fn new() -> Self { Ultralight::new() } @@ -165,7 +167,7 @@ impl BrowserEngine for Ultralight { &mut self.tabs } - fn new_tab(&mut self, url: &Url) { + fn new_tab(&mut self, url: &Url) -> u32 { if self .tabs .tabs @@ -226,8 +228,13 @@ impl BrowserEngine for Ultralight { cursor, }; - self.tabs.insert(Tab::new(site_url, title, tab_info)); + let tab = Tab::new(site_url, title, tab_info); + let id = tab.id; + + self.tabs.insert(tab); + return id; } + unreachable!(); } fn goto_tab(&mut self, id: u32) { diff --git a/src/widgets/browser_widgets.rs b/src/widgets/browser_widgets.rs index 0463e9f..5d65c0c 100644 --- a/src/widgets/browser_widgets.rs +++ b/src/widgets/browser_widgets.rs @@ -4,7 +4,7 @@ use url::Url; use super::{nav_bar, tab_bar, BrowserView}; use crate::{ engines::{BrowserEngine, PixelFormat}, - to_url, ImageInfo, TabInfo, + to_url, ImageInfo, }; #[cfg(feature = "ultralight")] @@ -17,8 +17,8 @@ pub enum Message { Refresh, GoHome, GoUrl(String), - ChangeTab(usize), - CloseTab(usize), + ChangeTab(u32), + CloseTab(u32), CreateTab, UrlChanged(String), SendKeyboardEvent(keyboard::Event), @@ -27,7 +27,7 @@ pub enum Message { DoWork, } -pub struct BrowserWidget> { +pub struct BrowserWidget { engine: Option, home: Url, url: String, @@ -38,7 +38,10 @@ pub struct BrowserWidget> { view_bounds: Size, } -impl> Default for BrowserWidget { +impl Default for BrowserWidget +where + Engine: BrowserEngine, +{ fn default() -> Self { let home = Url::parse(Self::HOME).unwrap(); Self { @@ -64,7 +67,10 @@ impl BrowserWidget { } } -impl> BrowserWidget { +impl BrowserWidget +where + Engine: BrowserEngine, +{ const HOME: &'static str = "https://duckduckgo.com"; pub fn new() -> Self { @@ -136,14 +142,15 @@ impl> BrowserWidget { Message::SendMouseEvent(point, event) => { self.engine_mut().handle_mouse_event(point, event); } - Message::ChangeTab(index) => self.engine_mut().goto_tab(index as u32).unwrap(), - Message::CloseTab(index) => { - self.engine_mut().close_tab(index as u32); + Message::ChangeTab(id) => self.engine_mut().goto_tab(id), + Message::CloseTab(id) => { + self.engine_mut().get_tabs_mut().remove(id); } Message::CreateTab => { self.url = self.home.to_string(); let home = self.home.clone(); - self.engine_mut().new_tab(&home); + let id = self.engine_mut().new_tab(&home); + self.engine_mut().get_tabs_mut().set_current_id(id); } Message::GoBackward => { self.engine().go_back(); diff --git a/src/widgets/tab_bar.rs b/src/widgets/tab_bar.rs index f7e8f43..43a833c 100644 --- a/src/widgets/tab_bar.rs +++ b/src/widgets/tab_bar.rs @@ -4,15 +4,16 @@ use iced_aw::core::icons::bootstrap::{icon_to_text, Bootstrap}; use iced_aw::{TabBar as TB, TabLabel}; use super::browser_widgets::Message; -use crate::engines::Tab; +use crate::engines::Tabs; // helper function to create navigation bar -pub fn tab_bar(tabs: Vec>, active_tab: usize) -> Element<'static, Message> { +pub fn tab_bar(tabs: &Tabs, active_tab: u32) -> Element<'static, Message> { let tab_bar = tabs + .tabs() .iter() .fold(TB::new(Message::ChangeTab), |tab_bar, tab| { - let idx = tab_bar.size(); - tab_bar.push(idx, TabLabel::Text(tab.title())) + let id = tab_bar.size(); + tab_bar.push(id as u32, TabLabel::Text(tab.title())) }) .set_active_tab(&active_tab) .on_close(Message::CloseTab)