Пример #1
0
        public static void Initialize(Dispatcher dispatcher)
        {
            Button.Implementation.Factory = (command, contentFactory, text, isDefault) =>
            {
                if (contentFactory != null)
                {
                    var states = new BehaviorSubject <ButtonStates>(ButtonStates.Unrooted);

                    var content = contentFactory(states.Switch());

                    return(Control.Create(self =>
                    {
                        Action action = () => { };
                        var button = new CustomButton();
                        button.Click += (s, a) => action();

                        states.OnNext(new ButtonStates(
                                          isPressed: button.ObserveDependencyProperty(instance => instance.IsPressed, ButtonBase.IsPressedProperty),
                                          isHovered: button.ObserveDependencyProperty(instance => instance.IsMouseOver, ButtonBase.IsMouseOverProperty),
                                          isEnabled: button.ObserveDependencyProperty(instance => instance.IsEnabled, ButtonBase.IsEnabledProperty)));

                        content.Mount(
                            new MountLocation.Mutable
                        {
                            NativeFrame = ObservableMath.RectangleWithSize(self.NativeFrame.Size),
                            AvailableSize = self.AvailableSize,
                            IsRooted = self.IsRooted,
                        });

                        var child = content.NativeHandle as UIElement;
                        if (child != null)
                        {
                            var grid = new Grid();
                            grid.Children.Add(
                                new System.Windows.Shapes.Rectangle()
                            {
                                Fill = new SolidColorBrush(Colors.Transparent)
                            });
                            grid.Children.Add(child);
                            button.Content = grid;
                        }
                        ;


                        self.BindNativeProperty(dispatcher, "command", command.Action, cmd =>
                        {
                            button.IsEnabled = cmd.HasValue;
                            action = () => cmd.Do(x => x());
                        });

                        self.BindNativeDefaults(button, dispatcher);

                        return button;
                    })
                           .WithSize(content.DesiredSize));
                }
                else
                {
                    var width = new ReplaySubject <Points>(1);

                    return(Control.Create(self =>
                    {
                        Action action = () => { };
                        var button = new System.Windows.Controls.Button();
                        button.Click += (s, a) => action();


                        command
                        .Action
                        .CombineLatest(text)
                        .Take(1)
                        .ObserveOn(Fusion.Application.MainThread)
                        .Subscribe(
                            cmdText =>
                        {
                            UpdateButtonFields(button, cmdText.Item1.HasValue, cmdText.Item2, width);
                        });

                        self.BindNativeProperty(dispatcher, "command", command.Action.CombineLatest(text), cmdText =>
                        {
                            UpdateButtonFields(button, cmdText.Item1.HasValue, cmdText.Item2, width);
                            action = () => cmdText.Item1.Do(x => x());
                        });

                        self.BindNativeDefaults(button, dispatcher);

                        return button;
                    })
                           .WithHeight(20)
                           .WithWidth(width)
                           .WithPadding(new Thickness <Points>(6)));
                }
            };
        }