Skip to content

Commit

Permalink
Main menu shortcuts should only fire if a editor is focused
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 5, 2023
1 parent e0b25c8 commit 1acb522
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
10 changes: 4 additions & 6 deletions Source/forms/simba.main.lfm
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
object SimbaForm: TSimbaForm
Left = 3003
Left = 4007
Height = 539
Top = 557
Top = 508
Width = 1015
Caption = 'Simba'
ClientHeight = 0
ClientWidth = 0
ClientHeight = 539
ClientWidth = 1015
DesignTimePPI = 120
KeyPreview = True
OnClose = FormClose
OnDestroy = FormDestroy
OnKeyDown = FormKeyDown
OnWindowStateChange = FormWindowStateChange
Position = poScreenCenter
LCLVersion = '3.0.0.2'
Expand Down
32 changes: 19 additions & 13 deletions Source/forms/simba.main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ TSimbaForm = class(TForm)

procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormWindowStateChange(Sender: TObject);
procedure ImagesGetWidthForPPI(Sender: TCustomImageList; AImageWidth, APPI: Integer; var AResultWidth: Integer);
procedure MainMenuMeasureItem(Sender: TObject; ACanvas: TCanvas; var AWidth, AHeight: Integer);
Expand Down Expand Up @@ -239,6 +238,9 @@ TSimbaForm = class(TForm)
procedure HandleException(Sender: TObject; E: Exception);
procedure HandleFormCreated(Sender: TObject; Form: TCustomForm);

// Handle main menu shortcuts if editor is focused
procedure DoApplicationKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

procedure DoTabLoaded(Sender: TObject);

procedure SetCustomFontSize(Value: Integer);
Expand Down Expand Up @@ -270,7 +272,7 @@ implementation
simba.dockinghelpers, simba.nativeinterface,
simba.scriptformatter, simba.theme,
simba.scriptbackup, simba.backupsform, simba.threading,
simba.downloadsimbaform;
simba.downloadsimbaform, simba.editor;

procedure TSimbaForm.HandleException(Sender: TObject; E: Exception);

Expand Down Expand Up @@ -547,6 +549,7 @@ procedure TSimbaForm.Setup;

Application.CaptureExceptions := True;
Application.OnException := @Self.HandleException;
Application.AddOnKeyDownBeforeHandler(@DoApplicationKeyDown);

Screen.AddHandlerFormAdded(@Self.HandleFormCreated, True);

Expand Down Expand Up @@ -589,20 +592,23 @@ procedure TSimbaForm.FormDestroy(Sender: TObject);
SimbaSettings.Save();
end;

procedure TSimbaForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure TSimbaForm.DoApplicationKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
Msg: TLMKey;
begin
Msg := Default(TLMKey);
Msg.CharCode := Key;
if (ssAlt in Shift) then
Msg.KeyData := MK_ALT;

if MainMenuFile.IsShortcut(Msg) or MainMenuView.IsShortcut(Msg) or
MainMenuEdit.IsShortcut(Msg) or MainMenuScript.IsShortcut(Msg) or
MainMenuTools.IsShortcut(Msg) or MainMenuHelp.IsShortcut(Msg) or
MainMenuSearch.IsShortcut(Msg) then
Key := 0;
if (Screen.ActiveControl is TSimbaEditor) then
begin
Msg := Default(TLMKey);
Msg.CharCode := Key;
if (ssAlt in Shift) then
Msg.KeyData := MK_ALT;

if MainMenuFile.IsShortcut(Msg) or MainMenuView.IsShortcut(Msg) or
MainMenuEdit.IsShortcut(Msg) or MainMenuScript.IsShortcut(Msg) or
MainMenuTools.IsShortcut(Msg) or MainMenuHelp.IsShortcut(Msg) or
MainMenuSearch.IsShortcut(Msg) then
Key := 0;
end;
end;

procedure TSimbaForm.FormWindowStateChange(Sender: TObject);
Expand Down

0 comments on commit 1acb522

Please sign in to comment.