Ejemplo n.º 1
0
        private void RegisterHandlers()
        {
            var broker = Broker.GetBroker();

            broker.RegisterHandler <GetScreenSizeRequest>(request =>
            {
                var screens  = System.Windows.Forms.Screen.AllScreens;
                var responce = new GetScreenSizeResponce
                {
                    Height  = (int)SystemParameters.VirtualScreenHeight,
                    Width   = (int)SystemParameters.VirtualScreenWidth,
                    Screens = screens.Select(s => new ScreenSize
                    {
                        Left   = s.Bounds.X,
                        Top    = s.Bounds.Y,
                        Width  = s.Bounds.Width,
                        Height = s.Bounds.Height
                    })
                };
                return(responce);
            });
            broker.RegisterHandler <GetFontsRequest>(request =>
            {
                return(new GetFontsResponce
                {
                    Fonts = System.Drawing.FontFamily.Families.Select(f => f.Name)
                });
            });
            broker.RegisterHandler <StartShowRequest>(request =>
            {
                var requestData = request as StartShowRequest;
                Dispatcher.Invoke(() =>
                {
                    _window.Height = requestData.Screens.Displays.Max(d => d.Top + d.Height) - requestData.Screens.Displays.Min(d => d.Top);
                    _window.Width  = requestData.Screens.Displays.Max(d => d.Left + d.Width) - requestData.Screens.Displays.Min(d => d.Left);
                    _window.Left   = requestData.Screens.Displays.Min(d => d.Left);
                    _window.Top    = requestData.Screens.Displays.Min(d => d.Top);
                    var bc         = new Media.BrushConverter();
                    var border     = new Border();
                    if (ColorConverter.TryToParseRGB(requestData.Background, out string colorHex))
                    {
                        border.Background = (Media.Brush)bc.ConvertFrom(colorHex);
                    }
                    var canvas = new Canvas();


                    var blockBuilder = new BlockBuilder();
                    foreach (var block in requestData.Blocks)
                    {
                        var element = blockBuilder.BuildElement(block);
                        if (element != null)
                        {
                            canvas.Children.Add(element);
                        }
                    }

                    border.Child    = canvas;
                    _window.Content = border;
                    _window.Show();
                });
                return(null);
            });
            broker.RegisterHandler <StopShowRequest>(request =>
            {
                Dispatcher.Invoke(() =>
                {
                    _window.Visibility = Visibility.Hidden;
                });
                return(null);
            });
            broker.RegisterHandler <GetVersionRequest>(request =>
            {
                var version = Assembly.GetExecutingAssembly().GetName().Version;
                return(new GetVersionResponce
                {
                    Major = version.Major,
                    Minor = version.Minor,
                    Build = version.Build
                });
            });
        }