summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/WindowsPlatformSpecificsGalleryHelpers.cs
blob: 9b3fbd851444a30023bcc846ce160109f27483f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Linq;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;

namespace Xamarin.Forms.Controls.GalleryPages.PlatformSpecificsGalleries
{
    internal static class WindowsPlatformSpecificsGalleryHelpers
    {
        const string CommandBarActionTitle = "Hey!";
        const string CommandBarActionMessage = "Command Bar Item Clicked";
        const string CommandBarActionDismiss = "OK";

        public static void AddToolBarItems(Page page)
        {
            Action action = () => page.DisplayAlert(CommandBarActionTitle, CommandBarActionMessage, CommandBarActionDismiss);

            var tb1 = new ToolbarItem("Primary 1", "coffee.png", action, ToolbarItemOrder.Primary)
            {
                IsEnabled = true,
                AutomationId = "toolbaritem_primary1"
            };

            var tb2 = new ToolbarItem("Primary 2", "coffee.png", action, ToolbarItemOrder.Primary)
            {
                IsEnabled = true,
                AutomationId = "toolbaritem_primary2"
            };

            var tb3 = new ToolbarItem("Seconday 1", "coffee.png", action, ToolbarItemOrder.Secondary)
            {
                IsEnabled = true,
                AutomationId = "toolbaritem_secondary3"
            };

            var tb4 = new ToolbarItem("Secondary 2", "coffee.png", action, ToolbarItemOrder.Secondary)
            {
                IsEnabled = true,
                AutomationId = "toolbaritem_secondary4"
            };

            page.ToolbarItems.Add(tb1);
            page.ToolbarItems.Add(tb2);
            page.ToolbarItems.Add(tb3);
            page.ToolbarItems.Add(tb4);
        }

        public static Layout CreateChanger(Type enumType, string defaultOption, Action<Picker> selectedIndexChanged, string label)
        {
            var picker = new Picker();
            string[] options = Enum.GetNames(enumType);
            foreach (string option in options)
            {
                picker.Items.Add(option);
            }

            picker.SelectedIndex = options.IndexOf(defaultOption);

            picker.SelectedIndexChanged += (sender, args) =>
            {
                selectedIndexChanged(picker);
            };

            var changerLabel = new Label { Text = label, VerticalOptions = LayoutOptions.Center };

            var layout = new Grid
            {
                HorizontalOptions = LayoutOptions.Center,
                ColumnDefinitions = new ColumnDefinitionCollection
                {
                    new ColumnDefinition { Width = 150 },
                    new ColumnDefinition { Width = 100 }
                },
                Children = { changerLabel, picker }
            };

            Grid.SetColumn(changerLabel, 0);
            Grid.SetColumn(picker, 1);

            return layout;
        }

        public static Layout CreateToolbarPlacementChanger(Page page)
        {
            Type enumType = typeof(ToolbarPlacement);

            return CreateChanger(enumType,
                Enum.GetName(enumType, page.On<Windows>().GetToolbarPlacement()),
                picker =>
                {
                    page.On<Windows>().SetToolbarPlacement((ToolbarPlacement)Enum.Parse(enumType, picker.Items[picker.SelectedIndex]));
                }, "Select Toolbar Placement");
        }

        public static Layout CreateAddRemoveToolBarItemButtons(Page page)
        {
            var layout = new StackLayout { Orientation = StackOrientation.Vertical, HorizontalOptions = LayoutOptions.Center };
            layout.Children.Add(new Label { Text = "Toolbar Items:" });

            var buttonLayout = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.Center
            };

            layout.Children.Add(buttonLayout);

            var addPrimary = new Button { Text = "Add Primary", BackgroundColor = Color.Gray };
            var addSecondary = new Button { Text = "Add Secondary", BackgroundColor = Color.Gray };
            var remove = new Button { Text = "Remove", BackgroundColor = Color.Gray };

            buttonLayout.Children.Add(addPrimary);
            buttonLayout.Children.Add(addSecondary);
            buttonLayout.Children.Add(remove);

            Action action = () => page.DisplayAlert(CommandBarActionTitle, CommandBarActionMessage, CommandBarActionDismiss);

            addPrimary.Clicked += (sender, args) =>
            {
                int index = page.ToolbarItems.Count(item => item.Order == ToolbarItemOrder.Primary) + 1;
                page.ToolbarItems.Add(new ToolbarItem($"Primary {index}", "coffee.png", action, ToolbarItemOrder.Primary));
            };

            addSecondary.Clicked += (sender, args) =>
            {
                int index = page.ToolbarItems.Count(item => item.Order == ToolbarItemOrder.Secondary) + 1;
                page.ToolbarItems.Add(new ToolbarItem($"Secondary {index}", "coffee.png", action, ToolbarItemOrder.Secondary));
            };

            remove.Clicked += (sender, args) =>
            {
                if (page.ToolbarItems.Any())
                {
                    page.ToolbarItems.RemoveAt(0);
                }
            };

            return layout;
        }
    }
}