Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp tab system #2

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 107 additions & 12 deletions src/engines/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use iced::keyboard;
use iced::mouse::{self, Interaction};
use iced::Size;
use std::sync::{Arc, RwLock};
// use iced::widget::image::{Handle, Image};
use iced::Point;
use rand::Rng;
use url::Url;

#[cfg(feature = "webkit")]
pub mod ultralight;

#[derive(Debug, Clone)]
pub struct Tab {
pub url: Url,
pub title: String,
// icon: Image<Handle>,
}

pub enum PixelFormat {
RGBA,
BGRA,
}

#[allow(unused)]
pub trait BrowserEngine {
type TabInfo: TabInfo;

fn new() -> Self;

fn do_work(&self);
Expand All @@ -37,11 +34,10 @@ pub trait BrowserEngine {
fn get_url(&self) -> Option<Url>;
fn goto_url(&self, url: &Url);
fn has_loaded(&self) -> bool;
fn new_tab(&mut self, url: &Url);
fn goto_tab(&mut self, idx: u32) -> Option<()>;
fn get_tabs(&self) -> Vec<Tab>;
fn current_tab(&self) -> (usize, Tab);
fn close_tab(&mut self, idx: u32);
fn new_tab(&mut self, url: &Url) -> u32;
fn goto_tab(&mut self, id: u32);
fn get_tabs(&self) -> &Tabs<Self::TabInfo>;
fn get_tabs_mut(&mut self) -> &mut Tabs<Self::TabInfo>;

fn refresh(&self);
fn go_forward(&self);
Expand All @@ -53,3 +49,102 @@ pub trait BrowserEngine {
fn handle_keyboard_event(&self, event: keyboard::Event);
fn handle_mouse_event(&mut self, point: Point, event: mouse::Event);
}

/// Engine specific tab information
pub trait TabInfo {}

/// 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<TabInfo> {
id: u32,
url: Arc<RwLock<String>>,
title: Arc<RwLock<String>>,
tab_info: TabInfo,
}

impl<TabInfo> Tab<TabInfo> {
pub fn new(url: Arc<RwLock<String>>, title: Arc<RwLock<String>>, tab_info: TabInfo) -> Self {
let id = rand::thread_rng().gen();
Self {
id,
url,
title,
tab_info,
}
}

pub fn id(&self) -> u32 {
self.id
}

pub fn url(&self) -> String {
self.url.read().unwrap().to_string()
}

pub fn title(&self) -> String {
self.title.read().unwrap().to_string()
}
}

pub struct Tabs<TabInfo> {
tabs: Vec<Tab<TabInfo>>,
current: u32,
}

impl<TabInfo> Tabs<TabInfo> {
pub fn new() -> Self {
Self {
tabs: Vec::new(),
current: 0,
}
}

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<Tab<TabInfo>> {
&self.tabs
}

pub fn insert(&mut self, tab: Tab<TabInfo>) -> u32 {
let id = tab.id;
self.tabs.push(tab);
id
}

pub fn remove(&mut self, id: u32) {
self.tabs.retain(|tab| tab.id != id)
}

pub fn get_current(&self) -> &Tab<TabInfo> {
self.get(self.current)
}

pub fn get_current_mut(&mut self) -> &mut Tab<TabInfo> {
self.get_mut(self.current)
}

pub fn get(&self, id: u32) -> &Tab<TabInfo> {
for tab in self.tabs.iter() {
if tab.id == id {
return tab;
}
}
panic!("Unable to find Tab with id: {}", id);
}

pub fn get_mut(&mut self, id: u32) -> &mut Tab<TabInfo> {
for tab in self.tabs.iter_mut() {
if tab.id == id {
return tab;
}
}
panic!("Unable to find Tab with id: {}", id);
}
}
Loading
Loading