-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SImulator: introduce web view support; allow to control media at askA…
…nswer stage
- Loading branch information
1 parent
3c2f79b
commit e3305c6
Showing
29 changed files
with
358 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/SImulator/SImulator.ViewModel/Contracts/IDisplayDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace SImulator.ViewModel.Contracts; | ||
|
||
/// <summary> | ||
/// Provides display info. | ||
/// </summary> | ||
public interface IDisplayDescriptor | ||
{ | ||
/// <summary> | ||
/// Display name. | ||
/// </summary> | ||
string Name { get; } | ||
|
||
/// <summary> | ||
/// Full screen marker. | ||
/// </summary> | ||
bool IsFullScreen { get; } | ||
|
||
/// <summary> | ||
/// Should the screen display a web UI. | ||
/// </summary> | ||
bool IsWebView => false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SImulator.ViewModel.Contracts; | ||
|
||
public interface IWebInterop | ||
{ | ||
event Action<string> SendJsonMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
src/SImulator/SImulator.ViewModel/PlatformSpecific/IScreen.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/SImulator/SImulator.ViewModel/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.Web.WebView2.Core; | ||
using Microsoft.Web.WebView2.Wpf; | ||
using SImulator.ViewModel.Contracts; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Windows; | ||
|
||
namespace SImulator.Behaviors; | ||
|
||
/// <summary> | ||
/// Attaches additional behavior for WebView2 control. | ||
/// </summary> | ||
internal static class WebView2Behavior | ||
{ | ||
public static bool GetIsAttached(DependencyObject obj) => (bool)obj.GetValue(IsAttachedProperty); | ||
|
||
public static void SetIsAttached(DependencyObject obj, bool value) => obj.SetValue(IsAttachedProperty, value); | ||
|
||
public static readonly DependencyProperty IsAttachedProperty = | ||
DependencyProperty.RegisterAttached("IsAttached", typeof(bool), typeof(WebView2), new PropertyMetadata(false, OnIsAttachedChanged)); | ||
|
||
public static void OnIsAttachedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (!(bool)e.NewValue) | ||
{ | ||
return; | ||
} | ||
|
||
var webView2 = (WebView2)d; | ||
|
||
UpdateWebView2Environment(webView2); | ||
|
||
var coreWebView = webView2.CoreWebView2; | ||
|
||
webView2.Unloaded += WebView2_Unloaded; | ||
} | ||
|
||
private static void WebView2_Unloaded(object sender, RoutedEventArgs e) | ||
{ | ||
var webView2 = (WebView2)sender; | ||
|
||
if (webView2 == null) | ||
{ | ||
return; | ||
} | ||
|
||
webView2.Unloaded -= WebView2_Unloaded; | ||
|
||
var coreWebView = webView2.CoreWebView2; | ||
|
||
if (coreWebView != null) | ||
{ | ||
// Prevent WebView for producing sound after unload | ||
coreWebView.IsMuted = true; | ||
|
||
if (webView2.DataContext is IWebInterop webInterop) | ||
{ | ||
webInterop.SendJsonMessage -= coreWebView.PostWebMessageAsJson; | ||
} | ||
} | ||
} | ||
|
||
private static async void UpdateWebView2Environment(WebView2 webView2) | ||
{ | ||
try | ||
{ | ||
// Allowing autoplay | ||
var options = new CoreWebView2EnvironmentOptions("--autoplay-policy=no-user-gesture-required"); | ||
var environment = await CoreWebView2Environment.CreateAsync(null, null, options); | ||
|
||
await webView2.EnsureCoreWebView2Async(environment); | ||
|
||
if (webView2.DataContext is IWebInterop webInterop) | ||
{ | ||
webInterop.SendJsonMessage += webView2.CoreWebView2.PostWebMessageAsJson; | ||
} | ||
} | ||
catch (Exception exc) | ||
{ | ||
Trace.TraceError(exc.ToString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.