Inheritance: Disposable
        public void SubscribeForLifetimeToEvent()
        {
            var observable = new SomeObservable();

            var triggerCount = 0;

            using (var lifetime = new Lifetime())
            {
                observable.SomeEvent.SubscribeForLifetime(() => { triggerCount++; }, lifetime.LifetimeManager);

                Assert.AreEqual(0, triggerCount);
                observable.SomeEvent.Fire();
                Assert.AreEqual(1, triggerCount);
            }

            observable.SomeEvent.Fire();
            Assert.AreEqual(1, triggerCount);
        }
        public void SubscribeForLifetimeToProperty()
        {
            var observable = new SomeObservable();

            var triggerCount = 0;

            using (Lifetime lifetime = new Lifetime())
            {
                observable.SubscribeForLifetime(nameof(SomeObservable.Name), () =>
                {
                    triggerCount++;
                }, lifetime.LifetimeManager);

                Assert.AreEqual(0, triggerCount);
                observable.Name = "Some value";
                Assert.AreEqual(1, triggerCount);
            }

            observable.Name = "Some new value";
            Assert.AreEqual(1, triggerCount);
        }
        public void SubscribeToAllProperties()
        {
            var observable = new SomeObservable();
            int numChanged = 0;

            using (var lifetime = new Lifetime())
            {
                observable.SubscribeForLifetime(ObservableObject.AnyProperty, () => { numChanged++; }, lifetime.LifetimeManager);

                Assert.AreEqual(0, numChanged);
                observable.Name = "Foo";
                Assert.AreEqual(1, numChanged);
                observable.Number = 1;
                Assert.AreEqual(2, numChanged);
            }

            Assert.AreEqual(2, numChanged);
            observable.Name = "Foo2";
            Assert.AreEqual(2, numChanged);
            observable.Number = 2;
            Assert.AreEqual(2, numChanged);
        }
        public void SynchronizeCollection()
        {
            var observable = new SomeObservable();
            int addCalls = 0, removeCalls = 0, changedCalls = 0;

            observable.Strings.Add("a");
            observable.Strings.Add("b");

            using (var lifetime = new Lifetime())
            {
                observable.Strings.SynchronizeForLifetime((s) => { addCalls++; }, (s) => { removeCalls++; }, () => { changedCalls++; }, lifetime.LifetimeManager);

                Assert.AreEqual(2, addCalls);
                Assert.AreEqual(0, removeCalls);
                Assert.AreEqual(1, changedCalls);

                observable.Strings.Add("c");
                Assert.AreEqual(3, addCalls);
                Assert.AreEqual(0, removeCalls);
                Assert.AreEqual(2, changedCalls);

                observable.Strings.Remove("a");
                Assert.AreEqual(3, addCalls);
                Assert.AreEqual(1, removeCalls);
                Assert.AreEqual(3, changedCalls);
            }

            observable.Strings.Add("d");
            observable.Strings.Remove("d");
            Assert.AreEqual(3, addCalls);
            Assert.AreEqual(1, removeCalls);
            Assert.AreEqual(3, changedCalls);
        }
