Ejemplo n.º 1
0
        private FREObject SetViewPort(FREContext ctx, uint argc, FREObject[] argv)
        {
            System.Windows.Rect viewPort;
            try {
                viewPort = argv[0].AsRect();
            }
            catch (Exception e) {
                return(new FreException(e).RawValue);
            }

            if (Environment.OSVersion.Version.Major > 7)
            {
                _scaleFactor = _useHiDpi ? WinApi.GetScaleFactor(_airWindow) : 1.0;
            }
            else
            {
                _scaleFactor = _useHiDpi ? WinApi.GetScaleFactor() : 1.0;
            }


            _view.ScaleFactor = _scaleFactor;

            var tmpX      = Convert.ToInt32(viewPort.X * _scaleFactor);
            var tmpY      = Convert.ToInt32(viewPort.Y * _scaleFactor);
            var tmpWidth  = Convert.ToInt32(viewPort.Width * _scaleFactor);
            var tmpHeight = Convert.ToInt32(viewPort.Height * _scaleFactor);

            var updateWidth  = false;
            var updateHeight = false;
            var updateX      = false;
            var updateY      = false;

            if (tmpWidth != _view.ViewWidth)
            {
                _view.ViewWidth = tmpWidth;
                updateWidth     = true;
            }

            if (tmpHeight != _view.ViewHeight)
            {
                _view.ViewHeight = tmpHeight;
                updateHeight     = true;
            }

            if (tmpX != _view.X)
            {
                _view.X = tmpX;
                updateX = true;
            }

            if (tmpY != _view.Y)
            {
                _view.Y = tmpY;
                updateY = true;
            }

            if (!updateX && !updateY && !updateWidth && !updateHeight)
            {
                return(FREObject.Zero);
            }
            var flags = (WindowPositionFlags)0;

            if (!updateWidth && !updateHeight)
            {
                flags |= WindowPositionFlags.SWP_NOSIZE;
            }

            if (!updateX && !updateY)
            {
                flags |= WindowPositionFlags.SWP_NOMOVE;
            }

            WinApi.SetWindowPos(_webViewWindow, new Hwnd(0), _view.X, _view.Y, _view.ViewWidth, _view.ViewHeight,
                                flags);
            WinApi.UpdateWindow(_webViewWindow);
            return(FREObject.Zero);
        }
