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: Clean up Widgets 1/2 #13349

Closed
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
1 change: 1 addition & 0 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private IHost ConfigureHost()
.AddSingleton<NetworkDrivesViewModel>()
.AddSingleton<OngoingTasksViewModel>()
.AddSingleton<AppearanceViewModel>()
.AddSingleton<HomeViewModel>()
).Build();
}

Expand Down
1 change: 0 additions & 1 deletion src/Files.App/Data/Contexts/Tags/TagsContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.ViewModels.Widgets;
using System.Collections.Immutable;

namespace Files.App.Data.Contexts
Expand Down
48 changes: 48 additions & 0 deletions src/Files.App/Data/EventArguments/QuickAccessEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Windows.System;
using Windows.UI.Core;

namespace Files.App.Data.EventArguments
{
public class QuickAccessCardEventArgs : EventArgs
{
public LocationItem Item { get; set; }
}

public class QuickAccessCardInvokedEventArgs : EventArgs
{
public string Path { get; set; }
}

public class ModifyQuickAccessEventArgs : EventArgs
{
public string[] Paths { get; set; }
public ShellFileItem[] Items { get; set; }
public bool Add;
public bool Pin = true;
public bool Reset = false;

public ModifyQuickAccessEventArgs(string[] paths, bool add)
{
Paths = paths;
Add = add;
}

public ModifyQuickAccessEventArgs(ShellFileItem[] items, bool add)
{
Paths = items.Select(x => x.FilePath).ToArray();
Items = items;
Add = add;
}
}
}
46 changes: 46 additions & 0 deletions src/Files.App/Data/Items/Widgets/DriveCardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Media.Imaging;

