Skip to content

Commit

Permalink
show msg if WebView2 runtime is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 28, 2023
1 parent caccb41 commit 8fe2572
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Source/Components/ImageGlass.Base/Language/IgLang.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2010 - 2023 DUONG DIEU PHAP
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
Project homepage: https://imageglass.org
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -167,6 +167,8 @@ public void InitDefaultLanguage()
_ = TryAdd("_._UserAction._MethodArgumentNotSupported", "The argument type of method '{0}' is not supported"); // v9.0
_ = TryAdd("_._UserAction._Win32ExeError", "Cannot execute command '{0}'. Make sure the name is correct."); // v9.0

_ = TryAdd("_._Webview2._NotFound", "ImageGlass could not detect WebView2 Runtime 64-bit on your machine."); // 9.1

// Gallery tooltip
_ = TryAdd($"_.Metadata._{nameof(IgMetadata.FileSize)}", "File size"); //v9.0
_ = TryAdd($"_.Metadata._{nameof(IgMetadata.FileCreationTime)}", "Date created"); //v9.0
Expand Down
43 changes: 41 additions & 2 deletions Source/Components/ImageGlass.Base/Webview2/Web2.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2010 - 2023 DUONG DIEU PHAP
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
Project homepage: https://imageglass.org
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -74,6 +74,27 @@ public Color AccentColor
/// </summary>
public bool IsWeb2Ready => this.CoreWebView2 != null;


/// <summary>
/// Gets version of <see cref="WebView2"/> runtime,
/// returns <c>null</c> if <see cref="WebView2"/> runtime is not installed.
/// </summary>
public static Version? Webview2Version
{
get
{
try
{
var version = CoreWebView2Environment.GetAvailableBrowserVersionString();
return new Version(version);
}
catch (WebView2RuntimeNotFoundException) { }

return null;
}
}


#endregion // Properties


Expand Down Expand Up @@ -264,6 +285,15 @@ await Invoke(async delegate
/// </summary>
public async Task EnsureWeb2Async()
{
// if WebView2 runtime is not installed
if (!Web2.CheckWebview2Installed())
{
MessageBox.Show($"{nameof(Web2)}: WebView2 Runtime 64-bit is not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;
}


var options = new CoreWebView2EnvironmentOptions
{
AdditionalBrowserArguments = "--disable-web-security --allow-file-access-from-files --allow-file-access",
Expand Down Expand Up @@ -313,7 +343,7 @@ public async Task EnsureWeb2Async()
// Operation aborted (0x80004004 (E_ABORT))
if (ex.HResult == -2147467260)
{

// ignore
}
else
{
Expand All @@ -325,6 +355,15 @@ public async Task EnsureWeb2Async()
}


/// <summary>
/// Checks if <see cref="WebView2"/> runtime is installed.
/// </summary>
public static bool CheckWebview2Installed()
{
return Web2.Webview2Version != null;
}


/// <summary>
/// Sets accent color to <see cref="Web2"/> content. Example:
/// <code>
Expand Down
28 changes: 26 additions & 2 deletions Source/Components/ImageGlass.Settings/Forms/WebForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2010 - 2023 DUONG DIEU PHAP
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
Project homepage: https://imageglass.org
This program is free software: you can redistribute it and/or modify
Expand All @@ -24,6 +24,11 @@ namespace ImageGlass;

public partial class WebForm : ThemedForm
{
/// <summary>
/// Checks if WebView2 runtime is installed.
/// </summary>
public static bool IsWebView2Installed => Web2.CheckWebview2Installed();

// Public events
#region Public events

Expand Down Expand Up @@ -58,7 +63,10 @@ public WebForm()
Web2.Visible = false;
Web2.EnableDebug = Config.EnableDebug;

_ = Web2.EnsureWeb2Async();
if (IsWebView2Installed)
{
_ = Web2.EnsureWeb2Async();
}
}


Expand All @@ -70,6 +78,22 @@ protected override void OnLoad(EventArgs e)
base.OnLoad(e);
if (DesignMode) return;


// show message if WebView2 runtime is not found
if (!IsWebView2Installed)
{
var label = new ModernLabel()
{
Text = Config.Language["_._Webview2._NotFound"],
Dock = DockStyle.Top,
AutoSize = true,
TextAlign = ContentAlignment.TopCenter,
Padding = new Padding(20),
};

Controls.Add(label);
}

_ = Config.UpdateFormIcon(this);
ApplyTheme(Config.Theme.Settings.IsDarkMode);
}
Expand Down

0 comments on commit 8fe2572

Please sign in to comment.