Ejemplo n.º 2
0
        private FREObject InitView(FREContext ctx, uint argc, FREObject[] argv)
        {
            FreSharpLogger.GetInstance().Context = Context;
            _airWindow = Process.GetCurrentProcess().MainWindowHandle;
            if (_airWindow == Hwnd.Zero)
            {
                return(new FreException(
                           "Cannot find AIR window to attach webView to. Ensure you init the ANE AFTER your main Sprite is initialized. " +
                           "Please see https://forum.starling-framework.org/topic/webviewane-for-osx/page/7?replies=201#post-105524 for more details")
                       .RawValue);
            }

            bool useTransparentBackground;

            try {
                dynamic settings = new FreObjectSharp(argv[2]);
                _useEdge = settings.engine == 1;

                UrlRequestHeaderManager.GetInstance().PersistRequestHeaders = settings.persistRequestHeaders;
                dynamic    cefSettings = new FreObjectSharp(settings.cef);
                var        freUrl      = argv[0];
                UrlRequest initialUrl  = null;
                if (FreObjectTypeSharp.Null != freUrl.Type())
                {
                    initialUrl = new UrlRequest(freUrl, _useEdge);
                    UrlRequestHeaderManager.GetInstance().Add(initialUrl);
                }

                var viewPort = argv[1].AsRect();

                useTransparentBackground = settings.useTransparentBackground;
                FREArray clArr = cefSettings.commandLineArgs;

                var argsDict = new Dictionary <string, string>();
                foreach (var argFre in clArr)
                {
                    var key = argFre.GetProp("key").AsString();
                    var val = argFre.GetProp("value").AsString();
                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(val))
                    {
                        continue;
                    }
                    argsDict.Add(key, val);
                }

                ArrayList whiteList = settings.urlWhiteList.AsArrayList();
                ArrayList blackList = settings.urlBlackList.AsArrayList();

                _backgroundColor = argv[4].AsColor();
                var backgroundMediaColor = new System.Windows.Media.Color {
                    A = _backgroundColor.A,
                    R = _backgroundColor.R,
                    G = _backgroundColor.G,
                    B = _backgroundColor.B
                };

                _useHiDpi = settings.useHiDPI;

                if (Environment.OSVersion.Version.Major > 7)
                {
                    _scaleFactor = _useHiDpi ? WinApi.GetScaleFactor(_airWindow) : 1.0;
                }
                else
                {
                    _scaleFactor = _useHiDpi ? WinApi.GetScaleFactor() : 1.0;
                }

                if (_useEdge)
                {
                    EdgeView.Context = Context;
                    _view            = new EdgeView {
                        Background = new SolidColorBrush(backgroundMediaColor)
                    };
                }
                else
                {
                    CefView.Context = Context;
                    dynamic contextMenu = new FreObjectSharp(settings.contextMenu);
                    dynamic popup       = new FreObjectSharp(settings.popup);
                    dynamic dimensions  = new FreObjectSharp(popup.dimensions);

                    _view = new CefView {
                        Background          = new SolidColorBrush(backgroundMediaColor),
                        RemoteDebuggingPort = cefSettings.remoteDebuggingPort,
                        EnablePrintPreview  = cefSettings.enablePrintPreview,
                        CachePath           = cefSettings.cachePath,
                        DownloadPath        = settings.downloadPath,
                        EnableDownloads     = settings.enableDownloads,
                        CacheEnabled        = settings.cacheEnabled,
                        LogLevel            = cefSettings.logSeverity,
                        AcceptLanguageList  = cefSettings.acceptLanguageList,
                        Locale             = cefSettings.locale,
                        ContextMenuEnabled = contextMenu.enabled,
                        UserAgent          = settings.userAgent,
                        UserDataPath       = cefSettings.userDataPath,
                        CommandLineArgs    = argsDict,
                        PopupBehaviour     = (PopupBehaviour)popup.behaviour,
                        PopupDimensions    = new Tuple <int, int>(dimensions.width, dimensions.height),
                        ScaleFactor        = _scaleFactor
                    };
                }

                _view.InitialUrl = initialUrl;
                _view.WhiteList  = whiteList;
                _view.BlackList  = blackList;
                _view.X          = Convert.ToInt32(viewPort.X * _scaleFactor);
                _view.Y          = Convert.ToInt32(viewPort.Y * _scaleFactor);
                _view.ViewWidth  = Convert.ToInt32(viewPort.Width * _scaleFactor);
                _view.ViewHeight = Convert.ToInt32(viewPort.Height * _scaleFactor);
                _view.Init();
            }
            catch (Exception e) {
                return(new FreException(e).RawValue);
            }

            var parameters = new HwndSourceParameters();

            parameters.SetPosition(_view.X, _view.Y);
            parameters.SetSize(_view.ViewWidth, _view.ViewHeight);
            parameters.ParentWindow = _airWindow;
            parameters.WindowName   = "Cef Window";
            parameters.WindowStyle  = (int)WindowStyles.WS_CHILD;
            parameters.AcquireHwndFocusInMenuMode = true;

            if (useTransparentBackground && Environment.OSVersion.Version.Major > 7)
            {
                parameters.ExtendedWindowStyle      = (int)WindowExStyles.WS_EX_LAYERED;
                parameters.UsesPerPixelTransparency = true;
            }

            var source = _useEdge
                ? new HwndSource(parameters)
            {
                RootVisual = (EdgeView)_view
            }
                : new HwndSource(parameters)
            {
                RootVisual = (CefView)_view
            };

            if (useTransparentBackground && source.CompositionTarget != null)
            {
                source.CompositionTarget.BackgroundColor = Colors.Transparent;
            }

            _webViewWindow = source.Handle;

            MessageTouchDevice.RegisterTouchWindow(_webViewWindow);

            return(FREObject.Zero);
        }