Skip to content

Commit

Permalink
Add function for setting sheet active.
Browse files Browse the repository at this point in the history
  • Loading branch information
anmcgrath committed Oct 14, 2024
1 parent a20bc4a commit 7415cfa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/BlazorDatasheet.SharedPages/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@using BlazorDatasheet.Core.Validation
@using System.Drawing
@using BlazorDatasheet.Core.Commands.Data
@using BlazorDatasheet.Events

<PageTitle>Index</PageTitle>

Expand Down Expand Up @@ -43,6 +44,7 @@
<button @onclick="@SortSelectionDesc">Sort Desc</button>
<button @onclick="@SortSelectionAsc">Sort ASc</button>
<button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat() { NumberFormat = "C2" }))">Currency</button>
<button @onclick="@(() => _datasheet.SetActiveAsync(true))">Set sheet active</button>
</div>

<div>
Expand Down
26 changes: 22 additions & 4 deletions src/BlazorDatasheet/Datasheet.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public partial class Datasheet : SheetComponentBase
public bool CanUserInsertRows { get; set; } = true;

/// <summary>
/// If set to true, the user can insert columns using the context menu.
/// If set to true, the user can insert columns using the context menu.f
/// </summary>
[Parameter]
public bool CanUserInsertCols { get; set; } = true;
Expand Down Expand Up @@ -134,6 +134,11 @@ public partial class Datasheet : SheetComponentBase

[Parameter] public bool ShowFormulaDependents { get; set; }

/// <summary>
/// Fired when the Datasheet becomes active or inactive (able to receive keyboard inputs).
/// </summary>
[Parameter] public EventCallback<SheetActiveEventArgs> OnSheetActiveChanged { get; set; }

/// <summary>
/// Exists so that we can determine whether the sheet has changed
/// during parameter set
Expand Down Expand Up @@ -529,15 +534,15 @@ private void HandleCellMouseOver(object? sender, SheetPointerEventArgs args)
this.UpdateSelectingEndPosition(args.Row, args.Col);
}

private Task<bool> HandleWindowMouseDown(MouseEventArgs e)
private async Task<bool> HandleWindowMouseDown(MouseEventArgs e)
{
bool changed = IsDataSheetActive != IsMouseInsideSheet;
IsDataSheetActive = IsMouseInsideSheet;
await SetActiveAsync(IsMouseInsideSheet);

if (changed)
StateHasChanged();

return Task.FromResult(false);
return false;
}

private Task<bool> HandleWindowMouseUp(MouseEventArgs arg)
Expand Down Expand Up @@ -812,4 +817,17 @@ private RenderFragment GetIconRenderFragment(string? cellIcon)
return rf;
return _ => { };
}

/// <summary>
/// Set the datasheet as active, which controls whether the sheet is ready to receive keyboard input events.
/// </summary>
/// <param name="value"></param>
public async Task SetActiveAsync(bool value = true)
{
if(value == IsDataSheetActive)
return;

IsDataSheetActive = value;
await OnSheetActiveChanged.InvokeAsync(new SheetActiveEventArgs(this, value));
}
}
13 changes: 13 additions & 0 deletions src/BlazorDatasheet/Events/SheetActiveEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BlazorDatasheet.Events;

public class SheetActiveEventArgs
{
public Datasheet DataSheet { get; }
public bool IsActive { get; }

public SheetActiveEventArgs(Datasheet dataSheet, bool isActive)
{
DataSheet = dataSheet;
IsActive = isActive;
}
}

0 comments on commit 7415cfa

Please sign in to comment.