Skip to content

Commit ea9dabc

Browse files
committed
CQ: Fix possible error when loading TitleBar property
1 parent b2e8305 commit ea9dabc

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/Files.App/UserControls/TabBar/TabBar.xaml.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ public TabBar()
7676
tabHoverTimer.Interval = TimeSpan.FromMilliseconds(Constants.DragAndDrop.HoverToOpenTimespan);
7777
tabHoverTimer.Tick += TabHoverSelected;
7878

79-
var appWindow = MainWindow.Instance.AppWindow;
80-
81-
TitleBarWidth =
82-
new(FilePropertiesHelpers.FlowDirectionSettingIsRightToLeft
83-
? appWindow.TitleBar.LeftInset
84-
: appWindow.TitleBar.RightInset);
79+
InitializeTitleBarAsync();
8580

8681
AppearanceSettingsService.PropertyChanged += (s, e) =>
8782
{
@@ -94,6 +89,40 @@ public TabBar()
9489
};
9590
}
9691

92+
/// <summary>
93+
/// Initializes the title bar asynchronously. This method attempts to get the title bar's left and right insets.
94+
/// If the values are properly initialized, it sets the <see cref="TitleBarWidth"/> property. If the initialization fails,
95+
/// it falls back to a default width.
96+
/// </summary>
97+
private async void InitializeTitleBarAsync()
98+
{
99+
const int maxAttempts = 10; // Maximum number of attempts (1 second total wait)
100+
const int delayInterval = 100; // Delay interval in milliseconds
101+
const double defaultWidth = 138; // Default width if initialization fails
102+
103+
var appWindow = MainWindow.Instance.AppWindow;
104+
105+
for (int attempt = 0; attempt < maxAttempts; attempt++)
106+
{
107+
// If TitleBar values are properly initialized, set TitleBarWidth
108+
if (appWindow?.TitleBar.LeftInset != appWindow?.TitleBar.RightInset)
109+
{
110+
TitleBarWidth = new GridLength(
111+
FilePropertiesHelpers.FlowDirectionSettingIsRightToLeft
112+
? appWindow!.TitleBar.LeftInset
113+
: appWindow!.TitleBar.RightInset
114+
);
115+
return;
116+
}
117+
118+
// Wait for the next attempt
119+
await Task.Delay(delayInterval);
120+
}
121+
122+
// Fallback to default width if initialization fails
123+
TitleBarWidth = new GridLength(defaultWidth);
124+
}
125+
97126
private void TabView_TabItemsChanged(TabView sender, Windows.Foundation.Collections.IVectorChangedEventArgs args)
98127
{
99128
if (args.CollectionChange == Windows.Foundation.Collections.CollectionChange.ItemRemoved)

0 commit comments

Comments
 (0)