Skip to content

Commit

Permalink
Merge pull request mayerwin#66 from RedX2501/add-compact-menubar-support
Browse files Browse the repository at this point in the history
Adds support for changing compact menu title
  • Loading branch information
mayerwin authored Feb 27, 2022
2 parents e303e91 + 04c9486 commit 2b6efbc
Showing 1 changed file with 89 additions and 4 deletions.
93 changes: 89 additions & 4 deletions CustomizeVSWindowTitleShared/CustomizeVSWindowTitlePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.VisualStudio.PlatformUI;
using SolutionEvents = Microsoft.VisualStudio.Shell.Events.SolutionEvents;
using Task = System.Threading.Tasks.Task;
using Window = EnvDTE.Window;

//If refactoring, do not change namespace as it'd cause existing settings to be lost.
namespace ErwinMayerLabs.RenameVSWindowTitle {
Expand Down Expand Up @@ -398,7 +403,9 @@ select this.GetVSSolutionName(vsInstance.MainWindowTitle)).Any(vsInstanceName =>
var settings = this.GetSettings(solutionFp);

var pattern = this.GetPattern(solutionFp, useDefaultPattern, settings);
this.ChangeWindowTitle(this.GetNewTitle(solution, pattern, settings));
var newTitle = this.GetNewTitle(solution, pattern, settings);
this.ChangeWindowTitle(newTitle);
this.ChangeXamlTitle(newTitle);
}
catch (Exception ex) {
try {
Expand Down Expand Up @@ -551,10 +558,12 @@ private void ChangeWindowTitle(string title) {
try {
Globals.BeginInvokeOnUIThread(() => {
try {
System.Windows.Application.Current.MainWindow.Title = Globals.DTE.MainWindow.Caption;
if (System.Windows.Application.Current.MainWindow.Title != title) {
System.Windows.Application.Current.MainWindow.Title = title;
var mw = System.Windows.Application.Current.MainWindow;
mw.Title = Globals.DTE.MainWindow.Caption;
if (mw.Title != title) {
mw.Title = title;
}

//foreach (var windowObj in System.Windows.Application.Current.Windows) {
// var window = windowObj as Microsoft.VisualStudio.PlatformUI.Shell.Controls.FloatingWindow;
// if (window != null) {
Expand All @@ -577,6 +586,22 @@ private void ChangeWindowTitle(string title) {
}
}

private void ChangeXamlTitle(string title)
{
var mw = System.Windows.Application.Current.MainWindow;
// This part was found by looking at the Document tree. Could possibly change in future releases
// but this is the name for 2019 and 2022.
var statusBartTextBlock = FindChild<DependencyObject>(mw, "PART_SolutionNameTextBlock");

if (statusBartTextBlock is null)
{
return;
}

var textBlock = FindChild<TextBlock>(statusBartTextBlock);
textBlock.Text = title;
}

public static void WriteOutput(string str, params object[] args) {
try {
Globals.InvokeOnUIThread(() => {
Expand All @@ -594,5 +619,65 @@ public static void WriteOutput(string str, params object[] args) {
// ignored
}
}


/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child.</param>
/// <returns>
/// IF <paramref name="childName"/> is not <see langword="null"/> or empty
/// the first element to have that name is returned.
/// If <paramref name="childName"/> is <see langword="null"/> the
/// first element to match <typeparamref name="T"/> is returned.
/// If not matching item can be found,
/// a <see langword="null"/> is returned.</returns>
private static T FindChild<T>(DependencyObject parent, string childName = null)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;

T foundChild = null;

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (!string.IsNullOrEmpty(childName))
{
// If the child is not of the request child type child
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null)
{
if (frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
}
else
{
if (child is T typedChild)
{
foundChild = typedChild;
break;
}
}

// recursively drill down the tree
foundChild = FindChild<T>(child, childName);

// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}

return foundChild;
}
}
}

0 comments on commit 2b6efbc

Please sign in to comment.