<commit_before>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media;
using Windows.Storage;

namespace AppStudio.Uwp.Samples
{
    abstract public class SamplePage : Page
    {
        private bool _restoreContent = true;
        private UIElement _content = null;

        public SamplePage()
        {
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _content = this.Content;
        }

        abstract public string Caption { get; }

        protected string SampleName
        {
            get { return this.GetType().Name.Substring(0, this.GetType().Name.Length - 4); }
        }

        #region PrimaryCommands
        public IEnumerable<ICommandBarElement> PrimaryCommands
        {
            get { return (IEnumerable<ICommandBarElement>)GetValue(PrimaryCommandsProperty); }
            set { SetValue(PrimaryCommandsProperty, value); }
        }

        public static readonly DependencyProperty PrimaryCommandsProperty = DependencyProperty.Register("PrimaryCommands", typeof(IEnumerable<ICommandBarElement>), typeof(SamplePage), new PropertyMetadata(null));
        #endregion

        private bool _xamlExists = false;
        private bool _codeExists = false;

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            _xamlExists = await DocumentFileExists($"{SampleName}Xaml.xml");
            _codeExists = await DocumentFileExists($"{SampleName}CSharp.cs");

            this.PrimaryCommands = CreatePrimaryCommands().ToArray();

            base.OnNavigatedTo(e);
        }

        protected virtual IEnumerable<ICommandBarElement> CreatePrimaryCommands()
        {
            yield return CreateAppBarToggleButton(Symbol.Help, "Help", OnHelpButton);
            yield return CreateAppBarToggleButton(Symbol.Setting, "Settings", OnSettingsButton);

            if (_xamlExists || _codeExists)
            {
                yield return new AppBarSeparator();
            }
            if (_xamlExists)
            {
                yield return CreateAppBarToggleButton(new Uri("ms-appx:///Assets/Icons/Xaml.png"), "Xaml Code", OnXamlCodeButton);
            }
            if (_codeExists)
            {
                yield return CreateAppBarToggleButton(new Uri("ms-appx:///Assets/Icons/CSharp.png"), "Source Code", OnSourceCodeButton);
            }
        }

        private void OnHelpButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                this.ReleaseToggleButtons(button);
                OnHelp();
            }
            else
            {
                AppShell.Current.Shell.HideTopPane();
            }
        }
        protected virtual async void OnHelp()
        {
            var control = new DocumentControl();
            AppShell.Current.Shell.ShowTopPane(control);
            await control.ShowHelp(new Uri($"ms-appx:///Pages/{SampleName}/Docs/{SampleName}Help.html"));
        }

        private void OnSettingsButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                this.ReleaseToggleButtons(button);
                OnSettings();
            }
            else
            {
                AppShell.Current.Shell.HideRightPane();
            }
        }
        protected abstract void OnSettings();

        private void OnSourceCodeButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                _restoreContent = false;
                this.ReleaseToggleButtons(button);
                OnSourceCode();
                _restoreContent = true;
            }
            else
            {<commit_msg>Change OnSettings from PageBase<commit_after>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media;
using Windows.Storage;

namespace AppStudio.Uwp.Samples
{
    abstract public class SamplePage : Page
    {
        private bool _restoreContent = true;
        private UIElement _content = null;

        public SamplePage()
        {
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _content = this.Content;
        }

        abstract public string Caption { get; }

        protected string SampleName
        {
            get { return this.GetType().Name.Substring(0, this.GetType().Name.Length - 4); }
        }

        #region PrimaryCommands
        public IEnumerable<ICommandBarElement> PrimaryCommands
        {
            get { return (IEnumerable<ICommandBarElement>)GetValue(PrimaryCommandsProperty); }
            set { SetValue(PrimaryCommandsProperty, value); }
        }

        public static readonly DependencyProperty PrimaryCommandsProperty = DependencyProperty.Register("PrimaryCommands", typeof(IEnumerable<ICommandBarElement>), typeof(SamplePage), new PropertyMetadata(null));
        #endregion

        private bool _xamlExists = false;
        private bool _codeExists = false;

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            _xamlExists = await DocumentFileExists($"{SampleName}Xaml.xml");
            _codeExists = await DocumentFileExists($"{SampleName}CSharp.cs");

            this.PrimaryCommands = CreatePrimaryCommands().ToArray();

            base.OnNavigatedTo(e);
        }

        protected virtual IEnumerable<ICommandBarElement> CreatePrimaryCommands()
        {
            yield return CreateAppBarToggleButton(Symbol.Help, "Help", OnHelpButton);
            yield return CreateAppBarToggleButton(Symbol.Setting, "Settings", OnSettingsButton);

            if (_xamlExists || _codeExists)
            {
                yield return new AppBarSeparator();
            }
            if (_xamlExists)
            {
                yield return CreateAppBarToggleButton(new Uri("ms-appx:///Assets/Icons/Xaml.png"), "Xaml Code", OnXamlCodeButton);
            }
            if (_codeExists)
            {
                yield return CreateAppBarToggleButton(new Uri("ms-appx:///Assets/Icons/CSharp.png"), "Source Code", OnSourceCodeButton);
            }
        }

        private void OnHelpButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                this.ReleaseToggleButtons(button);
                OnHelp();
            }
            else
            {
                AppShell.Current.Shell.HideTopPane();
            }
        }
        protected virtual async void OnHelp()
        {
            var control = new DocumentControl();
            AppShell.Current.Shell.ShowTopPane(control);
            await control.ShowHelp(new Uri($"ms-appx:///Pages/{SampleName}/Docs/{SampleName}Help.html"));
        }

        private void OnSettingsButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                this.ReleaseToggleButtons(button);
                OnSettings();
            }
            else
            {
                AppShell.Current.Shell.HideRightPane();
            }
        }
        protected virtual void OnSettings() { }

        private void OnSourceCodeButton(object sender, RoutedEventArgs e)
        {
            var button = sender as AppBarToggleButton;
            if (button.IsChecked.Value)
            {
                _restoreContent = false;
                this.ReleaseToggleButtons(button);
                OnSourceCode();
                _restoreContent = true;
            }
            else
            {
{'fraction_non_alphanumeric': 0.08574556069082948, 'fraction_numerical': 0.00048649963512527365, 'mean_word_length': 1.9159574468085105, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4258074', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2025, 'n_words': 429}