Dans un projet legacy, on tombe toujours sur des événements fantômes qui survivent à des composants supprimés depuis trois ans. D’où mon goût pour les événements créés à la volée — et l’envie de pouvoir chaîner un traitement sans écraser celui d’origine.
En Delphi, un délégué permet d’attacher un bloc de code à un type procédural. Voici comment bricoler un petit TEventDispatcher pour câbler vos événements en deux lignes avec des procédures anonymes.
Un dispatcheur pour TNotifyEvent
On s’appuie sur TProc<TObject> (on aurait tout aussi bien pu déclarer son propre type TDelegateNotifyEvent = reference to procedure(Sender: TObject);) pour retourner un vrai TNotifyEvent :
type
TEventDispatcher = class
protected
FDelegateNotifyEvent: TProc<TObject>;
procedure OnNotifyEvent(Sender: TObject);
public
class function Create(const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent; overload;
function Wire(const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent; overload;
end;
{ TEventDispatcher }
procedure TEventDispatcher.OnNotifyEvent(Sender: TObject);
begin
if Assigned(FDelegateNotifyEvent) then
FDelegateNotifyEvent(Sender);
end;
function TEventDispatcher.Wire(
const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent;
begin
FDelegateNotifyEvent := ADelegateNotifyEvent;
Result := Self.OnNotifyEvent;
end;
class function TEventDispatcher.Create(
const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent;
begin
Result := TEventDispatcher.Create().Wire(ADelegateNotifyEvent);
end;
Plus besoin de passer par l’inspecteur d’objets pour un OnClick : on câble la procédure anonyme directement dans FormCreate :
implementation
uses EventDispatcher;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
BtnExemple.OnClick := TEventDispatcher.Create(procedure(Sender: TObject)
begin
ShowMessage('BtnExemple Clicked');
end);
end;
end.
Étendre à d’autres événements (ex. TMouseEvent)
Même mécanique pour supporter autant de signatures d’événements que voulu — par exemple TMouseEvent :
unit EventDispatcher;
interface
uses
System.SysUtils,
System.Classes,
Vcl.Controls;
type
TProcMouseEvent = reference to procedure(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
TEventDispatcher = class
protected
FDelegateNotifyEvent: TProc<TObject>;
FDelegateMouseEvent: TProcMouseEvent;
procedure OnNotifyEvent(Sender: TObject);
procedure OnMouseEvent(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); overload;
public
class function Create(const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent; overload;
class function Create(const ADelegateMouseEvent: TProcMouseEvent): TMouseEvent; overload;
function Wire(const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent; overload;
function Wire(const ADelegateMouseEvent: TProcMouseEvent): TMouseEvent; overload;
end;
implementation
{ TEventDispatcher }
function TEventDispatcher.Wire(
const ADelegateMouseEvent: TProcMouseEvent): TMouseEvent;
begin
FDelegateMouseEvent := ADelegateMouseEvent;
Result := Self.OnMouseEvent;
end;
class function TEventDispatcher.Create(
const ADelegateMouseEvent: TProcMouseEvent): TMouseEvent;
begin
Result := TEventDispatcher.Create().Wire(ADelegateMouseEvent);
end;
procedure TEventDispatcher.OnMouseEvent(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Assigned(FDelegateMouseEvent) then
FDelegateMouseEvent(Sender, Button, Shift, X, Y);
end;
procedure TEventDispatcher.OnNotifyEvent(Sender: TObject);
begin
if Assigned(FDelegateNotifyEvent) then
FDelegateNotifyEvent(Sender);
end;
function TEventDispatcher.Wire(
const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent;
begin
FDelegateNotifyEvent := ADelegateNotifyEvent;
Result := Self.OnNotifyEvent;
end;
class function TEventDispatcher.Create(
const ADelegateNotifyEvent: TProc<TObject>): TNotifyEvent;
begin
Result := TEventDispatcher.Create().Wire(ADelegateNotifyEvent);
end;
end.
Exemple appliqué à deux TShape qui partagent le même comportement au clic :
var
OnMouseUp: TProcMouseEvent;
begin
OnMouseUp := procedure(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer)
begin
ColorDialog.Color := TShape(Sender).Brush.Color;
if ColorDialog.Execute then
TShape(Sender).Brush.Color := ColorDialog.Color;
end;
TopShape.OnMouseUp := TEventDispatcher.Create(OnMouseUp);
BottomShape.OnMouseUp := TEventDispatcher.Create(OnMouseUp);
end;
Bonus : enrichir un événement sans l’écraser
Grâce à la capture de variables des procédures anonymes, on peut même conserver l’événement d’origine et exécuter notre traitement juste après :
var
C: TComponent;
InheritedNotifyEvent: TNotifyEvent;
begin
// ...
if (C is TSpinEdit) then
begin
InheritedNotifyEvent := nil;
if Assigned(TSpinEdit(C).OnChange) then
InheritedNotifyEvent := TSpinEdit(C).OnChange;
TSpinEdit(C).OnChange := TEventDispatcher.Create(
procedure(Sender: TObject)
begin
if Assigned(InheritedNotifyEvent) then
InheritedNotifyEvent(Sender);
DoSomeTreatment(TSpinEdit(Sender).Name);
end);
end;
end;