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 01c8a50 commit 394a927
Show file tree
Hide file tree
Showing 16 changed files with 746 additions and 118 deletions.
14 changes: 12 additions & 2 deletions MicaDemo/App.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<Application
x:Class="MicaDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:MicaDemo.Helpers.Converters">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Themes/Color.xaml" />
</ResourceDictionary.MergedDictionaries>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<FontFamily x:Key="SymbolThemeFontFamily">Segoe Fluent Icons,Segoe MDL2 Assets,Segoe UI Symbol</FontFamily>
<Style x:Key="IconButtonStyle" TargetType="Button">
<Setter Property="Width" Value="40" />
<Setter Property="Height" Value="40" />
Expand All @@ -15,6 +18,14 @@
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontFamily" Value="{StaticResource SymbolThemeFontFamily}" />
</Style>
<Style x:Key="IconToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Width" Value="40" />
<Setter Property="Height" Value="40" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontFamily" Value="{StaticResource SymbolThemeFontFamily}" />
</Style>
<Style TargetType="Frame">
<Setter Property="ContentTransitions">
<Setter.Value>
Expand All @@ -24,7 +35,6 @@
</Setter.Value>
</Setter>
</Style>
<FontFamily x:Key="SymbolThemeFontFamily">Segoe Fluent Icons,Segoe MDL2 Assets,Segoe UI Symbol</FontFamily>
</ResourceDictionary>
</Application.Resources>
</Application>
5 changes: 5 additions & 0 deletions MicaDemo/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.Foundation.Metadata;
using Windows.System.Profile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
Expand All @@ -26,6 +27,10 @@ public App()
InitializeComponent();
Suspending += OnSuspending;
UnhandledException += Application_UnhandledException;
if (ApiInformation.IsEnumNamedValuePresent("Windows.UI.Xaml.FocusVisualKind", "Reveal") && AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Xbox")
{
FocusVisualKind = FocusVisualKind.Reveal;
}
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion MicaDemo/Controls/TitleBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<Grid Height="40" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
Expand All @@ -40,6 +40,7 @@
Content="&#xE72B;"
IsEnabled="{x:Bind IsBackEnable, Mode=OneWay}"
Style="{StaticResource IconButtonStyle}"
ToolTipService.ToolTip="Back"
Visibility="{x:Bind BackButtonVisibility, Mode=OneWay}" />
<TextBlock
x:Name="TitleBlock"
Expand Down Expand Up @@ -72,6 +73,7 @@
Click="RefreshButton_Click"
Content="&#xE72C;"
Style="{StaticResource IconButtonStyle}"
ToolTipService.ToolTip="Refresh"
Visibility="{x:Bind RefreshButtonVisibility, Mode=OneWay}" />
</Grid>
</Grid>
Expand Down
89 changes: 89 additions & 0 deletions MicaDemo/Helpers/Converters/BoolToObjectConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace MicaDemo.Helpers.Converters
{
/// <summary>
/// This class converts a boolean value into an other object.
/// Can be used to convert true/false to visibility, a couple of colors, couple of images, etc.
/// </summary>
public partial class BoolToObjectConverter : DependencyObject, IValueConverter
{
/// <summary>
/// Identifies the <see cref="TrueValue"/> property.
/// </summary>
public static readonly DependencyProperty TrueValueProperty =
DependencyProperty.Register(nameof(TrueValue), typeof(object), typeof(BoolToObjectConverter), new PropertyMetadata(null));

/// <summary>
/// Identifies the <see cref="FalseValue"/> property.
/// </summary>
public static readonly DependencyProperty FalseValueProperty =
DependencyProperty.Register(nameof(FalseValue), typeof(object), typeof(BoolToObjectConverter), new PropertyMetadata(null));

/// <summary>
/// Gets or sets the value to be returned when the boolean is true
/// </summary>
public object TrueValue
{
get { return GetValue(TrueValueProperty); }
set { SetValue(TrueValueProperty, value); }
}

/// <summary>
/// Gets or sets the value to be returned when the boolean is false
/// </summary>
public object FalseValue
{
get { return GetValue(FalseValueProperty); }
set { SetValue(FalseValueProperty, value); }
}

/// <summary>
/// Convert a boolean value to an other object.
/// </summary>
/// <param name="value">The source data being passed to the target.</param>
/// <param name="targetType">The type of the target property, as a type reference.</param>
/// <param name="parameter">An optional parameter to be used to invert the converter logic.</param>
/// <param name="language">The language of the conversion.</param>
/// <returns>The value to be passed to the target dependency property.</returns>
public object Convert(object value, Type targetType, object parameter, string language)
{
bool boolValue = value is bool && (bool)value;

// Negate if needed
if (ConverterTools.TryParseBool(parameter))
{
boolValue = !boolValue;
}

return ConverterTools.Convert(boolValue ? TrueValue : FalseValue, targetType);
}

/// <summary>
/// Convert back the value to a boolean
/// </summary>
/// <remarks>If the <paramref name="value"/> parameter is a reference type, <see cref="TrueValue"/> must match its reference to return true.</remarks>
/// <param name="value">The target data being passed to the source.</param>
/// <param name="targetType">The type of the target property, as a type reference (System.Type for Microsoft .NET, a TypeName helper struct for Visual C++ component extensions (C++/CX)).</param>
/// <param name="parameter">An optional parameter to be used to invert the converter logic.</param>
/// <param name="language">The language of the conversion.</param>
/// <returns>The value to be passed to the source object.</returns>
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
bool result = Equals(value, ConverterTools.Convert(TrueValue, value.GetType()));

if (ConverterTools.TryParseBool(parameter))
{
result = !result;
}

return result;
}
}
}
23 changes: 23 additions & 0 deletions MicaDemo/Helpers/Converters/BoolToVisibilityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.UI.Xaml;

