|
| 1 | +// Based on AirspaceFixer (https://github.com/chris84948/AirspaceFixer) and its fork AirspaceFixerDotNet |
| 2 | +// (https://github.com/ellipticbit/AirspaceFixerDotNet), adapted for NETworkManager. |
| 3 | +// |
| 4 | +// MIT License |
| 5 | +// |
| 6 | +// Copyright (c) 2017 chris84948 |
| 7 | +// Copyright (c) 2024 EllipticBit, LLC |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in all |
| 17 | +// copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +// SOFTWARE. |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.Drawing.Imaging; |
| 29 | +using System.IO; |
| 30 | +using System.Windows; |
| 31 | +using System.Windows.Controls; |
| 32 | +using System.Windows.Media.Imaging; |
| 33 | + |
| 34 | +namespace NETworkManager.Controls; |
| 35 | + |
| 36 | +/// <summary> |
| 37 | +/// Hosts content with embedded windows (e.g. Remote Desktop, PuTTY, WebView2), which are always drawn on top of |
| 38 | +/// WPF elements (airspace issue). While <see cref="FixAirspace" /> is <see langword="true" />, the content is |
| 39 | +/// replaced by a screenshot of it, so that WPF elements like dialogs can be shown above it. |
| 40 | +/// The template is defined in <c>Resources/Styles/AirspacePanelStyles.xaml</c> of the application. |
| 41 | +/// </summary> |
| 42 | +[TemplatePart(Name = PartAirspaceContent, Type = typeof(ContentControl))] |
| 43 | +[TemplatePart(Name = PartAirspaceScreenshot, Type = typeof(Image))] |
| 44 | +public class AirspacePanel : ContentControl |
| 45 | +{ |
| 46 | + private const string PartAirspaceContent = "PART_AirspaceContent"; |
| 47 | + private const string PartAirspaceScreenshot = "PART_AirspaceScreenshot"; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Identifies the <see cref="FixAirspace" /> dependency property. |
| 51 | + /// </summary> |
| 52 | + public static readonly DependencyProperty FixAirspaceProperty = DependencyProperty.Register(nameof(FixAirspace), |
| 53 | + typeof(bool), typeof(AirspacePanel), new FrameworkPropertyMetadata(false, OnFixAirspaceChanged)); |
| 54 | + |
| 55 | + private ContentControl _airspaceContent; |
| 56 | + private Image _airspaceScreenshot; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Replace the content with a screenshot of it (e.g. while a dialog is shown above the content). |
| 60 | + /// </summary> |
| 61 | + public bool FixAirspace |
| 62 | + { |
| 63 | + get => (bool)GetValue(FixAirspaceProperty); |
| 64 | + set => SetValue(FixAirspaceProperty, value); |
| 65 | + } |
| 66 | + |
| 67 | + public override void OnApplyTemplate() |
| 68 | + { |
| 69 | + base.OnApplyTemplate(); |
| 70 | + |
| 71 | + _airspaceContent = GetTemplateChild(PartAirspaceContent) as ContentControl; |
| 72 | + _airspaceScreenshot = GetTemplateChild(PartAirspaceScreenshot) as Image; |
| 73 | + } |
| 74 | + |
| 75 | + private static void OnFixAirspaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 76 | + { |
| 77 | + if (d is AirspacePanel panel) |
| 78 | + panel.UpdateAirspace((bool)e.NewValue); |
| 79 | + } |
| 80 | + |
| 81 | + private void UpdateAirspace(bool fix) |
| 82 | + { |
| 83 | + // Template not applied (yet) |
| 84 | + if (_airspaceContent == null || _airspaceScreenshot == null) |
| 85 | + return; |
| 86 | + |
| 87 | + if (fix) |
| 88 | + { |
| 89 | + // Content not visible or not shown in a window - nothing to capture, keep the content |
| 90 | + var screenshot = CreateScreenshotFromContent(); |
| 91 | + |
| 92 | + if (screenshot == null) |
| 93 | + return; |
| 94 | + |
| 95 | + // Show the screenshot in the size of the content when it was taken (top left aligned in the template), so |
| 96 | + // it is not stretched / scaled if the window is resized while it is shown |
| 97 | + _airspaceScreenshot.Width = _airspaceContent.ActualWidth; |
| 98 | + _airspaceScreenshot.Height = _airspaceContent.ActualHeight; |
| 99 | + _airspaceScreenshot.Source = screenshot; |
| 100 | + _airspaceContent.Visibility = Visibility.Hidden; |
| 101 | + _airspaceScreenshot.Visibility = Visibility.Visible; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + // Always show the content again, even if the size changed in the meantime |
| 106 | + _airspaceContent.Visibility = Visibility.Visible; |
| 107 | + _airspaceScreenshot.Visibility = Visibility.Hidden; |
| 108 | + _airspaceScreenshot.Source = null; |
| 109 | + _airspaceScreenshot.ClearValue(WidthProperty); |
| 110 | + _airspaceScreenshot.ClearValue(HeightProperty); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private BitmapSource CreateScreenshotFromContent() |
| 115 | + { |
| 116 | + var presentationSource = PresentationSource.FromVisual(_airspaceContent); |
| 117 | + |
| 118 | + if (presentationSource?.CompositionTarget == null) |
| 119 | + return null; |
| 120 | + |
| 121 | + // The application is per monitor DPI aware (PerMonitorV2, see app.manifest): PointToScreen returns physical |
| 122 | + // pixels, but the size is in device independent pixels and has to be scaled with the DPI of the monitor. |
| 123 | + var transformToDevice = presentationSource.CompositionTarget.TransformToDevice; |
| 124 | + |
| 125 | + var upperLeftPoint = _airspaceContent.PointToScreen(new Point(0, 0)); |
| 126 | + var width = (int)Math.Round(_airspaceContent.ActualWidth * transformToDevice.M11); |
| 127 | + var height = (int)Math.Round(_airspaceContent.ActualHeight * transformToDevice.M22); |
| 128 | + |
| 129 | + if (width <= 0 || height <= 0) |
| 130 | + return null; |
| 131 | + |
| 132 | + using var bitmap = new System.Drawing.Bitmap(width, height); |
| 133 | + |
| 134 | + using (var graphics = System.Drawing.Graphics.FromImage(bitmap)) |
| 135 | + { |
| 136 | + graphics.CopyFromScreen((int)Math.Round(upperLeftPoint.X), (int)Math.Round(upperLeftPoint.Y), 0, 0, |
| 137 | + new System.Drawing.Size(width, height)); |
| 138 | + } |
| 139 | + |
| 140 | + return GetBitmapSource(bitmap); |
| 141 | + } |
| 142 | + |
| 143 | + private static BitmapSource GetBitmapSource(System.Drawing.Bitmap bitmap) |
| 144 | + { |
| 145 | + using var memoryStream = new MemoryStream(); |
| 146 | + |
| 147 | + bitmap.Save(memoryStream, ImageFormat.Png); |
| 148 | + memoryStream.Position = 0; |
| 149 | + |
| 150 | + var bitmapImage = new BitmapImage(); |
| 151 | + bitmapImage.BeginInit(); |
| 152 | + bitmapImage.StreamSource = memoryStream; |
| 153 | + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; |
| 154 | + bitmapImage.EndInit(); |
| 155 | + bitmapImage.Freeze(); |
| 156 | + |
| 157 | + return bitmapImage; |
| 158 | + } |
| 159 | +} |
0 commit comments