unit AboutForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SProgressIndicator, Vcl.StdCtrls,
  Vcl.ExtCtrls, AppEvnts;

type
  TAboutFrm = class(TForm)
    SProgressIndicator: TSProgressIndicator;
    ClientPanel: TPanel;
    stCaption: TStaticText;
    stKernelVersion: TStaticText;
    stFrontEndVersion: TStaticText;
    stCopyright: TStaticText;
    llWebAlgosim: TLinkLabel;
    stBuildTime: TStaticText;
    llWebRejbrand: TLinkLabel;
    llSpace: TLinkLabel;
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure llWebAlgosimLinkClick(Sender: TObject; const Link: string;
      LinkType: TSysLinkType);
    procedure UpdateDWMColors;
  private
    FAppEvents: TApplicationEvents;
    procedure AppActivate(Sender: TObject);
    procedure AppDeactivate(Sender: TObject);
  protected
    procedure Activate; override;
    procedure Deactivate; override;
  public
    class procedure ShowAboutForm; static;
  end;

implementation

uses
  MainForm, ASKernelDefs, FrontEndProps, ShellAPI, Math, Rux, UxTheme, DwmApi,
  UxPanel;

{$R *.dfm}

procedure TAboutFrm.Activate;
begin
  inherited;
  UpdateDWMColors;
end;

procedure TAboutFrm.AppActivate(Sender: TObject);
begin
  UpdateDWMColors;
end;

procedure TAboutFrm.AppDeactivate(Sender: TObject);
begin
  UpdateDWMColors;
end;

procedure TAboutFrm.Deactivate;
begin
  inherited;
  UpdateDWMColors;
end;

procedure TAboutFrm.FormCreate(Sender: TObject);
begin
  FAppEvents := TApplicationEvents.Create(Self);
  FAppEvents.OnActivate := AppActivate;
  FAppEvents.OnDeActivate := AppDeactivate;
  stKernelVersion.Caption := 'Kernel version: ' + ASKernelDefs.KernelVersion.ToString;
  stFrontEndVersion.Caption := 'Front end version: ' + FrontEndProps.GetFrontEndVersion.ToString;
  stBuildTime.Caption := 'Build time: ' + DateToStr(ASKernelDefs.LinkerTimestamp);
  SProgressIndicator.StartTime := AlgosimMainForm.Kernel.StartTime;
  TUx.RegisterCallback(SProgressIndicator,
    procedure
    begin
      SProgressIndicator.AccentColor := TUx.ThemeData.ActiveCaptionColor;
    end
  );
  TUx.RegisterCallback(Self,
    procedure
    begin
      UpdateDWMColors;
    end
  );
end;

procedure TAboutFrm.FormResize(Sender: TObject);
begin
  SProgressIndicator.Width := Min(ClientHeight, ClientWidth div 2);
end;

procedure TAboutFrm.llWebAlgosimLinkClick(Sender: TObject; const Link: string;
  LinkType: TSysLinkType);
begin
  if LinkType = sltURL then
    ShellExecute(Handle, nil, PChar(Link), nil, nil, SW_SHOWNORMAL);
end;

var
  AboutFrm: TAboutFrm;

class procedure TAboutFrm.ShowAboutForm;
begin
  if AboutFrm = nil then
    AboutFrm := TAboutFrm.Create(Application);
  AboutFrm.Show;
  AboutFrm.BringToFront;
end;

procedure TAboutFrm.UpdateDWMColors;
const
  DWMWA_BORDER_COLOR = 34;
  DWMWA_CAPTION_COLOR = 35;
  DWMWA_TEXT_COLOR = 36;
var
  c: DWORD;
begin
  if not GThemedBorders then
    Exit;
  if Active then
    c := TUx.ThemeData.ActiveCaptionColor
  else
    c := TUx.ThemeData.InactiveCaptionColor;
  DwmSetWindowAttribute(Handle, DWMWA_CAPTION_COLOR, @c, SizeOf(c));
  c := TUx.ThemeData.ActiveCaptionColor;
  DwmSetWindowAttribute(Handle, DWMWA_BORDER_COLOR, @c, SizeOf(c));
  if Active then
    c := TUx.ThemeData.ActiveCaptionTextColor
  else
    c := TUx.ThemeData.InactiveCaptionTextColor;
  DwmSetWindowAttribute(Handle, DWMWA_TEXT_COLOR, @c, SizeOf(c));
end;

end.
