wrapped custom widget items in containers so we have more control of the surrounding, i.e. divider

This commit is contained in:
TheXamlGuy
2024-01-10 19:25:16 +00:00
parent 197454ba1e
commit d7d90b3d54
45 changed files with 352 additions and 110 deletions
+2 -2
View File
@@ -3,14 +3,14 @@
x:Class="Hyperbar.Windows.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:desktop="using:Hyperbar.Windows">
xmlns:ui="using:Hyperbar.Windows.UI">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="DefaultDataTemplate">
<desktop:TemplateGeneratorControl />
<ui:TemplateGeneratorControl />
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
+11 -11
View File
@@ -1,13 +1,11 @@
using Hyperbar.Widget.Contextual;
using Hyperbar.Windows.Controls;
using Hyperbar.Windows.Controls;
using Hyperbar.Windows.Primary;
using Hyperbar.Windows.UI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using System.Text.Json;
namespace Hyperbar.Windows;
@@ -17,7 +15,7 @@ public partial class App :
public App() => InitializeComponent();
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
{
base.OnLaunched(args);
DispatcherQueueSynchronizationContext context = new(DispatcherQueue.GetForCurrentThread());
@@ -44,21 +42,23 @@ public partial class App :
services.AddTransient<ITemplateFactory, TemplateFactory>();
services.AddTransient<DesktopFlyout>();
services.AddContentTemplate<CommandViewModel, CommandView>();
// services.AddWidgetProvider<ContextualWidgetProvider>();
services.AddContentTemplate<WidgetBarViewModel, WidgetBarView>();
services.AddWidgetProvider<MediaControllerWidgetProvider>();
services.AddWidgetProvider<PrimaryWidgetProvider>();
services.AddTransient(provider =>
{
static IEnumerable<IWidgetViewModel> Resolve(IServiceProvider services)
static IEnumerable<WidgetContainerViewModel> Resolve(IServiceProvider services)
{
foreach (IWidgetContext widgetContext in services.GetServices<IWidgetContext>())
{
if (widgetContext.ServiceProvider.GetService<IWidgetViewModel>() is
IWidgetViewModel viewModel)
if (widgetContext.ServiceProvider.GetServices<IWidgetViewModel>() is
IEnumerable<IWidgetViewModel> viewModels)
{
yield return viewModel;
yield return (WidgetContainerViewModel)ActivatorUtilities.CreateInstance(widgetContext.ServiceProvider,
typeof(WidgetContainerViewModel), viewModels);
}
}
}
+7
View File
@@ -15,6 +15,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Views\WidgetButtonView.xaml" />
<None Remove="Views\WidgetContainerView.xaml" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
@@ -38,10 +39,16 @@
<ProjectReference Include="..\Hyperbar.Windows.Contextual\Hyperbar.Widget.Contextual.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.Controls\Hyperbar.Windows.Controls.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.Interop\Hyperbar.Windows.Interop.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.MediaController\Hyperbar.Windows.MediaController.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.Primary\Hyperbar.Widget.Primary.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.UI\Hyperbar.Windows.UI.csproj" />
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
</ItemGroup>
<ItemGroup>
<Page Update="Views\WidgetContainerView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="Views\WidgetButtonView.xaml">
<Generator>MSBuild:Compile</Generator>
@@ -3,8 +3,8 @@ using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.Windows;
public class AppInitializer([FromKeyedServices(nameof(CommandViewModel))] CommandView view,
[FromKeyedServices(nameof(CommandViewModel))] CommandViewModel viewModel,
public class AppInitializer([FromKeyedServices(nameof(WidgetBarViewModel))] WidgetBarView view,
[FromKeyedServices(nameof(WidgetBarViewModel))] WidgetBarViewModel viewModel,
DesktopFlyout desktopFlyout) :
IInitializer
{
@@ -1,5 +1,5 @@
using Hyperbar.Windows.Interop;
using Hyperbar.Windows.Primary;
using Hyperbar.Windows.UI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -28,20 +28,23 @@ namespace Hyperbar.Windows
})
.ConfigureServices((context, isolatedServices) =>
{
isolatedServices.AddHostedService<WidgetService>();
isolatedServices.AddSingleton<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
isolatedServices.AddSingleton<IVirtualKeyboard, VirtualKeyboard>();
isolatedServices.AddSingleton<IMediator, Mediator>();
isolatedServices.AddHandler<KeyAcceleratorHandler>();
isolatedServices.AddHandler<ProcesssAcceleratorHandler>();
isolatedServices.AddTransient<IWidgetView, WidgetView>();
isolatedServices.AddContentTemplate<WidgetButtonViewModel, WidgetButtonView>();
isolatedServices.AddHostedService<WidgetService>();
isolatedServices.AddTransient<ITemplateFactory, TemplateFactory>();
isolatedServices.AddSingleton<IMediator, Mediator>();
isolatedServices.AddSingleton<IVirtualKeyboard, VirtualKeyboard>();
isolatedServices.AddHandler<KeyAcceleratorHandler>();
isolatedServices.AddHandler<ProcesssAcceleratorHandler>();
isolatedServices.AddTransient<IWidgetView, WidgetView>();
isolatedServices.AddContentTemplate<WidgetContainerViewModel, WidgetContainerView>();
isolatedServices.AddContentTemplate<WidgetButtonViewModel, WidgetButtonView>();
builder.Create(context, isolatedServices);
@@ -1,11 +0,0 @@
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Windows;
public class DataTemplateConverter : ValueConverter<object, DataTemplateSelector>
{
protected override DataTemplateSelector? ConvertTo(object value, Type? targetType, object? parameter, string? language)
{
return new TemplateGenerator();
}
}
@@ -1,21 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.Windows;
public class TemplateFactory(IEnumerable<IContentTemplateDescriptor> descriptors,
IServiceProvider provider) :
ITemplateFactory
{
public object? Create(object key)
{
if (descriptors.FirstOrDefault(x => x.Key == key) is IContentTemplateDescriptor descriptor)
{
if (provider.GetRequiredKeyedService(descriptor.TemplateType, descriptor.Key) is { } template)
{
return template;
}
}
return default;
}
}
@@ -1,30 +0,0 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
namespace Hyperbar.Windows;
public class TemplateGenerator : DataTemplateSelector
{
protected override DataTemplate SelectTemplateCore(object item)
{
string xamlString = @"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:desktop='using:Hyperbar.Windows'>
<desktop:TemplateGeneratorControl />
</DataTemplate>";
return (DataTemplate)XamlReader.Load(xamlString);
}
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
string xamlString = @"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:desktop='using:Hyperbar.Windows'>
<desktop:TemplateGeneratorControl />
</DataTemplate>";
return (DataTemplate)XamlReader.Load(xamlString);
}
}
@@ -1,21 +0,0 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Windows;
public class TemplateGeneratorControl :
ContentControl
{
public TemplateGeneratorControl()
{
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (DataContext is ITemplatedViewModel templatedViewModel)
{
Content = templatedViewModel.TemplateFactory.Create(DataContext.GetType().Name);
}
}
}
@@ -1,45 +0,0 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
namespace Hyperbar.Windows;
public abstract class ValueConverter<TSource, TTarget> :
MarkupExtension,
IValueConverter
{
protected override object ProvideValue(IXamlServiceProvider serviceProvider)
{
return this;
}
public object? Convert(object value, Type targetType, object parameter, string language)
{
return ConvertTo((TSource)value, targetType, parameter, language);
}
public object? ConvertBack(object value, Type targetType, object parameter, string language)
{
return ConvertBackTo((TTarget)value, targetType, parameter, language);
}
public TTarget? Convert(TSource value)
{
return ConvertTo(value, null, null, null);
}
public TSource? ConvertBack(TTarget value)
{
return ConvertBackTo(value, null, null, null);
}
protected virtual TTarget? ConvertTo(TSource value, Type? targetType, object? parameter, string? language)
{
return default;
}
protected virtual TSource? ConvertBackTo(TTarget value, Type? targetType, object? parameter, string? language)
{
return default;
}
}
@@ -1,15 +0,0 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace Hyperbar.Windows;
public sealed partial class CommandView :
UserControl
{
public CommandView() => InitializeComponent();
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
base.OnKeyDown(e);
}
}
@@ -1,17 +0,0 @@
namespace Hyperbar.Windows;
public partial class CommandViewModel :
ObservableCollectionViewModel<IWidgetViewModel>,
ITemplatedViewModel
{
public CommandViewModel(ITemplateFactory templateFactory,
IServiceFactory serviceFactory,
IMediator mediator,
IEnumerable<IWidgetViewModel> items) : base(serviceFactory, mediator, items)
{
TemplateFactory = templateFactory;
}
public ITemplateFactory TemplateFactory { get; }
}
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="Hyperbar.Windows.CommandView"
x:Class="Hyperbar.Windows.WidgetBarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windows="using:Hyperbar.Windows">
<ItemsControl ItemTemplateSelector="{Binding Mode=TwoWay, Converter={windows:DataTemplateConverter}}" ItemsSource="{Binding}">
xmlns:ui="using:Hyperbar.Windows.UI">
<ItemsControl ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
@@ -0,0 +1,9 @@
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Windows;
public sealed partial class WidgetBarView :
UserControl
{
public WidgetBarView() => InitializeComponent();
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="Hyperbar.Windows.WidgetContainerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="using:Hyperbar.Windows.UI">
<ItemsControl ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</UserControl>
@@ -0,0 +1,9 @@
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Windows;
public sealed partial class WidgetContainerView :
UserControl
{
public WidgetContainerView() => InitializeComponent();
}
+2 -2
View File
@@ -3,8 +3,8 @@
x:Class="Hyperbar.Windows.WidgetView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windows="using:Hyperbar.Windows">
<ItemsControl ItemTemplateSelector="{Binding Mode=TwoWay, Converter={windows:DataTemplateConverter}}" ItemsSource="{Binding}">
xmlns:ui="using:Hyperbar.Windows.UI">
<ItemsControl ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Spacing="8" />