Skip to content

Commit

Permalink
Fix DPI, Extra white windows
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jul 10, 2022
1 parent 0690235 commit 25bfda1
Show file tree
Hide file tree
Showing 24 changed files with 486 additions and 249 deletions.
4 changes: 0 additions & 4 deletions src/Avalonia.WebView2.Sample/Avalonia.WebView2.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<AvaloniaXaml Include="**\*.xaml" />
</ItemGroup>

<ItemGroup>
<None Remove="WebView2Compat.xaml" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Avalonia.WebView2\Properties\AssemblyInfo.OS.cs">
<Link>Properties\AssemblyInfo.OS.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationCore/SR.cs

#if !NETFRAMEWORK && WINDOWS

namespace MS.Internal.PresentationCore;

internal static class SR
{
public static string Get(string name) => name switch
{
SRID.CantShowMBServiceWithOwner => "Cannot show MessageBox Service with Owner.",
_ => "",
};
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if !NETFRAMEWORK && WINDOWS

namespace MS.Internal.PresentationCore;

static class SRID
{
/// <summary>
/// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Resources/Strings.resx#L510
/// </summary>
public const string CantShowMBServiceWithOwner = "CantShowMBServiceWithOwner";
}

#endif
57 changes: 57 additions & 0 deletions src/Avalonia.WebView2.Sample/BCL/MS/Win32/ExternDll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/ExternDll.cs

#if !NETFRAMEWORK && WINDOWS

namespace MS.Win32;

internal static class ExternDll
{
//public const string Activeds = "activeds.dll";
//public const string Advapi32 = "advapi32.dll";
//public const string Comctl32 = "comctl32.dll";
//public const string Comdlg32 = "comdlg32.dll";
//public const string DwmAPI = "dwmapi.dll";
//public const string Gdi32 = "gdi32.dll";
//public const string Gdiplus = "gdiplus.dll";
//public const string Hhctrl = "hhctrl.ocx";
//public const string Imm32 = "imm32.dll";
//public const string Kernel32 = "kernel32.dll";
//public const string Loadperf = "Loadperf.dll";
//public const string Mqrt = "mqrt.dll";
//public const string Mscoree = "mscoree.dll";
//public const string MsDrm = "msdrm.dll";
//public const string Mshwgst = "mshwgst.dll";
//public const string Msi = "msi.dll";
//public const string NaturalLanguage6 = "naturallanguage6.dll";
//public const string Ntdll = "ntdll.dll";
//public const string Ole32 = "ole32.dll";
//public const string Oleacc = "oleacc.dll";
//public const string Oleaut32 = "oleaut32.dll";
//public const string Olepro32 = "olepro32.dll";
//public const string Penimc = "PenIMC_cor3.dll";
//public const string PresentationCore = "PresentationCore.dll";
//public const string PresentationFramework = "PresentationFramework.dll";
//public const string PresentationHostDll = "PresentationHost_cor3.dll";
//public const string PresentationNativeDll = "PresentationNative_cor3.dll";
//public const string Psapi = "psapi.dll";
//public const string Shcore = "shcore.dll";
//public const string Shell32 = "shell32.dll";
//public const string Shfolder = "shfolder.dll";
//public const string Urlmon = "urlmon.dll";
public const string User32 = "user32.dll";
//public const string Uxtheme = "uxtheme.dll";
//public const string Version = "version.dll";
//public const string Vsassert = "vsassert.dll";
//public const string WindowsBase = "windowsbase.dll";
//public const string Wininet = "wininet.dll";
//public const string Winmm = "winmm.dll";
//public const string Winspool = "winspool.drv";
//public const string Wldp = "wldp.dll";
//public const string WpfGfx = "WpfGfx_cor3.dll";
//public const string WtsApi32 = "wtsapi32.dll";
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsCLR.cs

#if !NETFRAMEWORK && WINDOWS

using System.Runtime.InteropServices;

namespace MS.Win32;

internal static class UnsafeNativeMethods
{
[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetActiveWindow();

[DllImport(ExternDll.User32, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);
}

#endif
248 changes: 248 additions & 0 deletions src/Avalonia.WebView2.Sample/BCL/System/Windows/MessageBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs

#if !NETFRAMEWORK && WINDOWS

using MS.Win32;
using System.ComponentModel;
using System.Runtime.InteropServices;
using SR_ = MS.Internal.PresentationCore.SR;
using SRID = MS.Internal.PresentationCore.SRID;

namespace System.Windows;

public static class MessageBox
{
const int IDOK = 1;
const int IDCANCEL = 2;
const int IDYES = 6;
const int IDNO = 7;
const int DEFAULT_BUTTON1 = 0x00000000;
const int DEFAULT_BUTTON2 = 0x00000100;
const int DEFAULT_BUTTON3 = 0x00000200;

static MessageBoxResult Win32ToMessageBoxResult(int value) => value switch
{
IDOK => MessageBoxResult.OK,
IDCANCEL => MessageBoxResult.Cancel,
IDYES => MessageBoxResult.Yes,
IDNO => MessageBoxResult.No,
_ => MessageBoxResult.No,
};

public static MessageBoxResult Show(
string messageBoxText,
string caption,
MessageBoxButton button,
MessageBoxImage icon,
MessageBoxResult defaultResult,
MessageBoxOptions options) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, options);

public static MessageBoxResult Show(
string messageBoxText,
string caption,
MessageBoxButton button,
MessageBoxImage icon,
MessageBoxResult defaultResult) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, 0);

public static MessageBoxResult Show(
string messageBoxText,
string caption,
MessageBoxButton button,
MessageBoxImage icon) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, 0, 0);

public static MessageBoxResult Show(
string messageBoxText,
string caption,
MessageBoxButton button) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, MessageBoxImage.None, 0, 0);

public static MessageBoxResult Show(string messageBoxText, string caption) => ShowCore(IntPtr.Zero, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, 0, 0);

public static MessageBoxResult Show(string messageBoxText) => ShowCore(IntPtr.Zero, messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None, 0, 0);

static int DefaultResultToButtonNumber(MessageBoxResult result, MessageBoxButton button)
{
if (result == 0) return DEFAULT_BUTTON1;

switch (button)
{
case MessageBoxButton.OK:
return DEFAULT_BUTTON1;
case MessageBoxButton.OKCancel:
if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON2;
return DEFAULT_BUTTON1;
case MessageBoxButton.YesNo:
if (result == MessageBoxResult.No) return DEFAULT_BUTTON2;
return DEFAULT_BUTTON1;
case MessageBoxButton.YesNoCancel:
if (result == MessageBoxResult.No) return DEFAULT_BUTTON2;
if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON3;
return DEFAULT_BUTTON1;
default:
return DEFAULT_BUTTON1;
}
}

internal static MessageBoxResult ShowCore(
IntPtr owner,
string messageBoxText,
string caption,
MessageBoxButton button,
MessageBoxImage icon,
MessageBoxResult defaultResult,
MessageBoxOptions options)
{
if (!IsValidMessageBoxButton(button))
{
throw new InvalidEnumArgumentException("button", (int)button, typeof(MessageBoxButton));
}
if (!IsValidMessageBoxImage(icon))
{
throw new InvalidEnumArgumentException("icon", (int)icon, typeof(MessageBoxImage));
}
if (!IsValidMessageBoxResult(defaultResult))
{
throw new InvalidEnumArgumentException("defaultResult", (int)defaultResult, typeof(MessageBoxResult));
}
if (!IsValidMessageBoxOptions(options))
{
throw new InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions));
}

