<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template10.Services;
using Prism.Navigation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Prism.Ioc;
using System.Collections.ObjectModel;
using Windows.UI.ViewManagement;
using win = Windows;
using System.Threading;
using Prism;
using Prism.Utilities;
using Prism.Services;

namespace Template10.Controls
{
    public class NavViewEx : NavigationView
    {
        private Button _togglePaneButton;
        private TextBlock _paneTitleTextBlock;
        private Button _backButton;
        private CoreDispatcher _dispatcher;
        private Frame _frame;

        public NavViewEx()
        {
            DefaultStyleKey = typeof(NavigationView);

            if (win.ApplicationModel.DesignMode.DesignModeEnabled
                || win.ApplicationModel.DesignMode.DesignMode2Enabled)
            {
                return;
            }

            Content = _frame = new Frame();
            _dispatcher = _frame.Dispatcher;

            _frame.Navigated += (s, e) =>
            {
                if (TryFindItem(e.SourcePageType, e.Parameter, out var item))
                {
                    SetSelectedItem(item);
                }
            };

            NavigationService = (IPlatformNavigationService)Prism.Navigation.NavigationService
                .Create(_frame, Gestures.Back, Gestures.Forward, Gestures.Refresh);

            ItemInvoked += (s, e) =>
            {
                SelectedItem = (e.IsSettingsInvoked) ? SettingsItem : Find(e.InvokedItem.ToString());
            };

            RegisterPropertyChangedCallback(IsPaneOpenProperty, (s, e) =>
            {
                UpdateAppTitleVisibility();
                UpdatePaneHeadersVisibility();
            });

            Window.Current.CoreWindow.SizeChanged += (s, e) =>
            {
                UpdatePageHeaderContent();
            };

            Loaded += (s, e) =>
            {
                UpdateAppTitleVisibility();
                UpdatePaneHeadersVisibility();
                UpdatePageHeaderContent();
                SetupBackButton();
            };
        }

        public IPlatformNavigationService NavigationService { get; }

        private void SetupBackButton()
        {
            var children = XamlUtilities.RecurseChildren(this);
            var grids = children.OfType<Grid>();
            var grid = grids.Single(x => x.Name == "TogglePaneTopPadding");
            grid.Visibility = Visibility.Collapsed;

            grid = grids.Single(x => x.Name == "ContentPaneTopPadding");
            grid.RegisterPropertyChangedCallback(HeightProperty, (s, args) =>
            {
                if (grid.Height != 44d)
                {
                    grid.Height = 44d;
                }
            });
            grid.Height = 44d;

            var child_buttons = children.OfType<Button>();

            _togglePaneButton = child_buttons.Single(x => x.Name == "TogglePaneButton");
            _togglePaneButton.RegisterPropertyChangedCallback(MarginProperty, (s, args) =>
            {
                if (_togglePaneButton.Margin.Top != 0)
                {
                    _togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
                }
            });
            _togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
            _togglePaneButton.Focus(FocusState.Programmatic);

            var parent_grid = _togglePaneButton.Parent as Grid;
            parent_grid.Width = double.NaN;
            parent_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(48) });
            parent_grid.ColumnDefinitions.Add(new ColumnDefinition { });
            parent_grid.RowDefinitions[0].Height = GridLength.Auto;
            parent_grid.RowDefinitions[1].Height = GridLength.Auto;

            _paneTitleTextBlock = new TextBlock
            {
                Name = "PaneTitleTextBlock",
                Margin = new Thickness(8, 18, 0, 0),
                FontSize = 15,
                FontFamily = new FontFamily("Segoe UI Bold"),
                TextWrapping = TextWrapping.NoWrap,
                Foreground = Resources["SystemControlForegroundBaseHighBrush"] as Brush,
                VerticalAlignment = VerticalAlignment.Top,
                IsHitTestVisible = false,
                Text = "<NAME>",
            };
            _paneTitleTextBlock.SetValue(Grid.ColumnProperty, 1);
            _paneTitleTextBlock.SetValue(Grid.RowProperty, 1);
            _paneTitleTextBlock.SetValue(Canvas.ZIndexProperty, 100);
            // parent_grid.Children.Add(_paneTitleTextBlock);

            _backButton = new Button
            {
                Name = "BackButton",
                Content = new SymbolIcon
                {
                    Symbol = Symbol.Back,
                    IsHitTestVisible = false
                },
                Style = Resources["PaneToggleButtonStyle"] as Style,
            };
            _backButton.SetValue(Canvas.ZIndexProperty, 100);
            parent_grid.Children.Insert(1, _backButton);

            NavigationService.CanGoBackChanged += (s, args) =>
            {
                _backButton.IsEnabled = NavigationService.CanGoBack();
            };

