diff --git a/CustomizeVSWindowTitleShared/CustomizeVSWindowTitlePackage.cs b/CustomizeVSWindowTitleShared/CustomizeVSWindowTitlePackage.cs index 0461217..a1fbce2 100644 --- a/CustomizeVSWindowTitleShared/CustomizeVSWindowTitlePackage.cs +++ b/CustomizeVSWindowTitleShared/CustomizeVSWindowTitlePackage.cs @@ -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 { @@ -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 { @@ -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) { @@ -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(mw, "PART_SolutionNameTextBlock"); + + if (statusBartTextBlock is null) + { + return; + } + + var textBlock = FindChild(statusBartTextBlock); + textBlock.Text = title; + } + public static void WriteOutput(string str, params object[] args) { try { Globals.InvokeOnUIThread(() => { @@ -594,5 +619,65 @@ public static void WriteOutput(string str, params object[] args) { // ignored } } + + + /// + /// Finds a Child of a given item in the visual tree. + /// + /// A direct parent of the queried item. + /// The type of the queried item. + /// x:Name or Name of child. + /// + /// IF is not or empty + /// the first element to have that name is returned. + /// If is the + /// first element to match is returned. + /// If not matching item can be found, + /// a is returned. + private static T FindChild(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(child, childName); + + // If the child is found, break so we do not overwrite the found child. + if (foundChild != null) break; + } + + return foundChild; + } } }