diff options
author | Florian Bruhin <me@the-compiler.org> | 2022-06-13 13:15:27 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2022-06-13 13:15:27 +0200 |
commit | ce27831740de4b3dc69c358b28dbd99596892ccb (patch) | |
tree | a6a19fc57a8f148a9626605dc5e4aeb6b355b98f | |
parent | 803866666f109c1f55538a1a77aa82c7353e8a49 (diff) | |
parent | 9a7a905f8f392f65417a49187d413471d2fc8f84 (diff) | |
download | qutebrowser-ce27831740de4b3dc69c358b28dbd99596892ccb.tar.gz qutebrowser-ce27831740de4b3dc69c358b28dbd99596892ccb.zip |
Merge remote-tracking branch 'origin/pr/6448'
-rw-r--r-- | doc/help/settings.asciidoc | 1 | ||||
-rw-r--r-- | qutebrowser/config/configdata.yml | 3 | ||||
-rw-r--r-- | qutebrowser/config/configtypes.py | 2 | ||||
-rw-r--r-- | qutebrowser/mainwindow/statusbar/bar.py | 80 | ||||
-rw-r--r-- | qutebrowser/mainwindow/statusbar/clock.py | 54 |
5 files changed, 106 insertions, 34 deletions
diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index 1236dc3ac..c086ed783 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -4131,6 +4131,7 @@ Valid values: * +keypress+: Display pressed keys when composing a vi command. * +progress+: Progress bar for the current page loading. * +text:foo+: Display the static text after the colon, `foo` in the example. + * +clock+: Display current time. Format can be changed by adding like `clock:$format`. For format strings see link:https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior[the python datetime library]. Default: diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index e91d9aaf1..7b79e761d 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -2062,6 +2062,9 @@ statusbar.widgets: - keypress: "Display pressed keys when composing a vi command." - progress: "Progress bar for the current page loading." - 'text:foo': "Display the static text after the colon, `foo` in the example." + - clock: "Display current time. Format can be changed by adding like + `clock:$format`. For format strings see + link:https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior[the python datetime library]." none_ok: true default: ['keypress', 'url', 'scroll', 'history', 'tabs', 'progress'] desc: "List of widgets displayed in the statusbar." diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 97011b7cf..eef43ded4 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -2015,6 +2015,6 @@ class StatusbarWidget(String): """ def _validate_valid_values(self, value: str) -> None: - if value.startswith("text:"): + if value.startswith("text:") or value.startswith("clock:"): return super()._validate_valid_values(value) diff --git a/qutebrowser/mainwindow/statusbar/bar.py b/qutebrowser/mainwindow/statusbar/bar.py index 46cf083bd..ae33a386a 100644 --- a/qutebrowser/mainwindow/statusbar/bar.py +++ b/qutebrowser/mainwindow/statusbar/bar.py @@ -31,7 +31,7 @@ from qutebrowser.keyinput import modeman from qutebrowser.utils import usertypes, log, objreg, utils from qutebrowser.mainwindow.statusbar import (backforward, command, progress, keystring, percentage, url, - tabindex, textbase) + tabindex, textbase, clock) @dataclasses.dataclass @@ -199,6 +199,7 @@ class StatusBar(QWidget): self.tabindex = tabindex.TabIndex() self.keystring = keystring.KeyString() self.prog = progress.Progress(self) + self.clock = clock.Clock() self._text_widgets = [] self._draw_widgets() @@ -208,6 +209,31 @@ class StatusBar(QWidget): def __repr__(self): return utils.get_repr(self) + def _get_widget_from_config(self, key): + """Return the widget that fits with config string key.""" + if key == 'url': + return self.url + elif key == 'scroll': + return self.percentage + elif key == 'scroll_raw': + return self.percentage + elif key == 'history': + return self.backforward + elif key == 'tabs': + return self.tabindex + elif key == 'keypress': + return self.keystring + elif key == 'progress': + return self.prog + elif key.startswith('text:'): + new_text_widget = textbase.TextBase() + self._text_widgets.append(new_text_widget) + return new_text_widget + elif key.startswith('clock:') or key == 'clock': + return self.clock + else: + raise utils.Unreachable(key) + @pyqtSlot(str) def _on_config_changed(self, option): if option == 'statusbar.show': @@ -225,47 +251,35 @@ class StatusBar(QWidget): # Read the list and set widgets accordingly for segment in config.val.statusbar.widgets: - if segment == 'url': - self._hbox.addWidget(self.url) - self.url.show() - elif segment == 'scroll': - self._hbox.addWidget(self.percentage) - self.percentage.show() - elif segment == 'scroll_raw': - self._hbox.addWidget(self.percentage) - self.percentage.set_raw() - self.percentage.show() - elif segment == 'history': - self._hbox.addWidget(self.backforward) - self.backforward.enabled = True - if tab: - self.backforward.on_tab_changed(tab) - elif segment == 'tabs': - self._hbox.addWidget(self.tabindex) - self.tabindex.show() - elif segment == 'keypress': - self._hbox.addWidget(self.keystring) - self.keystring.show() - elif segment == 'progress': - self._hbox.addWidget(self.prog) - self.prog.enabled = True + cur_widget = self._get_widget_from_config(segment) + self._hbox.addWidget(cur_widget) + + if segment == 'scroll_raw': + cur_widget.set_raw() + elif segment in ('history', 'progress'): + cur_widget.enabled = True if tab: - self.prog.on_tab_changed(tab) + cur_widget.on_tab_changed(tab) + + # Do not call .show() for these widgets. + return elif segment.startswith('text:'): - cur_widget = textbase.TextBase() - self._text_widgets.append(cur_widget) cur_widget.setText(segment.split(':', maxsplit=1)[1]) - self._hbox.addWidget(cur_widget) - cur_widget.show() - else: - raise utils.Unreachable(segment) + elif segment.startswith('clock:') or segment == 'clock': + split_segment = segment.split(':', maxsplit=1) + if len(split_segment) == 2 and split_segment[1]: + cur_widget.format = split_segment[1] + else: + cur_widget.format = '%X' + + cur_widget.show() def _clear_widgets(self): """Clear widgets before redrawing them.""" # Start with widgets hidden and show them when needed for widget in [self.url, self.percentage, self.backforward, self.tabindex, - self.keystring, self.prog, *self._text_widgets]: + self.keystring, self.prog, self.clock, *self._text_widgets]: assert isinstance(widget, QWidget) widget.hide() self._hbox.removeWidget(widget) diff --git a/qutebrowser/mainwindow/statusbar/clock.py b/qutebrowser/mainwindow/statusbar/clock.py new file mode 100644 index 000000000..20587a5ac --- /dev/null +++ b/qutebrowser/mainwindow/statusbar/clock.py @@ -0,0 +1,54 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org> +# +# 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 <https://www.gnu.org/licenses/>. + +"""Clock displayed in the statusbar.""" +from datetime import datetime + +from PyQt5.QtCore import Qt, QTimer + +from qutebrowser.mainwindow.statusbar import textbase + + +class Clock(textbase.TextBase): + + """Shows current time and date in the statusbar.""" + + UPDATE_DELAY = 500 # ms + + def __init__(self, parent=None): + super().__init__(parent, elidemode=Qt.ElideNone) + self.format = "" + + self.timer = QTimer(self) + self.timer.timeout.connect(self._show_time) + + def _show_time(self): + """Set text to current time, using self.format as format-string.""" + self.setText(datetime.now().strftime(self.format)) + + def hideEvent(self, event): + """Stop timer when widget is hidden.""" + self.timer.stop() + super().hideEvent(event) + + def showEvent(self, event): + """Override showEvent to show time and start self.timer for updating.""" + self.timer.start(Clock.UPDATE_DELAY) + self._show_time() + super().showEvent(event) |