Skip to content

Commit

Permalink
Added DrawingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
haavamoa committed Jan 23, 2020
1 parent 93269a7 commit c0f4420
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
76 changes: 67 additions & 9 deletions src/DIPS.Xamarin.UI/Controls/Sheet/SheetBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler
private ModalityLayout? m_modalityLayout;

private SheetView? m_sheetView;
public event EventHandler? OnOpen;
public event EventHandler? OnClose;

/// <summary>
/// <see cref="OnOpenCommand"/>
/// </summary>
Expand Down Expand Up @@ -166,7 +165,22 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler
typeof(SheetBehavior),
ColorPalette.QuinaryAir);

/// <summary>
/// <see cref="DrawOptions"/>
/// </summary>
public static readonly BindableProperty DrawOptionsProperty = BindableProperty.Create(nameof(DrawOptions), typeof(SheetContentDrawOptions), typeof(SheetBehavior));

/// <summary>
/// Determines when the sheet content should be drawn on the screen.
/// </summary>
public SheetContentDrawOptions DrawOptions
{
get => (SheetContentDrawOptions)GetValue(DrawOptionsProperty);
set => SetValue(DrawOptionsProperty, value);
}

private bool m_fromIsDraggingContext;
private bool m_isLazyLoadingView;

/// <summary>
/// Determines the position of the sheet when it appears.
Expand Down Expand Up @@ -240,7 +254,12 @@ public bool IsOpen
}

/// <summary>
/// Command that executes when the sheet has completed it's animation and is open
/// Event that gets raised when the sheet has completed it's animation and is open
/// </summary>
public event EventHandler? OnOpen;

/// <summary>
/// Command that executes when the sheet has completed it's animation and is open.
/// </summary>
public ICommand OnOpenCommand
{
Expand All @@ -249,7 +268,7 @@ public ICommand OnOpenCommand
}

/// <summary>
/// The parameter to pass to the <see cref="OnOpenCommand"/>
/// The parameter to pass to the <see cref="OnOpenCommand"/>.
/// </summary>
public object OnOpenCommandParameter
{
Expand All @@ -258,7 +277,12 @@ public object OnOpenCommandParameter
}

/// <summary>
/// Command that executes when the sheet has completed it's animation and is closed
/// Event that gets raised when the sheet has completed it's animation and is closed
/// </summary>
public event EventHandler? OnClose;

/// <summary>
/// Command that executes when the sheet has completed it's animation and is closed.
/// </summary>
public ICommand OnCloseCommand
{
Expand All @@ -267,7 +291,7 @@ public ICommand OnCloseCommand
}

/// <summary>
/// The parameter to pass to the <see cref="OnCloseCommand"/>
/// The parameter to pass to the <see cref="OnCloseCommand"/>.
/// </summary>
public object OnCloseCommandParameter
{
Expand Down Expand Up @@ -394,9 +418,17 @@ private async void ToggleSheetVisibility()
return;
}

m_isLazyLoadingView = true;

if (IsOpen)
{
m_sheetView = new SheetView(this);

if (DrawOptions == SheetContentDrawOptions.BeforeOpen)
{
m_sheetView.SheetContentView.Content = SheetContent;
}

m_sheetView.Initialize();
SheetContent.BindingContext = BindingContextFactory?.Invoke() ?? BindingContext;

Expand All @@ -416,19 +448,30 @@ private async void ToggleSheetVisibility()
_ => throw new ArgumentOutOfRangeException()
};


//Set position based on size of content
if (Position <= 0)
{
//Calculate what size the content needs if the position is set to 0
Position = m_sheetView.SheetContentHeighRequest / m_modalityLayout.Height;
if (DrawOptions == SheetContentDrawOptions.OnOpen)
{
Position = MinPosition;
}
else
{
//Calculate what size the content needs if the position is set to 0
Position = m_sheetView.SheetContentHeightRequest / m_modalityLayout.Height;
}
}
else //Set position from input
{
await TranslateBasedOnPosition(false);
OnOpenCommand?.Execute(OnOpenCommandParameter);
OnOpen?.Invoke(this, new EventArgs());
}

if (DrawOptions == SheetContentDrawOptions.OnOpen)
{
m_sheetView.SheetContentView.Content = SheetContent;
}
}
else
{
Expand Down Expand Up @@ -552,4 +595,19 @@ public enum ContentAlignment
/// </summary>
Fill
}

/// <summary>
/// The sheet content draw options
/// </summary>
public enum SheetContentDrawOptions
{
/// <summary>
/// Draw the sheet content before the sheet is open, this happens the moment the sheet starts to open
/// </summary>
BeforeOpen = 0,
/// <summary>
/// Draw the sheet content when the sheet is open, this happens after the sheet has animated to it's position
/// </summary>
OnOpen
}
}
3 changes: 1 addition & 2 deletions src/DIPS.Xamarin.UI/Internal/Xaml/SheetView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<ContentView x:Name="SheetContent"
Content="{Binding SheetContent}" />
<ContentView x:Name="sheetContentView" />
</Grid>
</Grid>
</Frame>
Expand Down
9 changes: 7 additions & 2 deletions src/DIPS.Xamarin.UI/Internal/Xaml/SheetView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ public SheetView(SheetBehavior sheetBehavior)
/// <summary>
/// The height that the sheet content needs if it should display all of its content
/// </summary>
internal double SheetContentHeighRequest =>
SheetContent.Content.Height + HandleBoxView.Height + OuterSheetFrame.Padding.Top + OuterSheetFrame.Padding.Bottom + OuterSheetFrame.CornerRadius;
internal double SheetContentHeightRequest =>
sheetContentView.Content != null
? SheetContentView.Content.Height + HandleBoxView.Height + OuterSheetFrame.Padding.Top + OuterSheetFrame.Padding.Bottom +
OuterSheetFrame.CornerRadius
: 0;

/// <summary>
/// The internal outer sheet frame of the view
/// </summary>
internal Frame SheetFrame => OuterSheetFrame;

internal ContentView SheetContentView => sheetContentView;

private double m_newY;
private void OnDrag(object sender, PanUpdatedEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
Position="{Binding Position}"
MinPosition="{Binding MinPosition}"
MaxPosition="{Binding MaxPosition}"
BindingContextFactory="{Binding SheetViewModelFactory}">
BindingContextFactory="{Binding SheetViewModelFactory}"
DrawOptions="OnOpen">
<StackLayout>
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
Expand Down

0 comments on commit c0f4420

Please sign in to comment.