[gnome-chess/mcatanzaro/gtk4] progress



commit 64644d4606f09dd465235f5173d1817b0922a71d
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Tue Dec 22 18:46:26 2020 -0600

    progress

 src/gnome-chess.vala | 326 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 195 insertions(+), 131 deletions(-)
---
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 27a378d..67d8c20 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -53,8 +53,14 @@ public class ChessApplication : Gtk.Application
     private ComboBox timer_increment_units_combo;
     private ComboBox custom_duration_units_combo;
     private uint save_duration_timeout = 0;
-    private FileChooserNative open_dialog = null;
+    private FileChooserNative? open_dialog = null;
     private FileChooserNative? save_dialog = null;
+    private delegate void PromptSaveGameCallback (bool cancelled);
+    private PromptSaveGameCallback? prompt_save_game_cb = null;
+    private MessageDialog? prompt_save_game_dialog = null;
+    private MessageDialog? save_error_dialog = null;
+    private MessageDialog? claim_draw_dialog = null;
+    private MessageDialog? resign_dialog = null;
     private AboutDialog? about_dialog = null;
     private Dialog? promotion_type_selector_dialog = null;
 
@@ -1483,136 +1489,172 @@ return null;
         black_time_label.queue_draw ();
     }
 
-    private bool prompt_save_game (string prompt_text)
+    private void prompt_save_game_response_cb (int response_id)
+        requires (prompt_save_game_cb != null)
     {
-#if 0
-        if (!game_needs_saving)
-            return true;
-
-        var dialog = new MessageDialog (window,
-                                        DialogFlags.MODAL,
-                                        MessageType.QUESTION,
-                                        ButtonsType.NONE,
-                                        prompt_text);
-        dialog.add_button (_("_Cancel"), ResponseType.CANCEL);
-
-        if (game.result == ChessResult.IN_PROGRESS)
+        if (response_id == ResponseType.CANCEL || response_id == ResponseType.DELETE_EVENT)
         {
-            dialog.add_button (_("_Abandon game"), ResponseType.NO);
-            dialog.add_button (_("_Save game for later"), ResponseType.YES);
+            prompt_save_game_cb (true);
+            prompt_save_game_cb = null;
         }
-        else
-        {
-            dialog.add_button (_("_Discard game"), ResponseType.NO);
-            dialog.add_button (_("_Save game log"), ResponseType.YES);
-        }
-
-        var result = dialog.run ();
-        dialog.destroy ();
-
-        if (result == ResponseType.CANCEL || result == ResponseType.DELETE_EVENT)
-        {
-            return false;
-        }
-        else if (result == ResponseType.YES)
+        else if (response_id == ResponseType.YES)
         {
             present_save_dialog ();
+            prompt_save_game_cb (false);
         }
         else
         {
-            warn_if_fail (result == ResponseType.NO);
+            warn_if_fail (response_id == ResponseType.NO);
             /* Remove completed game from history */
             game_needs_saving = false;
             autosave ();
+
+            prompt_save_game_cb (false);
+            prompt_save_game_cb = null;
         }
 
-#endif
-        return true;
+        prompt_save_game_dialog.hide ();
     }
 
