This repository was archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeRangeWindowsPackage.cs
More file actions
133 lines (124 loc) · 6 KB
/
FreeRangeWindowsPackage.cs
File metadata and controls
133 lines (124 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using Microsoft.VisualStudio.Platform.WindowManagement;
using Microsoft.VisualStudio.PlatformUI.Shell;
using Microsoft.VisualStudio.PlatformUI.Shell.Controls;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace Shmuelie.FreeRangeWindows
{
[ProvideAutoLoad(UIContextGuids80.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[Guid(FreeRangeWindowsPackage.PackageGuidString)]
[CLSCompliant(false)]
public sealed class FreeRangeWindowsPackage : AsyncPackage
{
/// <summary>
/// FreeRangeWindowsPackage GUID string.
/// </summary>
public const string PackageGuidString = "214337a7-2957-45e5-b42e-b8cf91cbe11f";
private static readonly PriorityMultiValueConverter priorityMultiValueConverter = new PriorityMultiValueConverter();
protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress);
if (cancellationToken.IsCancellationRequested)
{
return;
}
ActivityLog.LogInformation(nameof(FreeRangeWindows), "Registering for FloatingWindow changes");
UIElement.VisibilityProperty.OverrideMetadata(typeof(FloatingWindow), new FrameworkPropertyMetadata(Visibility.Collapsed, FloatingWindowVisibilityChanged));
ViewElement.IsSelectedProperty.OverrideMetadata(typeof(View), new PropertyMetadata(false, ViewElementBooleanDependencyPropertyChanged));
ViewElement.IsSelectedProperty.OverrideMetadata(typeof(ToolWindowView), new PropertyMetadata(false, ViewElementBooleanDependencyPropertyChanged));
View.IsActiveProperty.OverrideMetadata(typeof(ToolWindowView), new FrameworkPropertyMetadata(false, ViewElementBooleanDependencyPropertyChanged));
}
#pragma warning disable U2U1003 // Avoid declaring methods used in delegate constructors static
private static void ViewElementBooleanDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
#pragma warning restore U2U1003 // Avoid declaring methods used in delegate constructors static
{
if ((bool)e.NewValue && d != null && d is View view && view.Content != null)
{
UIElement content = (UIElement)view.Content;
Window window = Window.GetWindow(content);
if (window != null)
{
BindTitle(window, view);
}
else
{
void isVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
if ((bool)args.NewValue)
{
BindTitle(Window.GetWindow((UIElement)sender), view);
}
}
content.IsVisibleChanged += isVisibleChanged;
}
}
}
private static void BindTitle(Window window, View view)
{
Window mainWindow = Application.Current.MainWindow;
if (window == mainWindow)
{
return;
}
Binding mainTitle = new Binding("Title")
{
Mode = BindingMode.OneWay,
Source = mainWindow
};
Binding simpleViewTitle = new Binding("Title")
{
Mode = BindingMode.OneWay,
Source = view
};
Binding complexViewTitle = new Binding("Title.Title")
{
Mode = BindingMode.OneWay,
Source = view
};
window.SetBinding(Window.TitleProperty, new MultiBinding() { Bindings = { mainTitle, complexViewTitle, simpleViewTitle }, Converter = priorityMultiValueConverter, Mode = BindingMode.OneWay });
}
private sealed class PriorityMultiValueConverter : IMultiValueConverter
{
object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
// Treat first as special
string prefix = values[0].ToString();
for (int i = 1; i < values.Length; i++)
{
object value = values[i];
if (value != null && value != DependencyProperty.UnsetValue)
{
return string.Concat(prefix, " - ", value);
}
}
return prefix;
}
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => null;
}
#pragma warning disable U2U1003 // Avoid declaring methods used in delegate constructors static
private static void FloatingWindowVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
#pragma warning restore U2U1003 // Avoid declaring methods used in delegate constructors static
{
if ((Visibility)e.NewValue == Visibility.Visible)
{
FloatingWindow window = (FloatingWindow)d;
window.ChangeOwner(IntPtr.Zero);
window.ShowInTaskbar = true;
ActivityLog.LogInformation(nameof(FreeRangeWindows), "Making " + window.Title + " free range");
}
}
}
}