Browse Source

Alternate approach to #2947

pull/2948/head
tom-englert 3 years ago committed by Christoph Wille
parent
commit
ba58ecf017
  1. 5
      ILSpy/ExtensionMethods.cs
  2. 39
      ILSpy/NativeMethods.cs
  3. 56
      ILSpy/Themes/WindowStyleManagerBehavior.cs

5
ILSpy/ExtensionMethods.cs

@ -161,5 +161,10 @@ namespace ICSharpCode.ILSpy
container.IsSelected = true; container.IsSelected = true;
view.Focus(); view.Focus();
} }
public static double ToGray(this Color? color)
{
return color?.R * 0.3 + color?.G * 0.6 + color?.B * 0.1 ?? 0.0;
}
} }
} }

39
ILSpy/NativeMethods.cs

@ -193,6 +193,16 @@ namespace ICSharpCode.ILSpy
return null; return null;
} }
} }
[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute attr, ref int attrValue, int attrSize);
public static bool UseImmersiveDarkMode(IntPtr hWnd, bool enable)
{
int darkMode = enable ? 1 : 0;
int hr = DwmSetWindowAttribute(hWnd, DwmWindowAttribute.UseImmersiveDarkMode, ref darkMode, sizeof(int));
return hr >= 0;
}
} }
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
@ -212,4 +222,33 @@ namespace ICSharpCode.ILSpy
this.Buffer = buffer; this.Buffer = buffer;
} }
} }
public enum DwmWindowAttribute : uint
{
NCRenderingEnabled = 1,
NCRenderingPolicy,
TransitionsForceDisabled,
AllowNCPaint,
CaptionButtonBounds,
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds,
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Cloak,
Cloaked,
FreezeRepresentation,
PassiveUpdateMode,
UseHostBackdropBrush,
UseImmersiveDarkMode = 20,
WindowCornerPreference = 33,
BorderColor,
CaptionColor,
TextColor,
VisibleFrameBorderThickness,
SystemBackdropType,
Last
}
} }

56
ILSpy/Themes/WindowStyleManagerBehavior.cs

@ -16,11 +16,16 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Options;
using TomsToolbox.Essentials;
using TomsToolbox.Wpf; using TomsToolbox.Wpf;
using TomsToolbox.Wpf.Interactivity; using TomsToolbox.Wpf.Interactivity;
@ -30,31 +35,45 @@ namespace ICSharpCode.ILSpy.Themes
{ {
private static readonly DispatcherThrottle restartNotificationThrottle = new DispatcherThrottle(ShowRestartNotification); private static readonly DispatcherThrottle restartNotificationThrottle = new DispatcherThrottle(ShowRestartNotification);
private INotifyChanged _foreground;
private INotifyChanged _background;
protected override void OnAttached() protected override void OnAttached()
{ {
base.OnAttached(); base.OnAttached();
MainWindow.Instance.CurrentDisplaySettings.PropertyChanged += DisplaySettings_PropertyChanged; MainWindow.Instance.CurrentDisplaySettings.PropertyChanged += DisplaySettings_PropertyChanged;
UpdateWindowStyle(); _foreground = AssociatedObject.Track(Control.ForegroundProperty);
_background = AssociatedObject.Track(Control.BackgroundProperty);
_foreground.Changed += Color_Changed;
_background.Changed += Color_Changed;
UpdateWindowStyle();
ApplyThemeToWindowCaption();
} }
protected override void OnDetaching() protected override void OnDetaching()
{ {
base.OnDetaching(); base.OnDetaching();
_foreground.Changed -= Color_Changed;
_background.Changed -= Color_Changed;
MainWindow.Instance.CurrentDisplaySettings.PropertyChanged -= DisplaySettings_PropertyChanged; MainWindow.Instance.CurrentDisplaySettings.PropertyChanged -= DisplaySettings_PropertyChanged;
} }
private void UpdateWindowStyle() private void Color_Changed(object sender, EventArgs e)
{
if (!MainWindow.Instance.CurrentDisplaySettings.StyleWindowTitleBar)
{ {
return; ApplyThemeToWindowCaption();
} }
private void UpdateWindowStyle()
{
var window = AssociatedObject; var window = AssociatedObject;
if (MainWindow.Instance.CurrentDisplaySettings.StyleWindowTitleBar)
window.Style = (Style)window.FindResource(TomsToolbox.Wpf.Styles.ResourceKeys.WindowStyle); window.Style = (Style)window.FindResource(TomsToolbox.Wpf.Styles.ResourceKeys.WindowStyle);
} }
@ -76,5 +95,32 @@ namespace ICSharpCode.ILSpy.Themes
UpdateWindowStyle(); UpdateWindowStyle();
} }
} }
private void ApplyThemeToWindowCaption()
{
var window = AssociatedObject;
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd != IntPtr.Zero)
{
var foreground = ((window.Foreground as SolidColorBrush)?.Color).ToGray();
var background = ((window.Background as SolidColorBrush)?.Color).ToGray();
var isDarkTheme = background < foreground;
NativeMethods.UseImmersiveDarkMode(hwnd, isDarkTheme);
}
else
{
void Initialized(object o, EventArgs eventArgs)
{
ApplyThemeToWindowCaption();
window.SourceInitialized -= Initialized;
}
window.SourceInitialized += Initialized;
}
}
} }
} }

Loading…
Cancel
Save