Skip to content

Commit 83b04fd

Browse files
committed
Focus url_bar on new_tab
Fixes #25
1 parent 465abb1 commit 83b04fd

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
@@ -208,8 +208,8 @@ impl Window {
208208
imp.action_previous.borrow_mut().replace(action_previous);
209209
imp.action_next.borrow_mut().replace(action_next);
210210

211-
self_action!(self, "new-tab", add_tab_focused);
212-
self_action!(self, "show-bookmarks", add_tab_focused);
211+
self_action!(self, "new-tab", new_tab);
212+
self_action!(self, "show-bookmarks", show_bookmarks);
213213
self_action!(self, "bookmark-current", bookmark_current);
214214
self_action!(self, "close-tab", close_tab);
215215
self_action!(self, "focus-url-bar", focus_url_bar);
@@ -277,6 +277,10 @@ impl Window {
277277
false,
278278
clone!(@weak self as this => @default-panic, move |_| {
279279
this.set_special_color_from_hash();
280+
let bar = this.active_url_bar();
281+
if bar.focus_child().is_none() {
282+
bar.set_text(&this.url());
283+
}
280284
None
281285
}),
282286
);
@@ -384,6 +388,12 @@ impl Window {
384388
fn page_switched(&self, tab_view: &adw::TabView) {
385389
let imp = self.imp();
386390
let mut btp = imp.binded_tab_properties.borrow_mut();
391+
392+
// Unset the focus from the url_bar
393+
if let Some(r) = tab_view.root() {
394+
r.set_focus(None::<&gtk::Widget>)
395+
}
396+
387397
if let Some(page) = tab_view.selected_page() {
388398
let tab = self.inner_tab(&page);
389399

@@ -430,11 +440,15 @@ impl Window {
430440
]);
431441
};
432442
}
433-
fn add_tab_focused(&self) {
443+
fn new_tab(&self) {
444+
self.show_bookmarks();
445+
self.active_url_bar().grab_focus();
446+
}
447+
fn show_bookmarks(&self) {
434448
let imp = self.imp();
435449
let p = self.add_tab();
436-
self.inner_tab(&p).spawn_open_url(bookmarks_url());
437450
imp.tab_view.set_selected_page(&p);
451+
self.inner_tab(&p).spawn_open_url(bookmarks_url());
438452
}
439453
fn close_tab(&self) {
440454
let imp = self.imp();
@@ -444,14 +458,17 @@ impl Window {
444458
std::process::exit(0); // TODO: maybe there's a better way for gtk apps...
445459
}
446460
}
447-
fn focus_url_bar(&self) {
461+
fn active_url_bar(&self) -> &gtk::SearchEntry {
448462
let imp = self.imp();
449463
if self.is_small_screen() {
450-
imp.small_url_bar.grab_focus();
464+
&*imp.small_url_bar
451465
} else {
452-
imp.url_bar.grab_focus();
466+
&*imp.url_bar
453467
}
454468
}
469+
fn focus_url_bar(&self) {
470+
self.active_url_bar().grab_focus();
471+
}
455472

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

0 commit comments

Comments
 (0)