Skip to content

Commit

Permalink
Merge pull request #92 from strem-app/spike/try-photino-native-dialogs
Browse files Browse the repository at this point in the history
Spike/try photino native dialogs
  • Loading branch information
grofit authored Jan 19, 2024
2 parents 44e42f7 + ad6b982 commit 3137206
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
@inject IAudioHandler AudioHandler

<div class="field">
<label class="label">Flow</label>
<label class="label">Sound File</label>
<div class="control">
<FileInputBrowser @bind-Value="Data.SoundFile" FileFilter="wav,mp3,ogg,midi,aac"></FileInputBrowser>
<FileInputBrowser @bind-Value="Data.SoundFile" FileFilter="Audio Files (*.wav;*.mp3;*.ogg;*.midi;*.acc;*.opus)|*.wav;*.mp3;*.ogg;*.midi;*.acc;*.opus"></FileInputBrowser>
</div>
</div>
<div class="field">
Expand Down
5 changes: 0 additions & 5 deletions src/Strem.Platforms.Windows/Plugin/WindowsPlatformModule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using Strem.Core.Plugins;
using Strem.Core.Services.Audio;
using Strem.Core.Services.Browsers.File;
using Strem.Core.Services.Clipboard;
using Strem.Core.Services.TTS;
using Strem.Platforms.Windows.Services.Audio;
using Strem.Platforms.Windows.Services.Browsers;
using Strem.Platforms.Windows.Services.Clipboard;
using Strem.Platforms.Windows.Services.TTS;

