<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Native.Interop;
using Avalonia.OpenGL;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Threading;

namespace Avalonia.Native
{
    public class MacOSTopLevelWindowHandle : IPlatformHandle, IMacOSTopLevelPlatformHandle
    {
        IAvnWindowBase _native;

        public MacOSTopLevelWindowHandle(IAvnWindowBase native)
        {
            _native = native;
        }

        public IntPtr Handle => NSWindow;

        public string HandleDescriptor => "NSWindow";

        public IntPtr NSView => _native.ObtainNSViewHandle();

        public IntPtr NSWindow => _native.ObtainNSWindowHandle();

        public IntPtr GetNSViewRetained()
        {
            return _native.ObtainNSViewHandleRetained();
        }

        public IntPtr GetNSWindowRetained()
        {
            return _native.ObtainNSWindowHandleRetained();
        }
    }

    public abstract class WindowBaseImpl : IWindowBaseImpl,
        IFramebufferPlatformSurface, ITopLevelImplWithNativeControlHost
    {
        protected IInputRoot _inputRoot;
        IAvnWindowBase _native;
        private object _syncRoot = new object();
        private bool _deferredRendering = false;
        private bool _gpu = false;
        private readonly MouseDevice _mouse;
        private readonly IKeyboardDevice _keyboard;
        private readonly IStandardCursorFactory _cursorFactory;
        private Size _savedLogicalSize;
        private Size _lastRenderedLogicalSize;
        private double _savedScaling;
        private GlPlatformSurface _glSurface;
        private NativeControlHostImpl _nativeControlHost;
        private IGlContext _glContext;

        internal WindowBaseImpl(AvaloniaNativePlatformOptions opts, GlPlatformFeature glFeature)
        {
            _gpu = opts.UseGpu && glFeature != null;
            _deferredRendering = opts.UseDeferredRendering;

            _keyboard = AvaloniaLocator.Current.GetService<IKeyboardDevice>();
            _mouse = new MouseDevice();
            _cursorFactory = AvaloniaLocator.Current.GetService<IStandardCursorFactory>();
        }

        protected void Init(IAvnWindowBase window, IAvnScreens screens, IGlContext glContext)
        {
            _native = window;
            _glContext = glContext;

            Handle = new MacOSTopLevelWindowHandle(window);
            if (_gpu)
                _glSurface = new GlPlatformSurface(window, _glContext);
            Screen = new ScreenImpl(screens);
            _savedLogicalSize = ClientSize;
            _savedScaling = RenderScaling;
            _nativeControlHost = new NativeControlHostImpl(_native.CreateNativeControlHost());

            var monitor = Screen.AllScreens.OrderBy(x => x.PixelDensity)
                    .FirstOrDefault(m => m.Bounds.Contains(Position));

            Resize(new Size(monitor.WorkingArea.Width * 0.75d, monitor.WorkingArea.Height * 0.7d));
        }

        public Size ClientSize 
        {
            get
            {
                var s = _native.GetClientSize();
                return new Size(s.Width, s.Height);
            }
        }

        public IEnumerable<object> Surfaces => new[] {
            (_gpu ? _glSurface : (object)null),
            this 
        };

        public INativeControlHostImpl NativeControlHost => _nativeControlHost;

        public ILockedFramebuffer Lock()
        {
            var w = _savedLogicalSize.Width * _savedScaling;
            var h = _savedLogicalSize.Height * _savedScaling;
            var dpi = _savedScaling * 96;
            return new DeferredFramebuffer(cb =>
            {
                lock (_syncRoot)
                {
                    if (_native == null)
                        return false;
                    cb(_native);
                    _lastRenderedLogicalSize = _savedLogicalSize;
                    return true;<commit_msg>prevent popup crashing when mainwindow has closed and popup still open.<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Native.Interop;
using Avalonia.OpenGL;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Threading;

namespace Avalonia.Native
{
    public class MacOSTopLevelWindowHandle : IPlatformHandle, IMacOSTopLevelPlatformHandle
    {
        IAvnWindowBase _native;

        public MacOSTopLevelWindowHandle(IAvnWindowBase native)
        {
            _native = native;
        }

        public IntPtr Handle => NSWindow;

        public string HandleDescriptor => "NSWindow";

        public IntPtr NSView => _native.ObtainNSViewHandle();

        public IntPtr NSWindow => _native.ObtainNSWindowHandle();

        public IntPtr GetNSViewRetained()
        {
            return _native.ObtainNSViewHandleRetained();
        }

        public IntPtr GetNSWindowRetained()
        {
            return _native.ObtainNSWindowHandleRetained();
        }
    }

    public abstract class WindowBaseImpl : IWindowBaseImpl,
        IFramebufferPlatformSurface, ITopLevelImplWithNativeControlHost
    {
        protected IInputRoot _inputRoot;
        IAvnWindowBase _native;
        private object _syncRoot = new object();
        private bool _deferredRendering = false;
        private bool _gpu = false;
        private readonly MouseDevice _mouse;
        private readonly IKeyboardDevice _keyboard;
        private readonly IStandardCursorFactory _cursorFactory;
        private Size _savedLogicalSize;
        private Size _lastRenderedLogicalSize;
        private double _savedScaling;
        private GlPlatformSurface _glSurface;
        private NativeControlHostImpl _nativeControlHost;
        private IGlContext _glContext;

        internal WindowBaseImpl(AvaloniaNativePlatformOptions opts, GlPlatformFeature glFeature)
        {
            _gpu = opts.UseGpu && glFeature != null;
            _deferredRendering = opts.UseDeferredRendering;

            _keyboard = AvaloniaLocator.Current.GetService<IKeyboardDevice>();
            _mouse = new MouseDevice();
            _cursorFactory = AvaloniaLocator.Current.GetService<IStandardCursorFactory>();
        }

        protected void Init(IAvnWindowBase window, IAvnScreens screens, IGlContext glContext)
        {
            _native = window;
            _glContext = glContext;

            Handle = new MacOSTopLevelWindowHandle(window);
            if (_gpu)
                _glSurface = new GlPlatformSurface(window, _glContext);
            Screen = new ScreenImpl(screens);
            _savedLogicalSize = ClientSize;
            _savedScaling = RenderScaling;
            _nativeControlHost = new NativeControlHostImpl(_native.CreateNativeControlHost());

            var monitor = Screen.AllScreens.OrderBy(x => x.PixelDensity)
                    .FirstOrDefault(m => m.Bounds.Contains(Position));

            Resize(new Size(monitor.WorkingArea.Width * 0.75d, monitor.WorkingArea.Height * 0.7d));
        }

        public Size ClientSize 
        {
            get
            {
                if (_native != null)
                {
                    var s = _native.GetClientSize();
                    return new Size(s.Width, s.Height);
                }

                return default;
            }
        }

        public IEnumerable<object> Surfaces => new[] {
            (_gpu ? _glSurface : (object)null),
            this 
        };

        public INativeControlHostImpl NativeControlHost => _nativeControlHost;

        public ILockedFramebuffer Lock()
        {
            var w = _savedLogicalSize.Width * _savedScaling;
            var h = _savedLogicalSize.Height * _savedScaling;
            var dpi = _savedScaling * 96;
            return new DeferredFramebuffer(cb =>
            {
                lock (_syncRoot)
                {
                    if (_native == null)
                        return false;
                    cb(_native);
                    _lastRenderedLogicalSize = _savedLogicalSize;
                    return true;
{'fraction_non_alphanumeric': 0.06669820245979187, 'fraction_numerical': 0.0016556291390728477, 'mean_word_length': 2.1973534971644613, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, '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': '7573328', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2135, 'n_words': 486}