]> git.phdru.name Git - m_librarian.git/commitdiff
Feat(wx): Save window size in session config
authorOleg Broytman <phd@phdru.name>
Tue, 19 Dec 2023 13:39:57 +0000 (16:39 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 19 Dec 2023 16:07:53 +0000 (19:07 +0300)
[skip ci]

m_librarian/wx/Application.py
m_librarian/wx/session_config.py

index 308d65d4e6a5635123e5f902d97c9dfb47d929f6..adfd03b5f344904bce40c1effe31c34037f91c41 100644 (file)
@@ -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):
 
index 315c107feb1cac063a9735f34005cbc4286d6b03..93b725cd1a65d336547431b88ada6dd220eddeb1 100755 (executable)
@@ -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