mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-12 09:39:16 +02:00
IDE: Optimize KeyMappings, add a cache for commands and eliminate a linear search.
git-svn-id: trunk@47124 -
This commit is contained in:
parent
10f31d95f1
commit
fad5ba7edc
@ -32,11 +32,9 @@ unit KeyMapping;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
LCLIntf, LCLType, LCLProc, AvgLvlTree, Laz2_XMLCfg,
|
Classes, SysUtils, contnrs, Forms, LCLType, LCLProc, AvgLvlTree, Laz2_XMLCfg,
|
||||||
Forms, Classes, SysUtils, Buttons, LResources, Controls, contnrs,
|
|
||||||
Dialogs, StringHashList, ExtCtrls,
|
|
||||||
SynEditKeyCmds, SynPluginTemplateEdit, SynPluginSyncroEdit,
|
SynEditKeyCmds, SynPluginTemplateEdit, SynPluginSyncroEdit,
|
||||||
PropEdits, IDECommands, LazarusIDEStrConsts, Debugger;
|
IDECommands, LazarusIDEStrConsts, Debugger;
|
||||||
|
|
||||||
type
|
type
|
||||||
TKeyMapScheme = (
|
TKeyMapScheme = (
|
||||||
@ -152,10 +150,12 @@ type
|
|||||||
TKeyCommandRelationList = class(TIDECommands)
|
TKeyCommandRelationList = class(TIDECommands)
|
||||||
private
|
private
|
||||||
fLastKey: TIDEShortCut; // for multiple key commands
|
fLastKey: TIDEShortCut; // for multiple key commands
|
||||||
fRelations: TList; // list of TKeyCommandRelation, sorted with Command
|
fRelations: TFPList; // list of TKeyCommandRelation
|
||||||
fCategories: TList;// list of TKeyCommandCategory
|
fCategories: TFPList; // list of TKeyCommandCategory
|
||||||
fExtToolCount: integer;
|
fExtToolCount: integer;
|
||||||
fLoadedKeyCommands: TAvgLvlTree;// tree of TLoadedKeyCommand sorted for name
|
fLoadedKeyCommands: TAvgLvlTree; // tree of TLoadedKeyCommand sorted for name
|
||||||
|
fCmdRelCache: TAvgLvlTree; // cache for TKeyCommandRelation sorted for command
|
||||||
|
function AddRelation(CmdRel: TKeyCommandRelation): Integer;
|
||||||
function GetRelation(Index: integer): TKeyCommandRelation;
|
function GetRelation(Index: integer): TKeyCommandRelation;
|
||||||
function GetRelationCount: integer;
|
function GetRelationCount: integer;
|
||||||
function AddCategory(const Name, Description: string;
|
function AddCategory(const Name, Description: string;
|
||||||
@ -273,6 +273,26 @@ begin
|
|||||||
if (i and 16)<>0 then include(Result,ssSuper);
|
if (i and 16)<>0 then include(Result,ssSuper);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
// Compare functions for fCmdRelCache
|
||||||
|
function CompareCmdRels(Data1, Data2: Pointer): integer;
|
||||||
|
var
|
||||||
|
Key1: TKeyCommandRelation absolute Data1;
|
||||||
|
Key2: TKeyCommandRelation absolute Data2;
|
||||||
|
begin
|
||||||
|
Result:=Key1.Command - Key2.Command;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function CompareCmdWithCmdRel(aCommand, Key: Pointer): integer;
|
||||||
|
var
|
||||||
|
Cmd1, Cmd2: PtrInt;
|
||||||
|
CmdRel: TKeyCommandRelation absolute Key;
|
||||||
|
begin
|
||||||
|
Pointer(Cmd1):=aCommand;
|
||||||
|
Cmd2:=CmdRel.Command;
|
||||||
|
Result:=Cmd1-Cmd2;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Compare functions for fLoadedKeyCommands
|
||||||
function CompareLoadedKeyCommands(Data1, Data2: Pointer): integer;
|
function CompareLoadedKeyCommands(Data1, Data2: Pointer): integer;
|
||||||
var
|
var
|
||||||
Key1: TLoadedKeyCommand absolute Data1;
|
Key1: TLoadedKeyCommand absolute Data1;
|
||||||
@ -2417,10 +2437,11 @@ end;
|
|||||||
constructor TKeyCommandRelationList.Create;
|
constructor TKeyCommandRelationList.Create;
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
FRelations:=TList.Create;
|
FRelations:=TFPList.Create;
|
||||||
fCategories:=TList.Create;
|
fCategories:=TFPList.Create;
|
||||||
fExtToolCount:=0;
|
fExtToolCount:=0;
|
||||||
fLoadedKeyCommands:=TAvgLvlTree.Create(@CompareLoadedKeyCommands);
|
fLoadedKeyCommands:=TAvgLvlTree.Create(@CompareLoadedKeyCommands);
|
||||||
|
fCmdRelCache:=TAvgLvlTree.Create(@CompareCmdRels);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TKeyCommandRelationList.Destroy;
|
destructor TKeyCommandRelationList.Destroy;
|
||||||
@ -2428,6 +2449,7 @@ begin
|
|||||||
Clear;
|
Clear;
|
||||||
FRelations.Free;
|
FRelations.Free;
|
||||||
fCategories.Free;
|
fCategories.Free;
|
||||||
|
fCmdRelCache.Free;
|
||||||
fLoadedKeyCommands.Free;
|
fLoadedKeyCommands.Free;
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
@ -2984,11 +3006,18 @@ begin
|
|||||||
for a:=0 to FRelations.Count-1 do
|
for a:=0 to FRelations.Count-1 do
|
||||||
Relations[a].Free;
|
Relations[a].Free;
|
||||||
FRelations.Clear;
|
FRelations.Clear;
|
||||||
|
fCmdRelCache.Clear;
|
||||||
for a:=0 to fCategories.Count-1 do
|
for a:=0 to fCategories.Count-1 do
|
||||||
Categories[a].Free;
|
Categories[a].Free;
|
||||||
fCategories.Clear;
|
fCategories.Clear;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TKeyCommandRelationList.AddRelation(CmdRel: TKeyCommandRelation): Integer;
|
||||||
|
begin
|
||||||
|
Result := FRelations.Add(CmdRel);
|
||||||
|
fCmdRelCache.Add(CmdRel);
|
||||||
|
end;
|
||||||
|
|
||||||
function TKeyCommandRelationList.GetRelation(Index:integer):TKeyCommandRelation;
|
function TKeyCommandRelationList.GetRelation(Index:integer):TKeyCommandRelation;
|
||||||
begin
|
begin
|
||||||
Assert((Index>=0) and (Index<Count), Format('[TKeyCommandRelationList.GetRelation] '
|
Assert((Index>=0) and (Index<Count), Format('[TKeyCommandRelationList.GetRelation] '
|
||||||
@ -3041,7 +3070,7 @@ begin
|
|||||||
CmdRel.DefaultShortcutA:=CmdRel.ShortcutA;
|
CmdRel.DefaultShortcutA:=CmdRel.ShortcutA;
|
||||||
CmdRel.DefaultShortcutB:=CmdRel.ShortcutB;
|
CmdRel.DefaultShortcutB:=CmdRel.ShortcutB;
|
||||||
SetKeyCommandToLoadedValues(CmdRel);
|
SetKeyCommandToLoadedValues(CmdRel);
|
||||||
Result:=FRelations.Add(CmdRel);
|
Result:=AddRelation(CmdRel);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TKeyCommandRelationList.SetExtToolCount(NewCount: integer);
|
procedure TKeyCommandRelationList.SetExtToolCount(NewCount: integer);
|
||||||
@ -3063,7 +3092,7 @@ begin
|
|||||||
CmdRel:=TKeyCommandRelation.Create(ExtToolCat,
|
CmdRel:=TKeyCommandRelation.Create(ExtToolCat,
|
||||||
Format('External tool %d',[fExtToolCount]), // keep name untranslated
|
Format('External tool %d',[fExtToolCount]), // keep name untranslated
|
||||||
ToolLocalizedName, cmd);
|
ToolLocalizedName, cmd);
|
||||||
FRelations.Add(CmdRel);
|
AddRelation(CmdRel);
|
||||||
inc(fExtToolCount);
|
inc(fExtToolCount);
|
||||||
end;
|
end;
|
||||||
end else begin
|
end else begin
|
||||||
@ -3368,12 +3397,13 @@ end;
|
|||||||
|
|
||||||
function TKeyCommandRelationList.FindByCommand(ACommand: word): TKeyCommandRelation;
|
function TKeyCommandRelationList.FindByCommand(ACommand: word): TKeyCommandRelation;
|
||||||
var
|
var
|
||||||
i: integer;
|
AVLNode: TAvgLvlTreeNode;
|
||||||
begin
|
begin
|
||||||
Result:=nil;
|
AVLNode:=fCmdRelCache.FindKey(Pointer(PtrUInt(ACommand)), @CompareCmdWithCmdRel);
|
||||||
for i:=0 to FRelations.Count-1 do
|
if Assigned(AVLNode) then
|
||||||
if (Relations[i].Command=ACommand) then
|
Result:=TKeyCommandRelation(AVLNode.Data)
|
||||||
Exit(Relations[i]);
|
else
|
||||||
|
Result:=nil;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Command compare functions for AvgLvlTree for fast lookup.
|
// Command compare functions for AvgLvlTree for fast lookup.
|
||||||
@ -3596,7 +3626,7 @@ begin
|
|||||||
//DebugLn('TKeyCommandRelationList.Assign Add new command: ',OtherRelation.Name);
|
//DebugLn('TKeyCommandRelationList.Assign Add new command: ',OtherRelation.Name);
|
||||||
OurCategory:=FindCategoryByName(OtherRelation.Category.Name);
|
OurCategory:=FindCategoryByName(OtherRelation.Category.Name);
|
||||||
OurRelation:=TKeyCommandRelation.Create(OtherRelation,OurCategory);
|
OurRelation:=TKeyCommandRelation.Create(OtherRelation,OurCategory);
|
||||||
fRelations.Add(OurRelation);
|
AddRelation(OurRelation);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -3678,7 +3708,7 @@ begin
|
|||||||
NewName, Description, cmd,
|
NewName, Description, cmd,
|
||||||
TheShortcutA, TheShortcutB, OnExecuteMethod, OnExecuteProc);
|
TheShortcutA, TheShortcutB, OnExecuteMethod, OnExecuteProc);
|
||||||
SetKeyCommandToLoadedValues(CmdRel);
|
SetKeyCommandToLoadedValues(CmdRel);
|
||||||
FRelations.Add(CmdRel);
|
AddRelation(CmdRel);
|
||||||
Result:=CmdRel;
|
Result:=CmdRel;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -3700,8 +3730,7 @@ end;
|
|||||||
function TKeyCommandRelationList.AddCategory(const Name, Description: string;
|
function TKeyCommandRelationList.AddCategory(const Name, Description: string;
|
||||||
TheScope: TIDECommandScope): integer;
|
TheScope: TIDECommandScope): integer;
|
||||||
begin
|
begin
|
||||||
Result:=fCategories.Add(TKeyCommandCategory.Create(Name,Description,
|
Result:=fCategories.Add(TKeyCommandCategory.Create(Name,Description,TheScope));
|
||||||
TheScope));
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TKeyCommandRelationList.FindCategoryByName(const CategoryName: string): TIDECommandCategory;
|
function TKeyCommandRelationList.FindCategoryByName(const CategoryName: string): TIDECommandCategory;
|
||||||
@ -3747,8 +3776,7 @@ begin
|
|||||||
if (IDEWindowClass<>nil)
|
if (IDEWindowClass<>nil)
|
||||||
and (Category.Scope<>nil)
|
and (Category.Scope<>nil)
|
||||||
and (not Category.Scope.HasIDEWindowClass(IDEWindowClass)) then continue;
|
and (not Category.Scope.HasIDEWindowClass(IDEWindowClass)) then continue;
|
||||||
if KeyFits(ShortcutA) or KeyFits(ShortcutB)
|
if KeyFits(ShortcutA) or KeyFits(ShortcutB) then
|
||||||
then
|
|
||||||
Result.Add(Relations[i]);
|
Result.Add(Relations[i]);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user