unit FormMessages;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  Generics.Collections;

type
  TMessage = class
    Text: string;
  end;

  { TMessages }

  TMessages = class(TObjectList<TMessage>)
    function AddNew(Text: string): TMessage;
  end;

  { TFormMessages }

  TFormMessages = class(TForm)
    Memo1: TMemo;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FReloadPending: Boolean;
  public
    Messages: TMessages;
    procedure AddMessage(Text: string);
  end;


implementation

{$R *.lfm}

{ TMessages }

function TMessages.AddNew(Text: string): TMessage;
begin
  Result := TMessage.Create;
  Result.Text := Text;
  Add(Result);
end;

{ TFormMessages }

procedure TFormMessages.Timer1Timer(Sender: TObject);
var
  I: Integer;
begin
  if FReloadPending then begin
    FReloadPending := False;
    Memo1.Lines.BeginUpdate;
    Memo1.Lines.Clear;
    for I := 0 to Messages.Count - 1 do
      Memo1.Lines.Add(Messages[I].Text);
    Memo1.Lines.EndUpdate;
  end;
end;

procedure TFormMessages.FormCreate(Sender: TObject);
begin
  Messages := TMessages.Create;
end;

procedure TFormMessages.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Messages);
end;

procedure TFormMessages.AddMessage(Text: string);
begin
  Messages.AddNew(Text);
  FReloadPending := True;
end;

end.

