Skip to content

Commit

Permalink
Better TSimbaDropToolButton
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 7, 2023
1 parent 2560876 commit b8557b5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
49 changes: 12 additions & 37 deletions Source/components/simba.component_toolbar.pas
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TSimbaToolbar = class(TCustomControl)
constructor Create(AOwner: TComponent); override;

function AddButton(ImageIndex: Integer; HintText: String = ''; AOnClick: TNotifyEvent = nil): TSimbaTransparentButton;
function AddDropdownButton(ImageIndex: Integer; HintText: String = ''; AOnClick: TNotifyEvent = nil; APopupMenu: TPopupMenu = nil): TSimbaTransparentButton;
function AddDropdownButton(HintText: String = ''; APopupMenu: TPopupMenu = nil): TSimbaTransparentButton;
function AddDivider: TSimbaTransparentButton;

property Spacing: Integer read GetSpacing write SetSpacing;
Expand Down Expand Up @@ -67,11 +67,9 @@ TSimbaDropToolButton = class(TSimbaToolButton)
FMouseInDropdownArrow: Boolean;

function ScaleToToolbarSize(Value: Integer): Integer;
function GetDropdownArrowRect: TRect;

procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; WithThemeSpace: Boolean); override;
procedure PaintBackground(var PaintRect: TRect); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
public
DropdownMenu: TPopupMenu;

Expand All @@ -89,50 +87,27 @@ function TSimbaDropToolButton.ScaleToToolbarSize(Value: Integer): Integer;
Result := Value;
end;

function TSimbaDropToolButton.GetDropdownArrowRect: TRect;
begin
Result.Left := Width - ScaleToToolbarSize(10);
Result.Right := Width - 2;
Result.Top := 3;
Result.Bottom := Height - 3;
end;

procedure TSimbaDropToolButton.CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; WithThemeSpace: Boolean);
begin
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight, WithThemeSpace);

PreferredWidth := PreferredWidth + ScaleToToolbarSize(10);
Margin := ScaleToToolbarSize(5);
PreferredWidth := ScaleToToolbarSize(10);
end;

procedure TSimbaDropToolButton.PaintBackground(var PaintRect: TRect);
begin
inherited PaintBackground(PaintRect);

CanvasPaintTriangleDown(Canvas, SimbaTheme.ColorFont, GetDropdownArrowRect().CenterPoint, IfThen(FToolbar.FButtonSize >= 20, 2, 1));
//Canvas.Pen.Color := 255;
//Canvas.Frame(GetDropdownArrowRect());
end;

procedure TSimbaDropToolButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);

FMouseInDropdownArrow := GetDropdownArrowRect().Contains(TPoint.Create(X, Y));
CanvasPaintTriangleDown(Canvas, SimbaTheme.ColorFont, PaintRect.CenterPoint, IfThen(FToolbar.FButtonSize >= 20, 2, 1));
end;

procedure TSimbaDropToolButton.Click;
begin
if FMouseInDropdownArrow then
begin
if (DropdownMenu <> nil) then
with ClientToScreen(TPoint.Create(GetDropdownArrowRect().CenterPoint.X, Height)) do
DropdownMenu.PopUp(X, Y);

Exit;
end;

inherited Click();

if (DropdownMenu <> nil) then
with ClientToScreen(TPoint.Create(0,Height)) do
DropDownMenu.Popup(X, Y);
end;

procedure TSimbaToolButton.CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; WithThemeSpace: Boolean);
Expand All @@ -147,7 +122,10 @@ constructor TSimbaToolButton.Create(AOwner: TComponent);

FToolBar := AOwner as TSimbaToolBar;

BorderSpacing.Around := 2;
BorderSpacing.Left := 2;
BorderSpacing.Right := 2;
BorderSpacing.Top := 2;
BorderSpacing.Bottom := 2;
end;

procedure TSimbaToolButtonDivider.CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; WithThemeSpace: Boolean);
Expand Down Expand Up @@ -222,15 +200,12 @@ function TSimbaToolbar.AddButton(ImageIndex: Integer; HintText: String; AOnClick
Result.OnClick := AOnClick;
end;

function TSimbaToolbar.AddDropdownButton(ImageIndex: Integer; HintText: String; AOnClick: TNotifyEvent; APopupMenu: TPopupMenu): TSimbaTransparentButton;
function TSimbaToolbar.AddDropdownButton(HintText: String; APopupMenu: TPopupMenu): TSimbaTransparentButton;
begin
Result := TSimbaDropToolButton.Create(Self);
Result.Parent := FFlowPanel;
Result.Images := FImages;
Result.ImageIndex := ImageIndex;
Result.Hint := HintText;
Result.ShowHint := HintText <> '';
Result.OnClick := AOnClick;

TSimbaDropToolButton(Result).DropdownMenu := APopupMenu;
end;
Expand Down
9 changes: 9 additions & 0 deletions Source/forms/simba.main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,18 @@ procedure TSimbaForm.FormDestroy(Sender: TObject);
end;

procedure TSimbaForm.DoApplicationKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

function isEditorKeyStroke: Boolean;
begin
Result := (Screen.ActiveControl is TSimbaEditor) and (TSimbaEditor(Screen.ActiveControl).Keystrokes.FindKeycode(Key, Shift) > -1);
end;

var
Msg: TLMKey;
begin
if isEditorKeyStroke() then
Exit;

if (Screen.ActiveControl is TSimbaEditor) or (Screen.ActiveControl is TSimbaMenuBar) then
begin
Msg := Default(TLMKey);
Expand Down
6 changes: 5 additions & 1 deletion Source/simba.ide_maintoolbar.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ TSimbaMainToolBar = class(TComponent)

FButtonNew: TSimbaButton;
FButtonOpen: TSimbaButton;
FButtonOpenDrop: TSimbaButton;
FButtonSave: TSimbaButton;
FButtonSaveAll: TSimbaButton;

Expand Down Expand Up @@ -266,7 +267,10 @@ constructor TSimbaMainToolBar.Create;
FToolBar.Align := alClient;

FButtonNew := FToolBar.AddButton(IMG_NEW, 'New File (Ctrl + N)', @DoClickFileButton);
FButtonOpen := FToolBar.AddDropdownButton(IMG_OPEN, 'Open File (Ctrl + O)', @DoClickFileButton, SimbaForm.RecentFilesPopup);
FButtonOpen := FToolBar.AddButton(IMG_OPEN, 'Open File (Ctrl + O)', @DoClickFileButton);
FButtonOpen.BorderSpacing.Right := 0;
FButtonOpenDrop := FToolBar.AddDropdownButton('Open Recent File', SimbaForm.RecentFilesPopup);
FButtonOpenDrop.BorderSpacing.Left := 0;
FButtonSave := FToolBar.AddButton(IMG_SAVE, 'Save Script (Ctrl + S)', @DoClickFileButton);
FButtonSaveAll := FToolBar.AddButton(IMG_SAVE_ALL, 'Save All', @DoClickFileButton);

Expand Down

0 comments on commit b8557b5

Please sign in to comment.