Skip to content

Commit

Permalink
Split out "Overlays" and "Sheets" (#21)
Browse files Browse the repository at this point in the history
* Split generic "overlay" concept from "sheets"

* Some renames

* Use injected ISafeAreaService
  • Loading branch information
WamWooWam authored Dec 13, 2024
1 parent 0d99277 commit 50bf6fe
Show file tree
Hide file tree
Showing 29 changed files with 476 additions and 393 deletions.
2 changes: 1 addition & 1 deletion UniSky/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void ConfigureServices()
collection.AddSingleton<ITypedSettings, SettingsService>();
collection.AddSingleton<IThemeService, ThemeService>();
collection.AddSingleton<INavigationServiceLocator, NavigationServiceLocator>();
collection.AddScoped<ISafeAreaService, CoreWindowSafeAreaService>();
collection.AddScoped<ISafeAreaService, ApplicationViewSafeAreaService>();
collection.AddScoped<ISheetService, SheetService>();

collection.AddTransient<LoginService>();
Expand Down
4 changes: 1 addition & 3 deletions UniSky/Controls/Compose/ComposeSheet.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<sheet:SheetControl
x:Uid="ComposeSheet"
x:Class="UniSky.Controls.Compose.ComposeSheet"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -17,9 +18,6 @@
PrimaryButtonCommand="{x:Bind ViewModel.PostCommand}"
IsPrimaryButtonEnabled="{x:Bind ViewModel.CanPost, Mode=OneWay}"
SecondaryButtonCommand="{x:Bind ViewModel.HideCommand}">
<sheet:SheetControl.TitleContent>
<TextBlock x:Uid="ComposeSheetTitle" Text="NEW POST"/>
</sheet:SheetControl.TitleContent>
<sheet:SheetControl.PrimaryButtonContent>
<TextBlock x:Uid="ComposeSheetPrimaryText" Text="Post" Margin="8,0"/>
</sheet:SheetControl.PrimaryButtonContent>
Expand Down
9 changes: 5 additions & 4 deletions UniSky/Controls/Compose/ComposeSheet.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using UniSky.Controls.Overlay;
using UniSky.Controls.Sheet;
using UniSky.Services;
using UniSky.ViewModels.Compose;
Expand Down Expand Up @@ -48,7 +49,7 @@ protected override void OnBottomInsetsChanged(double leftInset, double rightInse
public bool Not(bool b, bool a)
=> !a && !b;

private void OnShowing(SheetControl sender, SheetShowingEventArgs e)
private void OnShowing(OverlayControl sender, OverlayShowingEventArgs e)
{
var inputPane = InputPane.GetForCurrentView();
inputPane.Showing += OnInputPaneShowing;
Expand All @@ -73,7 +74,7 @@ private void OnShowing(SheetControl sender, SheetShowingEventArgs e)
}
}

private void OnHidden(SheetControl sender, RoutedEventArgs args)
private void OnHidden(OverlayControl sender, RoutedEventArgs args)
{
var inputPane = InputPane.GetForCurrentView();
inputPane.Showing -= OnInputPaneShowing;
Expand All @@ -89,12 +90,12 @@ private void OnHidden(SheetControl sender, RoutedEventArgs args)
}
}

private void OnShown(SheetControl sender, RoutedEventArgs args)
private void OnShown(OverlayControl sender, RoutedEventArgs args)
{
PrimaryTextBox.Focus(FocusState.Programmatic);
}

