Skip to content
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

Code Quality: Use WindowContext instead of AppModel #15302

Closed
wants to merge 9 commits into from
Closed
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
9 changes: 5 additions & 4 deletions src/Files.App/Actions/FileSystem/PasteItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Files.App.Actions
internal sealed class PasteItemAction : ObservableObject, IAction
{
private readonly IContentPageContext context;
private IWindowContext WindowContext { get; } = Ioc.Default.GetRequiredService<IWindowContext>();

public string Label
=> "Paste".GetLocalizedResource();
Expand All @@ -35,7 +36,7 @@ public PasteItemAction()
context = Ioc.Default.GetRequiredService<IContentPageContext>();

context.PropertyChanged += Context_PropertyChanged;
App.AppModel.PropertyChanged += AppModel_PropertyChanged;
WindowContext.PropertyChanged += WindowContext_PropertyChanged;
}

public async Task ExecuteAsync(object? parameter = null)
Expand All @@ -50,7 +51,7 @@ public async Task ExecuteAsync(object? parameter = null)
public bool GetIsExecutable()
{
return
App.AppModel.IsPasteEnabled &&
App.WindowContext.IsPasteEnabled &&
context.PageType != ContentPageTypes.Home &&
context.PageType != ContentPageTypes.RecycleBin &&
context.PageType != ContentPageTypes.SearchResults;
Expand All @@ -62,9 +63,9 @@ private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
OnPropertyChanged(nameof(IsExecutable));
}

private void AppModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
private void WindowContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(AppModel.IsPasteEnabled))
if (e.PropertyName is nameof(IWindowContext.IsPasteEnabled))
OnPropertyChanged(nameof(IsExecutable));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Files.App.Actions
internal sealed class PasteItemToSelectionAction : BaseUIAction, IAction
{
private readonly IContentPageContext context;
private IWindowContext WindowContext { get; } = Ioc.Default.GetRequiredService<IWindowContext>();

public string Label
=> "Paste".GetLocalizedResource();
Expand All @@ -27,7 +28,7 @@ public PasteItemToSelectionAction()
context = Ioc.Default.GetRequiredService<IContentPageContext>();

context.PropertyChanged += Context_PropertyChanged;
App.AppModel.PropertyChanged += AppModel_PropertyChanged;
WindowContext.PropertyChanged += WindowContext_PropertyChanged;
}

public async Task ExecuteAsync(object? parameter = null)
Expand All @@ -44,7 +45,7 @@ public async Task ExecuteAsync(object? parameter = null)

public bool GetIsExecutable()
{
if (!App.AppModel.IsPasteEnabled)
if (!App.WindowContext.IsPasteEnabled)
return false;

if (context.PageType is ContentPageTypes.Home or ContentPageTypes.RecycleBin or ContentPageTypes.SearchResults)
Expand All @@ -68,9 +69,9 @@ private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
break;
}
}
private void AppModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
private void WindowContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(AppModel.IsPasteEnabled))
if (e.PropertyName is nameof(IWindowContext.IsPasteEnabled))
OnPropertyChanged(nameof(IsExecutable));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Actions/Navigation/NextTabAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public NextTabAction()