Expand All @@ -15,9 +13,6 @@ public class WindowsPlatformModule : IDependencyModule
{
public void Setup(IServiceCollection services)
{
// File Browsing
services.AddSingleton<IFileBrowser, FileBrowser>();

// TTS
services.AddSingleton<ITTSHandler, WindowsTTSHandler>();

Expand Down
19 changes: 0 additions & 19 deletions src/Strem.Platforms.Windows/Services/Browsers/FileBrowser.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Strem.Platforms.Windows/Strem.Platforms.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NativeFileDialogSharp" Version="0.5.0" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="System.Speech" Version="8.0.0" />
<PackageReference Include="TextCopy" Version="6.2.1" />
Expand Down
12 changes: 5 additions & 7 deletions src/Strem.Portals/Components/App/PortalElementEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<label class="label">Icon Image</label>
<div class="field">
<div class="control">
<FileInputBrowser Value="@Data.DefaultStyles.ImageUrl()" ValueChanged="x => Data.DefaultStyles.ImageUrl(x)" placeholder="i.e http://some-site.com/some-image.png or c:/some-file.png" />
<FileInputBrowser FileFilter="Image Files (*.jpg;*.jpeg;*.png;*.gif;)|*.jpg;*.jpeg;*.png;*.gif;" Value="@Data.DefaultStyles.ImageUrl()" ValueChanged="x => Data.DefaultStyles.ImageUrl(x)" placeholder="i.e http://some-site.com/some-image.png or c:/some-file.png" />
</div>
<HelperInfo>This can be a url to a file online or a local file, local files need more processing though so the larger they are the longer they will take to load, try to keep sizes below 256x256 pixels</HelperInfo>
</div>
Expand Down Expand Up @@ -124,10 +124,8 @@
</div>
</div>

@code {
[Parameter]
public EventCallback OnClosed { get; set; }

[Parameter]
public EventCallback<Guid> RequestedDeletion { get; set; }
@code
{
[Parameter] public EventCallback OnClosed { get; set; }
[Parameter] public EventCallback<Guid> RequestedDeletion { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</button>
</div>
<div class="control">
<FileInputBrowser Value="@Data.NewStyles.ImageUrl()" ValueChanged="ImageUrlChanged" disabled="@(!Data.ChangeImage)" />
<FileInputBrowser FileFilter="Image Files (*.jpg;*.jpeg;*.png;*.gif;)|*.jpg;*.jpeg;*.png;*.gif;" Value="@Data.NewStyles.ImageUrl()" ValueChanged="ImageUrlChanged" disabled="@(!Data.ChangeImage)" />
</div>
</div>
<HelperInfo>This can be a url to a file online or a local file, local files need more processing though so the larger they are the longer they will take to load, try to keep sizes below 256x256 pixels</HelperInfo>
Expand Down
2 changes: 1 addition & 1 deletion src/Strem.Portals/Extensions/ElementStylesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public static ButtonType ButtonType(this ElementStyles elementStyles)
}

public static void ButtonType(this ElementStyles elementStyles, ButtonType buttonType)
{ SetFromKey(elementStyles, SliderCellSizeKey, ((int)buttonType).ToString()); }
{ SetFromKey(elementStyles, ButtonTypeKey, ((int)buttonType).ToString()); }
}
21 changes: 21 additions & 0 deletions src/Strem.UnitTests/App/Services/BlazorNativeFileBrowserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Strem.Services.Dialogs;

namespace Strem.UnitTests.App.Services;

public class BlazorNativeFileBrowserTests
{
[Fact]
public void should_correctly_convert_filter_string_to_filter_tuple()
{
var filterString = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|CSV Files (*.csv)|*.csv";
var transformedFilters = BlazorNativeFileBrowser.FilterToBlazorFilter(filterString);
Assert.Equal(2, transformedFilters.Length);
Assert.Equal("Excel Files (*.xls, *.xlsx)", transformedFilters[0].Name);
Assert.Equal(2, transformedFilters[0].Extensions.Length);
Assert.Contains("*.xls", transformedFilters[0].Extensions);
Assert.Contains("*.xlsx", transformedFilters[0].Extensions);
Assert.Equal("CSV Files (*.csv)", transformedFilters[1].Name);
Assert.Equal(1, transformedFilters[1].Extensions.Length);
Assert.Contains("*.csv", transformedFilters[1].Extensions);
}
}
1 change: 1 addition & 0 deletions src/Strem.UnitTests/Strem.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<ProjectReference Include="..\Strem.Flows.Default\Strem.Flows.Default.csproj" />
<ProjectReference Include="..\Strem.Infrastructure\Strem.Infrastructure.csproj" />
<ProjectReference Include="..\Strem.Twitch\Strem.Twitch.csproj" />
<ProjectReference Include="..\Strem\Strem.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Strem/Application/BlazorBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void SetupWindow(PhotinoBlazorApp app)
appLauncher = appLauncher.SetDevToolsEnabled(false);
appLauncher = appLauncher.SetContextMenuEnabled(false);
#endif
appLauncher.Load(rootIndexPage);
}

public void SetupApp(Action<IServiceCollection> beforeCreated, Func<PhotinoBlazorApp, Task> afterCreated)
Expand Down
5 changes: 5 additions & 0 deletions src/Strem/Application/StremApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using Strem.Core.Events.Bus;
using Strem.Core.Extensions;
using Strem.Core.Plugins;
using Strem.Core.Services.Browsers.File;
using Strem.Core.Variables;
using Strem.Infrastructure.Extensions;
using Strem.Infrastructure.Plugin;
using Strem.Infrastructure.Services.Api;
using Strem.Services.Dialogs;
using Strem.Twitch.Plugin;

namespace Strem.Application;
Expand Down Expand Up @@ -57,6 +59,9 @@ public void PreLoadPlugins()

public void LoadPlugins(IServiceCollection services)
{
// File Browsing
services.AddSingleton<IFileBrowser, BlazorNativeFileBrowser>();

// Config
PreStartupLogs.Add("Setting up Application Config");
services.AddSingleton<IApplicationConfig>(AppConfig);
Expand Down
56 changes: 56 additions & 0 deletions src/Strem/Services/Dialogs/BlazorNativeFileBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Text.RegularExpressions;
using Photino.Blazor;
using Strem.Core.Services.Browsers.File;

namespace Strem.Services.Dialogs;

public class BlazorNativeFileBrowser : IFileBrowser
{
public static Regex FilterPattern { get; } = new Regex(@"(.*?\|[\;\.\-\*\w]*)\|?");

public PhotinoBlazorApp App { get; }

public BlazorNativeFileBrowser(PhotinoBlazorApp app)
{
App = app;
}

// EXAMPLE "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|CSV Files (*.csv)|*.csv"
public static (string Name, string[] Extensions)[] FilterToBlazorFilter(string filter)
{
if (string.IsNullOrEmpty(filter))
{ return Array.Empty<(string Name, string[] Extensions)>(); }

if (!filter.Contains('|'))
{ return [(filter, new[] { filter })]; }

var filterList = new List<(string Name, string[] Extensions)>();
var lineFilters = FilterPattern.Matches(filter).Select(x => x.Value);
foreach (var lineFilter in lineFilters)
{
if (!lineFilter.Contains('|'))
{ filterList.Add((lineFilter, new[] { lineFilter })); }
else
{
var filerSections = lineFilter.Split("|");
filterList.Add((filerSections[0], filerSections[1].Split(";")));
}
}

return filterList.ToArray();
}

public string BrowseToOpenFile(string startingDirectory = null, string filterList = null)
{
var filters = FilterToBlazorFilter(filterList);
var result = App.MainWindow.ShowOpenFile(defaultPath: startingDirectory, filters: filters, multiSelect: false);
return result.Length == 0 ? string.Empty : result[0];
}

public string BrowseToSaveFile(string startingDirectory = null, string filterList = null)
{
var filters = FilterToBlazorFilter(filterList);
var result = App.MainWindow.ShowSaveFile(defaultPath: startingDirectory, filters: filters);
return string.IsNullOrEmpty(result) ? string.Empty : result;
}
}
2 changes: 1 addition & 1 deletion src/Strem/Strem.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ItemGroup>
<PackageReference Include="BlazorMonaco" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView" Version="8.0.0" />
<PackageReference Include="Photino.Blazor" Version="2.6.0" />
<PackageReference Include="Photino.Blazor" Version="2.7.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 3137206

Please sign in to comment.