Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 'Restore window' option #155

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion data/com.github.hugolabe.Wike.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@

<key name="on-start-load" type="i">
<default>0</default>
<summary>On start load this page (0-Main, 1-Random, 2-Last)</summary>
<summary>On start load this page (0-Main, 1-Random, 2-Last article, 3-Restore all tabs)</summary>
</key>

<key name="last-uri" type="s">
<default>""</default>
<summary>Last Wikipedia uri loaded</summary>
</key>

<key name="last-window" type="a(ss)">
<default>[]</default>
<summary>Last Wikipedia window uri and title, one for each page</summary>
</key>

<key name="theme" type="i">
<default>3</default>
<summary>Choose theme (0-Light, 1-Dark, 2-Sepia, 3-System)</summary>
Expand Down
1 change: 1 addition & 0 deletions data/ui/prefs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
<item translatable="yes">Wikipedia main page</item>
<item translatable="yes">Random article</item>
<item translatable="yes">Last article</item>
<item translatable="yes">Restore all tabs</item>
</items>
</object>
</property>
Expand Down
9 changes: 9 additions & 0 deletions src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ def _quit_cb(self, action, parameter):
settings.set_string('last-uri', '')
else:
settings.set_string('last-uri', self._window.page.wikiview.get_base_uri())
pages_data = []
for i_page in range(self._window.tabview.get_n_pages()):
tabpage = self._window.tabview.get_nth_page(i_page)
page = tabpage.get_child()
if page._lazy_load:
pages_data.append(page._lazy_load)
else:
pages_data.append([page.wikiview.get_base_uri(), tabpage.get_title()])
settings.set_value('last-window', GLib.Variant("a(ss)", pages_data))

if not settings.get_boolean('keep-history'):
history.clear()
Expand Down
24 changes: 21 additions & 3 deletions src/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from wike.data import settings
from wike.view import WikiView


# Page box for each tab with webview and search bar

@Gtk.Template(resource_path='/com/github/hugolabe/Wike/ui/page.ui')
Expand All @@ -28,10 +27,12 @@ class PageBox(Gtk.Box):

# Add wikiview, initialize find controller and connect signals

def __init__(self, window):
def __init__(self, window, lazy_load=False):
super().__init__()

self._window = window
self._lazy_load = lazy_load
self._did_lazy_load = False

self.wikiview = WikiView()
self.wikiview.set_vexpand(True)
Expand All @@ -54,6 +55,19 @@ def __init__(self, window):
find_controller.connect('failed-to-find-text', self._find_controller_not_found_cb)
nav_list.connect('changed', self._nav_list_changed_cb)

# Load a non-loaded page, for example when the user selects the tab

def load_page_now(self):
uri, title = self._lazy_load

if uri == 'blank' or uri == 'notfound':
self.wikiview.load_message(uri)
else:
self.wikiview.load_wiki(uri)

self._lazy_load = False
self._did_lazy_load = True

# Manage wikiview load page events

def _wikiview_load_changed_cb(self, wikiview, event):
Expand All @@ -67,7 +81,9 @@ def _wikiview_load_changed_cb(self, wikiview, event):
if self.search_bar.get_search_mode():
self.search_bar.set_search_mode(False)
self.view_stack.set_visible_child_name('wikiview')
tabpage.set_title(_('Loading'))
if self._lazy_load or not self._did_lazy_load:
tabpage.set_title(_('Loading'))
self._did_lazy_load = False
tabpage.set_loading(True)
if tabpage.get_selected():
self._window.headerbar.search_box.reset()
Expand All @@ -83,6 +99,7 @@ def _wikiview_load_changed_cb(self, wikiview, event):
self._window.refresh_menu_actions(wikiview.is_local())

elif event == WebKit.LoadEvent.FINISHED:
self._did_lazy_load = False
tabpage.set_loading(False)
if wikiview.is_local():
self._show_status_page(wikiview.get_uri())
Expand Down Expand Up @@ -183,3 +200,4 @@ def _nav_list_changed_cb(self, nav_list, item_added, item_removed):
tabpage = self._window.tabview.get_page(self)
if tabpage.get_selected():
self._window.refresh_nav_actions(self.wikiview)