namespace MicaDemo.Helpers.Converters
{
/// <summary>
/// This class converts a boolean value into a Visibility enumeration.
/// </summary>
public class BoolToVisibilityConverter : BoolToObjectConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="BoolToVisibilityConverter"/> class.
/// </summary>
public BoolToVisibilityConverter()
{
TrueValue = Visibility.Visible;
FalseValue = Visibility.Collapsed;
}
}
}
39 changes: 39 additions & 0 deletions MicaDemo/Helpers/Converters/ConverterTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Reflection;
using Windows.UI.Xaml.Markup;

namespace MicaDemo.Helpers.Converters
{
/// <summary>
/// Static class used to provide internal tools
/// </summary>
internal static class ConverterTools
{
/// <summary>
/// Helper method to safely cast an object to a boolean
/// </summary>
/// <param name="parameter">Parameter to cast to a boolean</param>
/// <returns>Bool value or false if cast failed</returns>
internal static bool TryParseBool(object parameter)
{
bool parsed = false;
if (parameter != null)
{
bool.TryParse(parameter.ToString(), out parsed);
}

return parsed;
}

/// <summary>
/// Helper method to convert a value from a source type to a target type.
/// </summary>
/// <param name="value">The value to convert</param>
/// <param name="targetType">The target type</param>
/// <returns>The converted value</returns>
internal static object Convert(object value, Type targetType)
{
return targetType.IsInstanceOfType(value) ? value : XamlBindingHelper.ConvertValue(targetType, value);
}
}
}
14 changes: 7 additions & 7 deletions MicaDemo/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class ThemeHelper
// Keep reference so it does not get optimized/garbage collected
public static UISettings UISettings { get; } = new UISettings();
public static AccessibilitySettings AccessibilitySettings { get; } = new AccessibilitySettings();

#region UISettingChanged

