Пример #1
0
 private void CheckSize()
 {
     if (Width < options.MinWidth || Height < options.MinHeight)
     {
         if (tooSmallLifetime == null)
         {
             tooSmallLifetime = new Lifetime();
             IsVisible        = true;
             CanFocus         = true;
             Application.QueueAction(() => this.TryFocus());
             Application.FocusManager.Push();
             Application.FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Escape, null, () => { }, this);
             options.OnMinimumSizeNotMet();
             OnTooSmall();
         }
     }
     else
     {
         IsVisible = false;
         CanFocus  = false;
         if (tooSmallLifetime != null)
         {
             tooSmallLifetime.Dispose();
             tooSmallLifetime = null;
             Application.FocusManager.Pop();
             options.OnMinimumSizeMet();
         }
         else
         {
             options.OnMinimumSizeMet();
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Subscribes to the given event for at most one notification
        /// </summary>
        /// <param name="route">the event to subscribe to</param>
        /// <param name="handler">the event handler</param>
        public void RegisterOnce(string route, Action <RoutedEvent <T> > handler)
        {
            var routeLifetime = new Lifetime();

            GetOrAddRoutedEvent(route).SubscribeForLifetime((t) =>
            {
                handler(t);
                routes.Remove(route);
                routeLifetime.Dispose();
            }, routeLifetime);
        }
Пример #3
0
        public Lifetime GetPropertyValueLifetime(string propertyName)
        {
            Lifetime    ret = new Lifetime();
            IDisposable sub = null;

            sub = SubscribeUnmanaged(propertyName, () =>
            {
                sub.Dispose();
                ret.Dispose();
            });

            return(ret);
        }
Пример #4
0
        public Lifetime CreateChildLifetime()
        {
            var ret = new Lifetime();

            LifetimeManager.Manage(new Subscription(() =>
            {
                if (ret.IsExpired == false)
                {
                    ret.Dispose();
                }
            }));
            return(ret);
        }
Пример #5
0
        public Lifetime CreateChildLifetime()
        {
            var ret = new Lifetime();

            _manager.OnDisposed(() =>
            {
                if (ret.IsExpired == false)
                {
                    ret.Dispose();
                }
            });
            return(ret);
        }
Пример #6
0
        private void AssertSizeRequirements(bool initialCheck)
        {
            if (initialCheck)
            {
                if (RequiredWidth.HasValue && this.Bitmap.Console.WindowWidth < RequiredWidth.Value)
                {
                    this.Bitmap.Console.WindowWidth = RequiredWidth.Value;
                    this.Bitmap.Console.BufferWidth = RequiredWidth.Value;
                }

                if (RequiredHeight.HasValue && this.Bitmap.Console.WindowHeight < RequiredHeight.Value)
                {
                    this.Bitmap.Console.WindowHeight = RequiredHeight.Value;
                }
            }

            var currentHeight = ConsoleProvider.Current.WindowHeight;
            var currentWidth  = ConsoleProvider.Current.WindowWidth;
            var tallEnough    = this.RequiredHeight.HasValue == false || currentHeight >= this.RequiredHeight.Value;
            var wideEnough    = this.RequiredWidth.HasValue == false || currentWidth >= this.RequiredWidth.Value;

            if (tallEnough && wideEnough)
            {
                if (tooSmallLifetime != null || initialCheck)
                {
                    tooSmallLifetime?.Dispose();
                    RequiredSizeMet.Fire();
                }
            }

            else if (tooSmallLifetime == null)
            {
                isKeyboardInputEnabled = false;
                RequiredSizeNotMet.Fire();
                tooSmallLifetime = new Lifetime();
                var mask = LayoutRoot.Add(new ConsolePanel()).Fill();
                mask.ZIndex = int.MaxValue;
                mask.Add(new Label()
                {
                    Text = "Increase the console window's size to view the app".ToYellow()
                }).CenterBoth();
                tooSmallLifetime.OnDisposed(() =>
                {
                    isKeyboardInputEnabled = true;
                    LayoutRoot.Controls.Remove(mask);
                    tooSmallLifetime = null;
                });
            }
        }
Пример #7
0
        public static Lifetime EarliestOf(IEnumerable <Lifetime> others)
        {
            Lifetime ret = new Lifetime();

            foreach (var other in others)
            {
                other.LifetimeManager.Manage(new Subscription(() =>
                {
                    if (ret.IsExpired == false)
                    {
                        ret.Dispose();
                    }
                }));
            }
            return(ret);
        }
Пример #8
0
        public static Lifetime EarliestOf(IEnumerable <Lifetime> others)
        {
            Lifetime ret = new Lifetime();

            foreach (var other in others)
            {
                other.OnDisposed(() =>
                {
                    if (ret.IsExpired == false)
                    {
                        ret.Dispose();
                    }
                });
            }
            return(ret);
        }
Пример #9
0
        private IDisposable DeepSubscribeInternal(string path, Action handler, Lifetime rootLifetime = null)
        {
            rootLifetime = rootLifetime ?? new Lifetime();
            var observableRoot = DeepObservableRoot ?? this;
            IObservableObject currentObservable = observableRoot;
            var pathExpression = ObjectPathExpression.Parse(path);
            List <IObjectPathElement> currentPath = new List <IObjectPathElement>();

            var anyChangeLifetime = new Lifetime();

            for (int i = 0; i < pathExpression.Elements.Count; i++)
            {
                var propertyElement = pathExpression.Elements[i] as PropertyPathElement;
                if (propertyElement == null)
                {
                    throw new NotSupportedException("Indexers are not supported for deep observability");
                }

                currentPath.Add(propertyElement);

                ObjectPathExpression.TraceNode eval;

                if (i == pathExpression.Elements.Count - 1 && propertyElement.PropertyName == ObservableObject.AnyProperty)
                {
                    eval = new ObjectPathExpression.TraceNode()
                    {
                        Value = null
                    };
                }
                else
                {
                    eval = new ObjectPathExpression(currentPath).EvaluateAndTraceInfo(observableRoot).Last();
                    if (i != pathExpression.Elements.Count - 1 && (eval.MemberInfo as PropertyInfo).PropertyType.GetInterfaces().Contains(typeof(IObservableObject)) == false)
                    {
                        throw new NotSupportedException($"element {eval.MemberInfo.Name} is not observable");
                    }
                }

                currentObservable.SubscribeForLifetime(propertyElement.PropertyName, () =>
                {
                    handler();
                    if (anyChangeLifetime.IsExpired == false)
                    {
                        anyChangeLifetime.Dispose();
                    }

                    if (rootLifetime.IsExpired == false)
                    {
                        DeepSubscribeInternal(path, handler, rootLifetime);
                    }
                }, EarliestOf(anyChangeLifetime, rootLifetime).LifetimeManager);

                currentObservable = eval.Value as IObservableObject;

                if (currentObservable == null)
                {
                    break;
                }
            }

            return(rootLifetime);
        }
Пример #10
0
 /// <summary>
 /// Closes the dialog.
 /// </summary>
 public void CloseDialog() => callerLifetime.Dispose();
Пример #11
0
        private void HardRefresh(ConsoleString outputValue = null)
        {
            refreshLt?.Dispose();
            refreshLt = new Lifetime();
            var myLt = refreshLt;

            Controls.Clear();
            if (Width < 10 || Height < 5)
            {
                return;
            }

            def = CreateDefinition();
            var gridLayout = Add(new GridLayout(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.RemainderValue, Height = 1,
                    },                                                                          // 5 output
                    new GridRowDefinition()
                    {
                        Type = GridValueType.Pixels, Height = 1,
                    },                                                                          // 6 empty
                }
            }));

            gridLayout.Fill();
            gridLayout.RefreshLayout();
            var welcomePanel = gridLayout.Add(new ConsolePanel(), 1, 1);

            welcomePanel.Add(new Label()
            {
                Text = "Welcome to the console".ToWhite()
            }).CenterHorizontally();

            var escapePanel = gridLayout.Add(new ConsolePanel(), 1, 2);

            escapePanel.Add(new Label()
            {
                Text = "Press escape to resume".ToGray()
            }).CenterHorizontally();

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

            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));
            ConsoleApp.Current.QueueAction(() =>
            {
                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();
                }
            });

            var outputPanel = gridLayout.Add(new ConsolePanel()
            {
                Background = ConsoleColor.Black
            }, 1, 5);

            outputLabel = outputPanel.Add(new Label()
            {
                Text = outputValue ?? UpdateAssistiveText(), Mode = LabelRenderMode.MultiLineSmartWrap
            }).Fill();

            InputBox.KeyInputReceived.SubscribeForLifetime(async(keyInfo) => await OnHandleHey(keyInfo), InputBox);
        }
Пример #12
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);
        }