<commit_before>﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using BCad.EventArguments;
using BCad.Extensions;
using BCad.Primitives;
using BCad.Services;

namespace BCad.UI.View
{
    /// <summary>
    /// Interaction logic for XamlRenderer.xaml
    /// </summary>
    public partial class XamlRenderer : UserControl, IRenderer
    {
        IViewControl ViewControl;
        private IWorkspace Workspace;
        private IInputService InputService;
        private Color AutoColor;
        private Plane DisplayPlane;
        private BindingClass BindObject = new BindingClass();

        private class BindingClass : INotifyPropertyChanged
        {
            private double thickness = 0.0;
            public double Thickness
            {
                get { return thickness; }
                set
                {
                    thickness = value;
                    OnPropertyChanged("Thickness");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string property)
            {
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(property));
                }
            }
        }

        public XamlRenderer()
        {
            InitializeComponent();
        }

        public XamlRenderer(IViewControl viewControl, IWorkspace workspace, IInputService inputService)
            : this()
        {
            ViewControl = viewControl;
            Workspace = workspace;
            InputService = inputService;

            Workspace.CommandExecuted += (_, __) => this.RubberBandCanvas.Children.Clear();
            Workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
            Workspace.SettingsManager.PropertyChanged += SettingsManager_PropertyChanged;

            this.Loaded += (_, __) =>
                {
                    foreach (var setting in new[] { Constants.BackgroundColorString })
                        SettingsManager_PropertyChanged(Workspace.SettingsManager, new PropertyChangedEventArgs(setting));

                    RecalcTransform();
                };
            this.SizeChanged += (_, __) => RecalcTransform();
        }