// UserInteractive??
//
/*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {
throw new InvalidOperationException("UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)");
}*/

if ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{
if (owner != IntPtr.Zero)
{
throw new ArgumentException(SR_.Get(SRID.CantShowMBServiceWithOwner));
}
}
else
{
if (owner == IntPtr.Zero)
{
owner = UnsafeNativeMethods.GetActiveWindow();
}
}

int style = (int)button | (int)icon | (int)DefaultResultToButtonNumber(defaultResult, button) | (int)options;

// modal dialog notification?
//
//Application.BeginModalMessageLoop();
//MessageBoxResult result = Win32ToMessageBoxResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), messageBoxText, caption, style));
MessageBoxResult result = Win32ToMessageBoxResult(UnsafeNativeMethods.MessageBox(new HandleRef(null, owner), messageBoxText, caption, style));
// modal dialog notification?
//
//Application.EndModalMessageLoop();

return result;
}

static bool IsValidMessageBoxButton(MessageBoxButton value) => value == MessageBoxButton.OK
|| value == MessageBoxButton.OKCancel
|| value == MessageBoxButton.YesNo
|| value == MessageBoxButton.YesNoCancel;

static bool IsValidMessageBoxImage(MessageBoxImage value) => value == MessageBoxImage.Asterisk
|| value == MessageBoxImage.Error
|| value == MessageBoxImage.Exclamation
|| value == MessageBoxImage.Hand
|| value == MessageBoxImage.Information
|| value == MessageBoxImage.None
|| value == MessageBoxImage.Question
|| value == MessageBoxImage.Stop
|| value == MessageBoxImage.Warning;

