From ba03e9394aa21c1a6da628dd987709b670668811 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Nov 2018 14:45:10 +0100 Subject: [PATCH] Move zoom commands to components/ --- qutebrowser/app.py | 2 +- qutebrowser/browser/commands.py | 67 ------------------- qutebrowser/components/zoomcommands.py | 92 ++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 68 deletions(-) create mode 100644 qutebrowser/components/zoomcommands.py diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 0bb7e14cb..7548c08d0 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -77,7 +77,7 @@ from qutebrowser.utils import (log, version, message, utils, urlutils, objreg, usertypes, standarddir, error, qtutils) # pylint: disable=unused-import # We import those to run the cmdutils.register decorators. -from qutebrowser.components import scrollcommands, caretcommands +from qutebrowser.components import scrollcommands, caretcommands, zoomcommands from qutebrowser.mainwindow.statusbar import command from qutebrowser.misc import utilcmds # pylint: enable=unused-import diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index f1bb8fb35..f6fecdce8 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -793,73 +793,6 @@ class CommandDispatcher: modeman.leave(self._win_id, KeyMode.caret, "yank selected", maybe=True) - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def zoom_in(self, count=1, quiet=False): - """Increase the zoom level for the current tab. - - Args: - count: How many steps to zoom in. - quiet: Don't show a zoom level message. - """ - tab = self._current_widget() - try: - perc = tab.zoom.apply_offset(count) - except ValueError as e: - raise cmdutils.CommandError(e) - if not quiet: - message.info("Zoom level: {}%".format(int(perc)), replace=True) - - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def zoom_out(self, count=1, quiet=False): - """Decrease the zoom level for the current tab. - - Args: - count: How many steps to zoom out. - quiet: Don't show a zoom level message. - """ - tab = self._current_widget() - try: - perc = tab.zoom.apply_offset(-count) - except ValueError as e: - raise cmdutils.CommandError(e) - if not quiet: - message.info("Zoom level: {}%".format(int(perc)), replace=True) - - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def zoom(self, zoom=None, count=None, quiet=False): - """Set the zoom level for the current tab. - - The zoom can be given as argument or as [count]. If neither is - given, the zoom is set to the default zoom. If both are given, - use [count]. - - Args: - zoom: The zoom percentage to set. - count: The zoom percentage to set. - quiet: Don't show a zoom level message. - """ - if zoom is not None: - try: - zoom = int(zoom.rstrip('%')) - except ValueError: - raise cmdutils.CommandError("zoom: Invalid int value {}" - .format(zoom)) - - level = count if count is not None else zoom - if level is None: - level = config.val.zoom.default - tab = self._current_widget() - - try: - tab.zoom.set_factor(float(level) / 100) - except ValueError: - raise cmdutils.CommandError("Can't zoom {}%!".format(level)) - if not quiet: - message.info("Zoom level: {}%".format(int(level)), replace=True) - @cmdutils.register(instance='command-dispatcher', scope='window') def tab_only(self, prev=False, next_=False, force=False): """Close all tabs except for the current one. diff --git a/qutebrowser/components/zoomcommands.py b/qutebrowser/components/zoomcommands.py new file mode 100644 index 000000000..c525b771c --- /dev/null +++ b/qutebrowser/components/zoomcommands.py @@ -0,0 +1,92 @@ +# 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 . + +"""Zooming-related commands.""" + +from qutebrowser.api import cmdutils, apitypes, message, config + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def zoom_in(tab: apitypes.Tab, count=1, quiet=False): + """Increase the zoom level for the current tab. + + Args: + count: How many steps to zoom in. + quiet: Don't show a zoom level message. + """ + try: + perc = tab.zoom.apply_offset(count) + except ValueError as e: + raise cmdutils.CommandError(e) + if not quiet: + message.info("Zoom level: {}%".format(int(perc)), replace=True) + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def zoom_out(tab: apitypes.Tab, count=1, quiet=False): + """Decrease the zoom level for the current tab. + + Args: + count: How many steps to zoom out. + quiet: Don't show a zoom level message. + """ + try: + perc = tab.zoom.apply_offset(-count) + except ValueError as e: + raise cmdutils.CommandError(e) + if not quiet: + message.info("Zoom level: {}%".format(int(perc)), replace=True) + + +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def zoom(tab: apitypes.Tab, zoom=None, count=None, quiet=False): + """Set the zoom level for the current tab. + + The zoom can be given as argument or as [count]. If neither is + given, the zoom is set to the default zoom. If both are given, + use [count]. + + Args: + zoom: The zoom percentage to set. + count: The zoom percentage to set. + quiet: Don't show a zoom level message. + """ + if zoom is not None: + try: + zoom = int(zoom.rstrip('%')) + except ValueError: + raise cmdutils.CommandError("zoom: Invalid int value {}" + .format(zoom)) + + level = count if count is not None else zoom + if level is None: + level = config.val.zoom.default + + try: + tab.zoom.set_factor(float(level) / 100) + except ValueError: + raise cmdutils.CommandError("Can't zoom {}%!".format(level)) + if not quiet: + message.info("Zoom level: {}%".format(int(level)), replace=True)