Skip to content

Code Quality: Refactor InfoPane selection #14095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion src/Files.App/UserControls/Pane/InfoPane.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
d:DesignHeight="300"
d:DesignWidth="400"
AutomationProperties.Name="{helpers:ResourceString Name=SelectedFilePreviewPane/AutomationProperties/Name}"
Loaded="Root_Loaded"
SizeChanged="Root_SizeChanged"
Unloaded="Root_Unloaded"
mc:Ignorable="d">
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/UserControls/Pane/InfoPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public void UpdatePosition(double panelWidth, double panelHeight)

private string GetLocalizedResource(string resName) => resName.GetLocalizedResource();

private void Root_Loaded(object sender, RoutedEventArgs e)
=> ViewModel?.UpdateSelectedItemPreviewAsync();

private void Root_Unloaded(object sender, RoutedEventArgs e)
{
PreviewControlPresenter.Content = null;
Expand Down
65 changes: 46 additions & 19 deletions src/Files.App/ViewModels/UserControls/InfoPaneViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace Files.App.ViewModels.UserControls
public class InfoPaneViewModel : ObservableObject, IDisposable
{
private IInfoPaneSettingsService infoPaneSettingsService { get; } = Ioc.Default.GetRequiredService<IInfoPaneSettingsService>();

private readonly IContentPageContext contentPageContextService;
private IContentPageContext contentPageContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>();

private CancellationTokenSource loadCancellationTokenSource;

/// <summary>
/// Value indicating if the info pane is on/off
/// </summary>
private bool isEnabled;
public bool IsEnabled
{
Expand All @@ -30,13 +32,10 @@ public bool IsEnabled
}
}

private bool isItemSelected;
public bool IsItemSelected
{
get => isItemSelected;
set => SetProperty(ref isItemSelected, value);
}

/// <summary>
/// Current selected item in the file list.
/// TODO see about removing this and accessing it from the page context instead
/// </summary>
private ListedItem selectedItem;
public ListedItem SelectedItem
{
Expand All @@ -57,6 +56,9 @@ public ListedItem SelectedItem
}
}

/// <summary>
/// Enum indicating whether to show the details or preview tab
/// </summary>
public InfoPaneTabs SelectedTab
{
get => infoPaneSettingsService.SelectedTab;
Expand All @@ -69,6 +71,9 @@ public InfoPaneTabs SelectedTab
}
}

/// <summary>
/// Enum indicating if details/preview are available
/// </summary>
private PreviewPaneStates previewPaneState;
public PreviewPaneStates PreviewPaneState
{
Expand All @@ -80,6 +85,9 @@ public PreviewPaneStates PreviewPaneState
}
}

/// <summary>
/// Value indicating if the download cloud files option should be displayed
/// </summary>
private bool showCloudItemButton;
public bool ShowCloudItemButton
{
Expand All @@ -101,13 +109,31 @@ PreviewPaneState is PreviewPaneStates.NoPreviewAvailable ||

public ObservableCollection<TagsListItem> Items { get; } = new();

public InfoPaneViewModel(IContentPageContext contentPageContextService = null)
public InfoPaneViewModel()
{
infoPaneSettingsService.PropertyChanged += PreviewSettingsService_OnPropertyChangedEvent;
contentPageContext.PropertyChanged += ContentPageContext_PropertyChanged;

IsEnabled = infoPaneSettingsService.IsEnabled;
}

this.contentPageContextService = contentPageContextService ?? Ioc.Default.GetRequiredService<IContentPageContext>();
private void ContentPageContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.Folder):
case nameof(IContentPageContext.SelectedItem):

if (contentPageContext.SelectedItems.Count == 1)
SelectedItem = contentPageContext.SelectedItems.First();
else
SelectedItem = null;

var shouldUpdatePreview = ((MainWindow.Instance.Content as Frame)?.Content as MainPage)?.ShouldPreviewPaneBeActive;
if (shouldUpdatePreview == true)
_ = UpdateSelectedItemPreviewAsync();
break;
}
}

