96 lines
2.8 KiB
C#
96 lines
2.8 KiB
C#
using Microsoft.Web.WebView2.WinForms;
|
|
|
|
namespace UdaraWindows;
|
|
|
|
public interface IWebView
|
|
{
|
|
void LoadPage(Uri url);
|
|
void Show();
|
|
}
|
|
|
|
public class WebViewLauncher
|
|
{
|
|
private readonly IWebView _webView;
|
|
private readonly string _defaultUrl;
|
|
|
|
public WebViewLauncher(IWebView webView, string defaultUrl)
|
|
{
|
|
_webView = webView ?? throw new ArgumentNullException(nameof(webView));
|
|
_defaultUrl = defaultUrl ?? throw new ArgumentNullException(nameof(defaultUrl));
|
|
}
|
|
|
|
public void Launch(string? url = null)
|
|
{
|
|
string targetUrl = string.IsNullOrWhiteSpace(url) ? _defaultUrl : url;
|
|
|
|
if (!Uri.TryCreate(targetUrl, UriKind.Absolute, out var uri))
|
|
throw new ArgumentException("Invalid URL provided.", nameof(url));
|
|
|
|
_webView.LoadPage(uri);
|
|
_webView.Show();
|
|
}
|
|
}
|
|
|
|
public partial class WebViewForm : Form, IWebView
|
|
{
|
|
private readonly WebView2 _webView;
|
|
|
|
public WebViewForm()
|
|
{
|
|
Text = "PSTW Udara v2";
|
|
ClientSize = new Size(1280, 800);
|
|
MinimumSize = new Size(800, 450);
|
|
MaximumSize = new Size(Screen.PrimaryScreen!.Bounds.Width, Screen.PrimaryScreen!.Bounds.Height);
|
|
|
|
Icon = new Icon("UdaraWindows.ico");
|
|
|
|
_webView = new WebView2 { Dock = DockStyle.Fill };
|
|
Controls.Add(_webView);
|
|
|
|
Load += async (_, _) =>
|
|
{
|
|
await _webView.EnsureCoreWebView2Async();
|
|
_webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
|
|
_webView.CoreWebView2.ContextMenuRequested += (sender, e) =>
|
|
{
|
|
e.Handled = true;
|
|
};
|
|
_webView.NavigationCompleted += async (sender, e) =>
|
|
{
|
|
if (e.IsSuccess)
|
|
{
|
|
string cssScript = @"
|
|
const style = document.createElement('style');
|
|
style.textContent = `
|
|
html, body, * {
|
|
-webkit-user-select: none;
|
|
-ms-user-select: none;
|
|
user-select: none;
|
|
}
|
|
html::-webkit-scrollbar {
|
|
display: none;
|
|
overflow-y: hidden;
|
|
}
|
|
`;
|
|
document.head.appendChild(style);
|
|
";
|
|
await _webView.CoreWebView2.ExecuteScriptAsync(cssScript);
|
|
|
|
Text = _webView.CoreWebView2.DocumentTitle;
|
|
|
|
}
|
|
};
|
|
};
|
|
}
|
|
|
|
public void LoadPage(Uri url)
|
|
{
|
|
_webView.Source = url;
|
|
}
|
|
|
|
public new void Show()
|
|
{
|
|
Application.Run(this);
|
|
}
|
|
}
|