Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ transitions when switching tools; check `AppearanceManager.AnimationsEnabled` wh
`ChildWindowAnimation`, `SelectionIndicatorAnimation`, `ExpanderAnimation`, `SmoothProgress`, notification slide-in, tab
reorder slide (Dragablz `StackOrganiser`, `Canvas.Left`, switch set in `App.xaml.cs`). Short color / opacity fades
(hover layers, tooltip, overlay) are always shown. The accent glow is only in the window frame. Native windows (Remote
Desktop, PuTTY, PowerShell, TigerVNC, WebView2) can't be animated, clipped or put in transparent windows.
Desktop, PuTTY, PowerShell, TigerVNC, WebView2) can't be animated, clipped or put in per-pixel transparent windows
(`AllowsTransparency`); the whole window can be transparent (`WindowTransparencyHelper`, uniform alpha).

## Architecture

Expand Down
27 changes: 27 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5966,4 +5966,13 @@ Do you want to close all hosts?</value>
<data name="Actions" xml:space="preserve">
<value>Actions</value>
</data>
<data name="Transparency" xml:space="preserve">
<value>Transparency</value>
</data>
<data name="EnableTransparency" xml:space="preserve">
<value>Enable transparency</value>
</data>
<data name="HelpMessage_Transparency" xml:space="preserve">
<value>Makes the main window, the windows of dragged-out tabs, the status window and the notifications transparent, so you can see what is behind them.</value>
</data>
</root>
13 changes: 13 additions & 0 deletions Source/NETworkManager.Settings/AppearanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ public static class AppearanceManager
AnimationMode.Off => false,
_ => SystemParameters.ClientAreaAnimation
};

/// <summary>
/// Transparency of the main window, the windows of dragged-out tabs, the status window and the notifications in
/// percent (0 = opaque): the setting
/// (<see cref="SettingsInfo.Appearance_Transparency" />, limited to the range of the slider) if
/// <see cref="SettingsInfo.Appearance_UseTransparency" /> is enabled. Opaque in screenshot mode.
/// </summary>
public static int WindowTransparency =>
!CommandLineManager.Current.Screenshot && SettingsManager.Current?.Appearance_UseTransparency == true
? Math.Clamp(SettingsManager.Current.Appearance_Transparency,
GlobalStaticConfiguration.Appearance_TransparencyMin,
GlobalStaticConfiguration.Appearance_TransparencyMax)
: 0;
#endregion

#region Constructor
Expand Down
6 changes: 6 additions & 0 deletions Source/NETworkManager.Settings/GlobalStaticConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public static class GlobalStaticConfiguration
public static AnimationMode Appearance_Animations => AnimationMode.System;
// Medium for new installations (subtle, static); saved in the settings
public static AccentGlowIntensity Appearance_AccentGlowIntensity => AccentGlowIntensity.Medium;
// Transparency of the main window, the windows of dragged-out tabs, the status window and the notifications
// (percent, 0 = opaque); more than 30 % makes the texts hard to read
public static bool Appearance_UseTransparency => false;
public static int Appearance_Transparency => 5; // %
public static int Appearance_TransparencyMin => 1; // %
public static int Appearance_TransparencyMax => 30; // %

// Network
public static bool Network_AddDNSSuffix => true;
Expand Down
26 changes: 26 additions & 0 deletions Source/NETworkManager.Settings/SettingsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ public AccentGlowIntensity Appearance_AccentGlowIntensity
}
} = GlobalStaticConfiguration.Appearance_AccentGlowIntensity;

public bool Appearance_UseTransparency
{
get;
set
{
if (value == field)
return;

field = value;
OnPropertyChanged();
}
} = GlobalStaticConfiguration.Appearance_UseTransparency;

public int Appearance_Transparency
{
get;
set
{
if (value == field)
return;

field = value;
OnPropertyChanged();
}
} = GlobalStaticConfiguration.Appearance_Transparency;

