unit uUIAutoScaler;

interface

uses
  Vcl.Forms, Vcl.StdCtrls, Vcl.DBGrids, Vcl.Controls, System.Classes,
  System.SysUtils, Vcl.Graphics, Winapi.Windows, System.Math,
  System.TypInfo, System.Generics.Collections;

type
  TFormScaleInfo = record
    InitialWidth: Integer;
    InitialHeight: Integer;
    ScaledOnce: Boolean;
  end;

  TUIAutoScaler = class
  private
    class var FFormRegistry: TDictionary<TForm, TFormScaleInfo>;
  public
    class constructor Create;
    class destructor Destroy;
    class procedure RegisterForm(AForm: TForm);
    class procedure UnregisterForm(AForm: TForm);
    class procedure ScaleForm(AForm: TForm);
  end;

implementation

type
  // Truco canónico de Delphi para acceder a miembros protegidos de TControl
  THackControl = class(TControl);

{ TUIAutoScaler }

class constructor TUIAutoScaler.Create;
begin
  FFormRegistry := TDictionary<TForm, TFormScaleInfo>.Create;
end;

class destructor TUIAutoScaler.Destroy;
begin
  FreeAndNil(FFormRegistry);
end;

class procedure TUIAutoScaler.RegisterForm(AForm: TForm);
var
  Info: TFormScaleInfo;
begin
  if not Assigned(AForm) or not Assigned(FFormRegistry) then Exit;
  
  // Forzar a que se dibuje en el mismo monitor que el formulario principal
  if (Application.MainForm <> nil) and (AForm <> Application.MainForm) then
    AForm.DefaultMonitor := dmMainForm;
  
  if not FFormRegistry.ContainsKey(AForm) then
  begin
    Info.InitialWidth := AForm.Width;
    Info.InitialHeight := AForm.Height;
    Info.ScaledOnce := False;
    FFormRegistry.Add(AForm, Info);
  end;
end;

class procedure TUIAutoScaler.UnregisterForm(AForm: TForm);
begin
  if Assigned(FFormRegistry) and Assigned(AForm) then
    FFormRegistry.Remove(AForm);
end;

class procedure TUIAutoScaler.ScaleForm(AForm: TForm);
begin
  // No-op: El autoajuste de fuentes y tamaño por resolución/escala ha sido deshabilitado.
  // El autoajuste de columnas de DBGrid se realiza automáticamente a través de uDBGridHelper.
end;

end.
