Save/restore window geometry.
This commit is contained in:
parent
96d01f8755
commit
27d354633c
@ -22,6 +22,7 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
import functools
|
import functools
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import configparser
|
||||||
from signal import signal, SIGINT
|
from signal import signal, SIGINT
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
@ -316,8 +317,15 @@ class QuteBrowser(QApplication):
|
|||||||
return
|
return
|
||||||
self.shutting_down = True
|
self.shutting_down = True
|
||||||
logging.debug("Shutting down... (do_quit={})".format(do_quit))
|
logging.debug("Shutting down... (do_quit={})".format(do_quit))
|
||||||
if config.config is not None:
|
try:
|
||||||
config.config.save()
|
config.config.save()
|
||||||
|
except AttributeError:
|
||||||
|
logging.exception("Could not save config.")
|
||||||
|
try:
|
||||||
|
self._save_geometry()
|
||||||
|
config.state.save()
|
||||||
|
except AttributeError:
|
||||||
|
logging.exception("Could not save window geometry.")
|
||||||
try:
|
try:
|
||||||
if do_quit:
|
if do_quit:
|
||||||
self.mainwindow.tabs.shutdown_complete.connect(self.quit)
|
self.mainwindow.tabs.shutdown_complete.connect(self.quit)
|
||||||
@ -330,6 +338,18 @@ class QuteBrowser(QApplication):
|
|||||||
if do_quit:
|
if do_quit:
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
|
def _save_geometry(self):
|
||||||
|
"""Save the window geometry to the state config."""
|
||||||
|
rect = self.mainwindow.geometry()
|
||||||
|
try:
|
||||||
|
config.state.add_section('mainwindow')
|
||||||
|
except configparser.DuplicateSectionError:
|
||||||
|
pass
|
||||||
|
config.state['mainwindow']['x'] = str(rect.x())
|
||||||
|
config.state['mainwindow']['y'] = str(rect.y())
|
||||||
|
config.state['mainwindow']['w'] = str(rect.width())
|
||||||
|
config.state['mainwindow']['h'] = str(rect.height())
|
||||||
|
|
||||||
@pyqtSlot(tuple)
|
@pyqtSlot(tuple)
|
||||||
def cmd_handler(self, tpl):
|
def cmd_handler(self, tpl):
|
||||||
"""Handle commands and delegate the specific actions.
|
"""Handle commands and delegate the specific actions.
|
||||||
|
@ -27,19 +27,23 @@ import os
|
|||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
import logging
|
import logging
|
||||||
|
import configparser
|
||||||
from configparser import ConfigParser, ExtendedInterpolation
|
from configparser import ConfigParser, ExtendedInterpolation
|
||||||
|
|
||||||
from qutebrowser.utils.misc import read_file
|
from qutebrowser.utils.misc import read_file
|
||||||
|
|
||||||
config = None
|
config = None
|
||||||
|
state = None
|
||||||
colordict = {}
|
colordict = {}
|
||||||
fontdict = {}
|
fontdict = {}
|
||||||
|
|
||||||
|
|
||||||
def init(confdir):
|
def init(confdir):
|
||||||
"""Initialize the global objects based on the config in configdir."""
|
"""Initialize the global objects based on the config in configdir."""
|
||||||
global config, colordict, fontdict
|
global config, state, colordict, fontdict
|
||||||
config = Config(confdir)
|
logging.debug("Config init, confdir {}".format(confdir))
|
||||||
|
config = Config(confdir, 'qutebrowser.conf', read_file('qutebrowser.conf'))
|
||||||
|
state = Config(confdir, 'state', always_save=True)
|
||||||
try:
|
try:
|
||||||
colordict = ColorDict(config['colors'])
|
colordict = ColorDict(config['colors'])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -126,31 +130,37 @@ class FontDict(dict):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class Config(ConfigParser):
|
class Config(ConfigParser):
|
||||||
|
|
||||||
"""Our own ConfigParser subclass."""
|
"""Our own ConfigParser subclass."""
|
||||||
|
|
||||||
configdir = None
|
configdir = None
|
||||||
FNAME = 'config'
|
|
||||||
default_cp = None
|
default_cp = None
|
||||||
config_loaded = False
|
config_loaded = False
|
||||||
|
|
||||||
def __init__(self, configdir):
|
def __init__(self, configdir, fname, default_config=None,
|
||||||
|
always_save=False):
|
||||||
"""Config constructor.
|
"""Config constructor.
|
||||||
|
|
||||||
configdir -- directory to store the config in.
|
configdir -- directory to store the config in.
|
||||||
|
fname -- Filename of the config file.
|
||||||
|
default_config -- Default config as string.
|
||||||
|
always_save -- Whether to always save the config, even when it wasn't
|
||||||
|
loaded.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
super().__init__(interpolation=ExtendedInterpolation())
|
super().__init__(interpolation=ExtendedInterpolation())
|
||||||
|
self.always_save = always_save
|
||||||
|
self.configdir = configdir
|
||||||
self.default_cp = ConfigParser(interpolation=ExtendedInterpolation())
|
self.default_cp = ConfigParser(interpolation=ExtendedInterpolation())
|
||||||
self.default_cp.optionxform = lambda opt: opt # be case-insensitive
|
self.default_cp.optionxform = lambda opt: opt # be case-insensitive
|
||||||
self.default_cp.read_string(read_file('qutebrowser.conf'))
|
if default_config is not None:
|
||||||
|
self.default_cp.read_string(default_config)
|
||||||
if not self.configdir:
|
if not self.configdir:
|
||||||
return
|
return
|
||||||
self.optionxform = lambda opt: opt # be case-insensitive
|
self.optionxform = lambda opt: opt # be case-insensitive
|
||||||
self.configdir = configdir
|
self.configdir = configdir
|
||||||
self.configfile = os.path.join(self.configdir, self.FNAME)
|
self.configfile = os.path.join(self.configdir, fname)
|
||||||
if not os.path.isfile(self.configfile):
|
if not os.path.isfile(self.configfile):
|
||||||
return
|
return
|
||||||
logging.debug("Reading config from {}".format(self.configfile))
|
logging.debug("Reading config from {}".format(self.configfile))
|
||||||
|
@ -17,12 +17,13 @@
|
|||||||
|
|
||||||
"""The main window of QuteBrowser."""
|
"""The main window of QuteBrowser."""
|
||||||
|
|
||||||
|
from PyQt5.QtCore import QRect
|
||||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout
|
from PyQt5.QtWidgets import QWidget, QVBoxLayout
|
||||||
|
|
||||||
from qutebrowser.widgets.statusbar import StatusBar
|
from qutebrowser.widgets.statusbar import StatusBar
|
||||||
from qutebrowser.widgets.browser import TabbedBrowser
|
from qutebrowser.widgets.browser import TabbedBrowser
|
||||||
from qutebrowser.widgets.completion import CompletionView
|
from qutebrowser.widgets.completion import CompletionView
|
||||||
|
import qutebrowser.utils.config as config
|
||||||
|
|
||||||
class MainWindow(QWidget):
|
class MainWindow(QWidget):
|
||||||
|
|
||||||
@ -41,8 +42,20 @@ class MainWindow(QWidget):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.setWindowTitle('qutebrowser')
|
self.setWindowTitle('qutebrowser')
|
||||||
# FIXME maybe store window position/size on exit
|
try:
|
||||||
self.resize(800, 600)
|
x = int(config.state['mainwindow']['x'])
|
||||||
|
y = int(config.state['mainwindow']['y'])
|
||||||
|
w = int(config.state['mainwindow']['w'])
|
||||||
|
h = int(config.state['mainwindow']['h'])
|
||||||
|
except KeyError:
|
||||||
|
rect = QRect()
|
||||||
|
else:
|
||||||
|
rect = QRect(x, y, w, h)
|
||||||
|
if not rect.isValid():
|
||||||
|
rect = QRect(50, 50, 800, 600)
|
||||||
|
# FIXME there is no setFrameGeometry, but this seems to do the wrong
|
||||||
|
# thing.
|
||||||
|
self.setGeometry(rect)
|
||||||
|
|
||||||
self.vbox = QVBoxLayout(self)
|
self.vbox = QVBoxLayout(self)
|
||||||
self.vbox.setContentsMargins(0, 0, 0, 0)
|
self.vbox.setContentsMargins(0, 0, 0, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user