Skip to content

Commit

Permalink
现在可以更换背景了
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Jul 1, 2024
1 parent 394a927 commit aca6787
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 1 deletion.
93 changes: 93 additions & 0 deletions MicaDemo/Common/Enumerable.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,93 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace MicaDemo.Common
{
public static class Enumerable
{
/// <summary>
/// Adds the elements of the specified collection to the end of the <see cref="ICollection{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="ICollection{TSource}"/> to be added.</param>
/// <param name="collection">The collection whose elements should be added to the end of the <see cref="ICollection{TSource}"/>.
/// The collection itself cannot be <see langword="null"/>, but it can contain elements that are
/// <see langword="null"/>, if type <typeparamref name="TSource"/> is a reference type.</param>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="collection"/> is null.</exception>
public static void AddRange<TSource>(this ICollection<TSource> source, IEnumerable<TSource> collection)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}

if (source is List<TSource> list)
{
list.AddRange(collection);
}
else if (source is TSource[] array)
{
int length = collection.Count();
int count = collection is List<TSource> _list
? _list.FindLastIndex(x => x != null) + 1
: collection is TSource[] _array
? Array.FindLastIndex(_array, x => x != null) + 1
: length;

if (count > 0)
{
int _size = Array.FindLastIndex(array, x => x != null) + 1;
if (array.Length - _size < count)
{
throw new ArgumentOutOfRangeException(nameof(array));
}

if (count == length)
{
if (collection is ICollection<TSource> _collection)
{
_collection.CopyTo(array, _size);
}
else
{
foreach (TSource item in collection)
{
array[_size++] = item;
}
}
}
else
{
using (IEnumerator<TSource> enumerator = collection.GetEnumerator())
{
while (--count >= 0 && enumerator.MoveNext())
{
array[_size++] = enumerator.Current;
}
}
}
}
}
else if (source is ISet<TSource> set)
{
set.UnionWith(collection);
}
else
{
foreach (TSource item in collection)
{
source.Add(item);
}
}
}

