Browse Source

Issue 2366 – Monitor placement and maximization is not remembered on monitors with different scaling than the primary one

https://winscp.net/tracker/2366
(cherry picked from commit c4a4c297d4b7a023e21998331dcd150d49faa34d)

Source commit: d814290719ad465a025922341d901cbde37ace98
Martin Prikryl 6 months ago
parent
commit
0cff6df9c4

+ 3 - 2
source/forms/CustomScpExplorer.cpp

@@ -1470,14 +1470,15 @@ void __fastcall TCustomScpExplorerForm::StoreParams()
   WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
 }
 //---------------------------------------------------------------------------
-void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
+void __fastcall TCustomScpExplorerForm::Loaded()
 {
+  // Loaded is called in more deterministic moment than previously used CreateParams
   if (!FFormRestored)
   {
     FFormRestored = true;
     RestoreFormParams();
   }
-  TForm::CreateParams(Params);
+  TForm::Loaded();
 }
 //---------------------------------------------------------------------------
 void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)

+ 1 - 1
source/forms/CustomScpExplorer.h

@@ -425,7 +425,7 @@ protected:
   virtual bool __fastcall RemoteTransferDialog(TManagedTerminal *& Session,
     TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
     bool NoConfirmation, bool Move);
-  virtual void __fastcall CreateParams(TCreateParams & Params);
+  virtual void __fastcall Loaded();
   void __fastcall DeleteFiles(TOperationSide Side, TStrings * FileList, bool Alternative);
   bool __fastcall RemoteTransferFiles(TStrings * FileList, bool NoConfirmation,
     bool Move, TManagedTerminal * Session);

+ 1 - 1
source/forms/ScpCommander.cpp

@@ -157,7 +157,7 @@ void __fastcall TScpCommanderForm::RestoreFormParams()
 {
   DebugAssert(WinConfiguration);
   TCustomScpExplorerForm::RestoreFormParams();
-  RestoreForm(WinConfiguration->ScpCommander.WindowParams, this);
+  RestoreForm(WinConfiguration->ScpCommander.WindowParams, this, false, ScpCommanderWindowParamsDefault);
 }
 //---------------------------------------------------------------------------
 void __fastcall TScpCommanderForm::RestorePanelParams(

+ 1 - 1
source/forms/ScpExplorer.cpp

@@ -68,7 +68,7 @@ void __fastcall TScpExplorerForm::RestoreFormParams()
   DebugAssert(Configuration);
 
   TCustomScpExplorerForm::RestoreFormParams();
-  RestoreForm(WinConfiguration->ScpExplorer.WindowParams, this);
+  RestoreForm(WinConfiguration->ScpExplorer.WindowParams, this, false, ScpExplorerWindowParamsDefault);
 }
 //---------------------------------------------------------------------------
 void __fastcall TScpExplorerForm::ConfigurationChanged()

+ 34 - 18
source/windows/Tools.cpp

@@ -183,10 +183,17 @@ void __fastcall LoadListViewStr(TListView * ListView, UnicodeString ALayoutStr)
   }
 }
 //---------------------------------------------------------------------------