private async Task LoadPreviewControlAsync(CancellationToken token, bool downloadItem)
Expand Down Expand Up @@ -148,7 +174,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b
{
ShowCloudItemButton = false;

if (SelectedItem.IsRecycleBinItem)
if (item.IsRecycleBinItem)
{
if (item.PrimaryItemAttribute == StorageItemTypes.Folder && !item.IsArchive)
{
Expand All @@ -159,7 +185,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b
}
else
{
var model = new BasicPreviewViewModel(SelectedItem);
var model = new BasicPreviewViewModel(item);
await model.LoadAsync();

return new BasicPreview(model);
Expand All @@ -168,7 +194,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b

if (item.IsShortcut)
{
var model = new ShortcutPreviewViewModel(SelectedItem);
var model = new ShortcutPreviewViewModel(item);
await model.LoadAsync();

return new BasicPreview(model);
Expand All @@ -187,7 +213,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b
var model = new FolderPreviewViewModel(item);
await model.LoadAsync();

if (!isItemSelected)
if (contentPageContext.SelectedItems.Count == 0)
item.FileTags ??= FileTagsHelper.ReadFileTag(item.ItemPath);

return new FolderPreview(model);
Expand All @@ -206,7 +232,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b
var ext = item.FileExtension.ToLowerInvariant();

if (!item.IsFtpItem &&
contentPageContextService.PageType != ContentPageTypes.ZipFolder &&
contentPageContext.PageType != ContentPageTypes.ZipFolder &&
(FileExtensionHelpers.IsAudioFile(ext) || FileExtensionHelpers.IsVideoFile(ext)))
{
var model = new MediaPreviewViewModel(item);
Expand Down Expand Up @@ -291,7 +317,7 @@ private async Task<UserControl> GetBuiltInPreviewControlAsync(ListedItem item, b
public async Task UpdateSelectedItemPreviewAsync(bool downloadItem = false)
{
loadCancellationTokenSource?.Cancel();
if (SelectedItem is not null && IsItemSelected)
if (SelectedItem is not null && contentPageContext.SelectedItems.Count == 1)
{
SelectedItem?.FileDetails?.Clear();

Expand Down Expand Up @@ -328,15 +354,15 @@ public async Task UpdateSelectedItemPreviewAsync(bool downloadItem = false)
PreviewPaneState = PreviewPaneStates.NoPreviewOrDetailsAvailable;
}
}
else if (IsItemSelected)
else if (contentPageContext.SelectedItems.Count > 0)
{
PreviewPaneContent = null;
PreviewPaneState = PreviewPaneStates.NoPreviewOrDetailsAvailable;
}
else
{
SelectedItem?.FileDetails?.Clear();
var currentFolder = contentPageContextService.Folder;
var currentFolder = contentPageContext.Folder;

if (currentFolder is null)
{
Expand Down Expand Up @@ -388,6 +414,7 @@ private async void PreviewSettingsService_OnPropertyChangedEvent(object? sender,
if (isEnabled != newEnablingStatus)
{
isEnabled = newEnablingStatus;
_ = UpdateSelectedItemPreviewAsync();
OnPropertyChanged(nameof(IsEnabled));
}
}
Expand Down
31 changes: 0 additions & 31 deletions src/Files.App/Views/Layouts/BaseLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ internal set
{
if (value != selectedItems)
{
UpdatePreviewPaneSelection(value);

selectedItems = value;

if (selectedItems?.Count == 0 || selectedItems?[0] is null)
Expand Down Expand Up @@ -507,9 +505,6 @@ navigationArguments.SelectItems is not null &&
}
else if (navigationArguments is not null && navigationArguments.FocusOnNavigation)
{
if (SelectedItems?.Count == 0)
UpdatePreviewPaneSelection(null);

// Set focus on layout specific file list control
ItemManipulationModel.FocusFileList();
}
Expand Down Expand Up @@ -1429,32 +1424,6 @@ await DispatcherQueue.EnqueueOrInvokeAsync(() =>
}
}

public void ReloadPreviewPane()
{
UpdatePreviewPaneSelection(SelectedItems);
}

protected void UpdatePreviewPaneSelection(List<ListedItem>? value)
{
if (LockPreviewPaneContent)
return;

if (value?.FirstOrDefault() != InfoPaneViewModel.SelectedItem)
{
// Update preview pane properties
InfoPaneViewModel.IsItemSelected = value?.Count > 0;
InfoPaneViewModel.SelectedItem = value?.Count == 1 ? value.First() : null;

// Check if the preview pane is open before updating the model
if (InfoPaneViewModel.IsEnabled && !App.AppModel.IsMainWindowClosed)
{
var isPaneEnabled = ((MainWindow.Instance.Content as Frame)?.Content as MainPage)?.ShouldPreviewPaneBeActive ?? false;
if (isPaneEnabled)
_ = InfoPaneViewModel.UpdateSelectedItemPreviewAsync();
}
}
}

public class ContextMenuExtensions : DependencyObject
{
public static ItemsControl GetItemsControl(DependencyObject obj)
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Views/Layouts/ColumnsLayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,6 @@ public void MoveFocusToNextBlade(int currentBladeIndex)
if (activeBladeColumnViewBase is not null)
{
activeBladeColumnViewBase.FileList.SelectedIndex = 0;
var selectedItem = activeBladeColumnViewBase.FileList.Items.FirstOrDefault() as ListedItem;
if (selectedItem is not null)
UpdatePreviewPaneSelection(new List<ListedItem>() { selectedItem });
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/Views/Layouts/IBaseLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ public interface IBaseLayoutPage : IDisposable
CommandBarFlyout ItemContextMenuFlyout { get; set; }

CommandBarFlyout BaseContextMenuFlyout { get; set; }

void ReloadPreviewPane();
}
}
2 changes: 0 additions & 2 deletions src/Files.App/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ public void MultitaskingControl_CurrentInstanceChanged(object? sender, CurrentIn

e.CurrentInstance.ContentChanged -= TabItemContent_ContentChanged;
e.CurrentInstance.ContentChanged += TabItemContent_ContentChanged;

SidebarAdaptiveViewModel.PaneHolder?.ActivePaneOrColumn.SlimContentPage?.ReloadPreviewPane();
}

private void PaneHolder_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
8 changes: 0 additions & 8 deletions src/Files.App/Views/PaneHolderPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,7 @@ private async void Pane_GotFocus(object sender, RoutedEventArgs e)

var activePane = isLeftPane ? PaneLeft : PaneRight;
if (ActivePane != activePane)
{
ActivePane = activePane;

if (ActivePane?.SlimContentPage is IBaseLayoutPage page && !page.IsItemSelected)
{
page.InfoPaneViewModel.IsItemSelected = false;
await page.InfoPaneViewModel.UpdateSelectedItemPreviewAsync();
}
}
}

private void Pane_RightTapped(object sender, RoutedEventArgs e)
Expand Down