public Task ExecuteAsync(object? parameter = null)
{
App.AppModel.TabStripSelectedIndex = (App.AppModel.TabStripSelectedIndex + 1) % multitaskingContext.TabCount;
App.WindowContext.TabBarSelectedItemIndex = (App.WindowContext.TabBarSelectedItemIndex + 1) % multitaskingContext.TabCount;

return Task.CompletedTask;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Files.App/Actions/Navigation/PreviousTabAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public PreviousTabAction()

public Task ExecuteAsync(object? parameter = null)
{
if (App.AppModel.TabStripSelectedIndex is 0)
App.AppModel.TabStripSelectedIndex = multitaskingContext.TabCount - 1;
if (App.WindowContext.TabBarSelectedItemIndex is 0)
App.WindowContext.TabBarSelectedItemIndex = multitaskingContext.TabCount - 1;
else
App.AppModel.TabStripSelectedIndex--;
App.WindowContext.TabBarSelectedItemIndex--;

return Task.CompletedTask;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public partial class App : Application
public static FileTagsManager FileTagsManager { get; private set; } = null!;
public static RecentItems RecentItemsManager { get; private set; } = null!;
public static LibraryManager LibraryManager { get; private set; } = null!;
public static AppModel AppModel { get; private set; } = null!;
public static IWindowContext WindowContext { get; private set; } = null!;
public static ILogger Logger { get; private set; } = null!;

/// <summary>
Expand Down Expand Up @@ -117,8 +117,8 @@ async Task ActivateAsync()
FileTagsManager = Ioc.Default.GetRequiredService<FileTagsManager>();
RecentItemsManager = Ioc.Default.GetRequiredService<RecentItems>();
LibraryManager = Ioc.Default.GetRequiredService<LibraryManager>();
WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
Logger = Ioc.Default.GetRequiredService<ILogger<App>>();
AppModel = Ioc.Default.GetRequiredService<AppModel>();

// Hook events for the window
MainWindow.Instance.Closed += Window_Closed;
Expand Down Expand Up @@ -177,7 +177,7 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(()
/// </summary>
private void Window_Activated(object sender, WindowActivatedEventArgs args)
{
AppModel.IsMainWindowClosed = false;
WindowContext.IsMainWindowClosed = false;

// TODO(s): Is this code still needed?
if (args.WindowActivationState != WindowActivationState.CodeActivated ||
Expand Down Expand Up @@ -231,7 +231,7 @@ private async void Window_Closed(object sender, WindowEventArgs args)

// Continue running the app on the background
if (userSettingsService.GeneralSettingsService.LeaveAppRunning &&
!AppModel.ForceProcessTermination &&
!WindowContext.ForceProcessTermination &&
!Process.GetProcessesByName("Files").Any(x => x.Id != Environment.ProcessId))
{
// Close open content dialogs
Expand Down Expand Up @@ -298,7 +298,7 @@ private async void Window_Closed(object sender, WindowEventArgs args)
Program.Pool.Dispose();
Program.Pool = null;

if (!AppModel.ForceProcessTermination)
if (!WindowContext.ForceProcessTermination)
{
args.Handled = true;
_ = AppLifecycleHelper.CheckAppUpdate();
Expand All @@ -324,7 +324,7 @@ private async void Window_Closed(object sender, WindowEventArgs args)

// Destroy cached properties windows
FilePropertiesHelpers.DestroyCachedWindows();
AppModel.IsMainWindowClosed = true;
WindowContext.IsMainWindowClosed = true;

// Wait for ongoing file operations
FileOperationsHelpers.WaitForCompletion();
Expand Down
10 changes: 6 additions & 4 deletions src/Files.App/Data/Contexts/Multitasking/MultitaskingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Files.App.Data.Contexts
{
internal sealed class MultitaskingContext : ObservableObject, IMultitaskingContext
{
private IWindowContext WindowContext { get; } = Ioc.Default.GetRequiredService<IWindowContext>();

private bool isPopupOpen = false;

private ITabBar? control;
Expand All @@ -29,7 +31,7 @@ internal sealed class MultitaskingContext : ObservableObject, IMultitaskingConte
public MultitaskingContext()
{
MainPageViewModel.AppInstances.CollectionChanged += AppInstances_CollectionChanged;
App.AppModel.PropertyChanged += AppModel_PropertyChanged;
WindowContext.PropertyChanged += WindowContext_PropertyChanged;
BaseTabBar.OnLoaded += BaseMultitaskingControl_OnLoaded;
TabBar.SelectedTabItemChanged += HorizontalMultitaskingControl_SelectedTabItemChanged;
FocusManager.GotFocus += FocusManager_GotFocus;
Expand All @@ -40,9 +42,9 @@ private void AppInstances_CollectionChanged(object? sender, NotifyCollectionChan
{
UpdateTabCount();
}
private void AppModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
private void WindowContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(AppModel.TabStripSelectedIndex))
if (e.PropertyName is nameof(IWindowContext.TabBarSelectedItemIndex))
UpdateCurrentTabIndex();
}

Expand Down Expand Up @@ -90,7 +92,7 @@ private void UpdateTabCount()

private void UpdateCurrentTabIndex()
{
if (SetProperty(ref currentTabIndex, (ushort)App.AppModel.TabStripSelectedIndex, nameof(CurrentTabIndex)))
if (SetProperty(ref currentTabIndex, (ushort)App.WindowContext.TabBarSelectedItemIndex, nameof(CurrentTabIndex)))
{
OnPropertyChanged(nameof(CurrentTabItem));
}
Expand Down
78 changes: 76 additions & 2 deletions src/Files.App/Data/Contexts/Window/IWindowContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,86 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.ComponentModel;

namespace Files.App.Data.Contexts
{
/// <summary>
/// Represents context for <see cref="MainWindow"/> comprehensive management.
/// </summary>
public interface IWindowContext : INotifyPropertyChanged
{
/// <summary>
/// Gets the value that indicates whether the window is in Compact Overlay.
/// </summary>
/// <remarks>
/// This feature comes from Windows, visit
/// <a href="https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/migrate-to-windows-app-sdk/guides/windowing#compact-overlay">Compact Overlay in WinAppSdk</a>.
/// </remarks>
bool IsCompactOverlay { get; }

/// <summary>
/// Gets or sets the index of the current selected <see cref="TabBarItem"/>.
/// </summary>
int TabBarSelectedItemIndex { get; set; }

/// <summary>
/// Gets or sets the value that indicates whether the application process is elevated.
/// </summary>
bool IsAppElevated { get; set; }

/// <summary>
/// Gets or sets the value that indicates whether the paste file filesystem operation is enabled.
/// </summary>
bool IsPasteEnabled { get; set; }

/// <summary>
/// Gets or sets the value that indicates the application window is closed.
/// </summary>
bool IsMainWindowClosed { get; set; }

/// <summary>
/// Gets or sets the count of properties windows that are being opened
/// </summary>
int PropertiesWindowCount { get; }

/// <summary>
/// Gets or sets the value that indicates whether the application must be terminated on closed.
/// </summary>
bool ForceProcessTermination { get; set; }

/// <summary>
/// Gets or sets a value indicating the path for Google Drive.
/// </summary>
string GoogleDrivePath { get; set; }

/// <summary>
/// Gets or sets a value indicating the path for pCloud Drive.
/// </summary>
string PCloudDrivePath { get; set; }

/// <summary>
/// Gets or sets a value indicating the AppWindow DPI.
/// </summary>
/// <remarks>
/// TODO: update value if the DPI changes
/// </remarks>
float AppWindowDPI { get; set; }

/// <summary>
/// Increments the count of properties windows that are being opened.
/// </summary>
/// <remarks>
/// Call this make sure the property <see cref="PropertiesWindowCount"/>'s setter is thread-safe.
/// </remarks>
/// <returns>The updated count of properties window that are being opened.</returns>
int IncrementPropertiesWindowCount();

/// <summary>
/// Decreases the count of properties windows that are being opened.
/// </summary>
/// <remarks>
/// Call this make sure the property <see cref="PropertiesWindowCount"/>'s setter is thread-safe.
/// </remarks>
/// <returns>The updated count of properties window that are being opened.</returns>
int DecrementPropertiesWindowCount();
}
}
Loading
Loading