Skip to content

Commit 3d4430b

Browse files
committed
refactor add_tab
1 parent c795c9d commit 3d4430b

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/tab.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Tab {
190190
imp.req_handle
191191
.replace(Some(glibctx().spawn_local_with_handle(fut).unwrap()));
192192
}
193-
pub fn spawn_open(&self, url: Url) {
193+
pub fn spawn_open_url(&self, url: Url) {
194194
let imp = self.imp();
195195

196196
self.emit_by_name_with_values("progress-changed", &[0.0.to_value()]);
@@ -208,7 +208,7 @@ impl Tab {
208208

209209
let this = self.clone();
210210
let fut = async move {
211-
match Self::open_url(&mut req_ctx).await {
211+
match Self::spawn_request(&mut req_ctx).await {
212212
Ok(Some(cache)) => {
213213
this.add_cache(cache);
214214
info!("Page loaded and cached ({})", url.clone());
@@ -231,7 +231,7 @@ impl Tab {
231231
let HistoryItem { url, cache, .. } = item;
232232
match cache {
233233
Some(cache) => self.spawn_open_cached(url, cache),
234-
None => self.spawn_open(url),
234+
None => self.spawn_open_url(url),
235235
}
236236
}
237237
fn spawn_open_cached(&self, url: Url, cache: Vec<u8>) {
@@ -292,7 +292,7 @@ impl Tab {
292292
}
293293
let link = Self::extract_linkhandler(draw_ctx.as_ref().unwrap(), x, y)?;
294294
let url = self.parse_link(&link)?;
295-
self.spawn_open(url);
295+
self.spawn_open_url(url);
296296
Ok(())
297297
}
298298
/* FIXME: fn extend_textview_menu(menu: &gtk::Menu, url: String, sender: flume::Sender<TabMsg>) {
@@ -369,7 +369,7 @@ impl Tab {
369369
}
370370
Ok(())
371371
}
372-
async fn open_url(req: &mut RequestCtx) -> Result<Option<Vec<u8>>> {
372+
async fn spawn_request(req: &mut RequestCtx) -> Result<Option<Vec<u8>>> {
373373
req.draw_ctx.clear();
374374
match req.url.scheme() {
375375
"about" => {

src/window.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ impl Window {
9898

9999
this.bind_signals();
100100
this.setup_actions();
101-
this.add_tab();
102-
this.open_url(bookmarks_url());
101+
this.open_in_new_tab(bookmarks_url().as_str());
103102
this
104103
}
105104

106105
fn setup_actions(&self) {
107106
self_action!(self, "back", back);
108-
self_action!(self, "new-tab", add_tab);
109-
self_action!(self, "show-bookmarks", add_tab);
107+
self_action!(self, "new-tab", add_tab_focused);
108+
self_action!(self, "show-bookmarks", add_tab_focused);
110109
self_action!(self, "bookmark-current", bookmark_current);
111110
self_action!(self, "close-tab", close_tab);
112111
self_action!(self, "focus-url-bar", focus_url_bar);
@@ -137,7 +136,7 @@ impl Window {
137136
);
138137
self.add_action(&act_set_clipboard);
139138
}
140-
fn add_tab(&self) {
139+
fn add_tab(&self) -> adw::TabPage {
141140
let imp = self.imp();
142141
let tab = Tab::new(imp.config.borrow().clone());
143142
let tab_view = imp.tab_view.clone();
@@ -164,9 +163,13 @@ impl Window {
164163
}),
165164
);
166165

167-
let w = imp.tab_view.append(&tab);
168-
imp.tab_view.set_selected_page(&w);
169-
self.open_url(bookmarks_url());
166+
imp.tab_view.append(&tab)
167+
}
168+
fn add_tab_focused(&self) {
169+
let imp = self.imp();
170+
let p = self.add_tab();
171+
self.inner_tab(&p).spawn_open_url(bookmarks_url());
172+
imp.tab_view.set_selected_page(&p);
170173
}
171174
fn close_tab(&self) {
172175
let imp = self.imp();
@@ -228,7 +231,7 @@ impl Window {
228231
imp.progress_animation.replace(Some(animation));
229232
}
230233
fn open_url(&self, url: Url) {
231-
self.current_tab().spawn_open(url);
234+
self.current_tab().spawn_open_url(url);
232235
}
233236
fn back(&self) {
234237
match self.current_tab().back() {
@@ -262,12 +265,23 @@ impl Window {
262265
}
263266
}
264267
fn open_in_new_tab(&self, v: &str) {
265-
self.add_tab();
266-
self.open_url_str(v);
268+
let w = self.add_tab();
269+
let url = Url::parse(v);
270+
match url {
271+
Ok(url) => self.inner_tab(&w).spawn_open_url(url),
272+
Err(e) => error!("Failed to parse url: {:?}", e),
273+
}
267274
}
268275
fn set_clipboard(&self, v: &str) {
269276
gdk::Display::default().unwrap().clipboard().set_text(v);
270277
}
278+
fn tab_page(&self, tab: &Tab) -> adw::TabPage {
279+
let imp = self.imp();
280+
imp.tab_view.page(tab)
281+
}
282+
fn inner_tab(&self, tab: &adw::TabPage) -> Tab {
283+
tab.child().downcast().unwrap()
284+
}
271285
//TODO: Reintroduce colors
272286
//fn set_special_color_from_hash(&self, hash: u64) {
273287
// let color1 = Color(

0 commit comments

Comments
 (0)