        private void SettingsManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case Constants.BackgroundColorString:
                    this.Background = new SolidColorBrush(Workspace.SettingsManager.BackgroundColor.ToMediaColor());
                    AutoColor = Workspace.SettingsManager.BackgroundColor.GetAutoContrastingColor().ToMediaColor();
                    break;
            }
        }

        private void Workspace_WorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
        {
            var redraw = false;
            if (e.IsActiveViewPortChange)
            {
                RecalcTransform();
            }
            if (e.IsDrawingChange)
            {
                redraw = true;
                this.RubberBandCanvas.Children.Clear();
            }

            if (redraw)
            {
                this.Dispatcher.BeginInvoke((Action)(() => Redraw()));
            }
        }

        private void RecalcTransform()
        {
            var start = DateTime.UtcNow;
            if (Workspace == null || Workspace.ViewControl == null)
                return;

            DisplayPlane = new Plane(Workspace.ActiveViewPort.BottomLeft, Workspace.ActiveViewPort.Sight);

            var scale = Workspace.ViewControl.DisplayHeight / Workspace.ActiveViewPort.ViewHeight;
            var t = new TransformGroup();
            t.Children.Add(new TranslateTransform(-Workspace.ActiveViewPort.BottomLeft.X, -Workspace.ActiveViewPort.BottomLeft.Y));
            t.Children.Add(new ScaleTransform(scale, -scale));
            t.Children.Add(new TranslateTransform(0, Workspace.ViewControl.DisplayHeight));
            this.PrimitiveCanvas.RenderTransform = t;
            this.RubberBandCanvas.RenderTransform = t;
            var newThickness = 1.0 / scale;
            if (BindObject.Thickness != newThickness)
                BindObject.Thickness = newThickness;

            var end = DateTime.UtcNow;
            var elapsed = (end - start).TotalMilliseconds;
            Fps.Text = string.Format("Transform update done in {0} ms", elapsed);

            this.Dispatcher.BeginInvoke((Action)(() => RenderRubberBandLines()));
        }

        private void Redraw()
        {
            var start = DateTime.UtcNow;
            var drawing = Workspace.Drawing;
            this.PrimitiveCanvas.Children.Clear();
            foreach (var layer in drawing.GetLayers())
            {
                foreach (var entity in layer.GetEntities())
                {
                    foreach (var prim in entity.GetPrimitives())
                    {<commit_msg>update metro app to use xaml renderer<commit_after>﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using BCad.EventArguments;
using BCad.Extensions;
using BCad.Primitives;
using BCad.Services;

namespace BCad.UI.View
{
    /// <summary>
    /// Interaction logic for XamlRenderer.xaml
    /// </summary>
    public partial class XamlRenderer : UserControl, IRenderer
    {
        IViewControl ViewControl;
        private IWorkspace Workspace;
        private IInputService InputService;
        private Color AutoColor;
        private Plane DisplayPlane;
        private BindingClass BindObject = new BindingClass();

        private class BindingClass : INotifyPropertyChanged
        {
            private double thickness = 0.0;
            public double Thickness
            {
                get { return thickness; }
                set
                {
                    if (thickness == value)
                        return;
                    thickness = value;
                    OnPropertyChanged("Thickness");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string property)
            {
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(property));
                }
            }
        }

        public XamlRenderer()
        {
            InitializeComponent();
        }

        public XamlRenderer(IViewControl viewControl, IWorkspace workspace, IInputService inputService)
            : this()
        {
            ViewControl = viewControl;
            Workspace = workspace;
            InputService = inputService;

            Workspace.CommandExecuted += (_, __) => this.RubberBandCanvas.Children.Clear();
            Workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
            Workspace.SettingsManager.PropertyChanged += SettingsManager_PropertyChanged;

            this.Loaded += (_, __) =>
                {
                    foreach (var setting in new[] { Constants.BackgroundColorString })
                        SettingsManager_PropertyChanged(Workspace.SettingsManager, new PropertyChangedEventArgs(setting));

                    RecalcTransform();
                };
            this.SizeChanged += (_, __) => RecalcTransform();
        }

        private void SettingsManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case Constants.BackgroundColorString:
                    this.Background = new SolidColorBrush(Workspace.SettingsManager.BackgroundColor.ToMediaColor());
                    AutoColor = Workspace.SettingsManager.BackgroundColor.GetAutoContrastingColor().ToMediaColor();
                    break;
            }
        }

        private void Workspace_WorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
        {
            var redraw = false;
            if (e.IsActiveViewPortChange)
            {
                RecalcTransform();
            }
            if (e.IsDrawingChange)
            {
                redraw = true;
                this.RubberBandCanvas.Children.Clear();
            }

            if (redraw)
            {
                this.Dispatcher.BeginInvoke((Action)(() => Redraw()));
            }
        }

        private void RecalcTransform()
        {
            var start = DateTime.UtcNow;
            if (Workspace == null || Workspace.ViewControl == null)
                return;

            DisplayPlane = new Plane(Workspace.ActiveViewPort.BottomLeft, Workspace.ActiveViewPort.Sight);

            var scale = Workspace.ViewControl.DisplayHeight / Workspace.ActiveViewPort.ViewHeight;
            var t = new TransformGroup();
            t.Children.Add(new TranslateTransform(-Workspace.ActiveViewPort.BottomLeft.X, -Workspace.ActiveViewPort.BottomLeft.Y));
            t.Children.Add(new ScaleTransform(scale, -scale));
            t.Children.Add(new TranslateTransform(0, Workspace.ViewControl.DisplayHeight));
            this.PrimitiveCanvas.RenderTransform = t;
            this.RubberBandCanvas.RenderTransform = t;
            BindObject.Thickness = 1.0 / scale;

            var end = DateTime.UtcNow;
            var elapsed = (end - start).TotalMilliseconds;
            Fps.Text = string.Format("Transform update done in {0} ms", elapsed);

            this.Dispatcher.BeginInvoke((Action)(() => RenderRubberBandLines()));
        }

        private void Redraw()
        {
            var start = DateTime.UtcNow;
            var drawing = Workspace.Drawing;
            this.PrimitiveCanvas.Children.Clear();
            foreach (var layer in drawing.GetLayers())
            {
                foreach (var entity in layer.GetEntities())
                {
                    foreach (var prim in entity.GetPrimitives())
                    {
{'fraction_non_alphanumeric': 0.08204228783006919, 'fraction_numerical': 0.001169248757673195, 'mean_word_length': 1.7613666935700834, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '5504221', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2356, 'n_words': 506}