            _backButton.Click += (s, args) =>
            {
                var gesture_service = GestureService.GetForCurrentView();
                gesture_service.RaiseBackRequested();
            };
        }

        public IPlatformNavigationService Initialize()
        {
            var first = MenuItems
                .OfType<NavigationViewItem>()
                .SingleOrDefault(x => (bool)x.GetValue(NavViewProps.IsStartPageProperty));

            if (first != null)
            {
                SetSelectedItem(first);
            }

            return NavigationService;
        }

        public enum ItemHeaderBehaviors { Hide, Remove, None }
        public ItemHeaderBehaviors ItemHeaderBehavior { get; set; } = ItemHeaderBehaviors.Remove;

        public Uri SettingsNavigationUri { get; set; }
        public event EventHandler SettingsInvoked;

        public new object SelectedItem
        {
            set => SetSelectedItem(value);<commit_msg>Change uri to string (NavViewEx/SettingsNavigationUri) (#1671)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template10.Services;
using Prism.Navigation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Prism.Ioc;
using System.Collections.ObjectModel;
using Windows.UI.ViewManagement;
using win = Windows;
using System.Threading;
using Prism;
using Prism.Utilities;
using Prism.Services;

namespace Template10.Controls
{
    public class NavViewEx : NavigationView
    {
        private Button _togglePaneButton;
        private TextBlock _paneTitleTextBlock;
        private Button _backButton;
        private CoreDispatcher _dispatcher;
        private Frame _frame;

        public NavViewEx()
        {
            DefaultStyleKey = typeof(NavigationView);

            if (win.ApplicationModel.DesignMode.DesignModeEnabled
                || win.ApplicationModel.DesignMode.DesignMode2Enabled)
            {
                return;
            }

            Content = _frame = new Frame();
            _dispatcher = _frame.Dispatcher;

            _frame.Navigated += (s, e) =>
            {
                if (TryFindItem(e.SourcePageType, e.Parameter, out var item))
                {
                    SetSelectedItem(item);
                }
            };

            NavigationService = (IPlatformNavigationService)Prism.Navigation.NavigationService
                .Create(_frame, Gestures.Back, Gestures.Forward, Gestures.Refresh);

            ItemInvoked += (s, e) =>
            {
                SelectedItem = (e.IsSettingsInvoked) ? SettingsItem : Find(e.InvokedItem.ToString());
            };

            RegisterPropertyChangedCallback(IsPaneOpenProperty, (s, e) =>
            {
                UpdateAppTitleVisibility();
                UpdatePaneHeadersVisibility();
            });

            Window.Current.CoreWindow.SizeChanged += (s, e) =>
            {
                UpdatePageHeaderContent();
            };

            Loaded += (s, e) =>
            {
                UpdateAppTitleVisibility();
                UpdatePaneHeadersVisibility();
                UpdatePageHeaderContent();
                SetupBackButton();
            };
        }

        public IPlatformNavigationService NavigationService { get; }

        private void SetupBackButton()
        {
            var children = XamlUtilities.RecurseChildren(this);
            var grids = children.OfType<Grid>();
            var grid = grids.Single(x => x.Name == "TogglePaneTopPadding");
            grid.Visibility = Visibility.Collapsed;

            grid = grids.Single(x => x.Name == "ContentPaneTopPadding");
            grid.RegisterPropertyChangedCallback(HeightProperty, (s, args) =>
            {
                if (grid.Height != 44d)
                {
                    grid.Height = 44d;
                }
            });
            grid.Height = 44d;

            var child_buttons = children.OfType<Button>();

            _togglePaneButton = child_buttons.Single(x => x.Name == "TogglePaneButton");
            _togglePaneButton.RegisterPropertyChangedCallback(MarginProperty, (s, args) =>
            {
                if (_togglePaneButton.Margin.Top != 0)
                {
                    _togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
                }
            });
            _togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
            _togglePaneButton.Focus(FocusState.Programmatic);

            var parent_grid = _togglePaneButton.Parent as Grid;
            parent_grid.Width = double.NaN;
            parent_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(48) });
            parent_grid.ColumnDefinitions.Add(new ColumnDefinition { });
            parent_grid.RowDefinitions[0].Height = GridLength.Auto;
            parent_grid.RowDefinitions[1].Height = GridLength.Auto;

            _paneTitleTextBlock = new TextBlock
            {
                Name = "PaneTitleTextBlock",
                Margin = new Thickness(8, 18, 0, 0),
                FontSize = 15,
                FontFamily = new FontFamily("Segoe UI Bold"),
                TextWrapping = TextWrapping.NoWrap,
                Foreground = Resources["SystemControlForegroundBaseHighBrush"] as Brush,
                VerticalAlignment = VerticalAlignment.Top,
                IsHitTestVisible = false,
                Text = "<NAME>",
            };
            _paneTitleTextBlock.SetValue(Grid.ColumnProperty, 1);
            _paneTitleTextBlock.SetValue(Grid.RowProperty, 1);
            _paneTitleTextBlock.SetValue(Canvas.ZIndexProperty, 100);
            // parent_grid.Children.Add(_paneTitleTextBlock);

            _backButton = new Button
            {
                Name = "BackButton",
                Content = new SymbolIcon
                {
                    Symbol = Symbol.Back,
                    IsHitTestVisible = false
                },
                Style = Resources["PaneToggleButtonStyle"] as Style,
            };
            _backButton.SetValue(Canvas.ZIndexProperty, 100);
            parent_grid.Children.Insert(1, _backButton);

            NavigationService.CanGoBackChanged += (s, args) =>
            {
                _backButton.IsEnabled = NavigationService.CanGoBack();
            };

            _backButton.Click += (s, args) =>
            {
                var gesture_service = GestureService.GetForCurrentView();
                gesture_service.RaiseBackRequested();
            };
        }

        public IPlatformNavigationService Initialize()
        {
            var first = MenuItems
                .OfType<NavigationViewItem>()
                .SingleOrDefault(x => (bool)x.GetValue(NavViewProps.IsStartPageProperty));

            if (first != null)
            {
                SetSelectedItem(first);
            }

            return NavigationService;
        }

        public enum ItemHeaderBehaviors { Hide, Remove, None }
        public ItemHeaderBehaviors ItemHeaderBehavior { get; set; } = ItemHeaderBehaviors.Remove;

        public string SettingsNavigationUri { get; set; }
        public event EventHandler SettingsInvoked;

        public new object SelectedItem
        {
            set => SetSelectedItem(value);
{'fraction_non_alphanumeric': 0.08954065797641217, 'fraction_numerical': 0.006828057107386716, 'mean_word_length': 1.7880164395414233, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, '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': '5445889', 'n_tokens_mistral': 3361, 'n_tokens_neox': 3111, 'n_words': 616}