public AnimationMode Appearance_Animations
{
get;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NETworkManager.Utilities.WPF;
using Xunit;

namespace NETworkManager.Tests.Utilities;

public class WindowTransparencyHelperTests
{
[Theory]
[InlineData(0, 255)]
[InlineData(5, 242)]
[InlineData(15, 217)]
[InlineData(30, 179)]
[InlineData(100, 0)]
public void ConvertsTransparencyToAlpha(int transparency, byte expectedAlpha)
{
Assert.Equal(expectedAlpha, WindowTransparencyHelper.GetAlpha(transparency));
}

[Theory]
[InlineData(-10, 255)]
[InlineData(150, 0)]
public void ClampsTransparency(int transparency, byte expectedAlpha)
{
Assert.Equal(expectedAlpha, WindowTransparencyHelper.GetAlpha(transparency));
}
}
150 changes: 150 additions & 0 deletions Source/NETworkManager.Utilities.WPF/WindowTransparencyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace NETworkManager.Utilities.WPF;

/// <summary>
/// Makes a whole window transparent (uniform alpha of a system layered window, applied by Windows / DWM). Unlike
/// <see cref="Window.AllowsTransparency" /> (per-pixel transparency, rendered by WPF) it can be changed while the window
/// is shown and keeps the embedded windows (Remote Desktop, PuTTY, PowerShell, TigerVNC, WebView2) visible - they become
/// transparent too.
/// </summary>
/// <remarks>
/// WPF removes <c>WS_EX_LAYERED</c> from a top-level window without <see cref="Window.AllowsTransparency" /> in
/// <c>WM_STYLECHANGING</c> (<c>HwndTarget</c>: "prevents an external program from making us system-layered"), also when
/// it updates its render settings later. A hook of the window (called before the one of WPF) keeps the style; it is only
/// added while the window is transparent. Disabled (0) on a window that was never transparent changes nothing.
/// </remarks>
public static class WindowTransparencyHelper
{
// https://learn.microsoft.com/windows/win32/winmsg/extended-window-styles
private const int GWL_EXSTYLE = -20;
private const int WS_EX_LAYERED = 0x00080000;

// https://learn.microsoft.com/windows/win32/winmsg/wm-stylechanging
private const int WM_STYLECHANGING = 0x007C;

// https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-setlayeredwindowattributes
private const int LWA_ALPHA = 0x00000002;

// https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-redrawwindow
private const int RDW_INVALIDATE = 0x0001;
private const int RDW_ERASE = 0x0004;
private const int RDW_ALLCHILDREN = 0x0080;
private const int RDW_FRAME = 0x0400;

[DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);

/// <summary>
/// Hook of the window, only set while the window is transparent.
/// </summary>
private static readonly DependencyProperty HookProperty =
DependencyProperty.RegisterAttached("Hook", typeof(HwndSourceHook), typeof(WindowTransparencyHelper),
new PropertyMetadata(null));

/// <summary>
/// Converts the transparency in percent (0 = opaque) to the alpha of the layered window (255 = opaque).
/// </summary>
/// <param name="transparency">Transparency in percent, clamped to 0 - 100.</param>
/// <returns>Alpha value (0 - 255).</returns>
public static byte GetAlpha(int transparency)
{
var value = Math.Clamp(transparency, 0, 100);

// Rounded half up (integer math, Math.Round would round 178.5 to 178)
return (byte)((255 * (100 - value) + 50) / 100);
}

/// <summary>
/// Makes the window transparent (<paramref name="transparency" /> in percent) or opaque again (0). With 0 on a window
/// that was never transparent, nothing is changed (no hook, no style). Does nothing before the window has a handle
/// (call it in / after SourceInitialized).
/// </summary>
/// <param name="window">Window to change.</param>
/// <param name="transparency">Transparency in percent (0 = opaque, removes the hook and the layered style again).</param>
public static void Apply(Window window, int transparency)
{
transparency = Math.Clamp(transparency, 0, 100);

// The hook is only added while the window is transparent, so it also marks a window changed here
var hook = (HwndSourceHook)window.GetValue(HookProperty);

// Disabled and never enabled: don't touch the window (same behavior as without this option)
if (transparency == 0 && hook == null)
return;

var handle = new WindowInteropHelper(window).Handle;

if (handle == IntPtr.Zero)
return;

var source = HwndSource.FromHwnd(handle);

if (source == null)
return;

var exStyle = GetWindowLongPtr(handle, GWL_EXSTYLE).ToInt64();
var isLayered = (exStyle & WS_EX_LAYERED) != 0;

if (transparency == 0)
{
// Opaque again: remove the hook first, then the layered style (WPF only allows it with the hook, so it was
// set here), so the window is the same as before
source.RemoveHook(hook);
window.ClearValue(HookProperty);

if (!isLayered)
return;

SetWindowLongPtr(handle, GWL_EXSTYLE, new IntPtr(exStyle & ~WS_EX_LAYERED));

// Windows doesn't repaint the window after the layered style is removed
RedrawWindow(handle, IntPtr.Zero, IntPtr.Zero, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

return;
}

// Add the hook before the style is changed, so WPF doesn't remove the layered style again
if (hook == null)
{
hook = WndProc;
source.AddHook(hook);
window.SetValue(HookProperty, hook);
}

if (!isLayered)
SetWindowLongPtr(handle, GWL_EXSTYLE, new IntPtr(exStyle | WS_EX_LAYERED));

SetLayeredWindowAttributes(handle, 0, GetAlpha(transparency), LWA_ALPHA);
}

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Keep the layered style (the hook is only added while the window is transparent). Handled, so WPF
// (HwndTarget) doesn't remove it again. STYLESTRUCT: styleOld (offset 0), styleNew (offset 4). wParam unchecked
// like WPF: ToInt32() throws on 64-bit if the value doesn't fit.
if (msg != WM_STYLECHANGING || unchecked((int)wParam.ToInt64()) != GWL_EXSTYLE)
return IntPtr.Zero;

var styleNew = Marshal.ReadInt32(lParam, 4);
Marshal.WriteInt32(lParam, 4, styleNew | WS_EX_LAYERED);

handled = true;

return IntPtr.Zero;
}
}
3 changes: 3 additions & 0 deletions Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NETworkManager.Models.RemoteDesktop;
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.Utilities.WPF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -425,6 +426,8 @@ protected override void OnSourceInitialized(EventArgs e)

_hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
_hwndSource?.AddHook(HwndHook);

WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency);
}

