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

Overhaul for widgets codebase #4448

Merged
merged 17 commits into from
Apr 5, 2021
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
17 changes: 14 additions & 3 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
<Compile Include="Helpers\NavigationHelpers.cs" />
<Compile Include="Helpers\UIHelpers.cs" />
<Compile Include="Helpers\WallpaperHelpers.cs" />
<Compile Include="Helpers\WidgetsHelpers.cs" />
<Compile Include="Helpers\Win32Helpers.cs" />
<Compile Include="UserControls\RestartControl.xaml.cs">
<DependentUpon>RestartControl.xaml</DependentUpon>
Expand Down Expand Up @@ -290,11 +291,14 @@
<Compile Include="UserControls\Widgets\Bundles.xaml.cs">
<DependentUpon>Bundles.xaml</DependentUpon>
</Compile>
<Compile Include="UserControls\Widgets\WidgetsListControl.xaml.cs">
<DependentUpon>WidgetsListControl.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\AdaptiveSidebarViewModel.cs" />
<Compile Include="ViewModels\BaseJsonSettingsViewModel.cs" />
<Compile Include="ViewModels\Bundles\BundleContainerViewModel.cs" />
<Compile Include="ViewModels\Bundles\BundleItemViewModel.cs" />
<Compile Include="ViewModels\Bundles\BundlesViewModel.cs" />
<Compile Include="ViewModels\Widgets\Bundles\BundleContainerViewModel.cs" />
<Compile Include="ViewModels\Widgets\Bundles\BundleItemViewModel.cs" />
<Compile Include="ViewModels\Widgets\Bundles\BundlesViewModel.cs" />
<Compile Include="ViewModels\Dialogs\DynamicDialogViewModel.cs" />
<Compile Include="ViewModels\FolderSettingsViewModel.cs" />
<Compile Include="ViewModels\MainPageViewModel.cs" />
Expand Down Expand Up @@ -455,6 +459,9 @@
<Compile Include="ViewModels\SettingsViewModels\OnStartupViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModels\PreferencesViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModels\WidgetsViewModel.cs" />
<Compile Include="ViewModels\Widgets\ICustomWidgetItemModel.cs" />
<Compile Include="ViewModels\Widgets\IWidgetItemModel.cs" />
<Compile Include="ViewModels\Widgets\WidgetsListControlViewModel.cs" />
<Compile Include="Views\ColumnParam.cs" />
<Compile Include="Views\ColumnShellPage.xaml.cs">
<DependentUpon>ColumnShellPage.xaml</DependentUpon>
Expand Down Expand Up @@ -916,6 +923,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="UserControls\Widgets\WidgetsListControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\ColumnShellPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
55 changes: 55 additions & 0 deletions Files/Helpers/WidgetsHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Files.UserControls.Widgets;
using Files.ViewModels.Widgets;

