Skip to content

Commit

Permalink
Merge pull request #157 from BalassaMarton/multiwindow-close
Browse files Browse the repository at this point in the history
Shell - Handle window close events correctly
  • Loading branch information
BalassaMarton authored Feb 21, 2023
2 parents fee34a5 + cea0db8 commit 26df8ef
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions Tryouts/Prototypes/Shell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properties/launchsettings.json
38 changes: 38 additions & 0 deletions Tryouts/Prototypes/Shell/Node-launcher/MultiWindowDemo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8" />
<title>Navigation demo</title>
</head>

<body>
<script>

let popup;

function getPopupUrl(content) {
return "popup.html?" + encodeURIComponent(content);
}

function showPopup() {
const content = document.getElementById("popupContent").value;
console.log(`Opening new window with content '${content}'`);

if (popup && !popup.closed) {
popup.location = getPopupUrl(content);
}
else {
popup = window.open(getPopupUrl(content));
}
}
</script>
<div>
<form onsubmit="showPopup(); return false;">
<input type="text" id="popupContent"></input>
<button>Open</button>
</form>
</div>
</body>

</html>
24 changes: 24 additions & 0 deletions Tryouts/Prototypes/Shell/Node-launcher/MultiWindowDemo/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>title</title>
</head>
<body>
<script>

window.onload = ev => {
const content = window.location.search.substring(1);
console.log(`Setting content to ${content}`);
document.getElementById("content").textContent = content;
}

</script>
<div>
<span id="content"></span>
</div>
<div>
<button onclick="window.close()">Close window</button>
</div>
</body>
</html>
8 changes: 0 additions & 8 deletions Tryouts/Prototypes/Shell/Properties/launchSettings.json

This file was deleted.

13 changes: 13 additions & 0 deletions Tryouts/Prototypes/Shell/WebWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public WebWindow(WebWindowOptions webWindowOptions)
if (args.IsSuccess)
{
webView.CoreWebView2.NewWindowRequested += (sender, args) => OnNewWindowRequested(args);
webView.CoreWebView2.WindowCloseRequested += (sender, args) => OnWindowCloseRequested(args);
}
};
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
this.RemoveLogicalChild(webView);
webView.Dispose();
}

private readonly ImageSourceProvider _iconProvider = new ImageSourceProvider(new EnvironmentImageSourcePolicy());

private void TrySetIconUrl(WebWindowOptions webWindowOptions)
Expand Down Expand Up @@ -64,5 +72,10 @@ private async void OnNewWindowRequested(CoreWebView2NewWindowRequestedEventArgs
e.NewWindow = window.webView.CoreWebView2;
deferral.Complete();
}

private void OnWindowCloseRequested(object args)
{
this.Close();
}
}
}

0 comments on commit 26df8ef

Please sign in to comment.