private async void OnHiding(SheetControl sender, SheetHidingEventArgs e)
private async void OnHiding(OverlayControl sender, OverlayHidingEventArgs e)
{
var deferral = e.GetDeferral();
try
Expand Down
118 changes: 118 additions & 0 deletions UniSky/Controls/Overlay/OverlayControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniSky.Services;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace UniSky.Controls.Overlay;

[ContentProperty(Name = nameof(OverlayContent))]
public abstract class OverlayControl : Control
{
public static readonly DependencyProperty OverlayContentProperty =
DependencyProperty.Register("OverlayContent", typeof(object), typeof(OverlayControl), new PropertyMetadata(null));

public static readonly DependencyProperty OverlayContentTemplateProperty =
DependencyProperty.Register("OverlayContentTemplate", typeof(DataTemplate), typeof(OverlayControl), new PropertyMetadata(null));

public static readonly DependencyProperty TitleContentProperty =
DependencyProperty.Register("TitleContent", typeof(object), typeof(OverlayControl), new PropertyMetadata(null));

public static readonly DependencyProperty TitleContentTemplateProperty =
DependencyProperty.Register("TitleContentTemplate", typeof(DataTemplate), typeof(OverlayControl), new PropertyMetadata(null));

public static readonly DependencyProperty PreferredWindowSizeProperty =
DependencyProperty.Register("PreferredWindowSize", typeof(Size), typeof(OverlayControl), new PropertyMetadata(new Size(320, 400)));

public IOverlayController Controller { get; private set; }

public object OverlayContent
{
get => (object)GetValue(OverlayContentProperty);
set => SetValue(OverlayContentProperty, value);
}

public DataTemplate OverlayContentTemplate
{
get => (DataTemplate)GetValue(OverlayContentTemplateProperty);
set => SetValue(OverlayContentTemplateProperty, value);
}

public object TitleContent
{
get => (object)GetValue(TitleContentProperty);
set => SetValue(TitleContentProperty, value);
}

public DataTemplate TitleContentTemplate
{
get => (DataTemplate)GetValue(TitleContentTemplateProperty);
set => SetValue(TitleContentTemplateProperty, value);
}

public Size PreferredWindowSize
{
get => (Size)GetValue(PreferredWindowSizeProperty);
set => SetValue(PreferredWindowSizeProperty, value);
}

public event TypedEventHandler<OverlayControl, RoutedEventArgs> Hidden;
public event TypedEventHandler<OverlayControl, OverlayHidingEventArgs> Hiding;
public event TypedEventHandler<OverlayControl, OverlayShowingEventArgs> Showing;
public event TypedEventHandler<OverlayControl, RoutedEventArgs> Shown;

internal void SetOverlayController(IOverlayController controller)
{
Controller = controller;
}

internal void InvokeHidden()
{
OnHidden(new RoutedEventArgs());
}

internal async Task<bool> InvokeHidingAsync()
{
var ev = new OverlayHidingEventArgs();
OnHiding(ev);

await ev.WaitOnDeferral();

return !ev.Cancel;
}

internal void InvokeShowing(object parameter)
{
OnShowing(new OverlayShowingEventArgs(parameter));
}

internal void InvokeShown()
{
OnShown(new RoutedEventArgs());
}

protected virtual void OnHiding(OverlayHidingEventArgs args)
{
Hiding?.Invoke(this, args);
}

protected virtual void OnHidden(RoutedEventArgs args)
{
Hidden?.Invoke(this, args);
}

protected virtual void OnShowing(OverlayShowingEventArgs args)
{
Showing?.Invoke(this, args);
}

protected virtual void OnShown(RoutedEventArgs args)
{
Shown?.Invoke(this, args);
}
}
32 changes: 32 additions & 0 deletions UniSky/Controls/Overlay/OverlayHidingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;

namespace UniSky.Controls.Overlay;

public class OverlayHidingEventArgs : RoutedEventArgs
{
private Deferral _deferral;
private TaskCompletionSource<object> _deferralCompletion;

public Deferral GetDeferral()
{
_deferralCompletion = new TaskCompletionSource<object>();
return (_deferral ??= new Deferral(OnDeferralCompleted));
}

public bool Cancel { get; set; } = false;

internal Task WaitOnDeferral()
{
if (_deferral == null)
return Task.CompletedTask;
else
return _deferralCompletion.Task;
}

private void OnDeferralCompleted()
{
_deferralCompletion?.SetResult(null);
}
}
13 changes: 13 additions & 0 deletions UniSky/Controls/Overlay/OverlayShowingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Windows.UI.Xaml;

namespace UniSky.Controls.Overlay;

public class OverlayShowingEventArgs : RoutedEventArgs
{
public object Parameter { get; }

public OverlayShowingEventArgs(object parameter)
{
Parameter = parameter;
}
}
8 changes: 4 additions & 4 deletions UniSky/Controls/PreviewPaneAuroraControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public partial class PreviewPaneAuroraControl : UserControl
{
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
get => (Color)GetValue(ColorProperty);
set => SetValue(ColorProperty, value);
}

public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(Color), typeof(PreviewPaneAuroraControl), new PropertyMetadata(Color.FromArgb(255, 0x85, 0x99, 0xB4), OnColorChanged));

public TimeSpan AnimationDuration
{
get { return (TimeSpan)GetValue(AnimationDurationProperty); }
set { SetValue(AnimationDurationProperty, value); }
get => (TimeSpan)GetValue(AnimationDurationProperty);
set => SetValue(AnimationDurationProperty, value);
}

// Using a DependencyProperty as the backing store for AnimationDuration. This enables animation, styling, binding, etc...
Expand Down
12 changes: 6 additions & 6 deletions UniSky/Controls/RichTextBlock/RichTextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ public sealed class RichTextBlock : Control

public TextWrapping TextWrapping
{
get { return (TextWrapping)GetValue(TextWrappingProperty); }
set { SetValue(TextWrappingProperty, value); }
get => (TextWrapping)GetValue(TextWrappingProperty);
set => SetValue(TextWrappingProperty, value);
}

public static readonly DependencyProperty TextWrappingProperty =
DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(RichTextBlock), new PropertyMetadata(TextWrapping.Wrap));

public bool IsTextSelectionEnabled
{
get { return (bool)GetValue(IsTextSelectionEnabledProperty); }
set { SetValue(IsTextSelectionEnabledProperty, value); }
get => (bool)GetValue(IsTextSelectionEnabledProperty);
set => SetValue(IsTextSelectionEnabledProperty, value);
}

public static readonly DependencyProperty IsTextSelectionEnabledProperty =
DependencyProperty.Register("IsTextSelectionEnabled", typeof(bool), typeof(RichTextBlock), new PropertyMetadata(true));

public IList<FacetInline> Inlines
{
get { return (IList<FacetInline>)GetValue(InlinesProperty); }
set { SetValue(InlinesProperty, value); }
get => (IList<FacetInline>)GetValue(InlinesProperty);
set => SetValue(InlinesProperty, value);
}

public static readonly DependencyProperty InlinesProperty =
Expand Down
5 changes: 3 additions & 2 deletions UniSky/Controls/Settings/SettingsSheet.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using UniSky.Controls.Overlay;
using UniSky.Controls.Sheet;
using UniSky.Services;
using UniSky.ViewModels.Settings;
Expand All @@ -19,12 +20,12 @@ public SettingsSheet()
this.Hiding += OnHiding;
}

private void OnShowing(SheetControl sender, SheetShowingEventArgs args)
private void OnShowing(OverlayControl sender, OverlayShowingEventArgs args)
{
this.DataContext = ActivatorUtilities.CreateInstance<SettingsViewModel>(ServiceContainer.Scoped);
}

private async void OnHiding(SheetControl sender, SheetHidingEventArgs args)
private async void OnHiding(OverlayControl sender, OverlayHidingEventArgs args)
{
if (!ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "RequestRestartAsync"))
return;
Expand Down
Loading

0 comments on commit 50bf6fe

Please sign in to comment.