<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MahApps.Metro.Controls
{
    [TemplatePart(Name = "PART_Presenter", Type = typeof(TransitioningContentControl))]
    [TemplatePart(Name = "PART_BackButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_ForwardButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_BannerGrid", Type = typeof(Grid))]
    [TemplatePart(Name = "PART_BannerLabel", Type = typeof(Label))]
    public class FlipView : Selector
    {
        private const string PART_Presenter = "PART_Presenter";
        private const string PART_BackButton = "PART_BackButton";
        private const string PART_ForwardButton = "PART_ForwardButton";
        private const string PART_BannerGrid = "PART_BannerGrid";
        private const string PART_BannerLabel = "PART_BannerLabel";

        private TransitioningContentControl presenter = null;
        private Button backButton = null;
        private Button forwardButton = null;
        private Grid bannerGrid = null;
        private Label bannerLabel = null;

        private Storyboard ShowBannerStoryboard = null;
        private Storyboard HideBannerStoryboard = null;
        private Storyboard HideControlStoryboard = null;
        private Storyboard ShowControlStoryboard = null;

        private EventHandler HideControlStoryboard_CompletedHandler = null;

        static FlipView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipView), new FrameworkPropertyMetadata(typeof(FlipView)));
        }
        public FlipView()
        {
            this.Unloaded += FlipView_Unloaded;
        }

        void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DetectControlButtonsStatus();
        }

        private void DetectControlButtonsStatus()
        {
            if (Items.Count > 0)
            {
                if (SelectedIndex == 0)
                {
                    backButton.Visibility = System.Windows.Visibility.Hidden;
                    forwardButton.Visibility = System.Windows.Visibility.Visible;
                }
                else
                    if (SelectedIndex == Items.Count - 1)
                    {
                        backButton.Visibility = System.Windows.Visibility.Visible;
                        forwardButton.Visibility = System.Windows.Visibility.Hidden;
                    }
                    else
                    {
                        backButton.Visibility = System.Windows.Visibility.Visible;
                        forwardButton.Visibility = System.Windows.Visibility.Visible;
                    }
            }
            else
            {
                backButton.Visibility = System.Windows.Visibility.Hidden;
                forwardButton.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        void FlipView_Unloaded(object sender, RoutedEventArgs e)
        {
            this.Unloaded -= FlipView_Unloaded;
            this.SelectionChanged -= FlipView_SelectionChanged;

            this.KeyDown -= FlipView_KeyDown;
            backButton.Click -= backButton_Click;
            forwardButton.Click -= forwardButton_Click;

            if (HideControlStoryboard_CompletedHandler != null)
                HideControlStoryboard.Completed -= HideControlStoryboard_CompletedHandler;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            presenter = this.Template.FindName(PART_Presenter, this) as TransitioningContentControl;
            backButton = this.Template.FindName(PART_BackButton, this) as Button;
            forwardButton = this.Template.FindName(PART_ForwardButton, this) as Button;
            bannerGrid = this.Template.FindName(PART_BannerGrid, this) as Grid;
            bannerLabel = this.Template.FindName(PART_BannerLabel, this) as Label;

            backButton.Click += backButton_Click;
            forwardButton.Click += forwardButton_Click;

            this.SelectionChanged += FlipView_SelectionChanged;
            this.KeyDown += FlipView_KeyDown;

            ShowBannerStoryboard = ((Storyboard)this.Template.Resources["ShowBannerStoryboard"]).Clone();
            HideBannerStoryboard = ((Storyboard)this.Template.Resources["HideBannerStoryboard"]).Clone();

            ShowControlStoryboard = ((Storyboard)this.Template.Resources["ShowControlStoryboard"]).Clone();
            HideControlStoryboard = ((Storyboard)this.Template.Resources["HideControlStoryboard"]).Clone();

            SelectedIndex = 0;

            DetectControlButtonsStatus();

            ShowBanner();
        }

        void FlipView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                GoBack();
                e.Handled = true;
            }
            else if (e.Key == Key.Right)
            {
                GoForward();
                e.Handled = true;
            }

            if (e.Handled)
                this.Focus();
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);

            SelectedIndex = 0;
        }

        void forwardButton_Click(object sender, RoutedEventArgs e)
        {
            GoForward();
        }

        void backButton_Click(object sender, RoutedEventArgs e)
        {
            GoBack();
        }

        public void GoBack()
        {
            if (SelectedIndex > 0)
            {
                presenter.Transition = "RightReplaceTransition";
                SelectedIndex--;
            }
        }

        public void GoForward()
        {
            if (SelectedIndex < Items.Count - 1)
            {
                presenter.Transition = "LeftReplaceTransition";
                SelectedIndex++;
            }
        }

        private void ShowBanner()
        {<commit_msg>Uses GetTemplateChild instead of Template.FindName. Added Show/Hide Controls methods.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MahApps.Metro.Controls
{
    [TemplatePart(Name = "PART_Presenter", Type = typeof(TransitioningContentControl))]
    [TemplatePart(Name = "PART_BackButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_ForwardButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_BannerGrid", Type = typeof(Grid))]
    [TemplatePart(Name = "PART_BannerLabel", Type = typeof(Label))]
    public class FlipView : Selector
    {
        private const string PART_Presenter = "PART_Presenter";
        private const string PART_BackButton = "PART_BackButton";
        private const string PART_ForwardButton = "PART_ForwardButton";
        private const string PART_BannerGrid = "PART_BannerGrid";
        private const string PART_BannerLabel = "PART_BannerLabel";

        private TransitioningContentControl presenter = null;
        private Button backButton = null;
        private Button forwardButton = null;
        private Grid bannerGrid = null;
        private Label bannerLabel = null;

        private Storyboard ShowBannerStoryboard = null;
        private Storyboard HideBannerStoryboard = null;
        private Storyboard HideControlStoryboard = null;
        private Storyboard ShowControlStoryboard = null;

        private EventHandler HideControlStoryboard_CompletedHandler = null;

        static FlipView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipView), new FrameworkPropertyMetadata(typeof(FlipView)));
        }
        public FlipView()
        {
            this.Unloaded += FlipView_Unloaded;
        }

        void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DetectControlButtonsStatus();
        }

        private void DetectControlButtonsStatus()
        {
            if (Items.Count > 0)
            {
                if (SelectedIndex == 0)
                {
                    backButton.Visibility = System.Windows.Visibility.Hidden;
                    forwardButton.Visibility = System.Windows.Visibility.Visible;
                }
                else
                    if (SelectedIndex == Items.Count - 1)
                    {
                        backButton.Visibility = System.Windows.Visibility.Visible;
                        forwardButton.Visibility = System.Windows.Visibility.Hidden;
                    }
                    else
                    {
                        backButton.Visibility = System.Windows.Visibility.Visible;
                        forwardButton.Visibility = System.Windows.Visibility.Visible;
                    }
            }
            else
            {
                backButton.Visibility = System.Windows.Visibility.Hidden;
                forwardButton.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        void FlipView_Unloaded(object sender, RoutedEventArgs e)
        {
            this.Unloaded -= FlipView_Unloaded;
            this.SelectionChanged -= FlipView_SelectionChanged;

            this.KeyDown -= FlipView_KeyDown;
            backButton.Click -= backButton_Click;
            forwardButton.Click -= forwardButton_Click;

            if (HideControlStoryboard_CompletedHandler != null)
                HideControlStoryboard.Completed -= HideControlStoryboard_CompletedHandler;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            presenter = GetTemplateChild(PART_Presenter) as TransitioningContentControl;
            backButton = GetTemplateChild(PART_BackButton) as Button;
            forwardButton = GetTemplateChild(PART_ForwardButton) as Button;
            bannerGrid = GetTemplateChild(PART_BannerGrid) as Grid;
            bannerLabel = GetTemplateChild(PART_BannerLabel) as Label;

            backButton.Click += backButton_Click;
            forwardButton.Click += forwardButton_Click;

            this.SelectionChanged += FlipView_SelectionChanged;
            this.KeyDown += FlipView_KeyDown;

            ShowBannerStoryboard = ((Storyboard)this.Template.Resources["ShowBannerStoryboard"]).Clone();
            HideBannerStoryboard = ((Storyboard)this.Template.Resources["HideBannerStoryboard"]).Clone();

            ShowControlStoryboard = ((Storyboard)this.Template.Resources["ShowControlStoryboard"]).Clone();
            HideControlStoryboard = ((Storyboard)this.Template.Resources["HideControlStoryboard"]).Clone();

            SelectedIndex = 0;

            DetectControlButtonsStatus();

            ShowBanner();
        }

        void FlipView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                GoBack();
                e.Handled = true;
            }
            else if (e.Key == Key.Right)
            {
                GoForward();
                e.Handled = true;
            }

            if (e.Handled)
                this.Focus();
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);

            SelectedIndex = 0;
        }

        void forwardButton_Click(object sender, RoutedEventArgs e)
        {
            GoForward();
        }

        void backButton_Click(object sender, RoutedEventArgs e)
        {
            GoBack();
        }

        public void GoBack()
        {
            if (SelectedIndex > 0)
            {
                presenter.Transition = "RightReplaceTransition";
                SelectedIndex--;
            }
        }

        public void GoForward()
        {
            if (SelectedIndex < Items.Count - 1)
            {
                presenter.Transition = "LeftReplaceTransition";
                SelectedIndex++;
            }
        }

        public void ShowControlButtons()
        {
            ExecuteWhenLoaded(this, () =>
                {
                    backButton.Visibility = System.Windows.Visibility.Visible;
                    forwardButton.Visibility = System.Windows.Visibility.Visible;
                });
        }
        public void HideControlButtons()
        {
            ExecuteWhenLoaded(this, () =>
                {
                    backButton.Visibility = System.Windows.Visibility.Hidden;
                    forwardButton.Visibility = System.Windows.Visibility.Hidden;
                });
        }

        private void ShowBanner()
        {
{'fraction_non_alphanumeric': 0.0811695558366193, 'fraction_numerical': 0.0010415891674726582, 'mean_word_length': 1.9811488134841428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2117085', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3168, 'n_words': 627}