/// <summary>
/// Performs the specified action on each element of the <see cref="IEnumerable{T}"/>.
/// </summary>
Expand All @@ -29,6 +111,17 @@ public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSo
{
list.ForEach(action);
}
else if (source is TSource[] array)
{
#if NETCORE463
Array.ForEach(array, action);
#else
foreach (TSource item in array)
{
action(item);
}
#endif
}
else if (source is ImmutableList<TSource> immutableList)
{
immutableList.ForEach(action);
Expand Down
42 changes: 42 additions & 0 deletions MicaDemo/Pages/BlurPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE890;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
<MenuFlyoutSubItem Text="Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xEB9F;" />
</helpers:UIElementHelper.Icon>
<MenuFlyoutItem
Click="Button_Click"
Tag="ChangeImage"
Text="Change Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE8AB;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
<MenuFlyoutItem
Click="Button_Click"
Tag="RemoveImage"
Text="Remove Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE74D;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
</MenuFlyoutSubItem>
<MenuFlyoutSubItem Text="Open New Window">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE78B;" />
Expand Down Expand Up @@ -126,6 +147,27 @@
Style="{StaticResource IconButtonStyle}"
Tag="ChangeTheme"
ToolTipService.ToolTip="Change Theme" />
<Button
HorizontalAlignment="Stretch"
Click="Button_Click"
Content="&#xEB9F;"
Style="{StaticResource IconButtonStyle}"
Tag="ChangeImage"
ToolTipService.ToolTip="Change Backgroud Image">
<helpers:UIElementHelper.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Click="Button_Click"
IsEnabled="{x:Bind Provider.IsAppWindowSupported}"
Tag="RemoveImage"
Text="Remove Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE74D;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
</MenuFlyout>
</helpers:UIElementHelper.ContextFlyout>
</Button>
<ToggleButton
HorizontalAlignment="Stretch"
Content="&#xE744;"
Expand Down
6 changes: 6 additions & 0 deletions MicaDemo/Pages/BlurPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ private async void Button_Click(object sender, RoutedEventArgs e)
case "ChangeTheme":
_ = ThemeHelper.IsDarkThemeAsync().ContinueWith(x => ThemeHelper.SetRootThemeAsync(x.Result ? ElementTheme.Light : ElementTheme.Dark));
break;
case "ChangeImage":
_ = Provider.PickImageAsync();
return;
case "RemoveImage":
Provider.BackgroundImage = null;
break;
case "HideSetting":
IsHideCard = true;
break;
Expand Down
44 changes: 43 additions & 1 deletion MicaDemo/Pages/MicaPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE890;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
<MenuFlyoutSubItem Text="Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xEB9F;" />
</helpers:UIElementHelper.Icon>
<MenuFlyoutItem
Click="Button_Click"
Tag="ChangeImage"
Text="Change Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE8AB;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
<MenuFlyoutItem
Click="Button_Click"
Tag="RemoveImage"
Text="Remove Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE74D;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
</MenuFlyoutSubItem>
<MenuFlyoutSubItem Text="Open New Window">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE78B;" />
Expand Down Expand Up @@ -126,6 +147,27 @@
Style="{StaticResource IconButtonStyle}"
Tag="ChangeTheme"
ToolTipService.ToolTip="Change Theme" />
<Button
HorizontalAlignment="Stretch"
Click="Button_Click"
Content="&#xEB9F;"
Style="{StaticResource IconButtonStyle}"
Tag="ChangeImage"
ToolTipService.ToolTip="Change Backgroud Image">
<helpers:UIElementHelper.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Click="Button_Click"
IsEnabled="{x:Bind Provider.IsAppWindowSupported}"
Tag="RemoveImage"
Text="Remove Backgroud Image">
<helpers:UIElementHelper.Icon>
<FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE74D;" />
</helpers:UIElementHelper.Icon>
</MenuFlyoutItem>
</MenuFlyout>
</helpers:UIElementHelper.ContextFlyout>
</Button>
<ToggleButton
HorizontalAlignment="Stretch"
Content="&#xE744;"
Expand Down Expand Up @@ -179,7 +221,7 @@
HorizontalAlignment="Stretch"
Header="BackgroundSource"
ItemsSource="{x:Bind Provider.BackgroundSources}"
SelectedIndex="1" />
SelectedIndex="2" />
<ToggleButton
x:Name="AlwaysUseFallback"
Margin="0,4"
Expand Down
6 changes: 6 additions & 0 deletions MicaDemo/Pages/MicaPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ private async void Button_Click(object sender, RoutedEventArgs e)
case "ChangeTheme":
_ = ThemeHelper.IsDarkThemeAsync().ContinueWith(x => ThemeHelper.SetRootThemeAsync(x.Result ? ElementTheme.Light : ElementTheme.Dark));
break;
case "ChangeImage":
_ = Provider.PickImageAsync();
return;
case "RemoveImage":
Provider.BackgroundImage = null;
break;
case "HideSetting":
IsHideCard = true;
break;
Expand Down
49 changes: 49 additions & 0 deletions MicaDemo/ViewModels/BrushViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.WindowManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;

namespace MicaDemo.ViewModels
{
public class BrushViewModel : INotifyPropertyChanged
{
private readonly static string[] imageTypes = new[] { ".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".heif", ".heic" };

public Thickness ScrollViewerMargin { get; } = UIHelper.ScrollViewerMargin;
public Array BackgroundSources { get; } = Enum.GetValues(typeof(BackgroundSource));
public bool IsAppWindowSupported { get; } = WindowHelper.IsAppWindowSupported;
Expand Down Expand Up @@ -112,6 +119,48 @@ public BrushViewModel(CoreDispatcher dispatcher)
Dispatcher = dispatcher;
BackgroundImage = new BitmapImage(new Uri("ms-appx:///Assets/Photos/BigFourSummerHeat.jpg"));
}

public async Task PickImageAsync()
{
FileOpenPicker fileOpen = new FileOpenPicker();
fileOpen.FileTypeFilter.AddRange(imageTypes);
fileOpen.SuggestedStartLocation = PickerLocationId.ComputerFolder;

StorageFile file = await fileOpen.PickSingleFileAsync();
if (file != null)
{
using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
{
BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareImage = await imageDecoder.GetSoftwareBitmapAsync();
try
{
WriteableBitmap writeableImage = new WriteableBitmap((int)imageDecoder.PixelWidth, (int)imageDecoder.PixelHeight);
await writeableImage.SetSourceAsync(stream);
BackgroundImage = writeableImage;
}
catch
{
try
{
using (InMemoryRandomAccessStream random = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, random);
encoder.SetSoftwareBitmap(softwareImage);
await encoder.FlushAsync();
WriteableBitmap writeableImage = new WriteableBitmap((int)imageDecoder.PixelWidth, (int)imageDecoder.PixelHeight);
await writeableImage.SetSourceAsync(random);
BackgroundImage = writeableImage;
}
}
catch
{
BackgroundImage = null;
}
}
}
}
}
}

public interface ICompactOverlay
Expand Down

0 comments on commit aca6787

Please sign in to comment.