[DebuggerStepThrough]
Expand Down
13 changes: 13 additions & 0 deletions Source/NETworkManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NETworkManager.Settings;
using NETworkManager.Update;
using NETworkManager.Utilities;
using NETworkManager.Utilities.WPF;
using NETworkManager.ViewModels;
using NETworkManager.Views;
using System;
Expand Down Expand Up @@ -51,6 +52,16 @@ private void SettingsManager_PropertyChanged(object sender, PropertyChangedEvent

break;

// Change the transparency of the main window, the windows of dragged-out tabs, the status window and the
// notifications
case nameof(SettingsInfo.Appearance_UseTransparency):
case nameof(SettingsInfo.Appearance_Transparency):
foreach (var window in Application.Current.Windows.OfType<Window>()
.Where(x => x is MainWindow or DragablzTabHostWindow or StatusWindow or NotificationWindow))
WindowTransparencyHelper.Apply(window, AppearanceManager.WindowTransparency);

break;

// Update TrayIcon if changed in the settings
case nameof(SettingsInfo.TrayIcon_AlwaysShowIcon):
if (SettingsManager.Current.TrayIcon_AlwaysShowIcon && _notifyIcon == null)
Expand Down Expand Up @@ -1440,6 +1451,8 @@ protected override void OnSourceInitialized(EventArgs e)
_hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
_hwndSource?.AddHook(HwndHook);

WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency);

RegisterHotKeys();
}

Expand Down
3 changes: 3 additions & 0 deletions Source/NETworkManager/NotificationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NETworkManager.Models;
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.Utilities.WPF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -265,6 +266,8 @@ protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);

WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency);

ApplyPosition();
SlideIn();

Expand Down
Loading