namespace Files.App.Data.Items
{
public class DriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, IComparable<DriveCardItem>
{
private BitmapImage thumbnail;
private byte[] thumbnailData;

public new DriveItem Item { get; private set; }
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
public BitmapImage Thumbnail
{
get => thumbnail;
set => SetProperty(ref thumbnail, value);
}
public DriveCardItem(DriveItem item)
{
Item = item;
Path = item.Path;
}

public async Task LoadCardThumbnailAsync()
{
// Try load thumbnail using ListView mode
if (thumbnailData is null || thumbnailData.Length == 0)
thumbnailData = await FileThumbnailHelper.LoadIconFromPathAsync(Item.Path, Convert.ToUInt32(Constants.Widgets.WidgetIconSize), Windows.Storage.FileProperties.ThumbnailMode.SingleItem, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);

// Thumbnail is still null, use DriveItem icon (loaded using SingleItem mode)
if (thumbnailData is null || thumbnailData.Length == 0)
{
await Item.LoadThumbnailAsync();
thumbnailData = Item.IconData;
}

// Thumbnail data is valid, set the item icon
if (thumbnailData is not null && thumbnailData.Length > 0)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(Constants.Widgets.WidgetIconSize));
}

public int CompareTo(DriveCardItem? other) => Item.Path.CompareTo(other?.Item?.Path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Files.Core.Storage;
using Files.Shared.Utils;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public sealed partial class FileTagsContainerViewModel : ObservableObject, IAsyncInitialize
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Files.Core.Storage;
using Files.Core.Storage.Extensions;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public sealed partial class FileTagsItemViewModel : WidgetCardItem
{
Expand Down
58 changes: 58 additions & 0 deletions src/Files.App/Data/Items/Widgets/FolderCardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Windows.System;
using Windows.UI.Core;

namespace Files.App.Data.Items
{
public class FolderCardItem : WidgetCardItem, IWidgetCardItem<LocationItem>
{
private BitmapImage thumbnail;
private byte[] thumbnailData;

public string AutomationProperties { get; set; }
public bool HasPath => !string.IsNullOrEmpty(Path);
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
public BitmapImage Thumbnail
{
get => thumbnail;
set => SetProperty(ref thumbnail, value);
}
public LocationItem Item { get; private set; }
public string Text { get; set; }
public bool IsPinned { get; set; }

public FolderCardItem(LocationItem item, string text, bool isPinned)
{
if (!string.IsNullOrWhiteSpace(text))
{
Text = text;
AutomationProperties = Text;
}
IsPinned = isPinned;
Item = item;
Path = item.Path;
}

public async Task LoadCardThumbnailAsync()
{
if (thumbnailData is null || thumbnailData.Length == 0)
{
thumbnailData = await FileThumbnailHelper.LoadIconFromPathAsync(Path, Convert.ToUInt32(Constants.Widgets.WidgetIconSize), Windows.Storage.FileProperties.ThumbnailMode.SingleItem, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
}
if (thumbnailData is not null && thumbnailData.Length > 0)
{
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(Constants.Widgets.WidgetIconSize));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.UI.Xaml.Media.Imaging;
using System.Threading.Tasks;

namespace Files.App.UserControls.Widgets
namespace Files.App.Data.Items
{
public interface IWidgetCardItem<T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public interface IWidgetItemModel : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using CommunityToolkit.Mvvm.ComponentModel;

namespace Files.App.UserControls.Widgets
namespace Files.App.Data.Items
{
public abstract class WidgetCardItem : ObservableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public class WidgetsListControlItemViewModel : ObservableObject, IDisposable
{
private readonly Action<bool> _expanderValueChangedCallback;

private readonly Func<bool> _expanderValueRequestedCallback;

private object _WidgetControl;
Expand All @@ -18,13 +17,6 @@ public object WidgetControl
set => SetProperty(ref _WidgetControl, value);
}

public WidgetsListControlItemViewModel(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
WidgetControl = widgetControl;
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;
}

public bool IsExpanded
{
get => _expanderValueRequestedCallback?.Invoke() ?? true;
Expand All @@ -49,12 +41,19 @@ public bool ShowMenuFlyout
{
get => WidgetItemModel.ShowMenuFlyout;
}

public MenuFlyoutItem MenuFlyoutItem
{
get => WidgetItemModel.MenuFlyoutItem;
}

public WidgetsListControlItemViewModel(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
WidgetControl = widgetControl;
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;
}

public void Dispose()
{
(WidgetControl as IDisposable)?.Dispose();
Expand Down
23 changes: 12 additions & 11 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dataitems="using:Files.App.Data.Items"
xmlns:helpers="using:Files.App.Helpers"
xmlns:local="using:Files.App.UserControls.Widgets"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -16,18 +17,8 @@
VerticalAlignment="Top"
ItemsSource="{x:Bind local:DrivesWidget.ItemsAdded, Mode=OneWay}">

<ItemsRepeater.Layout>
<UniformGridLayout
ItemsStretch="None"
MinColumnSpacing="8"
MinItemHeight="72"
MinItemWidth="240"
MinRowSpacing="8"
Orientation="Horizontal" />
</ItemsRepeater.Layout>

<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="local:DriveCardItem">
<DataTemplate x:DataType="dataitems:DriveCardItem">
<Button
Padding="0"
HorizontalAlignment="Stretch"
Expand Down Expand Up @@ -131,6 +122,16 @@
</DataTemplate>
</ItemsRepeater.ItemTemplate>

<ItemsRepeater.Layout>
<UniformGridLayout
ItemsStretch="None"
MinColumnSpacing="8"
MinItemHeight="72"
MinItemWidth="240"
MinRowSpacing="8"
Orientation="Horizontal" />
</ItemsRepeater.Layout>

</ItemsRepeater>
</Grid>
</local:HomePageWidget>
49 changes: 3 additions & 46 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI;
using Files.App.Data.Items;
using Files.App.Utils.Shell;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Windows.Input;
Expand All @@ -19,45 +12,9 @@

namespace Files.App.UserControls.Widgets
{
public class DriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, IComparable<DriveCardItem>
{
private BitmapImage thumbnail;
private byte[] thumbnailData;

public new DriveItem Item { get; private set; }
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
public BitmapImage Thumbnail
{
get => thumbnail;
set => SetProperty(ref thumbnail, value);
}
public DriveCardItem(DriveItem item)
{
Item = item;
Path = item.Path;
}

public async Task LoadCardThumbnailAsync()
{
// Try load thumbnail using ListView mode
if (thumbnailData is null || thumbnailData.Length == 0)
thumbnailData = await FileThumbnailHelper.LoadIconFromPathAsync(Item.Path, Convert.ToUInt32(Constants.Widgets.WidgetIconSize), Windows.Storage.FileProperties.ThumbnailMode.SingleItem, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);

// Thumbnail is still null, use DriveItem icon (loaded using SingleItem mode)
if (thumbnailData is null || thumbnailData.Length == 0)
{
await Item.LoadThumbnailAsync();
thumbnailData = Item.IconData;
}

// Thumbnail data is valid, set the item icon
if (thumbnailData is not null && thumbnailData.Length > 0)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(Constants.Widgets.WidgetIconSize));
}

public int CompareTo(DriveCardItem? other) => Item.Path.CompareTo(other?.Item?.Path);
}

/// <summary>
/// Represents a Widget for drives on Windows.
/// </summary>
public sealed partial class DrivesWidget : HomePageWidget, IWidgetItemModel, INotifyPropertyChanged
{
public IUserSettingsService userSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
Expand Down
Loading