From 0e8d298de58c4c9e7b55c651af1174ab1ee3ef25 Mon Sep 17 00:00:00 2001 From: Olly Date: Mon, 2 Oct 2023 00:00:15 +0100 Subject: [PATCH] Remove deprecated SimbaSettings.RegisterChangeHandler --- Source/editor/simba.editor.pas | 102 +++++++++--------- Source/forms/simba.main.pas | 48 +++++---- Source/forms/simba.outputform.pas | 34 +++--- .../simba.settingsform_editor_colors.pas | 4 +- Source/simba.settings.pas | 56 +++------- 5 files changed, 112 insertions(+), 132 deletions(-) diff --git a/Source/editor/simba.editor.pas b/Source/editor/simba.editor.pas index 23ce5d7fc..c00267447 100644 --- a/Source/editor/simba.editor.pas +++ b/Source/editor/simba.editor.pas @@ -41,8 +41,13 @@ TSimbaEditor = class(TSimbaSynEdit) procedure MaybeReplaceModifiers; - procedure SimbaSettingChanged(Setting: TSimbaSetting); - procedure DoSimbaSettingChanged_Colors(Setting: TSimbaSetting); + procedure DoSettingChanged_Colors(Setting: TSimbaSetting); + procedure DoSettingChanged_AllowCaretPastEOL(Setting: TSimbaSetting); + procedure DoSettingChanged_RightMargin(Setting: TSimbaSetting); + procedure DoSettingChanged_RightMarginVisible(Setting: TSimbaSetting); + procedure DoSettingChanged_AntiAliased(Setting: TSimbaSetting); + procedure DoSettingChanged_FontSize(Setting: TSimbaSetting); + procedure DoSettingChanged_FontName(Setting: TSimbaSetting); // Accept drop from TTreeView procedure DoDragDrop(Sender, Source: TObject; X, Y: Integer); @@ -100,8 +105,7 @@ TSimbaEditor = class(TSimbaSynEdit) implementation uses - SynEditPointClasses, SynGutterBase, SynGutter, SynHighlighterPas_Simba, SynEditMarkupHighAll, - LazSynEditMouseCmdsTypes, Forms, + SynEditPointClasses, SynGutter, SynHighlighterPas_Simba, SynEditMarkupHighAll, LazSynEditMouseCmdsTypes, Forms, simba.fonthelpers, simba.editor_blockcompletion, simba.editor_docgenerator, simba.editor_commentblock, simba.editor_mousewheelzoom, simba.editor_multicaret, @@ -290,52 +294,47 @@ procedure TSimbaEditor.MaybeReplaceModifiers; ReplaceKeyStrokeModifiers(ssCtrl, ssMeta); end; -procedure TSimbaEditor.SimbaSettingChanged(Setting: TSimbaSetting); +procedure TSimbaEditor.DoSettingChanged_Colors(Setting: TSimbaSetting); begin - case Setting.Name of - 'Editor.FontSize': - begin - Font.Size := Setting.Value; - end; + if FUseSimbaColors then + FAttributes.LoadFromFile(Setting.Value); +end; - 'Editor.FontName': - begin - if IsFontFixed(Setting.Value) then - Font.Name := Setting.Value; - end; +procedure TSimbaEditor.DoSettingChanged_AllowCaretPastEOL(Setting: TSimbaSetting); +begin + if Setting.Value then + Options := Options + [eoTrimTrailingSpaces, eoScrollPastEol] + else + Options := Options - [eoTrimTrailingSpaces, eoScrollPastEol]; +end; - 'Editor.RightMargin': - begin - RightEdge := Setting.Value; - end; +procedure TSimbaEditor.DoSettingChanged_RightMargin(Setting: TSimbaSetting); +begin + RightEdge := Setting.Value; +end; - 'Editor.RightMarginVisible': - begin - if Setting.Value then - Options := Options - [eoHideRightMargin] - else - Options := Options + [eoHideRightMargin]; - end; +procedure TSimbaEditor.DoSettingChanged_RightMarginVisible(Setting: TSimbaSetting); +begin + if Setting.Value then + Options := Options - [eoHideRightMargin] + else + Options := Options + [eoHideRightMargin]; +end; - 'Editor.AntiAliased': - begin - FontAntialising := Setting.Value; - end; +procedure TSimbaEditor.DoSettingChanged_AntiAliased(Setting: TSimbaSetting); +begin + FontAntialising := Setting.Value; +end; - 'Editor.AllowCaretPastEOL': - begin - if Setting.Value then - Options := Options + [eoTrimTrailingSpaces, eoScrollPastEol] - else - Options := Options - [eoTrimTrailingSpaces, eoScrollPastEol]; - end; - end; +procedure TSimbaEditor.DoSettingChanged_FontSize(Setting: TSimbaSetting); +begin + Font.Size := Setting.Value; end; -procedure TSimbaEditor.DoSimbaSettingChanged_Colors(Setting: TSimbaSetting); +procedure TSimbaEditor.DoSettingChanged_FontName(Setting: TSimbaSetting); begin - if FUseSimbaColors then - FAttributes.LoadFromFile(Setting.Value); + if IsFontFixed(Setting.Value) then + Font.Name := Setting.Value; end; procedure TSimbaEditor.DoSpecialLineColor(Sender: TObject; Line: Integer; var Special: Boolean; AMarkup: TSynSelectedColor); @@ -501,23 +500,20 @@ constructor TSimbaEditor.Create(AOwner: TComponent); MaybeReplaceModifiers(); {$ENDIF} - SimbaSettingChanged(SimbaSettings.Editor.AllowCaretPastEOL); - SimbaSettingChanged(SimbaSettings.Editor.RightMarginVisible); - SimbaSettingChanged(SimbaSettings.Editor.AntiAliased); - SimbaSettingChanged(SimbaSettings.Editor.FontSize); - SimbaSettingChanged(SimbaSettings.Editor.FontName); - - SimbaSettings.RegisterChangeHandler(@SimbaSettingChanged); - with SimbaSettings do - RegisterChangeHandler(Self, Editor.CustomColors, @DoSimbaSettingChanged_Colors); + begin + RegisterChangeHandler(Self, Editor.CustomColors, @DoSettingChanged_Colors); + RegisterChangeHandler(Self, Editor.AllowCaretPastEOL, @DoSettingChanged_AllowCaretPastEOL, True); + RegisterChangeHandler(Self, Editor.RightMargin, @DoSettingChanged_RightMargin, True); + RegisterChangeHandler(Self, Editor.RightMarginVisible, @DoSettingChanged_RightMarginVisible, True); + RegisterChangeHandler(Self, Editor.AntiAliased, @DoSettingChanged_AntiAliased, True); + RegisterChangeHandler(Self, Editor.FontSize, @DoSettingChanged_FontSize, True); + RegisterChangeHandler(Self, Editor.FontName, @DoSettingChanged_FontName, True); + end; end; destructor TSimbaEditor.Destroy; begin - if (SimbaSettings <> nil) then - SimbaSettings.UnRegisterChangeHandler(@SimbaSettingChanged); - if (FAttributes <> nil) then FreeAndNil(FAttributes); diff --git a/Source/forms/simba.main.pas b/Source/forms/simba.main.pas index e1d819e3e..91b551c3e 100644 --- a/Source/forms/simba.main.pas +++ b/Source/forms/simba.main.pas @@ -261,9 +261,12 @@ TSimbaForm = class(TForm) procedure SetupCompleted; procedure DoColorPicked(Data: PtrInt); - procedure DoSettingChanged_Toolbar(Setting: TSimbaSetting); - procedure SimbaSettingChanged(Setting: TSimbaSetting); + procedure DoSettingChanged_Toolbar(Setting: TSimbaSetting); + procedure DoSettingChanged_CustomFontSize(Setting: TSimbaSetting); + procedure DoSettingChanged_LockLayout(Setting: TSimbaSetting); + procedure DoSettingChanged_TrayIconVisible(Setting: TSimbaSetting); + procedure DoSettingChanged_ConsoleVisible(Setting: TSimbaSetting); procedure HandleRecentFileClick(Sender: TObject); procedure HandleException(Sender: TObject; E: Exception); @@ -638,19 +641,17 @@ procedure TSimbaForm.Setup; SimbaIDEEvents.RegisterMethodOnScriptTabChange(@DoScriptTabChange); SimbaIDEEvents.RegisterMethodOnScriptStateChange(@DoScriptStateChange); - SimbaSettings.RegisterChangeHandler(@SimbaSettingChanged); - with SimbaSettings do begin RegisterChangeHandler(Self, General.ToolbarSize, @DoSettingChanged_Toolbar, True); RegisterChangeHandler(Self, General.ToolbarPosition, @DoSettingChanged_Toolbar, True); RegisterChangeHandler(Self, General.ToolBarSpacing, @DoSettingChanged_Toolbar, True); - end; - SimbaSettingChanged(SimbaSettings.General.CustomFontSize); - SimbaSettingChanged(SimbaSettings.General.LockLayout); - SimbaSettingChanged(SimbaSettings.General.TrayIconVisible); - SimbaSettingChanged(SimbaSettings.General.ConsoleVisible); + RegisterChangeHandler(Self, General.CustomFontSize, @DoSettingChanged_CustomFontSize, True); + RegisterChangeHandler(Self, General.LockLayout, @DoSettingChanged_LockLayout, True); + RegisterChangeHandler(Self, General.TrayIconVisible, @DoSettingChanged_TrayIconVisible, True); + RegisterChangeHandler(Self, General.ConsoleVisible, @DoSettingChanged_ConsoleVisible, True); + end; Application.CaptureExceptions := True; Application.OnException := @Self.HandleException; @@ -708,9 +709,6 @@ procedure TSimbaForm.FormDestroy(Sender: TObject); FreeAndNil(FMouseLogger); end; - if (SimbaSettings <> nil) then - SimbaSettings.UnRegisterChangeHandler(@SimbaSettingChanged); - if (FRecentFiles <> nil) then begin SimbaSettings.General.RecentFiles.Value := FRecentFiles.Text; @@ -847,16 +845,24 @@ procedure TSimbaForm.DoSettingChanged_Toolbar(Setting: TSimbaSetting); ToolBar.BorderSpacing.Around := Setting.Value; end; -procedure TSimbaForm.SimbaSettingChanged(Setting: TSimbaSetting); +procedure TSimbaForm.DoSettingChanged_CustomFontSize(Setting: TSimbaSetting); +begin + SetCustomFontSize(Setting.Value); +end; + +procedure TSimbaForm.DoSettingChanged_LockLayout(Setting: TSimbaSetting); +begin + SetLayoutLocked(Setting.Value); +end; + +procedure TSimbaForm.DoSettingChanged_TrayIconVisible(Setting: TSimbaSetting); +begin + SetTrayIconVisible(Setting.Value); +end; + +procedure TSimbaForm.DoSettingChanged_ConsoleVisible(Setting: TSimbaSetting); begin - if (Setting = SimbaSettings.General.CustomFontSize) then - SetCustomFontSize(Setting.Value); - if (Setting = SimbaSettings.General.ConsoleVisible) then - SetConsoleVisible(Setting.Value); - if (Setting = SimbaSettings.General.LockLayout) then - SetLayoutLocked(Setting.Value); - if (Setting = SimbaSettings.General.TrayIconVisible) then - SetTrayIconVisible(Setting.Value); + SetConsoleVisible(Setting.Value); end; procedure TSimbaForm.MenuCloseTabClick(Sender: TObject); diff --git a/Source/forms/simba.outputform.pas b/Source/forms/simba.outputform.pas index aacab3bc6..00dca8782 100644 --- a/Source/forms/simba.outputform.pas +++ b/Source/forms/simba.outputform.pas @@ -31,7 +31,9 @@ TSimbaOutputBox = class(TSimbaMemo) end; end; - procedure SimbaSettingChanged(Setting: TSimbaSetting); + procedure DoSettingChange_FontName(Setting: TSimbaSetting); + procedure DoSettingChange_FontSize(Setting: TSimbaSetting); + procedure DoSettingChange_FontAntiAliased(Setting: TSimbaSetting); procedure DoOpenLink(Data: PtrInt); procedure DoSpecialLineMarkup(Sender: TObject; Line: Integer; var Special: Boolean; AMarkup: TSynSelectedColor); @@ -160,18 +162,22 @@ function TSimbaOutputBox.GetTab: TSimbaTab; Result := TSimbaTab(Parent); end; -procedure TSimbaOutputBox.SimbaSettingChanged(Setting: TSimbaSetting); +procedure TSimbaOutputBox.DoSettingChange_FontName(Setting: TSimbaSetting); begin - if Setting.Equals(SimbaSettings.OutputBox.FontAntiAliased) then - FontAntialising := Setting.Value - else - if Setting.Equals(SimbaSettings.OutputBox.FontSize) then - Font.Size := Setting.Value - else - if Setting.Equals(SimbaSettings.OutputBox.FontName) and IsFontFixed(Setting.Value) then + if IsFontFixed(Setting.Value) then Font.Name := Setting.Value; end; +procedure TSimbaOutputBox.DoSettingChange_FontSize(Setting: TSimbaSetting); +begin + Font.Size := Setting.Value; +end; + +procedure TSimbaOutputBox.DoSettingChange_FontAntiAliased(Setting: TSimbaSetting); +begin + FontAntialising := Setting.Value; +end; + procedure TSimbaOutputBox.DoOpenLink(Data: PtrInt); begin if (FMouseLink.DocPos.FileName <> '') then @@ -244,8 +250,6 @@ procedure TSimbaOutputBox.DoMouseLinkClick(Sender: TObject; Button: TMouseButton end; constructor TSimbaOutputBox.Create(AOwner: TComponent); -var - Setting: TSimbaSetting; begin inherited Create(AOwner, False); @@ -269,15 +273,13 @@ constructor TSimbaOutputBox.Create(AOwner: TComponent); with MouseTextActions.Add() do Command := emcMouseLink; - SimbaSettings.RegisterChangeHandler(@SimbaSettingChanged); - for Setting in [SimbaSettings.OutputBox.FontSize, SimbaSettings.OutputBox.FontName, SimbaSettings.OutputBox.FontAntiAliased] do - SimbaSettingChanged(Setting); + SimbaSettings.RegisterChangeHandler(Self, SimbaSettings.OutputBox.FontName, @DoSettingChange_FontName, True); + SimbaSettings.RegisterChangeHandler(Self, SimbaSettings.OutputBox.FontSize, @DoSettingChange_FontSize, True); + SimbaSettings.RegisterChangeHandler(Self, SimbaSettings.OutputBox.FontAntiAliased, @DoSettingChange_FontAntiAliased, True); end; destructor TSimbaOutputBox.Destroy; begin - SimbaSettings.UnRegisterChangeHandler(@SimbaSettingChanged); - if (FLock <> nil) then FreeAndNil(FLock); if (FBuffer <> nil) then diff --git a/Source/forms/simba.settingsform_editor_colors.pas b/Source/forms/simba.settingsform_editor_colors.pas index 24edce8d2..22029a629 100644 --- a/Source/forms/simba.settingsform_editor_colors.pas +++ b/Source/forms/simba.settingsform_editor_colors.pas @@ -185,7 +185,7 @@ procedure TEditorColorsFrame.Save; FEditor.Attributes.SaveToFile(FileName); SimbaSettings.Editor.CustomColors.Value := FileName; - SimbaSettings.Changed(SimbaSettings.Editor.CustomColors); + SimbaSettings.Editor.CustomColors.Changed(); end; procedure TEditorColorsFrame.DoSaveButtonClick(Sender: TObject); @@ -205,7 +205,7 @@ procedure TEditorColorsFrame.DoSaveButtonClick(Sender: TObject); FEditor.Attributes.SaveToFile(FileName); SimbaSettings.Editor.CustomColors.Value := FileName; - SimbaSettings.Changed(SimbaSettings.Editor.CustomColors); + SimbaSettings.Editor.CustomColors.Changed(); end; finally Free(); diff --git a/Source/simba.settings.pas b/Source/simba.settings.pas index 4059527e2..1993a6c30 100644 --- a/Source/simba.settings.pas +++ b/Source/simba.settings.pas @@ -37,6 +37,7 @@ TSimbaSetting = class constructor Create(ASettings: TSimbaSettings; ASection, AName: String; DefaultValue: Variant); destructor Destroy; override; + procedure Changed; procedure SetDefault; function IsDefault: Boolean; @@ -80,7 +81,6 @@ TSimbaSettings = class type TSettingList = specialize TFPGObjectList; protected - FChangeEventList: TMethodList; FList: TSettingList; FFirstLaunch: Boolean; public @@ -170,14 +170,9 @@ TSimbaSettings = class procedure Load; procedure Save; - procedure RegisterChangeHandler(Event: TSimbaSettingChangedEvent); overload; deprecated; - procedure UnRegisterChangeHandler(Event: TSimbaSettingChangedEvent); overload; - procedure RegisterChangeHandler(Owner: TComponent; Setting: TSimbaSetting; Event: TSimbaSettingChangedEvent; CallEventInitially: Boolean = False); overload; procedure UnRegisterChangeHandler(Owner: TComponent; Setting: TSimbaSetting; Event: TSimbaSettingChangedEvent); overload; - procedure Changed(Setting: TSimbaSetting); - constructor Create; destructor Destroy; override; end; @@ -283,11 +278,12 @@ function TSimbaSetting.GetName: String; procedure TSimbaSetting.SetValue(AValue: Variant); begin CheckValue(AValue); - if (AValue = FValue) then - Exit; - FValue := AValue; - FSettings.Changed(Self); + if (AValue <> FValue) then + begin + FValue := AValue; + Changed(); + end; end; constructor TSimbaSetting.Create(ASettings: TSimbaSettings; ASection, AName: String; DefaultValue: Variant); @@ -313,42 +309,25 @@ destructor TSimbaSetting.Destroy; inherited Destroy(); end; -procedure TSimbaSetting.SetDefault; -begin - Value := FDefaultValue; -end; - -function TSimbaSetting.IsDefault: Boolean; -begin - Result := Value = FDefaultValue; -end; - -procedure TSimbaSettings.Changed(Setting: TSimbaSetting); +procedure TSimbaSetting.Changed; var - i: Integer; + I: Integer; begin - if (FChangeEventList = nil) or (FChangeEventList.Count = 0) then - Exit; - - i := Setting.FChangeEventList.Count; - while Setting.FChangeEventList.NextDownIndex(i) do - TSimbaSettingChangedEvent(Setting.FChangeEventList.Items[i])(Setting); - - DebugLn('[TSimbaSettings.Changed] Setting changed: ' + Setting.Name); + DebugLn('[TSimbaSettings] Setting changed: ' + FName); - i := FChangeEventList.Count; - while FChangeEventList.NextDownIndex(i) do - TSimbaSettingChangedEvent(FChangeEventList.Items[i])(Setting); + I := FChangeEventList.Count; + while FChangeEventList.NextDownIndex(I) do + TSimbaSettingChangedEvent(FChangeEventList.Items[I])(Self); end; -procedure TSimbaSettings.RegisterChangeHandler(Event: TSimbaSettingChangedEvent); +procedure TSimbaSetting.SetDefault; begin - FChangeEventList.Add(TMethod(Event)); + Value := FDefaultValue; end; -procedure TSimbaSettings.UnRegisterChangeHandler(Event: TSimbaSettingChangedEvent); +function TSimbaSetting.IsDefault: Boolean; begin - FChangeEventList.Remove(TMethod(Event)); + Result := Value = FDefaultValue; end; type @@ -488,7 +467,6 @@ constructor TSimbaSettings.Create; FFirstLaunch := True; FList := TSettingList.Create(); - FChangeEventList := TMethodList.Create(); // General General.ConsoleVisible := TSimbaSetting_Boolean.Create(Self, 'General', 'ConsoleVisible', True); @@ -560,8 +538,6 @@ constructor TSimbaSettings.Create; destructor TSimbaSettings.Destroy; begin - if (FChangeEventList <> nil) then - FreeAndNil(FChangeEventList); if (FList <> nil) then FreeAndNil(FList);