Add WebLoginDialog for Windows Chardonnay

This commit is contained in:
Mbucari 2023-04-13 13:33:29 -06:00 committed by MBucari
parent df2936e0b6
commit 4456432116
34 changed files with 704 additions and 47 deletions

View file

@ -12,7 +12,10 @@ namespace WindowsConfigApp
public WinInterop() { }
public WinInterop(params object[] values) { }
public void SetFolderIcon(string image, string directory)
#nullable enable
public IWebViewAdapter? CreateWebViewAdapter() => new WindowsWebView2Adapter();
#nullable disable
public void SetFolderIcon(string image, string directory)
{
var icon = Image.Load(image).ToIcon();
new DirectoryInfo(directory)?.SetIcon(icon, "Music");
@ -50,5 +53,5 @@ namespace WindowsConfigApp
return Process.Start(psi);
}
}
}
}

View file

@ -2,8 +2,10 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
@ -23,6 +25,10 @@
<DebugType>embedded</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.45" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\LibationUiBase\LibationUiBase.csproj" />
</ItemGroup>

View file

@ -0,0 +1,114 @@
using LibationFileManager;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Threading.Tasks;
#nullable enable
namespace WindowsConfigApp;
internal class WindowsWebView2Adapter : IWebViewAdapter, IDisposable
{
public object NativeWebView { get; }
private readonly WebView2 _webView;
public WindowsWebView2Adapter()
{
NativeWebView = _webView = new WebView2();
PlatformHandle = new WebView2Handle { Handle = _webView.Handle };
_webView.CoreWebView2InitializationCompleted += _webView_CoreWebView2InitializationCompleted;
_webView.NavigationStarting += (s, a) =>
{
NavigationStarted?.Invoke(this, new WebViewNavigationEventArgs { Request = new Uri(a.Uri) });
};
_webView.NavigationCompleted += (s, a) =>
{
NavigationCompleted?.Invoke(this, new WebViewNavigationEventArgs { Request = _webView.Source });
};
}
private void _webView_CoreWebView2InitializationCompleted(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
_webView.CoreWebView2.DOMContentLoaded -= CoreWebView2_DOMContentLoaded;
_webView.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
}
private void CoreWebView2_DOMContentLoaded(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2DOMContentLoadedEventArgs e)
=> DOMContentLoaded?.Invoke(this, e);
public IPlatformHandle2 PlatformHandle { get; }
public bool CanGoBack => _webView.CanGoBack;
public bool CanGoForward => _webView.CanGoForward;
public Uri? Source
{
get => _webView.Source;
set => _webView.Source = value;
}
public event EventHandler<WebViewNavigationEventArgs>? NavigationStarted;
public event EventHandler<WebViewNavigationEventArgs>? NavigationCompleted;
public event EventHandler? DOMContentLoaded;
public void Dispose()
{
_webView.Dispose();
}
public bool GoBack()
{
_webView.GoBack();
return true;
}
public bool GoForward()
{
_webView.GoForward();
return true;
}
public async Task<string?> InvokeScriptAsync(string scriptName)
{
return await _webView.ExecuteScriptAsync(scriptName);
}
public void Navigate(Uri url)
{
_webView.Source = url;
}
public async Task NavigateToString(string text)
{
await _webView.EnsureCoreWebView2Async();
_webView.NavigateToString(text);
}
public void Refresh()
{
_webView.Refresh();
}
public void Stop()
{
_webView.Stop();
}
public void HandleResize(int width, int height, float zoom)
{
}
public bool HandleKeyDown(uint key, uint keyModifiers)
{
return false;
}
}
internal class WebView2Handle : IPlatformHandle2
{
public IntPtr Handle { get; init; }
public string HandleDescriptor => "HWND";
}