Example #5
0
        public void HardRefresh(ConsoleString outputValue = null)
        {
            refreshLt?.Dispose();
            refreshLt = new Lifetime();
            var myLt = refreshLt;

            Controls.Clear();

            var minHeight = SuperCompact ? 1 : 5;

            if (Width < 10 || Height < minHeight)
            {
                return;
            }

            def = CreateDefinition();

            var options = new GridLayoutOptions()
            {
                Columns = new System.Collections.Generic.List <GridColumnDefinition>()
                {
                    new GridColumnDefinition()
                    {
                        Type = GridValueType.Pixels, Width = 2
                    },                                                                              // 0 empty
                    new GridColumnDefinition()
                    {
                        Type = GridValueType.RemainderValue, Width = 1
                    },                                                                              // 1 content
                    new GridColumnDefinition()
                    {
                        Type = GridValueType.Pixels, Width = 2
                    },                                                                              // 2 empty
                },
                Rows = new System.Collections.Generic.List <GridRowDefinition>()
                {
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 0 empty
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 1 welcome message
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 2 press escape message
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 3 empty
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 4 input
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 5 empty
                    new GridRowDefinition()
                    {
                        Type = GridValueType.RemainderValue, Height = 1,
                    },                                                                          // 6 output
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 7 empty
                }
            };

            if (SuperCompact)
            {
                options.Rows.RemoveAt(0);                      // empty
                options.Rows.RemoveAt(0);                      // welcome
                options.Rows.RemoveAt(0);                      // press escape
                options.Rows.RemoveAt(0);                      // empty
                options.Rows.RemoveAt(options.Rows.Count - 1); // empty
                options.Rows.RemoveAt(options.Rows.Count - 1); // output
                options.Rows.RemoveAt(options.Rows.Count - 1); // empty
            }

            var gridLayout = Add(new GridLayout(options));



            gridLayout.Fill();
            gridLayout.RefreshLayout();

            var top = SuperCompact ? 0 : 1;

            if (SuperCompact == false)
            {
                var welcomePanel = gridLayout.Add(new ConsolePanel(), 1, top++);
                welcomePanel.Add(new Label()
                {
                    Text = WelcomeMessage
                }).CenterHorizontally();

                var escapePanel = gridLayout.Add(new ConsolePanel(), 1, top++);
                escapePanel.Add(new Label()
                {
                    Text = EscapeMessage
                }).CenterHorizontally();

                top++;
            }

            var inputPanel = gridLayout.Add(new ConsolePanel()
            {
            }, 1, top++);

            inputPanel.Add(new Label()
            {
                Text = "CMD> ".ToConsoleString()
            });
            InputBox = inputPanel.Add(new TextBox()
            {
                X = "CMD> ".Length, Width = inputPanel.Width - "CMD> ".Length, Foreground = ConsoleColor.Gray, Background = ConsoleColor.Black
            });
            InputBox.RichTextEditor.TabHandler.TabCompletionHandlers.Add(new PowerArgsRichCommandLineReader(def, new List <ConsoleString>(), false));
            OnInputBoxReady();
            top++;
            ConsoleApp.Current.InvokeNextCycle(() =>
            {
                if (myLt == refreshLt)
                {
                    InputBox.Focused.SubscribeForLifetime(() =>
                    {
                        if (focusLt != null && focusLt.IsExpired == false && focusLt.IsExpiring == false)
                        {
                            focusLt.Dispose();
                        }

                        focusLt = new Lifetime();


                        Application.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Tab, null, () =>
                        {
                            var forgotten = OnHandleHey(new ConsoleKeyInfo('\t', ConsoleKey.Tab, false, false, false));
                        }, focusLt);
                        Application.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Tab, ConsoleModifiers.Shift, () =>
                        {
                            var forgotten = OnHandleHey(new ConsoleKeyInfo('\t', ConsoleKey.Tab, true, false, false));
                        }, focusLt);
                    }, refreshLt);

                    InputBox.Unfocused.SubscribeForLifetime(() =>
                    {
                        if (focusLt != null && focusLt.IsExpired == false && focusLt.IsExpiring == false)
                        {
                            focusLt.Dispose();
                        }
                    }, refreshLt);

                    InputBox.TryFocus();
                }
            });

            if (SuperCompact == false)
            {
                var outputPanel = gridLayout.Add(new ConsolePanel()
                {
                    Background = ConsoleColor.Black
                }, 1, top);
                outputLabel = outputPanel.Add(new Label()
                {
                    Text =
                        string.IsNullOrWhiteSpace(outputValue?.StringValue) == false ? outputValue :
                        string.IsNullOrWhiteSpace(outputLabel?.Text?.StringValue) == false ? outputLabel?.Text :
                        CreateAssistiveText(),
                    Mode = LabelRenderMode.MultiLineSmartWrap
                }).Fill();
            }
            InputBox.KeyInputReceived.SubscribeForLifetime(async(keyInfo) => await OnHandleHey(keyInfo), InputBox);
        }