-void __fastcall LoadFormDimensions(
-  const UnicodeString & LeftStr, const UnicodeString & TopStr, const UnicodeString & RightStr, const UnicodeString & BottomStr,
-  int PixelsPerInch, Forms::TMonitor * Monitor, TForm * Form, TRect & Bounds, bool & DefaultPos)
+wchar_t FormDataSep = L';';
+//---------------------------------------------------------------------------
+void LoadFormDimensions(
+  const UnicodeString & AData, int PixelsPerInch, Forms::TMonitor * Monitor, TForm * Form, TRect & Bounds, bool & DefaultPos)
 {
+  UnicodeString Data = AData;
+  UnicodeString LeftStr = CutToChar(Data, FormDataSep, true);
+  UnicodeString TopStr = CutToChar(Data, FormDataSep, true);
+  UnicodeString RightStr = CutToChar(Data, FormDataSep, true);
+  UnicodeString BottomStr = CutToChar(Data, FormDataSep, true);
+
   DefaultPos = (StrToIntDef(LeftStr, 0) == -1) && (StrToIntDef(TopStr, 0) == -1);
   if (!DefaultPos)
   {
@@ -216,19 +223,26 @@ void __fastcall LoadFormDimensions(
   }
 }
 //---------------------------------------------------------------------------
-void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
+static void CenterOnMonitor(TForm * Form, Forms::TMonitor * Monitor, const TRect & Bounds)
+{
+  Form->BoundsRect = Monitor->BoundsRect.CenteredRect(Bounds);
+}
+//---------------------------------------------------------------------------
+void RestoreForm(const UnicodeString & AData, TForm * Form, bool PositionOnly, const UnicodeString & DefaultData)
 {
   DebugAssert(Form);
+  UnicodeString Data = AData;
   if (!Data.IsEmpty())
   {
     Forms::TMonitor * Monitor = FormMonitor(Form);
 
-    UnicodeString LeftStr = CutToChar(Data, L';', true);
-    UnicodeString TopStr = CutToChar(Data, L';', true);
-    UnicodeString RightStr = CutToChar(Data, L';', true);
-    UnicodeString BottomStr = CutToChar(Data, L';', true);
-    TWindowState State = (TWindowState)StrToIntDef(CutToChar(Data, L';', true), (int)wsNormal);
-    int PixelsPerInch = LoadPixelsPerInch(CutToChar(Data, L';', true), Form);
+    UnicodeString BoundsStr;
+    for (int Index = 0; Index < 4; Index++)
+    {
+      CutToChar(Data, FormDataSep, true);
+    }
+    TWindowState State = (TWindowState)StrToIntDef(CutToChar(Data, FormDataSep, true), (int)wsNormal);
+    int PixelsPerInch = LoadPixelsPerInch(CutToChar(Data, FormDataSep, true), Form);
 
     TRect OriginalBounds = Form->BoundsRect;
     int OriginalPixelsPerInch = Form->PixelsPerInch;
@@ -237,7 +251,7 @@ void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
     {
       bool DefaultPos;
       TRect Bounds = OriginalBounds;
-      LoadFormDimensions(LeftStr, TopStr, RightStr, BottomStr, PixelsPerInch, Monitor, Form, Bounds, DefaultPos);
+      LoadFormDimensions(AData, PixelsPerInch, Monitor, Form, Bounds, DefaultPos);
 
       int Padding = ScaleByPixelsPerInch(20, Monitor);
       if (DefaultPos ||
@@ -277,9 +291,7 @@ void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
           // to handle that ourselves, so place window to center
           if (!PositionOnly)
           {
-            Form->SetBounds(Monitor->Left + ((Monitor->Width - Bounds.Width()) / 2),
-              Monitor->Top + ((Monitor->Height - Bounds.Height()) / 2),
-              Bounds.Width(), Bounds.Height());
+            CenterOnMonitor(Form, Monitor, Bounds);
           }
           if (!Form->HandleAllocated())
           {
@@ -300,7 +312,7 @@ void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
           if (OriginalPixelsPerInch != Form->PixelsPerInch)
           {
             TRect Bounds2 = OriginalBounds;
-            LoadFormDimensions(LeftStr, TopStr, RightStr, BottomStr, PixelsPerInch, Monitor, Form, Bounds2, DefaultPos);
+            LoadFormDimensions(AData, PixelsPerInch, Monitor, Form, Bounds2, DefaultPos);
             DebugAssert(!DefaultPos);
             Form->BoundsRect = Bounds2;
           }
@@ -313,9 +325,13 @@ void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
 
       if (!PositionOnly)
       {
-        TRect Bounds2 = Form->BoundsRect;
-        OffsetRect(Bounds2, Monitor->Left, Monitor->Top);
-        Form->BoundsRect = Bounds2;
+        TRect Bounds2 = OriginalBounds;
+        bool DefaultPos;
+        // This gives the form after unmaximization somewhat reasonable size,
+        // but it does not really set exact dimensions, let only with scaling
+        LoadFormDimensions(DefaultData, USER_DEFAULT_SCREEN_DPI, Monitor, Form, Bounds2, DefaultPos);
+        DebugAssert(DefaultPos);
+        CenterOnMonitor(Form, Monitor, Bounds2);
       }
     }
   }

+ 1 - 1
source/windows/Tools.h

@@ -29,7 +29,7 @@ IShellLink * __fastcall CreateDesktopSessionShortCut(
   int SpecialFolder = -1, int IconIndex = SITE_ICON, bool Return = false);
 UnicodeString __fastcall GetListViewStr(TListView * ListView);
 void __fastcall LoadListViewStr(TListView * ListView, UnicodeString LayoutStr);
-void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly = false);
+void RestoreForm(const UnicodeString & Data, TForm * Form, bool PositionOnly = false, const UnicodeString & DefaultData = EmptyStr);
 UnicodeString __fastcall StoreForm(TForm * Form);
 void __fastcall RestoreFormSize(UnicodeString Data, TForm * Form);
 UnicodeString __fastcall StoreFormSize(TForm * Form);

+ 0 - 3
source/windows/VCLCommon.cpp

@@ -1049,8 +1049,6 @@ void __fastcall UseSystemSettingsPre(TForm * Control)
     Control->HelpKeyword = L"start";
   }
 
-  DebugAssert(SameFont(Control->Font, Application->DefaultFont));
-
   ApplySystemSettingsOnControl(Control);
 };
 //---------------------------------------------------------------------------
@@ -2626,7 +2624,6 @@ void TDesktopFontManager::UpdateControl(TControl * Control)
     if (DebugAlwaysTrue(SystemParametersInfoForPixelsPerInch(SPI_GETICONTITLELOGFONT, sizeof(LogFont), &LogFont, 0, PixelsPerInch)))
     {
       DesktopFont->Handle = CreateFontIndirect(&LogFont);
-      DebugAssert((PixelsPerInch == Screen->PixelsPerInch) || SameFont(DesktopFont.get(), Application->DefaultFont));
     }
   }
   else

