Skip to content

Commit

Permalink
Code Quality: Renamed layout page classes to improve readability (fil…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Nov 22, 2023
1 parent 1e1bbe7 commit b3c4d83
Show file tree
Hide file tree
Showing 45 changed files with 281 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ContentPageContext : ObservableObject, IContentPageContext

public IShellPage? ShellPage => context?.PaneOrColumn;

public Type PageLayoutType => ShellPage?.CurrentPageType ?? typeof(DetailsLayoutBrowser);
public Type PageLayoutType => ShellPage?.CurrentPageType ?? typeof(DetailsLayoutPage);

private ContentPageTypes pageType = ContentPageTypes.None;
public ContentPageTypes PageType => pageType;
Expand Down
10 changes: 5 additions & 5 deletions src/Files.App/Data/Models/FolderSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public Type GetLayoutType(string folderPath, bool changeLayoutMode = true)

return (prefsForPath.LayoutMode) switch
{
FolderLayoutModes.DetailsView => typeof(DetailsLayoutBrowser),
FolderLayoutModes.TilesView => typeof(GridViewBrowser),
FolderLayoutModes.GridView => typeof(GridViewBrowser),
FolderLayoutModes.ColumnView => typeof(ColumnViewBrowser),
_ => typeof(DetailsLayoutBrowser)
FolderLayoutModes.DetailsView => typeof(DetailsLayoutPage),
FolderLayoutModes.TilesView => typeof(GridLayoutPage),
FolderLayoutModes.GridView => typeof(GridLayoutPage),
FolderLayoutModes.ColumnView => typeof(ColumnsLayoutPage),
_ => typeof(DetailsLayoutPage)
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Data/Parameters/ColumnParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.Views;
using Files.App.Views.LayoutModes;
using Files.App.Views.Layouts;
using Microsoft.UI.Xaml.Controls;

namespace Files.App.Data.Parameters
Expand All @@ -13,6 +13,6 @@ public class ColumnParam : NavigationArguments

public ListView ListView { get; set; }

public ColumnViewBase? Source { get; set; }
public ColumnLayoutPage? Source { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Files.App/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
global using global::Files.App.ViewModels;
global using global::Files.App.ViewModels.UserControls;
global using global::Files.App.Views;
global using global::Files.App.Views.LayoutModes;
global using global::Files.App.Views.Layouts;
global using global::Files.App.Views.Shells;

// Files.Core
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Helpers/Interop/InteropHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public static IntPtr SetWindowLong(HWND hWnd, WindowLongFlags nIndex, IntPtr dwN
else
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}

[DllImport("User32.dll")]
public extern static short GetKeyState(int n);
}

[ComImport]
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/MenuFlyout/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.ViewModels.LayoutModes;
using Files.App.ViewModels.Layouts;
using Files.Shared.Helpers;
using Files.App.Helpers.ContextFlyouts;
using Files.App.ViewModels.LayoutModes;
using Files.App.ViewModels.Layouts;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using System.IO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
using Windows.Storage;
using Windows.System;

namespace Files.App.ViewModels.LayoutModes
namespace Files.App.ViewModels.Layouts
{
/// <summary>
/// Represents ViewModel for <see cref="BaseLayout"/>.
/// Represents ViewModel for <see cref="BaseLayoutPage"/>.
/// </summary>
public class BaseLayoutViewModel : IDisposable
{
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/ViewModels/UserControls/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ private void CloseSearchBox(bool doFocus = false)
{
var page = Ioc.Default.GetRequiredService<IContentPageContext>().ShellPage?.SlimContentPage;

if (page is StandardViewBase svb && svb.IsLoaded)
if (page is BaseGroupableLayoutPage svb && svb.IsLoaded)
page.ItemManipulationModel.FocusFileList();
else
AddressToolbar?.Focus(FocusState.Programmatic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.WinUI.UI;
using Files.App.ViewModels.LayoutModes;
using Files.App.ViewModels.Layouts;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using System.Runtime.InteropServices;
using Windows.System;
using Windows.UI.Core;

namespace Files.App.Views.LayoutModes
namespace Files.App.Views.Layouts
{
public abstract class StandardViewBase : BaseLayout
/// <summary>
/// Represents layout page that can be grouped by.
/// </summary>
public abstract class BaseGroupableLayoutPage : BaseLayoutPage
{
// Constants

private const int KEY_DOWN_MASK = 0x8000;

// Fields

protected int NextRenameIndex = 0;

// Properties

protected abstract ListViewBase ListViewBase { get; }
protected abstract SemanticZoom RootZoom { get; }

protected override ItemsControl ItemsControl => ListViewBase;

protected abstract SemanticZoom RootZoom { get; }
// Constructor

public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();

public StandardViewBase() : base()
public BaseGroupableLayoutPage() : base()
{
}

// Abstract methods

protected abstract void ItemManipulationModel_AddSelectedItemInvoked(object? sender, ListedItem e);
protected abstract void ItemManipulationModel_RemoveSelectedItemInvoked(object? sender, ListedItem e);
protected abstract void ItemManipulationModel_FocusSelectedItemsInvoked(object? sender, EventArgs e);
protected abstract void ItemManipulationModel_ScrollIntoViewInvoked(object? sender, ListedItem e);
protected abstract void FileList_PreviewKeyDown(object sender, KeyRoutedEventArgs e);
protected abstract void EndRename(TextBox textBox);

// Overridden methods

protected override void InitializeCommandsViewModel()
{
CommandsViewModel = new BaseLayoutViewModel(ParentShellPageInstance, ItemManipulationModel);
Expand Down Expand Up @@ -72,6 +90,28 @@ protected override void UnhookEvents()
ItemManipulationModel.RefreshItemsThumbnailInvoked -= ItemManipulationModel_RefreshItemsThumbnail;
}

protected override void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
{
if (ParentShellPageInstance is null ||
ParentShellPageInstance.CurrentPageType != this.GetType() ||
IsRenamingItem)
return;

// Don't block the various uses of enter key (key 13)
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
if (InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Enter) == CoreVirtualKeyStates.Down ||
(focusedElement is Button && !isHeaderFocused) || // Allow jumpstring when header is focused
focusedElement is TextBox ||
focusedElement is PasswordBox ||
DependencyObjectHelpers.FindParent<ContentDialog>(focusedElement) is not null)
return;

base.Page_CharacterReceived(sender, args);
}

// Virtual methods

protected virtual async void ItemManipulationModel_RefreshItemsThumbnail(object? sender, EventArgs e)
{
await ReloadSelectedItemsIconAsync();
Expand Down Expand Up @@ -148,14 +188,6 @@ protected virtual void ItemManipulationModel_StartRenameItemInvoked(object? send
StartRenameItem();
}

protected abstract void ItemManipulationModel_AddSelectedItemInvoked(object? sender, ListedItem e);

protected abstract void ItemManipulationModel_RemoveSelectedItemInvoked(object? sender, ListedItem e);

protected abstract void ItemManipulationModel_FocusSelectedItemsInvoked(object? sender, EventArgs e);

protected abstract void ItemManipulationModel_ScrollIntoViewInvoked(object? sender, ListedItem e);

protected virtual void ZoomIn(object? sender, GroupOption option)
{
if (option == GroupOption.None)
Expand All @@ -167,8 +199,6 @@ protected virtual void FileList_SelectionChanged(object sender, SelectionChanged
SelectedItems = ListViewBase.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();
}

protected abstract void FileList_PreviewKeyDown(object sender, KeyRoutedEventArgs e);

protected virtual void SelectionRectangle_SelectionEnded(object? sender, EventArgs e)
{
ListViewBase.Focus(FocusState.Programmatic);
Expand Down Expand Up @@ -216,8 +246,6 @@ protected virtual void StartRenameItem(string itemNameTextBox)
IsRenamingItem = true;
}

protected abstract void EndRename(TextBox textBox);

protected virtual async Task CommitRenameAsync(TextBox textBox)
{
EndRename(textBox);
Expand All @@ -236,6 +264,8 @@ protected virtual async void RenameTextBox_LostFocus(object sender, RoutedEventA
}
}

// Methods

protected async void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
var textBox = (TextBox)sender;
Expand Down Expand Up @@ -269,7 +299,7 @@ protected async void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
case VirtualKey.Tab:
textBox.LostFocus -= RenameTextBox_LostFocus;

var isShiftPressed = (GetKeyState((int)VirtualKey.Shift) & KEY_DOWN_MASK) != 0;
var isShiftPressed = (InteropHelpers.GetKeyState((int)VirtualKey.Shift) & KEY_DOWN_MASK) != 0;
NextRenameIndex = isShiftPressed ? -1 : 1;

if (textBox.Text != OldItemName)
Expand Down Expand Up @@ -312,39 +342,18 @@ protected bool TryStartRenameNextItem(ListedItem item)
return false;
}

protected override void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
{
if (ParentShellPageInstance is null ||
ParentShellPageInstance.CurrentPageType != this.GetType() ||
IsRenamingItem)
return;

// Don't block the various uses of enter key (key 13)
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
if (InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Enter) == CoreVirtualKeyStates.Down ||
(focusedElement is Button && !isHeaderFocused) || // Allow jumpstring when header is focused
focusedElement is TextBox ||
focusedElement is PasswordBox ||
DependencyObjectHelpers.FindParent<ContentDialog>(focusedElement) is not null)
return;

base.Page_CharacterReceived(sender, args);
}

protected void SelectionCheckbox_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
e.Handled = true;
}

// Disposer

public override void Dispose()
{
base.Dispose();
UnhookEvents();
CommandsViewModel?.Dispose();
}

[DllImport("User32.dll")]
private extern static short GetKeyState(int n);
}
}
Loading

0 comments on commit b3c4d83

Please sign in to comment.