private static readonly WeakEvent<bool> actions = new WeakEvent<bool>();
Expand Down Expand Up @@ -66,9 +66,9 @@ await Task.WhenAll(WindowHelper.ActiveWindows.Values.Select(async window =>
rootElement.RequestedTheme = value;
}
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out HashSet<AppWindow> appWindows))
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out Dictionary<XamlRoot, AppWindow> appWindows))
{
foreach (FrameworkElement element in appWindows.Select(x => x.GetXamlRootForWindow()).OfType<FrameworkElement>())
foreach (FrameworkElement element in appWindows.Keys.Select(x => x.Content).OfType<FrameworkElement>())
{
element.RequestedTheme = value;
}
Expand Down Expand Up @@ -144,9 +144,9 @@ public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar)
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = IsExtendsTitleBar;
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out HashSet<AppWindow> appWindows))
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out Dictionary<XamlRoot, AppWindow> appWindows))
{
foreach (AppWindow appWindow in appWindows)
foreach (AppWindow appWindow in appWindows.Values)
{
appWindow.TitleBar.ExtendsContentIntoTitleBar = IsExtendsTitleBar;
}
Expand Down Expand Up @@ -182,9 +182,9 @@ public static async void UpdateSystemCaptionButtonColors()
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = ExtendViewIntoTitleBar ? Colors.Transparent : BackgroundColor;
}
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out HashSet<AppWindow> appWindows))
if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out Dictionary<XamlRoot, AppWindow> appWindows))
{
foreach (AppWindow appWindow in appWindows)
foreach (AppWindow appWindow in appWindows.Values)
{
bool ExtendViewIntoTitleBar = appWindow.TitleBar.ExtendsContentIntoTitleBar;
AppWindowTitleBar TitleBar = appWindow.TitleBar;
Expand Down
4 changes: 2 additions & 2 deletions MicaDemo/Helpers/UIElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static void OnRightTapped(object sender, RightTappedRoutedEventArgs e)
/// </summary>
/// <param name="control">The element from which to read the property value.</param>
/// <returns>The graphic content of the menu flyout item.</returns>
public static IconElement GetIcon(MenuFlyoutItem control)
public static IconElement GetIcon(MenuFlyoutItemBase control)
{
return (IconElement)control.GetValue(IconProperty);
}
Expand All @@ -124,7 +124,7 @@ public static IconElement GetIcon(MenuFlyoutItem control)
/// </summary>
/// <param name="control">The element on which to set the attached property.</param>
/// <param name="value">The property value to set.</param>
public static void SetIcon(MenuFlyoutItem control, IconElement value)
public static void SetIcon(MenuFlyoutItemBase control, IconElement value)
{
control.SetValue(IconProperty, value);
}
Expand Down
30 changes: 21 additions & 9 deletions MicaDemo/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,41 @@ public static void TrackWindow(this Window window)

private static void TrackWindow(this AppWindow window, Frame frame)
{
if (!ActiveAppWindows.ContainsKey(frame.Dispatcher))
if (!ActiveAppWindows.TryGetValue(frame.Dispatcher, out Dictionary<XamlRoot, AppWindow> windows))
{
ActiveAppWindows[frame.Dispatcher] = new HashSet<AppWindow>();
ActiveAppWindows[frame.Dispatcher] = windows = new Dictionary<XamlRoot, AppWindow>();
}

if (!ActiveAppWindows[frame.Dispatcher].Contains(window))
if (!windows.ContainsKey(frame.XamlRoot))
{
window.Closed += (sender, args) =>
{
if (ActiveAppWindows.TryGetValue(frame.Dispatcher, out HashSet<AppWindow> windows))
{
windows?.Remove(window);
}
windows.Remove(frame.XamlRoot);
if (windows.Count <= 0)
{ ActiveAppWindows.Remove(frame.Dispatcher); }
frame.Content = null;
window = null;
};
ActiveAppWindows[frame.Dispatcher].Add(window);
windows[frame.XamlRoot] = window;
}
}

public static bool IsAppWindow(this UIElement element) =>
IsAppWindowSupported
&& element?.XamlRoot != null
&& ActiveAppWindows.TryGetValue(element.Dispatcher, out Dictionary<XamlRoot, AppWindow> windows)
&& windows.ContainsKey(element.XamlRoot);

public static AppWindow GetWindowForElement(this UIElement element) =>
IsAppWindowSupported
&& element?.XamlRoot != null
&& ActiveAppWindows.TryGetValue(element.Dispatcher, out Dictionary<XamlRoot, AppWindow> windows)
&& windows.TryGetValue(element.XamlRoot, out AppWindow window)
? window : null;

public static UIElement GetXamlRootForWindow(this AppWindow window) => ElementCompositionPreview.GetAppWindowContent(window);

public static Dictionary<CoreDispatcher, Window> ActiveWindows { get; } = new Dictionary<CoreDispatcher, Window>();
public static Dictionary<CoreDispatcher, HashSet<AppWindow>> ActiveAppWindows { get; } = IsAppWindowSupported ? new Dictionary<CoreDispatcher, HashSet<AppWindow>>() : null;
public static Dictionary<CoreDispatcher, Dictionary<XamlRoot, AppWindow>> ActiveAppWindows { get; } = IsAppWindowSupported ? new Dictionary<CoreDispatcher, Dictionary<XamlRoot, AppWindow>>() : null;
}
}
8 changes: 6 additions & 2 deletions MicaDemo/MicaDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP;NETCORE463</DefineConstants>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
Expand All @@ -90,7 +90,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'">
<OutputPath>bin\ARM64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;NETCORE463</DefineConstants>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
Expand Down Expand Up @@ -141,6 +141,9 @@
<DependentUpon>TitleBar.xaml</DependentUpon>
</Compile>
<Compile Include="Common\ExceptionHandling.cs" />
<Compile Include="Helpers\Converters\BoolToObjectConverter.cs" />
<Compile Include="Helpers\Converters\BoolToVisibilityConverter.cs" />
<Compile Include="Helpers\Converters\ConverterTools.cs" />
<Compile Include="Helpers\ThemeHelper.cs" />
<Compile Include="Common\ThreadSwitcher.cs" />
<Compile Include="Helpers\UIElementHelper.cs" />
Expand All @@ -157,6 +160,7 @@
<DependentUpon>MicaPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\BrushViewModel.cs" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
Expand Down
Loading

0 comments on commit 394a927

Please sign in to comment.