Skip to content

Commit

Permalink
Move logic from PercentageWidget to Percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
pylbrecht committed Jul 25, 2024
1 parent 9077f19 commit c65b057
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion qutebrowser/mainwindow/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def _connect_signals(self):
self.status.prog.on_load_started)

self.tabbed_browser.cur_scroll_perc_changed.connect(
self.status.percentage.widget.set_perc)
self.status.percentage.set_perc)
self.tabbed_browser.widget.tab_index_changed.connect(
self.status.tabindex.on_tab_index_changed)

Expand Down
2 changes: 1 addition & 1 deletion qutebrowser/mainwindow/statusbar/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def on_tab_changed(self, tab):
"""Notify sub-widgets when the tab has been changed."""
self.url.widget.on_tab_changed(tab)
self.prog.on_tab_changed(tab)
self.percentage.widget.on_tab_changed(tab)
self.percentage.on_tab_changed(tab)
self.backforward.on_tab_changed(tab)
self.maybe_hide()
assert tab.is_private == self._color_flags.private
Expand Down
29 changes: 14 additions & 15 deletions qutebrowser/mainwindow/statusbar/percentage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Scroll percentage displayed in the statusbar."""

from qutebrowser.mainwindow.statusbar.item import StatusBarItem
from qutebrowser.qt.core import pyqtSlot, Qt
from qutebrowser.qt.core import Qt

from qutebrowser.mainwindow.statusbar import textbase
from qutebrowser.misc import throttle
Expand All @@ -17,23 +17,20 @@ class PercentageWidget(textbase.TextBaseWidget):
"""Reading percentage displayed in the statusbar."""

def __init__(self, parent=None):
"""Constructor. Set percentage to 0%."""
super().__init__(parent, elidemode=Qt.TextElideMode.ElideNone)


class Percentage(StatusBarItem):

Check warning on line 23 in qutebrowser/mainwindow/statusbar/percentage.py

View workflow job for this annotation

GitHub Actions / linters (pylint)

Missing class docstring

Check warning on line 23 in qutebrowser/mainwindow/statusbar/percentage.py

View workflow job for this annotation

GitHub Actions / linters (flake8)

Missing docstring in public class
def __init__(self, widget: PercentageWidget):
"""Constructor. Set percentage to 0%."""
super().__init__(widget)
self._strings = self._calc_strings()
self._set_text = throttle.Throttle(self.setText, 100, parent=self)
self._set_text = throttle.Throttle(self.widget.setText, 100, parent=self.widget)
self.set_perc(0, 0)

def set_raw(self):
self._strings = self._calc_strings(raw=True)

def _calc_strings(self, raw=False):
"""Pre-calculate strings for the statusbar."""
fmt = '[{:02}]' if raw else '[{:02}%]'
strings = {i: fmt.format(i) for i in range(1, 100)}
strings.update({0: '[top]', 100: '[bot]'})
return strings

@pyqtSlot(int, int)
def set_perc(self, x, y):
"""Setter to be used as a Qt slot.
Expand All @@ -48,7 +45,9 @@ def on_tab_changed(self, tab):
"""Update scroll position when tab changed."""
self.set_perc(*tab.scroller.pos_perc())


class Percentage(StatusBarItem):
def set_raw(self):
self.widget.set_raw()
def _calc_strings(self, raw=False):
"""Pre-calculate strings for the statusbar."""
fmt = '[{:02}]' if raw else '[{:02}%]'
strings = {i: fmt.format(i) for i in range(1, 100)}
strings.update({0: '[top]', 100: '[bot]'})
return strings
10 changes: 5 additions & 5 deletions tests/unit/mainwindow/statusbar/test_percentage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

import pytest

from qutebrowser.mainwindow.statusbar.percentage import PercentageWidget
from qutebrowser.mainwindow.statusbar.percentage import Percentage, PercentageWidget


@pytest.fixture
def percentage(qtbot):
"""Fixture providing a Percentage widget."""
widget = PercentageWidget()
widget = Percentage(widget=PercentageWidget())
# Force immediate update of percentage widget
widget._set_text.set_delay(-1)
qtbot.add_widget(widget)
qtbot.add_widget(widget.widget)
return widget


Expand Down Expand Up @@ -44,12 +44,12 @@ def test_percentage_text(percentage, y, raw, expected):
if raw:
percentage.set_raw()
percentage.set_perc(x=None, y=y)
assert percentage.text() == expected
assert percentage.widget.text() == expected


def test_tab_change(percentage, fake_web_tab):
"""Make sure the percentage gets changed correctly when switching tabs."""
percentage.set_perc(x=None, y=10)
tab = fake_web_tab(scroll_pos_perc=(0, 20))
percentage.on_tab_changed(tab)
assert percentage.text() == '[20%]'
assert percentage.widget.text() == '[20%]'

0 comments on commit c65b057

Please sign in to comment.