Don't destroy chat when window loses focus (bug 623)
authorWuzzy <Wuzzy@disroot.org>
Wed, 24 May 2023 23:54:58 +0200
changeset 15984 6a985a680c44
parent 15983 416bca0a172c
child 15985 a4630009e733
Don't destroy chat when window loses focus (bug #623)
hedgewars/uChat.pas
hedgewars/uCommandHandlers.pas
--- a/hedgewars/uChat.pas	Wed May 24 21:23:33 2023 +0200
+++ b/hedgewars/uChat.pas	Wed May 24 23:54:58 2023 +0200
@@ -28,6 +28,7 @@
 procedure ReloadLines;
 procedure CleanupInput;
 procedure CloseChat;
+procedure RestoreChat;
 procedure AddChatString(s: shortstring);
 procedure DrawChat;
 procedure KeyPressChat(keysym: TSDL_Keysym);
@@ -55,6 +56,7 @@
 var Strs: array[0 .. MaxStrIndex] of TChatLine;
     MStrs: array[0 .. MaxStrIndex] of shortstring;
     LocalStrs: array[0 .. MaxStrIndex] of shortstring;
+    oldInput: shortstring;
     missedCount: LongWord;
     lastStr: LongWord;
     localLastStr: LongInt;
@@ -783,13 +785,43 @@
     ResetKbd;
 end;
 
+procedure OpenChat(s: shortstring);
+var i: Integer;
+begin
+    isInChatMode:= true;
+    SDL_StopTextInput();
+    SDL_StartTextInput();
+    //Make REALLY sure unexpected events are flushed (1 time is insufficient as of SDL 2.0.7)
+    for i := 1 to 2 do
+    begin
+        SDL_PumpEvents();
+        SDL_FlushEvent(SDL_TEXTINPUT);
+    end;
+    if length(s) = 0 then
+        SetLine(InputStr, '', true)
+    else
+        begin
+        SetLine(InputStr, s, true);
+        cursorPos:= length(s);
+        UpdateCursorCoords();
+        end;
+end;
+
 procedure CloseChat;
 begin
+    oldInput:= InputStr.s;
     SetLine(InputStr, '', true);
     ResetCursor();
     CleanupInput();
 end;
 
+procedure RestoreChat;
+begin
+    if length(oldInput) > 0 then
+        OpenChat(oldInput);
+    oldInput:= '';
+end;
+
 procedure DelBytesFromInputStrBack(endIdx: integer; count: byte);
 var startIdx: integer;
 begin
@@ -1070,7 +1102,9 @@
                 SetLine(InputStr, '', true);
                 ResetCursor();
                 end
-            else CleanupInput
+            else
+                CleanupInput;
+            oldInput:= '';
             end;
         SDL_SCANCODE_RETURN, SDL_SCANCODE_KP_ENTER:
             begin
@@ -1345,27 +1379,12 @@
 end;
 
 procedure chChat(var s: shortstring);
-var i: Integer;
 begin
     s:= s; // avoid compiler hint
-    isInChatMode:= true;
-    SDL_StopTextInput();
-    SDL_StartTextInput();
-    //Make REALLY sure unexpected events are flushed (1 time is insufficient as of SDL 2.0.7)
-    for i := 1 to 2 do
-    begin
-        SDL_PumpEvents();
-        SDL_FlushEvent(SDL_TEXTINPUT);
-    end;
-    //SDL_EnableKeyRepeat(200,45);
     if length(s) = 0 then
-        SetLine(InputStr, '', true)
+        OpenChat('')
     else
-        begin
-        SetLine(InputStr, '/clan ', true);
-        cursorPos:= 6;
-        UpdateCursorCoords();
-        end;
+        OpenChat('/clan ');
 end;
 
 procedure initModule;
--- a/hedgewars/uCommandHandlers.pas	Wed May 24 21:23:33 2023 +0200
+++ b/hedgewars/uCommandHandlers.pas	Wed May 24 23:54:58 2023 +0200
@@ -49,15 +49,16 @@
 begin
     s:= s; // avoid compiler hint
     if (GameState = gsGame) then
-    begin
+        begin
         CloseChat;
         GameState:= gsConfirm;
-    end
-    else begin
+        end
+    else
         if GameState = gsConfirm then
+            begin
             GameState:= gsGame;
-    end;
-
+            RestoreChat;
+            end;
     updateCursorVisibility;
 end;