Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Files.App/UserControls/TabBar/TabBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition x:Name="RightPaddingColumn" Width="0" />
<ColumnDefinition x:Name="RightPaddingColumn" Width="{x:Bind TitleBarWidth, Mode=OneWay}" />
</Grid.ColumnDefinitions>

<!-- Height is not divisble by four in order to properly align the button -->
Expand Down Expand Up @@ -260,7 +260,8 @@
Grid.ColumnSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Fill="Transparent" />
Fill="Transparent"
Loaded="DragAreaRectangle_Loaded" />

</Grid>
</TabView.TabStripFooter>
Expand Down
67 changes: 59 additions & 8 deletions src/Files.App/UserControls/TabBar/TabBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.WinUI.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Shapes;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics;
using Windows.Storage;
using Windows.Win32;

Expand All @@ -28,6 +30,8 @@ public sealed partial class TabBar : BaseTabBar, INotifyPropertyChanged

private bool _lockDropOperation = false;

private static int _gap = 40;

// Starting position when dragging a tab
private System.Drawing.Point dragStartPoint;

Expand All @@ -51,6 +55,14 @@ public bool AllowTabsDrag
public Rectangle DragArea
=> DragAreaRectangle;

private GridLength _titleBarWidth;

public GridLength TitleBarWidth
{
get => _titleBarWidth;
set => _titleBarWidth = new((value.Value / App.AppModel.AppWindowDPI) + _gap);
}

// Events

public static event EventHandler<TabBarItem?>? SelectedTabItemChanged;
Expand All @@ -64,14 +76,7 @@ public TabBar()
tabHoverTimer.Interval = TimeSpan.FromMilliseconds(Constants.DragAndDrop.HoverToOpenTimespan);
tabHoverTimer.Tick += TabHoverSelected;

var appWindow = MainWindow.Instance.AppWindow;

double rightPaddingColumnWidth =
FilePropertiesHelpers.FlowDirectionSettingIsRightToLeft
? appWindow.TitleBar.LeftInset
: appWindow.TitleBar.RightInset;

RightPaddingColumn.Width = new(rightPaddingColumnWidth >= 0 ? rightPaddingColumnWidth : 0);
InitializeTitleBarAsync();

AppearanceSettingsService.PropertyChanged += (s, e) =>
{
Expand All @@ -84,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 Expand Up @@ -366,5 +405,17 @@ private void TabViewItem_Loaded(object sender, RoutedEventArgs e)
});
}
}

private void DragAreaRectangle_Loaded(object sender, RoutedEventArgs e)
=> HorizontalTabView.Measure(new(HorizontalTabView.ActualWidth - TabBarAddNewTabButton.Width - TitleBarWidth.Value, HorizontalTabView.ActualHeight));

public int SetTitleBarDragRegion(InputNonClientPointerSource source, SizeInt32 size, double scaleFactor, Func<UIElement, RectInt32?, RectInt32> getScaledRect)
{
var height = (int)ActualHeight;
var width = (int)(ActualWidth - DragArea.ActualWidth);

source.SetRegionRects(NonClientRegionKind.Passthrough, [getScaledRect(this, new RectInt32(0, 0, width, height))]);
return height;
}
}
}
13 changes: 4 additions & 9 deletions src/Files.App/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ private void UserSettingsService_OnSettingChangedEvent(object? sender, SettingCh

private void HorizontalMultitaskingControl_Loaded(object sender, RoutedEventArgs e)
{
TabControl.DragArea.SizeChanged += (_, _) => MainWindow.Instance.RaiseSetTitleBarDragRegion(SetTitleBarDragRegion);
TabControl.DragArea.SizeChanged += (_, _) => MainWindow.Instance.RaiseSetTitleBarDragRegion(TabControl.SetTitleBarDragRegion);
TabControl.SizeChanged += (_, _) => MainWindow.Instance.RaiseSetTitleBarDragRegion(TabControl.SetTitleBarDragRegion);

if (ViewModel.MultitaskingControl is not TabBar)
{
ViewModel.MultitaskingControl = TabControl;
Expand All @@ -135,13 +137,6 @@ private void HorizontalMultitaskingControl_Loaded(object sender, RoutedEventArgs
}
}

private int SetTitleBarDragRegion(InputNonClientPointerSource source, SizeInt32 size, double scaleFactor, Func<UIElement, RectInt32?, RectInt32> getScaledRect)
{
var height = (int)TabControl.ActualHeight;
source.SetRegionRects(NonClientRegionKind.Passthrough, [getScaledRect(this, new RectInt32(0, 0, (int)(TabControl.ActualWidth + TabControl.Margin.Left - TabControl.DragArea.ActualWidth), height))]);
return height;
}

public async void TabItemContent_ContentChanged(object? sender, TabBarItemParameter e)
{
if (SidebarAdaptiveViewModel.PaneHolder is null)
Expand Down Expand Up @@ -279,7 +274,7 @@ protected override void OnLostFocus(RoutedEventArgs e)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
MainWindow.Instance.AppWindow.Changed += (_, _) => MainWindow.Instance.RaiseSetTitleBarDragRegion(SetTitleBarDragRegion);
MainWindow.Instance.AppWindow.Changed += (_, _) => MainWindow.Instance.RaiseSetTitleBarDragRegion(TabControl.SetTitleBarDragRegion);

// Defers the status bar loading until after the page has loaded to improve startup perf
FindName(nameof(StatusBar));
Expand Down