Skip to content

Commit b1d7dbd

Browse files
committed
rename all tab abstractions as views
1 parent 5f05e6b commit b1d7dbd

File tree

5 files changed

+184
-201
lines changed

5 files changed

+184
-201
lines changed

examples/embedded_webview.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct App {
2525
webview: WebView<Ultralight, Message>,
2626
show_webview: bool,
2727
webview_url: Option<String>,
28-
num_tabs: u32,
29-
current_tab: u32,
28+
num_views: u32,
29+
current_view: usize,
3030
}
3131

3232
impl App {
@@ -39,8 +39,8 @@ impl App {
3939
webview,
4040
show_webview: false,
4141
webview_url: None,
42-
num_tabs: 1,
43-
current_tab: 0,
42+
num_views: 1,
43+
current_view: 0,
4444
},
4545
task.map(Message::WebView),
4646
)
@@ -58,17 +58,17 @@ impl App {
5858
Task::none()
5959
}
6060
Message::CreateWebview => {
61-
self.num_tabs += 1;
62-
self.webview.update(webview::Action::CreateTab)
61+
self.num_views += 1;
62+
self.webview.update(webview::Action::CreateView)
6363
}
6464
Message::SwitchWebview => {
65-
if self.current_tab + 1 >= self.num_tabs {
66-
self.current_tab = 0;
65+
if self.current_view + 1 >= self.num_views as usize {
66+
self.current_view = 0;
6767
} else {
68-
self.current_tab += 1;
68+
self.current_view += 1;
6969
};
70-
let tab = iced_webview::TabSelectionType::Index(self.current_tab as usize);
71-
self.webview.update(webview::Action::ChangeTab(tab))
70+
self.webview
71+
.update(webview::Action::ChangeView(self.current_view))
7272
}
7373
}
7474
}
@@ -89,7 +89,7 @@ impl App {
8989
]]
9090
.push_maybe(if self.show_webview {
9191
Some(column![
92-
text(format!("view index: {}", self.current_tab)),
92+
text(format!("view index: {}", self.current_view)),
9393
self.webview.view().map(Message::WebView),
9494
text(format!("Url: {:?}", self.webview_url)),
9595
])

src/engines.rs

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum PixelFormat {
1717
}
1818

1919
pub trait Engine {
20-
type Info: TabInfo;
20+
type Info: ViewInfo;
2121

2222
fn do_work(&self);
2323
fn need_render(&self) -> bool;
@@ -32,9 +32,9 @@ pub trait Engine {
3232
fn goto_url(&self, url: &Url);
3333
fn goto_html(&self, html: &str);
3434
fn has_loaded(&self) -> Option<bool>;
35-
fn new_tab(&mut self, page_type: PageType, size: Size<u32>) -> Tab<Self::Info>;
36-
fn get_tabs(&self) -> &Tabs<Self::Info>;
37-
fn get_tabs_mut(&mut self) -> &mut Tabs<Self::Info>;
35+
fn new_view(&mut self, page_type: PageType, size: Size<u32>) -> View<Self::Info>;
36+
fn get_views(&self) -> &Views<Self::Info>;
37+
fn get_views_mut(&mut self) -> &mut Views<Self::Info>;
3838

3939
fn refresh(&self);
4040
fn go_forward(&self);
@@ -47,38 +47,38 @@ pub trait Engine {
4747
fn handle_mouse_event(&mut self, point: Point, event: mouse::Event);
4848
}
4949

50-
/// Engine specific tab information
51-
pub trait TabInfo {
50+
/// Engine specific view information
51+
pub trait ViewInfo {
5252
fn url(&self) -> String;
5353
fn title(&self) -> String;
5454
}
5555

56-
/// Can be converted from Tab to hold information for ResultType
56+
/// Can be converted from View to hold information for ResultType
5757
#[derive(Clone, Debug, PartialEq)]
58-
pub struct DisplayTab {
59-
pub id: u32,
58+
pub struct DisplayView {
59+
pub id: usize,
6060
pub url: String,
6161
pub title: String,
6262
}
6363

64-
impl<Info: TabInfo> From<Tab<Info>> for DisplayTab {
65-
fn from(tab: Tab<Info>) -> Self {
66-
DisplayTab {
67-
id: tab.id,
68-
url: tab.url(),
69-
title: tab.title(),
64+
impl<Info: ViewInfo> From<View<Info>> for DisplayView {
65+
fn from(view: View<Info>) -> Self {
66+
DisplayView {
67+
id: view.id,
68+
url: view.url(),
69+
title: view.title(),
7070
}
7171
}
7272
}
7373

74-
/// Stores Tab info like url & title
75-
pub struct Tab<Info: TabInfo> {
76-
id: u32,
74+
/// Stores view info like url & title
75+
pub struct View<Info: ViewInfo> {
76+
id: usize,
7777
view: ImageInfo,
7878
info: Info,
7979
}
8080

81-
impl<Info: TabInfo> Tab<Info> {
81+
impl<Info: ViewInfo> View<Info> {
8282
pub fn new(info: Info) -> Self {
8383
let id = rand::thread_rng().gen();
8484
Self {
@@ -88,8 +88,8 @@ impl<Info: TabInfo> Tab<Info> {
8888
}
8989
}
9090

91-
pub fn to_display_tab(&self) -> DisplayTab {
92-
DisplayTab {
91+
pub fn to_display_view(&self) -> DisplayView {
92+
DisplayView {
9393
id: self.id,
9494
url: self.url(),
9595
title: self.title(),
@@ -104,7 +104,7 @@ impl<Info: TabInfo> Tab<Info> {
104104
self.view = view;
105105
}
106106

107-
pub fn id(&self) -> u32 {
107+
pub fn id(&self) -> usize {
108108
self.id
109109
}
110110

@@ -117,94 +117,97 @@ impl<Info: TabInfo> Tab<Info> {
117117
}
118118
}
119119

120-
pub struct Tabs<Info: TabInfo> {
121-
tabs: Vec<Tab<Info>>,
122-
history: Vec<u32>,
120+
pub struct Views<Info: ViewInfo> {
121+
views: Vec<View<Info>>,
122+
history: Vec<usize>,
123123
}
124124

125-
impl<Info: TabInfo> Default for Tabs<Info> {
125+
impl<Info: ViewInfo> Default for Views<Info> {
126126
fn default() -> Self {
127127
Self::new()
128128
}
129129
}
130130

131-
impl<Info: TabInfo> Tabs<Info> {
131+
impl<Info: ViewInfo> Views<Info> {
132132
pub fn new() -> Self {
133133
Self {
134-
tabs: Vec::new(),
134+
views: Vec::new(),
135135
history: Vec::new(),
136136
}
137137
}
138138

139-
pub fn id_to_index(&self, id: u32) -> usize {
140-
for (idx, tab) in self.tabs.iter().enumerate() {
141-
if tab.id == id {
139+
pub fn id_to_index(&self, id: usize) -> usize {
140+
for (idx, view) in self.views.iter().enumerate() {
141+
if view.id == id {
142142
return idx;
143143
}
144144
}
145145
panic!("Id: {} was not found", id);
146146
}
147147

148-
pub fn index_to_id(&self, index: usize) -> u32 {
149-
self.tabs
148+
pub fn index_to_id(&self, index: usize) -> usize {
149+
self.views
150150
.get(index)
151151
.unwrap_or_else(|| panic!("Index {} was not found", index))
152152
.id
153153
}
154154

155-
pub fn get_current_id(&self) -> Option<u32> {
155+
pub fn get_current_id(&self) -> Option<usize> {
156156
Some(self.history.last()?.to_owned())
157157
}
158158

159-
pub fn set_current_id(&mut self, id: u32) {
159+
pub fn set_current_id(&mut self, id: usize) {
160160
self.history.push(id)
161161
}
162162

163-
pub fn tabs(&self) -> &Vec<Tab<Info>> {
164-
&self.tabs
163+
pub fn views(&self) -> &Vec<View<Info>> {
164+
&self.views
165165
}
166166

167-
pub fn display_tabs(&self) -> Vec<DisplayTab> {
168-
self.tabs.iter().map(|tab| tab.to_display_tab()).collect()
167+
pub fn display_views(&self) -> Vec<DisplayView> {
168+
self.views
169+
.iter()
170+
.map(|view| view.to_display_view())
171+
.collect()
169172
}
170173

171-
pub fn insert(&mut self, tab: Tab<Info>) -> u32 {
172-
let id = tab.id;
173-
self.tabs.push(tab);
174+
pub fn insert(&mut self, view: View<Info>) -> usize {
175+
let id = view.id;
176+
self.views.push(view);
174177
id
175178
}
176179

177-
/// Returns the newly active tab
178-
pub fn remove(&mut self, id: u32) -> Option<u32> {
179-
self.history.retain(|tab_id| *tab_id != id);
180+
/// Returns the newly active view
181+
pub fn remove(&mut self, id: usize) -> Option<usize> {
182+
self.history.retain(|view_id| *view_id != id);
180183

181-
self.tabs.retain(|tab| tab.id != id);
184+
self.views.retain(|view| view.id != id);
182185
self.get_current_id()
183186
}
184187

185-
pub fn get_current(&self) -> Option<&Tab<Info>> {
188+
pub fn get_current(&self) -> Option<&View<Info>> {
186189
self.get(self.get_current_id()?)
187190
}
188191

189-
pub fn get_current_mut(&mut self) -> Option<&mut Tab<Info>> {
192+
pub fn get_current_mut(&mut self) -> Option<&mut View<Info>> {
190193
Some(self.get_mut(self.get_current_id()?))
191194
}
192195

193-
pub fn get(&self, id: u32) -> Option<&Tab<Info>> {
194-
self.tabs.iter().find(|&tab| tab.id == id)
196+
pub fn get(&self, id: usize) -> Option<&View<Info>> {
197+
self.views.iter().find(|&view| view.id == id)
195198
}
196199

197-
pub fn get_mut(&mut self, id: u32) -> &mut Tab<Info> {
198-
for tab in self.tabs.iter_mut() {
199-
if tab.id == id {
200-
return tab;
200+
pub fn get_mut(&mut self, id: usize) -> &mut View<Info> {
201+
for view in self.views.iter_mut() {
202+
if view.id == id {
203+
return view;
201204
}
202205
}
203-
panic!("Unable to find Tab with id: {}", id);
206+
panic!("Unable to find view with id: {}", id);
204207
}
205208
}
206209

207-
/// Allows users to create new tabs with url or custom html
210+
/// Allows users to create new views with url or custom html
208211
#[derive(Clone)]
209212
pub enum PageType {
210213
Url(&'static str),

0 commit comments

Comments
 (0)