Skip to content

Commit

Permalink
CQ: Fix possible error when loading TitleBar property
Browse files Browse the repository at this point in the history
  • Loading branch information
XTorLukas committed Dec 2, 2024
1 parent b2e8305 commit ea9dabc
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/Files.App/UserControls/TabBar/TabBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ public TabBar()
tabHoverTimer.Interval = TimeSpan.FromMilliseconds(Constants.DragAndDrop.HoverToOpenTimespan);
tabHoverTimer.Tick += TabHoverSelected;

var appWindow = MainWindow.Instance.AppWindow;

TitleBarWidth =
new(FilePropertiesHelpers.FlowDirectionSettingIsRightToLeft
? appWindow.TitleBar.LeftInset
: appWindow.TitleBar.RightInset);
InitializeTitleBarAsync();

AppearanceSettingsService.PropertyChanged += (s, e) =>
{
Expand All @@ -94,6 +89,40 @@ public TabBar()
};
}

/// <summary>
/// Initializes the title bar asynchronously. This method attempts to get the title bar's left and right insets.
/// If the values are properly initialized, it sets the <see cref="TitleBarWidth"/> property. If the initialization fails,
/// it falls back to a default width.
/// </summary>
private async void InitializeTitleBarAsync()
{
const int maxAttempts = 10; // Maximum number of attempts (1 second total wait)
const int delayInterval = 100; // Delay interval in milliseconds
const double defaultWidth = 138; // Default width if initialization fails

var appWindow = MainWindow.Instance.AppWindow;

for (int attempt = 0; attempt < maxAttempts; attempt++)
{
// If TitleBar values are properly initialized, set TitleBarWidth
if (appWindow?.TitleBar.LeftInset != appWindow?.TitleBar.RightInset)
{
TitleBarWidth = new GridLength(
FilePropertiesHelpers.FlowDirectionSettingIsRightToLeft
? appWindow!.TitleBar.LeftInset
: appWindow!.TitleBar.RightInset
);
return;
}

// Wait for the next attempt
await Task.Delay(delayInterval);
}

// Fallback to default width if initialization fails
TitleBarWidth = new GridLength(defaultWidth);
}

private void TabView_TabItemsChanged(TabView sender, Windows.Foundation.Collections.IVectorChangedEventArgs args)
{
if (args.CollectionChange == Windows.Foundation.Collections.CollectionChange.ItemRemoved)
Expand Down

0 comments on commit ea9dabc

Please sign in to comment.