<commit_before>﻿// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;

using Avalonia.Diagnostics.Models;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace Avalonia.Diagnostics.ViewModels
{
    internal class EventTreeNode : EventTreeNodeBase
    {
        private RoutedEvent _event;
        private EventsViewModel _parentViewModel;
        private bool _isRegistered;
        private FiredEvent _currentEvent;

        public EventTreeNode(EventOwnerTreeNode parent, RoutedEvent @event, EventsViewModel vm)
            : base(parent, @event.Name)
        {
            Contract.Requires<ArgumentNullException>(@event != null);
            Contract.Requires<ArgumentNullException>(vm != null);

            this._event = @event;
            this._parentViewModel = vm;
        }

        public override bool? IsEnabled
        {
            get => base.IsEnabled;
            set
            {
                if (base.IsEnabled != value)
                {
                    base.IsEnabled = value;
                    UpdateTracker();
                    if (Parent != null && _updateParent)
                    {
                        try
                        {
                            Parent._updateChildren = false;
                            Parent.UpdateChecked();
                        }
                        finally
                        {
                            Parent._updateChildren = true;
                        }
                    }
                }
            }
        }

        private void UpdateTracker()
        {
            if (IsEnabled.GetValueOrDefault() && !_isRegistered)
            {
                _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true);
                _isRegistered = true;
            }
        }

        private void HandleEvent(object sender, RoutedEventArgs e)
        {
            if (!_isRegistered || IsEnabled == false)
                return;
            if (sender is IVisual v && DevTools.BelongsToDevTool(v))
                return;

            var s = sender;
            var handled = e.Handled;
            var route = e.Route;

            Action handler = delegate<commit_msg>Add a note to fix usage of AddClassHandler.<commit_after>﻿// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;

using Avalonia.Diagnostics.Models;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace Avalonia.Diagnostics.ViewModels
{
    internal class EventTreeNode : EventTreeNodeBase
    {
        private RoutedEvent _event;
        private EventsViewModel _parentViewModel;
        private bool _isRegistered;
        private FiredEvent _currentEvent;

        public EventTreeNode(EventOwnerTreeNode parent, RoutedEvent @event, EventsViewModel vm)
            : base(parent, @event.Name)
        {
            Contract.Requires<ArgumentNullException>(@event != null);
            Contract.Requires<ArgumentNullException>(vm != null);

            this._event = @event;
            this._parentViewModel = vm;
        }

        public override bool? IsEnabled
        {
            get => base.IsEnabled;
            set
            {
                if (base.IsEnabled != value)
                {
                    base.IsEnabled = value;
                    UpdateTracker();
                    if (Parent != null && _updateParent)
                    {
                        try
                        {
                            Parent._updateChildren = false;
                            Parent.UpdateChecked();
                        }
                        finally
                        {
                            Parent._updateChildren = true;
                        }
                    }
                }
            }
        }

        private void UpdateTracker()
        {
            if (IsEnabled.GetValueOrDefault() && !_isRegistered)
            {
                // FIXME: This leaks event handlers.
                _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true);
                _isRegistered = true;
            }
        }

        private void HandleEvent(object sender, RoutedEventArgs e)
        {
            if (!_isRegistered || IsEnabled == false)
                return;
            if (sender is IVisual v && DevTools.BelongsToDevTool(v))
                return;

            var s = sender;
            var handled = e.Handled;
            var route = e.Route;

            Action handler = delegate
{'fraction_non_alphanumeric': 0.07173689619732786, 'fraction_numerical': 0.00041109969167523125, 'mean_word_length': 1.3782991202346042, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4224874', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1112, 'n_words': 285}