From: Oleg Broytman Date: Tue, 19 Dec 2023 13:39:57 +0000 (+0300) Subject: Feat(wx): Save window size in session config X-Git-Tag: 0.3.0~9^2~38 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=1feae445c17aecb6355a2e26a3367738b3416d65;p=m_librarian.git Feat(wx): Save window size in session config [skip ci] --- diff --git a/m_librarian/wx/Application.py b/m_librarian/wx/Application.py index 308d65d..adfd03b 100644 --- a/m_librarian/wx/Application.py +++ b/m_librarian/wx/Application.py @@ -2,6 +2,7 @@ import wx, wx.adv from ..__version__ import __version__ +from .session_config import get_session_config class MainWindow(wx.Frame): @@ -21,6 +22,8 @@ class MainWindow(wx.Frame): self.Bind(wx.EVT_MENU, self.OnAbout, about) MenuBar.Append(about_menu, u"&О программе") + self.Bind(wx.EVT_SIZE, self.OnSize) + def OnQuit(self, event): self.Close(True) @@ -36,6 +39,14 @@ class MainWindow(wx.Frame): aboutInfo.SetLicense(u'GPL') wx.adv.AboutBox(aboutInfo) + def OnSize(self, event): + """Save window size in the session config""" + size = event.GetSize() + session_config = get_session_config() + session_config.set('main_window', 'width', str(size.width)) + session_config.set('main_window', 'height', str(size.height)) + session_config.save() + event.Skip() # Call other handlers class Application(wx.App): diff --git a/m_librarian/wx/session_config.py b/m_librarian/wx/session_config.py index 315c107..93b725c 100755 --- a/m_librarian/wx/session_config.py +++ b/m_librarian/wx/session_config.py @@ -28,6 +28,25 @@ def _find_config_dirs(): _ml_session_config = None +class SessionConfigWrapper(ConfigWrapper): + def __init__(self, config, config_path): + ConfigWrapper.__init__(self, config) + self.config_path = config_path + + def set(self, section, option, value): + if not self.config.has_section(section): + self.config.add_section(section) + super(SessionConfigWrapper, self).set(section, option, value) + + def save(self): + if self.config_path is None: + config_dirs = _find_config_dirs() + self.config_path = \ + os.path.join(config_dirs[0], 'm_librarian_session.conf') + with open(self.config_path, 'wt') as fp: + self.config.write(fp) + + def get_session_config(config_path=None): global _ml_session_config if _ml_session_config is None: @@ -38,7 +57,8 @@ def get_session_config(config_path=None): find_config_file(config_dirs, 'm_librarian_session.conf') if config_path is not None: _ml_session_config.read(config_path) - _ml_session_config = ConfigWrapper(_ml_session_config) + _ml_session_config = \ + SessionConfigWrapper(_ml_session_config, config_path) return _ml_session_config