예제 #1
0
        public void Run()
        {
            Gtk.Application.Init();
            SetupTheme();

            ApiExtensibility.Register(typeof(Windows.UI.Core.ICoreWindowExtension), o => new GtkCoreWindowExtension(o));
            ApiExtensibility.Register <Windows.UI.Xaml.Application>(typeof(Uno.UI.Xaml.IApplicationExtension), o => new GtkApplicationExtension(o));
            ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new GtkApplicationViewExtension(o));
            ApiExtensibility.Register(typeof(ISystemThemeHelperExtension), o => new GtkSystemThemeHelperExtension(o));
            ApiExtensibility.Register(typeof(Windows.Graphics.Display.IDisplayInformationExtension), o => _displayInformationExtension ??= new GtkDisplayInformationExtension(o, _window));
            ApiExtensibility.Register <TextBoxView>(typeof(ITextBoxViewExtension), o => new TextBoxViewExtension(o, _window));
            ApiExtensibility.Register(typeof(ILauncherExtension), o => new LauncherExtension(o));
            ApiExtensibility.Register <FileOpenPicker>(typeof(IFileOpenPickerExtension), o => new FileOpenPickerExtension(o));
            ApiExtensibility.Register <FolderPicker>(typeof(IFolderPickerExtension), o => new FolderPickerExtension(o));
            ApiExtensibility.Register(typeof(IClipboardExtension), o => new ClipboardExtensions(o));
            ApiExtensibility.Register <FileSavePicker>(typeof(IFileSavePickerExtension), o => new FileSavePickerExtension(o));

            _isDispatcherThread = true;
            _window             = new Gtk.Window("Uno Host");
            Size preferredWindowSize = ApplicationView.PreferredLaunchViewSize;

            if (preferredWindowSize != Size.Empty)
            {
                _window.SetDefaultSize((int)preferredWindowSize.Width, (int)preferredWindowSize.Height);
            }
            else
            {
                _window.SetDefaultSize(1024, 800);
            }
            _window.SetPosition(Gtk.WindowPosition.Center);

            _window.Realized += (s, e) =>
            {
                // Load the correct cursors before the window is shown
                // but after the window has been initialized.
                Cursors.Reload();
            };

            _window.DeleteEvent += delegate
            {
                Gtk.Application.Quit();
            };

            bool EnqueueNative(DispatcherQueuePriority priority, DispatcherQueueHandler callback)
            {
                Dispatch(() => callback());

                return(true);
            }

            Windows.System.DispatcherQueue.EnqueueNativeOverride = EnqueueNative;

            void Dispatch(System.Action d)
            {
                if (Gtk.Application.EventsPending())
                {
                    Gtk.Application.RunIteration(false);
                }

                GLib.Idle.Add(delegate
                {
                    if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace))
                    {
                        this.Log().Trace($"Iteration");
                    }

                    try
                    {
                        d();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    return(false);
                });
            }

            Windows.UI.Core.CoreDispatcher.DispatchOverride        = Dispatch;
            Windows.UI.Core.CoreDispatcher.HasThreadAccessOverride = () => _isDispatcherThread;

            _window.Realized += (s, e) =>
            {
                WUX.Window.Current.OnNativeSizeChanged(new Windows.Foundation.Size(_window.AllocatedWidth, _window.AllocatedHeight));
            };

            _window.SizeAllocated += (s, e) =>
            {
                WUX.Window.Current.OnNativeSizeChanged(new Windows.Foundation.Size(e.Allocation.Width, e.Allocation.Height));
            };

            _window.WindowStateEvent += OnWindowStateChanged;

            var overlay = new Overlay();

            _eventBox = new EventBox();
            _area     = new UnoDrawingArea();
            _fix      = new Fixed();
            overlay.Add(_area);
            overlay.AddOverlay(_fix);
            _eventBox.Add(overlay);
            _window.Add(_eventBox);

            /* avoids double invokes at window level */
            _area.AddEvents((int)GtkCoreWindowExtension.RequestedEvents);

            _window.ShowAll();

            void CreateApp(ApplicationInitializationCallbackParams _)
            {
                var app = _appBuilder();

                app.Host = this;
            }

            WUX.Application.Start(CreateApp, _args);

            UpdateWindowPropertiesFromPackage();

            Gtk.Application.Run();
        }