+ 12 - 2
source/windows/WinConfiguration.cpp

@@ -39,6 +39,8 @@ const UnicodeString ScpCommanderRemotePanelDirViewParamsDefault = ScpExplorerDir
 const UnicodeString ScpCommanderLocalPanelDirViewParamsDefault =
   L"0;1;0|150,1;70,1;120,1;150,1;55,0;55,0;@" + SaveDefaultPixelsPerInch() + L"|5;0;1;2;3;4";
 UnicodeString QueueViewLayoutDefault;
+UnicodeString ScpCommanderWindowParamsDefault;
+UnicodeString ScpExplorerWindowParamsDefault;
 //---------------------------------------------------------------------------
 static const wchar_t FileColorDataSeparator = L':';
 TFileColorData::TFileColorData() :
@@ -708,7 +710,11 @@ void __fastcall TWinConfiguration::Default()
 
   int ExplorerWidth = Min(WorkAreaWidthScaled - 40, 960);
   int ExplorerHeight = Min(WorkAreaHeightScaled - 30, 720);
-  FScpExplorer.WindowParams = FormatDefaultWindowParams(ExplorerWidth, ExplorerHeight);
+  if (ScpExplorerWindowParamsDefault.IsEmpty())
+  {
+    ScpExplorerWindowParamsDefault = FormatDefaultWindowParams(ExplorerWidth, ExplorerHeight);
+  }
+  FScpExplorer.WindowParams = ScpExplorerWindowParamsDefault;
 
   FScpExplorer.DirViewParams = ScpExplorerDirViewParamsDefault;
   FScpExplorer.ToolbarsLayout =
@@ -737,7 +743,11 @@ void __fastcall TWinConfiguration::Default()
 
   int CommanderWidth = Min(WorkAreaWidthScaled - 40, 1090);
   int CommanderHeight = Min(WorkAreaHeightScaled - 30, 700);
-  FScpCommander.WindowParams = FormatDefaultWindowParams(CommanderWidth, CommanderHeight);
+  if (ScpCommanderWindowParamsDefault.IsEmpty())
+  {
+    ScpCommanderWindowParamsDefault = FormatDefaultWindowParams(CommanderWidth, CommanderHeight);
+  }
+  FScpCommander.WindowParams = ScpCommanderWindowParamsDefault;
 
   FScpCommander.LocalPanelWidth = 0.5;
   FScpCommander.SwappedPanels = false;

+ 2 - 0
source/windows/WinConfiguration.h

@@ -218,6 +218,8 @@ extern const UnicodeString ScpExplorerDirViewParamsDefault;
 extern const UnicodeString ScpCommanderRemotePanelDirViewParamsDefault;
 extern const UnicodeString ScpCommanderLocalPanelDirViewParamsDefault;
 extern UnicodeString QueueViewLayoutDefault;
+extern UnicodeString ScpCommanderWindowParamsDefault;
+extern UnicodeString ScpExplorerWindowParamsDefault;
 //---------------------------------------------------------------------------
 struct TUpdatesConfiguration
 {