Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of Open In New Window #1053

Merged
merged 4 commits into from
Feb 17, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using DevToys.Blazor.BuiltInTools.Settings;
using DevToys.Core;
using NuGet.Packaging;
using NuGet.Packaging.Core;

Expand Down Expand Up @@ -131,13 +132,13 @@ public void OnDataReceived(string dataTypeName, object? parsedData)
private void OnFindMoreExtensionsOnlineButtonClick()
{
// TODO: Open documentation online
Shell.OpenFileInShell("http://url");
OSHelper.OpenFileInShell("http://url");
}

private void OnLearnDevelopExtensionButtonClick()
{
// TODO: Open documentation online
Shell.OpenFileInShell("http://url");
OSHelper.OpenFileInShell("http://url");
}

private async ValueTask OnInstallExtensionButtonClickAsync()
Expand Down Expand Up @@ -275,7 +276,7 @@ IUIButton uninstallButton

static void NavigateToUrl(string url)
{
Shell.OpenFileInShell(url);
OSHelper.OpenFileInShell(url);
}
}

Expand Down Expand Up @@ -318,7 +319,7 @@ await _view.Value.OpenDialogAsync(
void OnTermsConditionsDialogButtonClick()
{
// TODO: Open the terms and conditions page.
Shell.OpenFileInShell("https://url");
OSHelper.OpenFileInShell("https://url");
}

void OnIUnderstandDialogButtonClick()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using DevToys.Blazor.Core.Languages;
using DevToys.Core;
using DevToys.Core.Settings;

namespace DevToys.Blazor.BuiltInTools.Settings;
Expand Down Expand Up @@ -224,7 +225,7 @@ public void OnDataReceived(string dataTypeName, object? parsedData)

private void OnHelpTranslatingButtonClick()
{
Shell.OpenFileInShell("https://crowdin.com/project/devtoys");
OSHelper.OpenFileInShell("https://crowdin.com/project/devtoys");
}

private void OnLanguageSelected(IUIDropDownListItem? selectedItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
@inherits StyledComponentBase

<CascadingValue Name="ParentIsEnabled" Value="@IsActuallyEnabled">
<li class="list-box-item @(FinalCssClasses)"
style="@(Style)"
role="listitem"
@ref=Element
@attributes="AdditionalAttributes"
@onclick="OnClickAsync">
@ChildContent
</li>
<ContextMenu IsEnabled="IsActuallyEnabled"
Items="_contextMenuItems"
OnContextMenuOpening=@OnContextMenuOpeningAsync>
<li class="list-box-item @(FinalCssClasses)"
style="@(Style)"
role="listitem"
@ref=Element
@attributes="AdditionalAttributes"
@onclick="OnClickAsync">
@ChildContent
</li>
</ContextMenu>
</CascadingValue>
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace DevToys.Blazor.Components;
using System.Collections.ObjectModel;

namespace DevToys.Blazor.Components;

public partial class ListBoxItem : StyledComponentBase
{
private readonly ICollection<ContextMenuItem> _contextMenuItems = new ObservableCollection<ContextMenuItem>();

[Parameter]
public object Item { get; set; } = default!;

Expand All @@ -17,6 +21,9 @@ public partial class ListBoxItem : StyledComponentBase
[Parameter]
public RenderFragment? ChildContent { get; set; }

[Parameter]
public EventCallback<ListBoxItemBuildingContextMenuEventArgs> OnBuildingContextMenu { get; set; }

protected override void OnParametersSet()
{
if (IsSelected)
Expand Down Expand Up @@ -44,4 +51,9 @@ private Task OnClickAsync()
{
return OnSelected.InvokeAsync(Item);
}

private Task OnContextMenuOpeningAsync()
{
return OnBuildingContextMenu.InvokeAsync(new ListBoxItemBuildingContextMenuEventArgs(Item, _contextMenuItems));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DevToys.Blazor.Components;

public sealed class ListBoxItemBuildingContextMenuEventArgs : EventArgs
{
internal ListBoxItemBuildingContextMenuEventArgs(object? item, ICollection<ContextMenuItem> items)
{
Guard.IsNotNull(items);
ItemValue = item;
ContextMenuItems = items;
}

internal object? ItemValue { get; }

internal ICollection<ContextMenuItem> ContextMenuItems { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private async Task OpenMenuAsync(MouseEventArgs ev)
if (IsActuallyEnabled)
{
// This may update the item list.
await OnContextMenuOpening.InvokeAsync();
if (OnContextMenuOpening.HasDelegate)
{
await OnContextMenuOpening.InvokeAsync();
}

if (Items?.Count == 0)
{
Expand All @@ -83,7 +86,10 @@ private async Task OpenMenuAsync(MouseEventArgs ev)
_isOpen = true;
StateHasChanged();

await OnContextMenuOpened.InvokeAsync();
if (OnContextMenuOpened.HasDelegate)
{
await OnContextMenuOpened.InvokeAsync();
}
}
}
}
Expand All @@ -95,7 +101,10 @@ private async Task CloseMenuAsync(MouseEventArgs? _)
_isOpen = false;
StateHasChanged();

await OnContextMenuClosed.InvokeAsync();
if (OnContextMenuClosed.HasDelegate)
{
await OnContextMenuClosed.InvokeAsync();
}
}

private async Task OnEscapeKeyPressedAsync()
Expand All @@ -119,7 +128,10 @@ private void OnContextMenuItemSelected(int itemIndex)
if (_listBox is not null && _listBox.SelectedItem is not null && _listBox.SelectedItem.IsEnabled)
{
CloseMenuAsync(null).Forget();
_listBox.SelectedItem.OnClick.InvokeAsync(_listBox.SelectedItem).Forget();
if (_listBox.SelectedItem.OnClick.HasDelegate)
{
_listBox.SelectedItem.OnClick.InvokeAsync(_listBox.SelectedItem).Forget();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
TitleTemplate="NavBarItemTitleTemplate"
IconTemplate="NavBarItemIconTemplate"
Children="item is IGroup itemGroup ? itemGroup.ChildrenItems : null"
OnSelected="OnItemSelectedAsync" />
OnSelected="OnItemSelectedAsync"
OnBuildingContextMenu="OnBuildingContextMenuAsync" />
}
}
</ul>
Expand All @@ -153,7 +154,8 @@
TitleTemplate="NavBarItemTitleTemplate"
IconTemplate="NavBarItemIconTemplate"
Children="item is IGroup itemGroup ? itemGroup.ChildrenItems : null"
OnSelected="OnItemSelectedAsync" />
OnSelected="OnItemSelectedAsync"
OnBuildingContextMenu="OnBuildingContextMenuAsync" />
}
}
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public partial class NavBar<TElement, TSearchElement>
[Parameter]
public EventCallback<TElement> SelectedItemChanged { get; set; }

[Parameter]
public EventCallback<ListBoxItemBuildingContextMenuEventArgs> OnBuildingContextMenu { get; set; }

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

Expand Down Expand Up @@ -204,6 +207,12 @@ private Task OnItemSelectedAsync(TElement item)
return SelectedItemChanged.InvokeAsync(item);
}

private Task OnBuildingContextMenuAsync(ListBoxItemBuildingContextMenuEventArgs args)
{
Guard.IsAssignableToType<TElement>(args.ItemValue);
return OnBuildingContextMenu.InvokeAsync(args);
}

private void SidebarState_IsHiddenChanged(object? sender, EventArgs e)
{
if (OnHiddenStateChanged.HasDelegate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
IsSelected="Item == OwnerNavBar.SelectedItem"
Class="@FinalCssClasses"
Style="margin: 3px 5px;"
OnBuildingContextMenu=@OnBuildingContextMenuAsync
OnSelected=@OnItemSelectedAsync>
<div class="sidebar-item-icon">
@IconTemplate(Item)
Expand Down Expand Up @@ -56,7 +57,8 @@
IconTemplate="IconTemplate"
Children="childItem is IGroup childItemGroup ? childItemGroup.ChildrenItems : null"
Class="sidebar-child-item"
OnSelected="OnItemSelectedAsync" />
OnSelected="OnItemSelectedAsync"
OnBuildingContextMenu="OnBuildingContextMenuAsync"/>
}
</ul>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public partial class NavBarItem<TElement, TSearchElement>
[Parameter]
public EventCallback<TElement> OnSelected { get; set; }

[Parameter]
public EventCallback<ListBoxItemBuildingContextMenuEventArgs> OnBuildingContextMenu { get; set; }

public bool IsExpanded
{
get
Expand Down Expand Up @@ -82,6 +85,15 @@ private Task OnItemSelectedAsync(object item)
return OnSelected.InvokeAsync(strongItem);
}

private Task OnBuildingContextMenuAsync(ListBoxItemBuildingContextMenuEventArgs args)
{
if (args.ItemValue is not null)
{
Guard.IsAssignableToType<TElement>(args.ItemValue);
}
return OnBuildingContextMenu.InvokeAsync(args);
}

private void OnToggleExpandClick()
{
if (IsExpanded)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TODO: Add logs.
using DevToys.Core;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;

Expand Down Expand Up @@ -66,15 +67,15 @@ private Task OnViewImageButtonClickAsync()
return UIImageViewer.ImageSource.Value.Match(
(FileInfo imageFileInfo) =>
{
Shell.OpenFileInShell(imageFileInfo.FullName);
OSHelper.OpenFileInShell(imageFileInfo.FullName);
return Task.CompletedTask;
},
async (Image image) =>
{
string? fileExtension = image.Metadata.DecodedImageFormat?.FileExtensions.FirstOrDefault() ?? "png";
FileInfo tempFile = _fileStorage.CreateSelfDestroyingTempFile(fileExtension);
await image.SaveAsync(tempFile.FullName);
Shell.OpenFileInShell(tempFile.FullName);
OSHelper.OpenFileInShell(tempFile.FullName);
},
async (SandboxedFileReader imagePickedFile) =>
{
Expand All @@ -83,7 +84,7 @@ private Task OnViewImageButtonClickAsync()
{
await imagePickedFile.CopyFileContentToAsync(tempFileStream, CancellationToken.None);
}
Shell.OpenFileInShell(tempFile.FullName);
OSHelper.OpenFileInShell(tempFile.FullName);
});
}

Expand Down
39 changes: 0 additions & 39 deletions src/app/dev/DevToys.Blazor/Core/Helpers/Shell.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/app/dev/DevToys.Blazor/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@bind-SelectedItem="ViewModel.SelectedMenuItem"
OnBackButtonClicked="OnBackButtonClicked"
OnHiddenStateChanged="OnHiddenStateChanged"
OnBuildingContextMenu="OnBuildingContextMenuAsync"
SearchBarPlaceholder="@MainWindow.Search"
SearchQueryChanged="OnSearchQueryChanged"
SearchQuerySubmitted="OnSearchQuerySubmitted"
Expand Down
Loading