57 changes: 44 additions & 13 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ def __init__(self, app, launch_uri):
if settings.get_boolean('window-max'):
self.maximize()

self.page = PageBox(self)
tabpage = self.tabview.append(self.page)

self.actionbar = ActionBar()
self.window_box.append(self.actionbar)

Expand Down Expand Up @@ -101,21 +98,36 @@ def __init__(self, app, launch_uri):
self.flap_bookmarks_button.connect('toggled', self._flap_switcher_button_cb, 'bookmarks')
self.flap_history_button.connect('toggled', self._flap_switcher_button_cb, 'history')

self._finished_loading_tabs = False

if launch_uri == 'notfound':
self.page.wikiview.load_message(launch_uri)
self.new_page(launch_uri, None, True)
else:
if launch_uri != '':
self.page.wikiview.load_wiki(launch_uri)
self.new_page(launch_uri, None, True)
else:
if settings.get_int('on-start-load') == 0:
self.page.wikiview.load_main()
self.new_page('main', None, True)
elif settings.get_int('on-start-load') == 1:
self.page.wikiview.load_random()
else:
self.new_page('random', None, True)
elif settings.get_int('on-start-load') == 2:
if settings.get_string('last-uri'):
self.page.wikiview.load_wiki(settings.get_string('last-uri'))
self.new_page(settings.get_string('last-uri'), None, True)
else:
self.page.wikiview.load_main()
self.new_page('main', None, True)
else:
tabs_data = settings.get_value('last-window').unpack()
if len(tabs_data) == 0:
self.new_page('main', None, True)
else:
for tab_data in tabs_data:
uri, title = tab_data
if uri == settings.get_string('last-uri'):
self.new_page(uri, None, True)
else:
self.new_lazy_page(uri, title, None)

self._finished_loading_tabs = True

# Set actions for window

Expand Down Expand Up @@ -193,7 +205,12 @@ def new_page(self, uri, parent, select):
page = PageBox(self)
tabpage = self.tabview.add_page(page, parent)
tabpage.set_live_thumbnail(True)
if uri == 'blank' or uri == 'notfound':

if uri == 'main':
page.wikiview.load_main()
elif uri == 'random':
page.wikiview.load_random()
elif uri == 'blank' or uri == 'notfound':
page.wikiview.load_message(uri)
else:
page.wikiview.load_wiki(uri)
Expand All @@ -203,6 +220,16 @@ def new_page(self, uri, parent, select):

return tabpage

# New empty tab with a title for lazy-loading

def new_lazy_page(self, uri, title, parent):
page = PageBox(self, lazy_load=[uri,title])
tabpage = self.tabview.add_page(page, parent)
tabpage.set_live_thumbnail(True)
tabpage.set_title(title)

return tabpage

# New empty tab

def _new_tab_cb(self, action, parameter):
Expand Down Expand Up @@ -248,19 +275,23 @@ def _prev_tab_cb(self, action, parameter):
def _toggle_overview_cb(self, action, parameter):
self.taboverview.set_open(not self.taboverview.get_open())

# On tab selected event refresh headerbar and sidebar
# On tab selected event refresh headerbar and sidebar, and force loading

def _tabview_selected_page_cb(self, tabview, value):
tabpage = tabview.get_selected_page()
if not tabpage:
return

self.page = tabpage.get_child()
if self.page._lazy_load and self._finished_loading_tabs:
self.page.load_page_now()
self.refresh_nav_actions(self.page.wikiview)
self.refresh_menu_actions(self.page.wikiview.is_local())
self.toc_box.populate(self.page.wikiview.title, self.page.wikiview.sections)
self.langlinks_box.populate(self.page.wikiview.langlinks)
self.bookmarks_box.refresh_buttons()

if self._finished_loading_tabs:
self.bookmarks_box.refresh_buttons()

# On tab closed event destroy wikiview and confirm

Expand Down