browser: add a current_page property

This commit is contained in:
dece 2021-06-05 21:47:06 +02:00
parent 68bc524117
commit 647dbee325

View file

@ -114,6 +114,10 @@ class Browser:
"""Return the scheme of the current URL.""" """Return the scheme of the current URL."""
return parse_url(self._current_url)["scheme"] or "" return parse_url(self._current_url)["scheme"] or ""
@property
def current_page(self) -> Optional[Page]:
return self.page_pad.current_page
def setup_special_pages(self): def setup_special_pages(self):
"""Return a dict with the special pages functions.""" """Return a dict with the special pages functions."""
return { return {
@ -486,9 +490,9 @@ class Browser:
def handle_digit_input(self, init_char: int): def handle_digit_input(self, init_char: int):
"""Focus command-line to select the link ID to follow.""" """Focus command-line to select the link ID to follow."""
if self.page_pad.current_page is None: if self.current_page is None:
return return
links = self.page_pad.current_page.links links = self.current_page.links
if links is None: if links is None:
return return
err, val = self.command_line.focus_for_link_navigation(init_char, links) err, val = self.command_line.focus_for_link_navigation(init_char, links)
@ -521,14 +525,13 @@ class Browser:
If the click is on a link (appropriate line and columns), open it. If the click is on a link (appropriate line and columns), open it.
""" """
page = self.page_pad.current_page if not self.current_page:
if not page:
return return
px, py = self.page_pad.current_column, self.page_pad.current_line px, py = self.page_pad.current_column, self.page_pad.current_line
line_pos = y + py line_pos = y + py
if line_pos >= len(page.metalines): if line_pos >= len(self.current_page.metalines):
return return
meta, line = page.metalines[line_pos] meta, line = self.current_page.metalines[line_pos]
if meta["type"] != LineType.LINK: if meta["type"] != LineType.LINK:
return return
# "url" key is contained only in the first line of the link if its text # "url" key is contained only in the first line of the link if its text
@ -536,7 +539,7 @@ class Browser:
# get the URL. # get the URL.
while "url" not in meta: while "url" not in meta:
line_pos -= 1 line_pos -= 1
meta, line = page.metalines[line_pos] meta, line = self.current_page.metalines[line_pos]
url = meta["url"] url = meta["url"]
# The click is valid if it is on the link itself or the dimmed preview. # The click is valid if it is on the link itself or the dimmed preview.
col_pos = x + px col_pos = x + px
@ -662,7 +665,7 @@ class Browser:
"""Add the current URL as bookmark.""" """Add the current URL as bookmark."""
if not self.current_url: if not self.current_url:
return return
current_title = self.page_pad.current_page.title or "" current_title = self.current_page.title or ""
title = self.get_user_text_input( title = self.get_user_text_input(
"Bookmark title?", "Bookmark title?",
CommandLine.CHAR_TEXT, CommandLine.CHAR_TEXT,
@ -690,9 +693,9 @@ class Browser:
get_source = special_pages_functions.get("source") get_source = special_pages_functions.get("source")
source_filename = get_source() if get_source else None source_filename = get_source() if get_source else None
else: else:
if not self.page_pad.current_page: if not self.current_page:
return return
source = self.page_pad.current_page.source source = self.current_page.source
with tempfile.NamedTemporaryFile("wt", delete=False) as source_file: with tempfile.NamedTemporaryFile("wt", delete=False) as source_file:
source_file.write(source) source_file.write(source)
source_filename = source_file.name source_filename = source_file.name
@ -754,7 +757,7 @@ class Browser:
def show_page_info(self): def show_page_info(self):
"""Show some page informations in the status bar.""" """Show some page informations in the status bar."""
page = self.page_pad.current_page page = self.current_page
if not page: if not page:
return return
mime = page.mime.short if page.mime else "(unknown MIME type)" mime = page.mime.short if page.mime else "(unknown MIME type)"
@ -786,7 +789,7 @@ class Browser:
def toggle_render_mode(self): def toggle_render_mode(self):
"""Switch to the next render mode for the current page.""" """Switch to the next render mode for the current page."""
page = self.page_pad.current_page page = self.current_page
if not page or page.render is None: if not page or page.render is None:
return return
if page.render not in RENDER_MODES: if page.render not in RENDER_MODES:
@ -804,14 +807,13 @@ class Browser:
def search_in_page(self): def search_in_page(self):
"""Search for words in the page.""" """Search for words in the page."""
page = self.page_pad.current_page if not self.current_page:
if not page:
return return
search = self.get_user_text_input("Search", CommandLine.CHAR_TEXT) search = self.get_user_text_input("Search", CommandLine.CHAR_TEXT)
if not search: if not search:
return return
self.search_res_lines = [] self.search_res_lines = []
for index, (_, line) in enumerate(page.metalines): for index, (_, line) in enumerate(self.current_page.metalines):
if search in line: if search in line:
self.search_res_lines.append(index) self.search_res_lines.append(index)
if self.search_res_lines: if self.search_res_lines: