Skip to content

Commit c32f7c9

Browse files
authored
Merge pull request #27 from ranfdev/focus_address_bar
Focus url_bar on new_tab
2 parents bd21ea1 + 83b04fd commit c32f7c9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

data/resources/ui/window.blp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ template GeopardWindow: Adw.ApplicationWindow {
3232
width-request: 360;
3333
Gtk.SearchEntry url_bar {
3434
hexpand: true;
35-
text: bind GeopardWindow.url;
3635
}
3736
}
3837
[end]
@@ -56,7 +55,6 @@ template GeopardWindow: Adw.ApplicationWindow {
5655
[title]
5756
Gtk.SearchEntry small_url_bar {
5857
hexpand: true;
59-
text: bind GeopardWindow.url;
6058
}
6159
Gtk.WindowControls we {
6260
side: end;

src/widgets/window.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ impl Window {
209209
imp.action_previous.borrow_mut().replace(action_previous);
210210
imp.action_next.borrow_mut().replace(action_next);
211211

212-
self_action!(self, "new-tab", add_tab_focused);
213-
self_action!(self, "show-bookmarks", add_tab_focused);
212+
self_action!(self, "new-tab", new_tab);
213+
self_action!(self, "show-bookmarks", show_bookmarks);
214214
self_action!(self, "bookmark-current", bookmark_current);
215215
self_action!(self, "close-tab", close_tab);
216216
self_action!(self, "focus-url-bar", focus_url_bar);
@@ -294,6 +294,10 @@ impl Window {
294294
false,
295295
clone!(@weak self as this => @default-panic, move |_| {
296296
this.set_special_color_from_hash();
297+
let bar = this.active_url_bar();
298+
if bar.focus_child().is_none() {
299+
bar.set_text(&this.url());
300+
}
297301
None
298302
}),
299303
);
@@ -401,6 +405,12 @@ impl Window {
401405
fn page_switched(&self, tab_view: &adw::TabView) {
402406
let imp = self.imp();
403407
let mut btp = imp.binded_tab_properties.borrow_mut();
408+
409+
// Unset the focus from the url_bar
410+
if let Some(r) = tab_view.root() {
411+
r.set_focus(None::<&gtk::Widget>)
412+
}
413+
404414
if let Some(page) = tab_view.selected_page() {
405415
let tab = self.inner_tab(&page);
406416

@@ -447,11 +457,15 @@ impl Window {
447457
]);
448458
};
449459
}
450-
fn add_tab_focused(&self) {
460+
fn new_tab(&self) {
461+
self.show_bookmarks();
462+
self.active_url_bar().grab_focus();
463+
}
464+
fn show_bookmarks(&self) {
451465
let imp = self.imp();
452466
let p = self.add_tab();
453-
self.inner_tab(&p).spawn_open_url(bookmarks_url());
454467
imp.tab_view.set_selected_page(&p);
468+
self.inner_tab(&p).spawn_open_url(bookmarks_url());
455469
}
456470
fn close_tab(&self) {
457471
let imp = self.imp();
@@ -461,14 +475,17 @@ impl Window {
461475
std::process::exit(0); // TODO: maybe there's a better way for gtk apps...
462476
}
463477
}
464-
fn focus_url_bar(&self) {
478+
fn active_url_bar(&self) -> &gtk::SearchEntry {
465479
let imp = self.imp();
466480
if self.is_small_screen() {
467-
imp.small_url_bar.grab_focus();
481+
&*imp.small_url_bar
468482
} else {
469-
imp.url_bar.grab_focus();
483+
&*imp.url_bar
470484
}
471485
}
486+
fn focus_url_bar(&self) {
487+
self.active_url_bar().grab_focus();
488+
}
472489

473490
async fn append_bookmark(url: &str) -> anyhow::Result<()> {
474491
let mut file = async_fs::OpenOptions::new()

0 commit comments

Comments
 (0)