static bool IsValidMessageBoxResult(MessageBoxResult value) => value == MessageBoxResult.Cancel
|| value == MessageBoxResult.No
|| value == MessageBoxResult.None
|| value == MessageBoxResult.OK
|| value == MessageBoxResult.Yes;

static bool IsValidMessageBoxOptions(MessageBoxOptions value)
{
int mask = ~((int)MessageBoxOptions.ServiceNotification |
(int)MessageBoxOptions.DefaultDesktopOnly |
(int)MessageBoxOptions.RightAlign |
(int)MessageBoxOptions.RtlReading);

if (((int)value & mask) == 0)
return true;
return false;
}
}

public enum MessageBoxResult
{
None = 0,

OK = 1,

Cancel = 2,

Yes = 6,

No = 7,

// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxResult()
}

[Flags]
public enum MessageBoxOptions
{
None = 0x00000000,

ServiceNotification = 0x00200000,

DefaultDesktopOnly = 0x00020000,

RightAlign = 0x00080000,

RtlReading = 0x00100000,
}

public enum MessageBoxImage
{
None = 0,

Hand = 0x00000010,

Question = 0x00000020,

Exclamation = 0x00000030,

Asterisk = 0x00000040,

Stop = Hand,

Error = Hand,

Warning = Exclamation,

Information = Asterisk,

// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxIcon()
}

public enum MessageBoxButton
{
OK = 0x00000000,

OKCancel = 0x00000001,

YesNoCancel = 0x00000003,

YesNo = 0x00000004,

// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxButton()
}

#endif
4 changes: 2 additions & 2 deletions src/Avalonia.WebView2.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ static string GetTitle(string browserVersion, Architecture architecture = Unknow
{
if (architecture != Unknown)
{
return $"Microsoft Edge WebView2 {browserVersion} {architecture} for Avalonia";
return $"Microsoft Edge WebView2 {browserVersion} {architecture} for Avalonia on {System.Environment.OSVersion.VersionString}";
}
return $"Microsoft Edge WebView2 {browserVersion} for Avalonia";
return $"Microsoft Edge WebView2 {browserVersion} for Avalonia on {System.Environment.OSVersion.VersionString}";
}

void SetTitle(string browserVersion, Architecture architecture = Unknown) => Title.Content = base.Title = GetTitle(browserVersion, architecture);
Expand Down
Loading

0 comments on commit 25bfda1

Please sign in to comment.