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 4 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
16 changes: 13 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,8 @@
<Compile Include="ViewModels\SettingsViewModels\OnStartupViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModels\PreferencesViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModels\WidgetsViewModel.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 +922,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
60 changes: 60 additions & 0 deletions Files/Helpers/WidgetsHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Files.UserControls.Widgets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.Helpers
{
public static class WidgetsHelpers
{
public static LibraryCards GetLibraryCards()
{
if (App.AppSettings.ShowLibraryCardsWidget)
{
return new LibraryCards();
}
else
{
return null;
}
}

public static DrivesWidget GetDrivesWidget()
{
if (App.AppSettings.ShowDrivesWidget)
{
return new DrivesWidget();
}
else
{
return null;
}
}

public static Bundles GetBundles()
{
if (App.AppSettings.ShowBundlesWidget)
{
return new Bundles();
}
else
{
return null;
}
}

public static RecentFiles GetRecentFiles()
{
if (App.AppSettings.ShowBundlesWidget)
{
return new RecentFiles();
}
else
{
return null;
}
}
}
}
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
5 changes: 4 additions & 1 deletion Files/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Files.Helpers;
using Files.Interacts;
using Files.ViewModels;
using Files.ViewModels.Widgets;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -16,7 +17,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 +48,8 @@ public IShellPage AppInstance
}
}

public string WidgetName => nameof(DrivesWidget);

public DrivesWidget()
{
InitializeComponent();
Expand Down
5 changes: 4 additions & 1 deletion Files/UserControls/Widgets/LibraryCards.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -21,7 +22,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 @@ -45,6 +46,8 @@ public sealed partial class LibraryCards : UserControl, INotifyPropertyChanged

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
5 changes: 4 additions & 1 deletion Files/UserControls/Widgets/RecentFiles.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Files.Enums;
using Files.Filesystem;
using Files.ViewModels;
using Files.ViewModels.Widgets;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -15,7 +16,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 +30,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
40 changes: 40 additions & 0 deletions Files/UserControls/Widgets/WidgetsListControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<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>
<GridView
SelectionMode="None"
ItemsSource="{x:Bind ViewModel.Widgets, Mode=OneWay}">

<!-- Remove Reveal Highlight Effect -->
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<!-- Spacing -->
<Setter Property="Margin" Value="0,0,0,24" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewItem">
<ListViewItemPresenter
x:Name="Root"
RevealBackground="{ThemeResource GridViewItemRevealBackground}"
RevealBorderBrush="{ThemeResource GridViewItemRevealBorderBrush}"
RevealBorderThickness="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GridView.ItemContainerStyle>
</GridView>
</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
13 changes: 13 additions & 0 deletions Files/ViewModels/Widgets/IWidgetItemModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.ViewModels.Widgets
{
public interface IWidgetItemModel
{
string WidgetName { get; }
}
}
55 changes: 55 additions & 0 deletions Files/ViewModels/Widgets/WidgetsListControlViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml.Controls;

namespace Files.ViewModels.Widgets
{
public class WidgetsListControlViewModel : ObservableObject, IDisposable
{
public ObservableCollection<Control> Widgets { get; private set; } = new ObservableCollection<Control>();

public bool AddWidget(Control widgetModel)
{
// The widget must not be null and must implement IWidgetItemModel
if (!(widgetModel is IWidgetItemModel widgetItemModel))
{
return false;
}

// Don't add existing ones!
if (Widgets.Any((item) => (item as IWidgetItemModel).WidgetName == widgetItemModel.WidgetName))
{
return false;
}

Widgets.Add(widgetModel);
return true;
}

public void RemoveWidget(Control widgetModel)
{
int indexToRemove = Widgets.IndexOf(widgetModel);
(Widgets[indexToRemove] as IDisposable)?.Dispose();
Widgets.RemoveAt(indexToRemove);
}

public void ReorderWidget(Control widgetModel, int place)
{
int widgetIndex = Widgets.IndexOf(widgetModel);
Widgets.Move(widgetIndex, place);
}

public void Dispose()
{
for (int i = 0; i < Widgets.Count; i++)
{
(Widgets[i] as IDisposable)?.Dispose();
}

Widgets.Clear();
Widgets = null;
}
}
}
Loading