-    private void present_claim_draw_dialog ()
-        requires (game.can_claim_draw ())
+    private void prompt_save_game (string prompt_text)
+        requires (prompt_save_game_cb != null)
     {
-#if 0
-        game.pause (false);
-
-        var dialog = new MessageDialog (window,
-                                        DialogFlags.MODAL,
-                                        MessageType.QUESTION,
-                                        ButtonsType.NONE,
-                                        /* Title of claim draw dialog */
-                                        _("Would you like to claim a draw?"));
-
-        string reason;
-        if (game.is_fifty_move_rule_fulfilled ())
+        if (!game_needs_saving)
         {
-            /* Message in claim draw dialog when triggered by fifty-move rule */
-            reason = _("You may claim a draw because fifty moves have passed without a capture or pawn 
advancement. (The computer player may still choose to claim a draw even if you choose to keep playing.)");
+            prompt_save_game_cb (false);
+            return;
         }
-        else if (game.is_three_fold_repeat ())
+
+        if (prompt_save_game_dialog == null)
         {
-            /* Message in claim draw dialog when triggered by three-fold repetition */
-            reason = _("You may claim a draw because the current board position has occurred three times. 
(The computer player may still choose to claim a draw even if you choose to keep playing.)");
-        }
-        else assert_not_reached ();
+            prompt_save_game_dialog = new MessageDialog (window,
+                                                         DialogFlags.MODAL,
+                                                         MessageType.QUESTION,
+                                                         ButtonsType.NONE,
+                                                         prompt_text);
+            prompt_save_game_dialog.add_button (_("_Cancel"), ResponseType.CANCEL);
 
-        dialog.secondary_text = reason;
+            if (game.result == ChessResult.IN_PROGRESS)
+            {
+                prompt_save_game_dialog.add_button (_("_Abandon game"), ResponseType.NO);
+                prompt_save_game_dialog.add_button (_("_Save game for later"), ResponseType.YES);
+            }
+            else
+            {
+                prompt_save_game_dialog.add_button (_("_Discard game"), ResponseType.NO);
+                prompt_save_game_dialog.add_button (_("_Save game log"), ResponseType.YES);
+            }
+
+            prompt_save_game_dialog.response.connect (prompt_save_game_response_cb);
+        }
 
-        dialog.add_buttons (/* Option in claim draw dialog */
-                            _("_Keep Playing"), ResponseType.REJECT,
-                            /* Option in claim draw dialog */
-                            _("_Claim Draw"), ResponseType.ACCEPT,
-                            null);
+        prompt_save_game_dialog.show ();
+    }
 
-        var response = dialog.run ();
-        dialog.destroy ();
+    private void claim_draw_response_cb (int response_id)
+    {
         game.unpause ();
 
-        if (response == ResponseType.ACCEPT)
-        {
+        if (response_id == ResponseType.ACCEPT)
             game.current_player.claim_draw ();
+
+        claim_draw_dialog.hide ();
+    }
+
+    private void present_claim_draw_dialog ()
+        requires (game.can_claim_draw ())
+    {
+        game.pause (false);
+
+        if (claim_draw_dialog == null)
+        {
+            claim_draw_dialog = new MessageDialog (window,
+                                                   DialogFlags.MODAL,
+                                                   MessageType.QUESTION,
+                                                   ButtonsType.NONE,
+                                                   /* Title of claim draw dialog */
+                                                   _("Would you like to claim a draw?"));
+
+            string reason;
+            if (game.is_fifty_move_rule_fulfilled ())
+            {
+                /* Message in claim draw dialog when triggered by fifty-move rule */
+                reason = _("You may claim a draw because fifty moves have passed without a capture or pawn 
advancement. (The computer player may still choose to claim a draw even if you choose to keep playing.)");
+            }
+            else if (game.is_three_fold_repeat ())
+            {
+                /* Message in claim draw dialog when triggered by three-fold repetition */
+                reason = _("You may claim a draw because the current board position has occurred three 
times. (The computer player may still choose to claim a draw even if you choose to keep playing.)");
+            }
+            else assert_not_reached ();
+
+            claim_draw_dialog.secondary_text = reason;
+
+            claim_draw_dialog.add_buttons (/* Option in claim draw dialog */
+                                           _("_Keep Playing"), ResponseType.REJECT,
+                                           /* Option in claim draw dialog */
+                                           _("_Claim Draw"), ResponseType.ACCEPT,
+                                           null);
+
+            claim_draw_dialog.response.connect (claim_draw_response_cb);
         }
-#endif
+
+        claim_draw_dialog.show ();
     }
 
-    public void new_game_cb ()
+    private void new_game_prompt_save_game_cb (bool cancelled)
     {
-        if (prompt_save_game (_("Save this game before starting a new one?")))
+        prompt_save_game_cb = null;
+
+        if (!cancelled)
             start_new_game ();
     }
 
-    public void resign_cb ()
+    public void new_game_cb ()
+        requires (prompt_save_game_cb == null)
     {
-#if 0
-        game.pause (false);
+        prompt_save_game_cb = new_game_prompt_save_game_cb;
+        prompt_save_game (_("Save this game before starting a new one?"));
+    }
 
-        var dialog = new MessageDialog (window,
-                                        DialogFlags.MODAL,
-                                        MessageType.QUESTION,
-                                        ButtonsType.NONE,
-                                        /* Title of warning dialog when player clicks Resign */
-                                        _("Are you sure you want to resign?"));
-        dialog.format_secondary_text (
-            /* Text on warning dialog when player clicks Resign */
-            _("This makes sense if you plan to save the game as a record of your loss."));
-        dialog.add_buttons (/* Option on warning dialog when player clicks resign */
-                            _("_Keep Playing"), ResponseType.REJECT,
-                            /* Option on warning dialog when player clicks resign */
-                            _("_Resign"), ResponseType.ACCEPT,
-                            null);
-
-        var response = dialog.run ();
-        dialog.destroy ();
+    private void resign_response_cb (int response_id)
+    {
         game.unpause ();
 
-        if (response == ResponseType.ACCEPT)
+        if (response_id == ResponseType.ACCEPT)
         {
             if (human_player != null)
                 human_player.resign ();
             else
                 game.current_player.resign ();
         }
-#endif
+
+        resign_dialog.hide ();
+    }
+
+    public void resign_cb ()
+    {
+        game.pause (false);
+
+        if (resign_dialog == null)
+        {
+            resign_dialog = new MessageDialog (window,
+                                               DialogFlags.MODAL,
+                                               MessageType.QUESTION,
+                                               ButtonsType.NONE,
+                                               /* Title of warning dialog when player clicks Resign */
+                                               _("Are you sure you want to resign?"));
+            resign_dialog.format_secondary_text (
+                /* Text on warning dialog when player clicks Resign */
+                _("This makes sense if you plan to save the game as a record of your loss."));
+            resign_dialog.add_buttons (/* Option on warning dialog when player clicks resign */
+                                       _("_Keep Playing"), ResponseType.REJECT,
+                                       /* Option on warning dialog when player clicks resign */
+                                       _("_Resign"), ResponseType.ACCEPT,
+                                       null);
+        }
+
+        resign_dialog.show ();
     }
 
     public void undo_move_cb ()
@@ -2347,9 +2389,53 @@ return null;
         }
     }
 
-    private void present_save_dialog ()
+    private void save_dialog_response_cb (int response_id)
     {
+        if (response_id == ResponseType.ACCEPT)
+        {
+            update_pgn_time_remaining ();
+
+            try
+            {
+                game_file = save_dialog.get_file ();
+                pgn_game.write (game_file);
+
+                disable_window_action (SAVE_GAME_ACTION_NAME);
+                game_needs_saving = false;
+
 #if 0
+                headerbar.subtitle = game_file.get_basename ();
+#endif
+            }
+            catch (Error e)
+            {
+                if (save_error_dialog == null)
+                {
+                    save_error_dialog = new MessageDialog (window,
+                                                           DialogFlags.MODAL,
+                                                           MessageType.ERROR,
+                                                           ButtonsType.NONE,
+                                                           _("Failed to save game: %s"),
+                                                           e.message);
+                    save_error_dialog.add_button (_("_OK"), ResponseType.OK);
+                    save_error_dialog.response.connect (() => save_error_dialog.hide ());
+                }
+
+                save_error_dialog.show ();
+            }
+        }
+
+        save_dialog.hide ();
+
+        if (prompt_save_game_cb != null)
+        {
+            prompt_save_game_cb (false);
+            prompt_save_game_cb = null;
+        }
+    }
+
+    private void present_save_dialog ()
+    {
         /* Show active dialog */
         if (save_dialog == null)
         {
@@ -2359,7 +2445,7 @@ return null;
                                                  _("_Save"),
                                                  _("_Cancel"));
 
-            var set_filename = false;
+            var set_file = false;
             if (game_file != null)
             {
                 /* If the path is under /run, we are probably sandboxed, and the
@@ -2373,12 +2459,12 @@ return null;
                 var path = game_file.get_path ();
                 if (path != autosave_filename && !path.has_prefix ("/run"))
                 {
-                    save_dialog.set_filename (path);
-                    set_filename = true;
+                    save_dialog.set_file (game_file);
+                    set_file = true;
                 }
             }
 
-            if (!set_filename)
+            if (!set_file)
             {
                 save_dialog.set_current_name (/* Default filename for the save game dialog */
                                               _("Untitled Chess Game") + ".pgn");
@@ -2396,43 +2482,12 @@ return null;
             all_filter.set_filter_name (_("All files"));
             all_filter.add_pattern ("*");
             save_dialog.add_filter (all_filter);
-        }
-
-        var response_id = save_dialog.run ();
-        if (response_id == ResponseType.ACCEPT)
-        {
-            update_pgn_time_remaining ();
 
-            try
-            {
-                game_file = save_dialog.get_file ();
-                save_dialog.destroy ();
-                save_dialog = null;
-
-                pgn_game.write (game_file);
-
-                disable_window_action (SAVE_GAME_ACTION_NAME);
-                game_needs_saving = false;
-
-#if 0
-                headerbar.subtitle = game_file.get_basename ();
-#endif
-            }
-            catch (Error e)
-            {
-                var error_dialog = new MessageDialog (window,
-                                                      DialogFlags.MODAL,
-                                                      MessageType.ERROR,
-                                                      ButtonsType.NONE,
-                                                      _("Failed to save game: %s"),
-                                                      e.message);
-                error_dialog.add_button (_("_OK"), ResponseType.OK);
-
-                error_dialog.run ();
-                error_dialog.destroy ();
-            }
+            save_dialog.modal = true;
+            save_dialog.response.connect (save_dialog_response_cb);
         }
-#endif
+
+        save_dialog.show ();
     }
 
     public void save_game_cb ()
@@ -2473,9 +2528,11 @@ return null;
         open_dialog.hide ();
     }
 
-    public void open_game_cb ()
+    private void open_game_prompt_save_game_cb (bool cancelled)
     {
-        if (!prompt_save_game (_("Save this game before loading another one?")))
+        prompt_save_game_cb = null;
+
+        if (cancelled)
             return;
 
         /* Show active dialog */
@@ -2507,6 +2564,13 @@ return null;
         open_dialog.show ();
     }
 
+    public void open_game_cb ()
+        requires (prompt_save_game_cb == null)
+    {
+        prompt_save_game_cb = open_game_prompt_save_game_cb;
+        prompt_save_game (_("Save this game before loading another one?"));
+    }
+
     private void start_new_game ()
     {
         game_file = null;


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