Skip to content

Commit

Permalink
Implement going forwards
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Dec 16, 2023
1 parent cd53da7 commit cd21355
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
7 changes: 7 additions & 0 deletions hyperplane/gtk/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ template $HypWindow : Adw.ApplicationWindow {
tooltip-text: _("Back");
}

[start]
Button {
icon-name: "go-next-symbolic";
action-name: "win.forward";
tooltip-text: _("Forward");
}

[title]
Stack title_stack {
transition-type: crossfade;
Expand Down
2 changes: 1 addition & 1 deletion hyperplane/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def bind(self) -> None:

def unbind(self) -> None:
"""Cleanup after the object has been unbound from its item."""
pass
pass # TODO: Clean up

def __build(self) -> None:
self.play_button.set_visible(False)
Expand Down
50 changes: 41 additions & 9 deletions hyperplane/navigation_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class HypNavigationBin(Adw.Bin):
items_page: HypItemsPage
view: Adw.NavigationView

next_pages: list[Adw.NavigationPage]
tags: list[str] = []

def __init__(
Expand All @@ -43,14 +44,17 @@ def __init__(
self.set_child(self.view)

if initial_gfile:
self.view.push(HypItemsPage(gfile=initial_gfile))
self.view.add(HypItemsPage(gfile=initial_gfile))
elif initial_tags:
self.tags = list(initial_tags)
self.view.push(HypItemsPage(tags=self.tags))
self.view.add(HypItemsPage(tags=self.tags))

self.view.connect("popped", self.__popped)
self.view.connect("pushed", self.__pushed)

self.next_pages = []
self.view.connect("get-next-page", self.__next_page)

def new_page(
self,
gfile: Optional[Gio.File] = None,
Expand All @@ -60,26 +64,54 @@ def new_page(
"""Push a new page with the given path or tag to the navigation stack."""
if gfile:
self.tags = []
self.view.push(HypItemsPage(gfile=gfile))
page = HypItemsPage(gfile=gfile)
elif tag:
if tag in self.tags:
return
self.tags.append(tag)
self.view.push(HypItemsPage(tags=self.tags.copy()))
page = HypItemsPage(tags=self.tags.copy())
elif tags:
self.tags = list(tags)
self.view.push(HypItemsPage(tags=self.tags.copy()))
page = HypItemsPage(tags=self.tags.copy())
else:
return

self.view.add(page)
self.view.push(page)

def __pushed(self, *_args: Any) -> None:
page = self.view.get_visible_page()

# HACK: find a proper way of doing this
GLib.timeout_add(
10, self.get_root().set_focus, self.view.get_visible_page().scrolled_window
)
GLib.timeout_add(10, self.get_root().set_focus, page.scrolled_window)

if not self.next_pages:
return

if page == self.next_pages[-1]:
self.next_pages.pop()
else:
for next_page in self.next_pages:
self.view.remove(next_page)

self.next_pages = []

def __popped(
self,
_view: Adw.NavigationView,
page: Adw.NavigationPage,
) -> None:
self.next_pages.append(page)

def __popped(self, *_args: Any) -> None:
self.get_root().set_focus(self.view.get_visible_page().scrolled_window)

if tags := self.view.get_visible_page().tags:
self.tags = tags.copy()
else:
self.tags = []

def __next_page(self, *_args: Any) -> None:
if not self.next_pages:
return None

return self.next_pages[-1]
41 changes: 23 additions & 18 deletions hyperplane/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ def __init__(self, **kwargs: Any) -> None:
self.create_action("hide-path-bar", self.__hide_path_bar)
self.create_action("close", self.__on_close_action, ("<primary>w",))
self.create_action("search", self.__toggle_search_entry, ("<primary>f",))

self.create_action("back", self.__on_back_action)
self.lookup_action("back").set_enabled(False)

self.create_action("forward", self.__on_forward_action)
self.lookup_action("forward").set_enabled(False)

# TODO: This is tedious, maybe use GTK Expressions?
self.create_action("open-tag", self.__open_tag)
self.create_action("open-new-tab-tag", self.__open_new_tab_tag)
Expand Down Expand Up @@ -355,15 +359,7 @@ def __tab_changed(self, *_args: Any) -> None:
return

self.set_title(self.get_visible_page().get_title())
self.lookup_action("back").set_enabled(
bool(
self.tab_view.get_selected_page()
.get_child()
.view.get_navigation_stack()
.get_n_items()
- 1
)
)
self.__nav_stack_changed()

def __navigation_changed(self, view: Adw.NavigationView, *_args: Any) -> None:
self.__hide_search_entry()
Expand All @@ -376,15 +372,7 @@ def __navigation_changed(self, view: Adw.NavigationView, *_args: Any) -> None:
if self.tab_view.get_selected_page() == page:
self.set_title(title)

self.lookup_action("back").set_enabled(
bool(
self.tab_view.get_selected_page()
.get_child()
.view.get_navigation_stack()
.get_n_items()
- 1
)
)
self.__nav_stack_changed()

def __page_attached(self, _view: Adw.TabView, page: Adw.TabPage, _pos: int) -> None:
page.get_child().view.connect("popped", self.__navigation_changed)
Expand Down Expand Up @@ -521,6 +509,9 @@ def __on_close_action(self, *_args: Any) -> None:
def __on_back_action(self, *_args: Any) -> None:
self.tab_view.get_selected_page().get_child().view.pop()

def __on_forward_action(self, *_args: Any) -> None:
self.tab_view.get_selected_page().get_child().view.pop()

def __create_window(self, *_args: Any) -> Adw.TabView:
win = self.get_application().do_activate()

Expand Down Expand Up @@ -569,6 +560,20 @@ def __set_actions(self, *_args: Any) -> None:
}
)

def __nav_stack_changed(self) -> None:
self.lookup_action("back").set_enabled(
bool(
self.tab_view.get_selected_page()
.get_child()
.view.get_navigation_stack()
.get_n_items()
- 1
)
)
self.lookup_action("forward").set_enabled(
bool(self.tab_view.get_selected_page().get_child().next_pages)
)

def __trash_changed(self, *_args: Any) -> None:
self.trash_icon.set_from_icon_name(
"user-trash-full-symbolic"
Expand Down

0 comments on commit cd21355

Please sign in to comment.