Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add popup sizing page to cover more test scenarios #1770

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public partial class AppShell : Shell
CreateViewModelMapping<PopupAnchorPage, PopupAnchorViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<PopupLayoutAlignmentPage, PopupLayoutAlignmentViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<PopupPositionPage, PopupPositionViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<PopupSizingIssuesPage, PopupSizingIssuesViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<SemanticOrderViewPage, SemanticOrderViewPageViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<ShowPopupInOnAppearingPage, ShowPopupInOnAppearingPageViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<StylePopupPage, StylePopupViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
Expand Down
1 change: 1 addition & 0 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
services.AddTransientWithShellRoute<SemanticOrderViewPage, SemanticOrderViewPageViewModel>();
services.AddTransientWithShellRoute<ShowPopupInOnAppearingPage, ShowPopupInOnAppearingPageViewModel>();
services.AddTransientWithShellRoute<StylePopupPage, StylePopupViewModel>();
services.AddTransientWithShellRoute<PopupSizingIssuesPage, PopupSizingIssuesViewModel>();

// Add Popups
services.AddTransientPopup<CsharpBindingPopup, CsharpBindingPopupViewModel>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ protected BaseGalleryPage(string title, IDeviceInfo deviceInfo, TViewModel viewM

async void HandleSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
ArgumentNullException.ThrowIfNull(sender);
try
bijington marked this conversation as resolved.
Show resolved Hide resolved
{
ArgumentNullException.ThrowIfNull(sender);

var collectionView = (CollectionView)sender;
collectionView.SelectedItem = null;
Expand All @@ -34,6 +36,12 @@ async void HandleSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
await Shell.Current.GoToAsync(AppShell.GetPageRoute(sectionModel.ViewModelType));
}
}
catch (Exception ex)
{
_ = ex;
}

}

class GalleryDataTemplate : DataTemplate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
x:Class="CommunityToolkit.Maui.Sample.Pages.Views.PopupSizingIssuesPage"
Title="MultiplePopupPage"
x:TypeArguments="viewModels:PopupSizingIssuesViewModel"
x:DataType="viewModels:PopupSizingIssuesViewModel"
x:Name="Self">

<ContentPage.Content>
<ScrollView>
<VerticalStackLayout Spacing="12">
<Label Text="Select a container to show a popup" />
<Picker
ItemsSource="{Binding Containers}"
SelectedItem="{Binding SelectedContainer}"
ItemDisplayBinding="{Binding Name}"/>

<Label Text="Padding" />
<Entry Text="{Binding Padding}" />

<Button Text="Show Popup" Command="{Binding ShowPopupCommand}" CommandParameter="{Binding Source={x:Reference Self}}" />
</VerticalStackLayout>
</ScrollView>
</ContentPage.Content>

</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommunityToolkit.Maui.Sample.ViewModels.Views;

namespace CommunityToolkit.Maui.Sample.Pages.Views;

public partial class PopupSizingIssuesPage : BasePage<PopupSizingIssuesViewModel>
{
public PopupSizingIssuesPage(
PopupSizingIssuesViewModel popupSizingIssuesViewModel)
: base(popupSizingIssuesViewModel)
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using CommunityToolkit.Maui.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public partial class PopupSizingIssuesViewModel : BaseViewModel
{
public IList<ContainerViewModel> Containers { get; } =
[
new ("HorizontalStackLayout", new ControlTemplate(() => new HorizontalStackLayout())),
new ("VerticalStackLayout", new ControlTemplate(() => new VerticalStackLayout())),
new ("Border", new ControlTemplate(() => new Border())),
new ("Grid", new ControlTemplate(() => new Grid())),
new ("CollectionView", new ControlTemplate(() => new CollectionView()))
];

[ObservableProperty, NotifyCanExecuteChangedFor(nameof(ShowPopupCommand))]
ContainerViewModel? selectedContainer;

[ObservableProperty]
int padding = 0;

[RelayCommand]
void OnShowPopup(Page page)
{
var popup = new Popup();

if (SelectedContainer?.ControlTemplate.LoadTemplate() is not View container)
{
return;
}

container.GetType().GetProperty(nameof(IPaddingElement.Padding))?.SetValue(container, new Thickness(Padding));

const string longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

container.GetType().GetProperty(nameof(IContentView.Content))?.SetValue(container, GetContentLabel(longText));

if (container is Layout layout)
{
layout.Children.Add(GetContentLabel(longText));
}
else if (container is ItemsView itemsView)
{
itemsView.ItemsSource = Enumerable.Repeat(longText, 10);
itemsView.ItemTemplate = new DataTemplate(() => GetContentLabel(longText));
}

popup.Content = container;

page.ShowPopup(popup);
}

static Label GetContentLabel(string text)
{
return new Label
{
Text = text,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
}

bool CanShowPopup(Page page) => SelectedContainer != null;
}

public partial class ContainerViewModel : ObservableObject
{
public string Name { get; }

public ControlTemplate ControlTemplate { get; }

public ContainerViewModel(string name, ControlTemplate controlTemplate)
{
Name = name;
ControlTemplate = controlTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ViewsGalleryViewModel()
SectionModel.Create<ShowPopupInOnAppearingPageViewModel>("Show Popup in OnAppearing", Colors.Red, "Proves that we now support showing a popup before the platform is even ready."),
SectionModel.Create<SemanticOrderViewPageViewModel>("Semantic Order View", Colors.Red, "SemanticOrderView allows developers to indicate the focus order of visible controls when a user is navigating via TalkBack (Android), VoiceOver (iOS) or Narrator (Windows)."),
SectionModel.Create<StylePopupViewModel>("Popup Style Page", Colors.Red, "A page demonstrating how Popups can be styled in a .NET MAUI application."),
SectionModel.Create<PopupSizingIssuesViewModel>("Popup Sizing Issues Page", Colors.Red, "A page demonstrating how Popups can be styled in a .NET MAUI application."),
})
{
}
Expand Down