Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Butil/Bit.Butil/BitButil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public static IServiceCollection AddBitButilServices(this IServiceCollection ser

internal static bool FastInvokeEnabled { get; private set; }

/// <summary>
/// Enables the use of the fast APIs globally when available (Invoke methods of IJSInProcessRuntime).
/// </summary>
public static void UseFastInvoke()
{
FastInvokeEnabled = true;
}

/// <summary>
/// Disables the use of the fast APIs globally when available (Invoke methods of IJSInProcessRuntime).
/// </summary>
public static void UseNormalInvoke()
{
FastInvokeEnabled = false;
Expand Down
30 changes: 23 additions & 7 deletions src/Butil/Bit.Butil/Publics/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Bit.Butil;
/// <br />
/// More info: <see href="https://developer.mozilla.org/en-US/docs/Web/API/Window">https://developer.mozilla.org/en-US/docs/Web/API/Window</see>
/// </summary>
public class Window(IJSRuntime js)
public class Window(IJSRuntime js) : IAsyncDisposable
{
private const string ElementName = "window";

Expand Down Expand Up @@ -179,8 +179,9 @@ public async Task<string> Btoa(string data)
/// <br/>
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Window/close">https://developer.mozilla.org/en-US/docs/Web/API/Window/close</see>
/// </summary>
public async Task Close()
=> await js.InvokeVoid("BitButil.window.close");
public async Task Close(string? id = null)
=> await (id is null ? js.InvokeVoid("BitButil.window.close")
: js.InvokeVoid("BitButil.window.close", id));

/// <summary>
/// Displays a dialog with a message that the user needs to respond to.
Expand Down Expand Up @@ -232,15 +233,15 @@ public async Task<MediaQueryList> MatchMedia(string query)
/// <br/>
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Window/open">https://developer.mozilla.org/en-US/docs/Web/API/Window/open</see>
/// </summary>
public async Task<bool> Open(string? url = null, string? target = null, string? windowFeatures = null)
=> await js.Invoke<bool>("BitButil.window.open", url, target, windowFeatures);
public async Task<string> Open(string? url = null, string? target = null, string? windowFeatures = null)
=> await js.Invoke<string>("BitButil.window.open", Guid.NewGuid(), url, target, windowFeatures);
/// <summary>
/// Opens a new window.
/// <br/>
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Window/open">https://developer.mozilla.org/en-US/docs/Web/API/Window/open</see>
/// </summary>
public async Task<bool> Open(string? url = null, string? target = null, WindowFeatures? windowFeatures = null)
=> await js.Invoke<bool>("BitButil.window.open", url, target, windowFeatures?.ToString());
public async Task<string> Open(string? url = null, string? target = null, WindowFeatures? windowFeatures = null)
=> await js.Invoke<string>("BitButil.window.open", Guid.NewGuid(), url, target, windowFeatures?.ToString());

/// <summary>
/// Opens the Print Dialog to print the current document.
Expand Down Expand Up @@ -295,4 +296,19 @@ public async Task ScrollBy(float? x, float? y)
/// </summary>
public async Task Stop()
=> await js.InvokeVoid("BitButil.window.stop");

public async ValueTask DisposeAsync()
{
await DisposeAsync(true);

GC.SuppressFinalize(this);
}

protected virtual async ValueTask DisposeAsync(bool disposing)
{
if (disposing)
{
await js.InvokeVoid("BitButil.window.dispose");
}
}
}
27 changes: 22 additions & 5 deletions src/Butil/Bit.Butil/Scripts/window.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var BitButil = BitButil || {};

(function (butil: any) {
let _refs = {};

butil.window = {
addBeforeUnload,
removeBeforeUnload,
Expand All @@ -21,7 +23,7 @@ var BitButil = BitButil || {};
alert(message?: string) { window.alert(message) },
blur() { window.blur() },
btoa(data: string) { return window.btoa(data) },
close() { window.close() },
close,
confirm(message?: string) { return window.confirm(message) },
find,
focus() { window.focus() },
Expand All @@ -32,7 +34,8 @@ var BitButil = BitButil || {};
prompt(message?: string, defaultValue?: string) { return window.prompt(message, defaultValue) },
scroll,
scrollBy,
stop() { window.stop() }
stop() { window.stop() },
dispose
};

function addBeforeUnload() {
Expand All @@ -47,6 +50,15 @@ var BitButil = BitButil || {};
window.onbeforeunload = null;
}

function close(id: string | undefined) {
if (!id) return window.close();

const ref = _refs[id];
if (!ref) return;
delete _refs[id];
return ref.close();
}

function find(text?: string,
caseSensitive?: boolean,
backward?: boolean,
Expand All @@ -64,9 +76,10 @@ var BitButil = BitButil || {};
};
}

function open(url?: string, target?: string, windowFeatures?: string) {
const result = window.open(url, target, windowFeatures);
return !!result;
function open(id: string, url?: string, target?: string, windowFeatures?: string) {
const ref = window.open(url, target, windowFeatures);
_refs[id] = ref;
return id;
}

function scroll(options?: ScrollToOptions, x?: number, y?: number) {
Expand All @@ -84,4 +97,8 @@ var BitButil = BitButil || {};
window.scrollBy(x, y);
}
}

function dispose() {
_refs = {};
}
}(BitButil));
15 changes: 13 additions & 2 deletions src/Butil/Demo/Bit.Butil.Demo.Core/Pages/WindowPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
<br />

<button @onclick=OpenWindow>OpenWindow</button>
&nbsp;
<button @onclick=CloseWindow>CloseWindow</button>
<br />
<br />
<button @onclick=OpenPrint>OpenPrint</button>

<br />
Expand Down Expand Up @@ -356,10 +358,19 @@ Y: <input type="number" @bind-value="scrollByY"/>
await window.Prompt("Prompt from C#", string.Empty);
}

private List<string> windowIds = [];
private async Task OpenWindow()
{
var windowFeatures = new WindowFeatures() { Popup = true, Width = 848, Height = 568 };
await window.Open("/document", "popupWindow", windowFeatures);
windowIds.Add(await window.Open("/document", "_blank", windowFeatures));
}

private async Task CloseWindow()
{
if (windowIds.Count == 0) return;
var id = windowIds[^1];
await window.Close(id);
windowIds.Remove(id);
}

private async Task OpenPrint()
Expand Down
Loading