diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index f4703850a..07f6156e6 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -667,100 +667,6 @@ class CommandDispatcher: except navigate.Error as e: raise cmdutils.CommandError(e) - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def scroll_px(self, dx: int, dy: int, count: int = 1) -> None: - """Scroll the current tab by 'count * dx/dy' pixels. - - Args: - dx: How much to scroll in x-direction. - dy: How much to scroll in y-direction. - count: multiplier - """ - dx *= count - dy *= count - cmdutils.check_overflow(dx, 'int') - cmdutils.check_overflow(dy, 'int') - self._current_widget().scroller.delta(dx, dy) - - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def scroll(self, direction: str, count: int = 1) -> None: - """Scroll the current tab in the given direction. - - Note you can use `:run-with-count` to have a keybinding with a bigger - scroll increment. - - Args: - direction: In which direction to scroll - (up/down/left/right/top/bottom). - count: multiplier - """ - tab = self._current_widget() - funcs = { - 'up': tab.scroller.up, - 'down': tab.scroller.down, - 'left': tab.scroller.left, - 'right': tab.scroller.right, - 'top': tab.scroller.top, - 'bottom': tab.scroller.bottom, - 'page-up': tab.scroller.page_up, - 'page-down': tab.scroller.page_down, - } - try: - func = funcs[direction] - except KeyError: - expected_values = ', '.join(sorted(funcs)) - raise cmdutils.CommandError("Invalid value {!r} for direction - " - "expected one of: {}".format( - direction, expected_values)) - - if direction in ['top', 'bottom']: - func() - else: - func(count=count) - - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - @cmdutils.argument('horizontal', flag='x') - def scroll_to_perc(self, perc: float = None, horizontal: bool = False, - count: int = None) -> None: - """Scroll to a specific percentage of the page. - - The percentage can be given either as argument or as count. - If no percentage is given, the page is scrolled to the end. - - Args: - perc: Percentage to scroll. - horizontal: Scroll horizontally instead of vertically. - count: Percentage to scroll. - """ - # save the pre-jump position in the special ' mark - self.set_mark("'") - - if perc is None and count is None: - perc = 100 - elif count is not None: - perc = count - - if horizontal: - x = perc - y = None - else: - x = None - y = perc - - self._current_widget().scroller.to_perc(x, y) - - @cmdutils.register(instance='command-dispatcher', scope='window') - def scroll_to_anchor(self, name): - """Scroll to the given anchor in the document. - - Args: - name: The anchor to scroll to. - """ - self._current_widget().scroller.to_anchor(name) - @cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.argument('count', value=cmdutils.Value.count) @cmdutils.argument('top_navigate', metavar='ACTION', diff --git a/qutebrowser/components/__init__.py b/qutebrowser/components/__init__.py new file mode 100644 index 000000000..b42c87fb6 --- /dev/null +++ b/qutebrowser/components/__init__.py @@ -0,0 +1,20 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""qutebrowser "extensions" which only use the qutebrowser.API API.""" diff --git a/qutebrowser/components/scrollcommands.py b/qutebrowser/components/scrollcommands.py new file mode 100644 index 000000000..13deb9b6b --- /dev/null +++ b/qutebrowser/components/scrollcommands.py @@ -0,0 +1,123 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Scrolling-related commands.""" + +from qutebrowser.api import cmdutils, tab + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def scroll_px(tab: tab.Tab, dx: int, dy: int, count: int = 1) -> None: + """Scroll the current tab by 'count * dx/dy' pixels. + + Args: + dx: How much to scroll in x-direction. + dy: How much to scroll in y-direction. + count: multiplier + """ + dx *= count + dy *= count + cmdutils.check_overflow(dx, 'int') + cmdutils.check_overflow(dy, 'int') + tab.scroller.delta(dx, dy) + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def scroll(tab: tab.Tab, direction: str, count: int = 1) -> None: + """Scroll the current tab in the given direction. + + Note you can use `:run-with-count` to have a keybinding with a bigger + scroll increment. + + Args: + direction: In which direction to scroll + (up/down/left/right/top/bottom). + count: multiplier + """ + funcs = { + 'up': tab.scroller.up, + 'down': tab.scroller.down, + 'left': tab.scroller.left, + 'right': tab.scroller.right, + 'top': tab.scroller.top, + 'bottom': tab.scroller.bottom, + 'page-up': tab.scroller.page_up, + 'page-down': tab.scroller.page_down, + } + try: + func = funcs[direction] + except KeyError: + expected_values = ', '.join(sorted(funcs)) + raise cmdutils.CommandError("Invalid value {!r} for direction - " + "expected one of: {}".format( + direction, expected_values)) + + if direction in ['top', 'bottom']: + func() + else: + func(count=count) + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +@cmdutils.argument('horizontal', flag='x') +def scroll_to_perc(tab: tab.Tab, count: int = None, + perc: float = None, horizontal: bool = False) -> None: + """Scroll to a specific percentage of the page. + + The percentage can be given either as argument or as count. + If no percentage is given, the page is scrolled to the end. + + Args: + perc: Percentage to scroll. + horizontal: Scroll horizontally instead of vertically. + count: Percentage to scroll. + """ + # save the pre-jump position in the special ' mark + self.set_mark("'") + + if perc is None and count is None: + perc = 100 + elif count is not None: + perc = count + + if horizontal: + x = perc + y = None + else: + x = None + y = perc + + tab.scroller.to_perc(x, y) + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +def scroll_to_anchor(tab: tab.Tab, name): + """Scroll to the given anchor in the document. + + Args: + name: The anchor to scroll to. + """ + tab.scroller.to_anchor(name)