namespace Files.Helpers
{
public static class WidgetsHelpers
{
public static TWidget TryGetWidget<TWidget>(WidgetsListControlViewModel widgetsViewModel, TWidget defaultValue = default(TWidget)) where TWidget : IWidgetItemModel, new()
{
bool canAddWidget = widgetsViewModel.CanAddWidget(typeof(TWidget).Name);
bool isWidgetSettingEnabled = TryGetIsWidgetSettingEnabled<TWidget>();

if (canAddWidget && isWidgetSettingEnabled)
{
return new TWidget();
}
else if (!canAddWidget && !isWidgetSettingEnabled) // The widgets exists but the setting has been disabled for it
{
// Remove the widget
widgetsViewModel.RemoveWidget<TWidget>();
return default(TWidget);
}

return defaultValue;
}

public static bool TryGetIsWidgetSettingEnabled<TWidget>() where TWidget : IWidgetItemModel
{
if (typeof(TWidget) == typeof(LibraryCards))
{
return App.AppSettings.ShowLibraryCardsWidget;
}
if (typeof(TWidget) == typeof(DrivesWidget))
{
return App.AppSettings.ShowDrivesWidget;
}
if (typeof(TWidget) == typeof(Bundles))
{
return App.AppSettings.ShowBundlesWidget;
}
if (typeof(TWidget) == typeof(RecentFiles))
{
return App.AppSettings.ShowRecentFilesWidget;
}
// A custom widget it is - TWidget implements ICustomWidgetItemModel
if (typeof(ICustomWidgetItemModel).IsAssignableFrom(typeof(TWidget)))
{
// Return true for custom widgets - they're always enabled
return true;
}

return false;
}
}
}
9 changes: 6 additions & 3 deletions Files/UserControls/Widgets/Bundles.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using Files.ViewModels.Bundles;
using System;
using System;
using Windows.UI.Xaml.Controls;
using Files.ViewModels.Widgets.Bundles;
using Files.ViewModels.Widgets;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace Files.UserControls.Widgets
{
public sealed partial class Bundles : UserControl, IDisposable
public sealed partial class Bundles : UserControl, IWidgetItemModel, IDisposable
{
public BundlesViewModel ViewModel
{
get => (BundlesViewModel)DataContext;
private set => DataContext = value;
}

public string WidgetName => nameof(Bundles);

public Bundles()
{
this.InitializeComponent();
Expand Down
23 changes: 11 additions & 12 deletions Files/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Files.Helpers;
using Files.Interacts;
using Files.ViewModels;
using Files.ViewModels.Widgets;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
Expand All @@ -16,7 +18,7 @@

namespace Files.UserControls.Widgets
{
public sealed partial class DrivesWidget : UserControl, INotifyPropertyChanged
public sealed partial class DrivesWidget : UserControl, IWidgetItemModel, INotifyPropertyChanged
{
public SettingsViewModel AppSettings => App.AppSettings;

Expand Down Expand Up @@ -47,6 +49,8 @@ public IShellPage AppInstance
}
}

public string WidgetName => nameof(DrivesWidget);

public DrivesWidget()
{
InitializeComponent();
Expand Down Expand Up @@ -115,19 +119,9 @@ private void GridScaleNormal(object sender, Windows.UI.Xaml.Input.PointerRoutedE
visual.Scale = new Vector3(1);
}

private bool showMultiPaneControls;

public bool ShowMultiPaneControls
{
get => showMultiPaneControls;
set
{
if (value != showMultiPaneControls)
{
showMultiPaneControls = value;
NotifyPropertyChanged(nameof(ShowMultiPaneControls));
}
}
get => AppInstance.IsMultiPaneEnabled && AppInstance.IsPageMainPane;
}

private void OpenInNewPane_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -175,5 +169,10 @@ private async void GoToStorageSense_Click(object sender, RoutedEventArgs e)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:storagesense"));
}

public void Dispose()
{
Debugger.Break();
}
}
}
20 changes: 18 additions & 2 deletions Files/UserControls/Widgets/LibraryCards.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using Files.Interacts;
using Files.ViewModels;
using Files.ViewModels.Dialogs;
using Files.ViewModels.Widgets;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Toolkit.Uwp;
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
Expand All @@ -21,7 +23,7 @@

namespace Files.UserControls.Widgets
{
public sealed partial class LibraryCards : UserControl, INotifyPropertyChanged
public sealed partial class LibraryCards : UserControl, IWidgetItemModel, INotifyPropertyChanged
{
public SettingsViewModel AppSettings => App.AppSettings;

Expand All @@ -41,10 +43,14 @@ public sealed partial class LibraryCards : UserControl, INotifyPropertyChanged

public event LibraryCardDeleteInvokedEventHandler LibraryCardDeleteInvoked;

public event EventHandler LibraryCardShowMultiPaneControlsInvoked;

public event PropertyChangedEventHandler PropertyChanged;

public BulkConcurrentObservableCollection<LibraryCardItem> ItemsAdded = new BulkConcurrentObservableCollection<LibraryCardItem>();

public string WidgetName => nameof(LibraryCards);

public RelayCommand<LibraryCardItem> LibraryCardClicked => new RelayCommand<LibraryCardItem>(item =>
{
if (string.IsNullOrEmpty(item.Path))
Expand Down Expand Up @@ -154,7 +160,12 @@ private void GridScaleNormal(object sender, Windows.UI.Xaml.Input.PointerRoutedE

public bool ShowMultiPaneControls
{
get => showMultiPaneControls;
get
{
LibraryCardShowMultiPaneControlsInvoked?.Invoke(this, EventArgs.Empty);

return showMultiPaneControls;
}
set
{
if (value != showMultiPaneControls)
Expand Down Expand Up @@ -296,6 +307,11 @@ private async void DeleteLibrary_Click(object sender, RoutedEventArgs e)
await dialog.ShowAsync();
}
}

public void Dispose()
{
Debugger.Break();
}
}

public class LibraryCardInvokedEventArgs : EventArgs
Expand Down
11 changes: 10 additions & 1 deletion Files/UserControls/Widgets/RecentFiles.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Files.Enums;
using Files.Filesystem;
using Files.ViewModels;
using Files.ViewModels.Widgets;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
Expand All @@ -15,7 +17,7 @@

namespace Files.UserControls.Widgets
{
public sealed partial class RecentFiles : UserControl
public sealed partial class RecentFiles : UserControl, IWidgetItemModel
{
public delegate void RecentFilesOpenLocationInvokedEventHandler(object sender, PathNavigationEventArgs e);

Expand All @@ -29,6 +31,8 @@ public sealed partial class RecentFiles : UserControl
private EmptyRecentsText Empty { get; set; } = new EmptyRecentsText();
public SettingsViewModel AppSettings => App.AppSettings;

public string WidgetName => nameof(RecentFiles);

public RecentFiles()
{
InitializeComponent();
Expand Down Expand Up @@ -206,6 +210,11 @@ private void ClearRecentItems_Click(object sender, RoutedEventArgs e)
mru.Clear();
Empty.Visibility = Visibility.Visible;
}

public void Dispose()
{
Debugger.Break();
}
}

public class RecentItem
Expand Down
42 changes: 42 additions & 0 deletions Files/UserControls/Widgets/WidgetsListControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<UserControl
x:Class="Files.UserControls.Widgets.WidgetsListControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Files.UserControls.Widgets"
xmlns:vm="using:Files.ViewModels.Widgets"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Border>
<Grid>
<ListView
SelectionMode="None"
ItemsSource="{x:Bind ViewModel.Widgets, Mode=OneWay}">

<!-- Remove Reveal Highlight Effect -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<!-- Spacing -->
<Setter Property="Margin" Value="0,0,0,24" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
x:Name="Root"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
RevealBackground="{ThemeResource GridViewItemRevealBackground}"
RevealBorderBrush="{ThemeResource GridViewItemRevealBorderBrush}"
RevealBorderThickness="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Border>
</UserControl>
29 changes: 29 additions & 0 deletions Files/UserControls/Widgets/WidgetsListControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Windows.UI.Xaml.Controls;
using Files.ViewModels.Widgets;
using System;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace Files.UserControls.Widgets
{
public sealed partial class WidgetsListControl : UserControl, IDisposable
{
public WidgetsListControlViewModel ViewModel
{
get => (WidgetsListControlViewModel)DataContext;
set => DataContext = value;
}

public WidgetsListControl()
{
this.InitializeComponent();

this.ViewModel = new WidgetsListControlViewModel();
}

public void Dispose()
{
ViewModel?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Files.ViewModels.Bundles
namespace Files.ViewModels.Widgets.Bundles
{
/// <summary>
/// Bundle's contents view model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace Files.ViewModels.Bundles
namespace Files.ViewModels.Widgets.Bundles
{
public class BundleItemViewModel : ObservableObject, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Files.ViewModels.Bundles
namespace Files.ViewModels.Widgets.Bundles
{
/// <summary>
/// Bundles list View Model
Expand Down
Loading