[gnome-subtitles: 3/3] Only save the window state when closing the app



commit bcc3a328150eb12a942b781a1a59013cee99c563
Author: Pedro Castro <pedro gnomesubtitles org>
Date:   Sat Oct 12 21:32:06 2019 +0100

    Only save the window state when closing the app
    
    Fixes #143 - Only save the window state when closing the application
    Issue originally spotted by Fernando Fernandez @tarmaciltur

 gnome-subtitles.csproj                   |  1 +
 src/Glade/MainWindow.ui                  |  1 +
 src/GnomeSubtitles/Core/Base.cs          |  5 +++++
 src/GnomeSubtitles/Core/EventHandlers.cs | 16 ++++++++++++---
 src/GnomeSubtitles/Ui/MainUi.cs          | 20 +++++++++++++------
 src/GnomeSubtitles/Ui/WindowState.cs     | 34 ++++++++++++++++++++++++++++++++
 6 files changed, 68 insertions(+), 9 deletions(-)
---
diff --git a/gnome-subtitles.csproj b/gnome-subtitles.csproj
index 841e05a..704fc74 100644
--- a/gnome-subtitles.csproj
+++ b/gnome-subtitles.csproj
@@ -276,6 +276,7 @@
     <Compile Include="src\GnomeSubtitles\Core\ConfigBackendInMemory.cs" />
     <Compile Include="src\External\GtkSpell\SpellChecker.cs" />
     <Compile Include="src\GnomeSubtitles\Dialog\Message\MessageDialog.cs" />
+    <Compile Include="src\GnomeSubtitles\Ui\WindowState.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/src/Glade/MainWindow.ui b/src/Glade/MainWindow.ui
index e815e61..0c7813f 100644
--- a/src/Glade/MainWindow.ui
+++ b/src/Glade/MainWindow.ui
@@ -26,6 +26,7 @@
   <object class="GtkApplicationWindow" id="window">
     <property name="can_focus">False</property>
     <signal name="delete-event" handler="OnWindowDelete" swapped="no"/>
+    <signal name="destroy" handler="OnWindowDestroy" swapped="no"/>
     <signal name="size-allocate" handler="OnSizeAllocated" swapped="no"/>
     <child>
       <placeholder/>
diff --git a/src/GnomeSubtitles/Core/Base.cs b/src/GnomeSubtitles/Core/Base.cs
index 1ef8bd1..039c094 100644
--- a/src/GnomeSubtitles/Core/Base.cs
+++ b/src/GnomeSubtitles/Core/Base.cs
@@ -281,6 +281,11 @@ public static class Base {
 
                /* Initialize the GUI */
                ui = new MainUi(handlers);
+               
+               //The window must be made visible here because classes such as EventHandlers.OnSizeAllocated) 
may depend on
+               //'ui' being set when the window is made visible (so it can't be made visible inside MainUi's 
constructor).
+               ui.Show();
+
                clipboards.WatchPrimaryChanges = true;
 
                EmitInitFinishedEvent();
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index 8b25269..8fc45e1 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -459,10 +459,20 @@ public class EventHandlers {
        bool quit = Base.Quit();
        args.RetVal = !quit; //True to keep the window open
     }
+    
+    public void OnWindowDestroy (object o, EventArgs args) {
+               Base.Config.ViewWindowWidth = Base.Ui.WindowState.Width;
+               Base.Config.ViewWindowHeight = Base.Ui.WindowState.Height;
+       }
+
+       public void OnSizeAllocated (object o, SizeAllocatedArgs args) {
+               Window window = o as Window;
+
+       int width, height;
+       window.GetSize(out width, out height);
 
-    public void OnSizeAllocated (object o, SizeAllocatedArgs args) {
-       Base.Config.ViewWindowWidth = args.Allocation.Width;
-       Base.Config.ViewWindowHeight = args.Allocation.Height;
+       Base.Ui.WindowState.Width = width;
+       Base.Ui.WindowState.Height = height;
     }
 
 
diff --git a/src/GnomeSubtitles/Ui/MainUi.cs b/src/GnomeSubtitles/Ui/MainUi.cs
index 2761c6a..621a568 100644
--- a/src/GnomeSubtitles/Ui/MainUi.cs
+++ b/src/GnomeSubtitles/Ui/MainUi.cs
@@ -39,6 +39,7 @@ public class MainUi {
        private static Builder builder = null;
 
        private Window window = null;
+       private WindowState windowState = null;
        private Menus menus = null;
        private Video video = null;
        private SubtitleView view = null;
@@ -60,7 +61,8 @@ public class MainUi {
                window.IconName = Base.ExecutionContext.IconName;
                window.SetWmclass(Base.ExecutionContext.ApplicationName, 
Base.ExecutionContext.ApplicationName);
                
-               window.SetDefaultSize(Base.Config.ViewWindowWidth, Base.Config.ViewWindowHeight);
+               windowState = new WindowState(Base.Config.ViewWindowWidth, Base.Config.ViewWindowHeight);
+               window.SetDefaultSize(windowState.Width, windowState.Height);
                
                Base.ExecutionContext.Application.AddWindow(window);
 
@@ -72,8 +74,6 @@ public class MainUi {
 
                builder.Autoconnect(handlers);
                Base.InitFinished += OnBaseInitFinished;
-
-               window.Visible = true;
     }
 
 
@@ -82,6 +82,10 @@ public class MainUi {
        public Window Window {
                get { return window; }
        }
+       
+       public WindowState WindowState {
+               get { return windowState; }
+       }
 
        public Menus Menus {
                get { return menus; }
@@ -110,9 +114,13 @@ public class MainUi {
                return builder.GetObject(name) as Widget;
        }
 
-    /// <summary>Starts the GUI</summary>
-    /// <remarks>A file is opened if it was specified as argument. If it wasn't, a blank start is 
performed.</summary>
-    public void Start () {
+       public void Show () {
+               window.Visible = true;
+       }
+
+       /// <summary>Starts the GUI</summary>
+       /// <remarks>A file is opened if it was specified as argument. If it wasn't, a blank start is 
performed.</summary>
+       public void Start () {
                string subtitleFilePath = GetSubtitleFileArg(Base.ExecutionContext.Args);
                if (subtitleFilePath != null) {
                        Uri videoUri = Base.Config.VideoAutoChooseFile ? 
VideoFiles.FindMatchingVideo(subtitleFilePath) : null;
diff --git a/src/GnomeSubtitles/Ui/WindowState.cs b/src/GnomeSubtitles/Ui/WindowState.cs
new file mode 100644
index 0000000..8f12db9
--- /dev/null
+++ b/src/GnomeSubtitles/Ui/WindowState.cs
@@ -0,0 +1,34 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2019 Pedro Castro
+ *
+ * Gnome Subtitles 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+namespace GnomeSubtitles.Ui {
+
+public class WindowState {
+       
+       public int Width { get; set; }
+       public int Height { get; set; }
+
+       public WindowState (int width, int height) {
+               Width = width;
+               Height = height;
+       }
+
+}
+
+}
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]