From 489a802df65cbcb809845f8a1f2651ce9be68439 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 30 Jan 2023 21:02:14 +0000 Subject: [PATCH] Implementing nullable bool checked on Menu. --- Terminal.Gui/Views/Menu.cs | 64 +++- .../Scenarios/BackgroundWorkerCollection.cs | 2 +- UICatalog/Scenarios/ClassExplorer.cs | 4 +- .../Scenarios/CollectionNavigatorTester.cs | 4 +- UICatalog/Scenarios/ContextMenus.cs | 2 +- UICatalog/Scenarios/DynamicMenuBar.cs | 23 +- UICatalog/Scenarios/Editor.cs | 16 +- UICatalog/Scenarios/HexEditor.cs | 4 +- UICatalog/Scenarios/SyntaxHighlighting.cs | 2 +- UICatalog/Scenarios/TabViewExample.cs | 6 +- UICatalog/Scenarios/TableEditor.cs | 311 +++++++++--------- .../Scenarios/TextViewAutocompletePopup.cs | 22 +- UICatalog/Scenarios/TreeViewFileSystem.cs | 18 +- UICatalog/UICatalog.cs | 10 +- UnitTests/Menus/MenuTests.cs | 91 +++++ 15 files changed, 367 insertions(+), 212 deletions(-) diff --git a/Terminal.Gui/Views/Menu.cs b/Terminal.Gui/Views/Menu.cs index 2244531d1..e4019237f 100644 --- a/Terminal.Gui/Views/Menu.cs +++ b/Terminal.Gui/Views/Menu.cs @@ -33,6 +33,9 @@ namespace Terminal.Gui { public class MenuItem { ustring title; ShortcutHelper shortcutHelper; + bool allowNullChecked; + MenuItemCheckStyle checkType; + internal int TitleLength => GetMenuBarItemLength (Title); /// @@ -158,19 +161,42 @@ namespace Terminal.Gui { internal int Width => 1 + // space before Title TitleLength + 2 + // space after Title - BUGBUG: This should be 1 - (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) + // check glyph + space + (Checked == true || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) + // check glyph + space (Help.ConsoleWidth > 0 ? 2 + Help.ConsoleWidth : 0) + // Two spaces before Help (ShortcutTag.ConsoleWidth > 0 ? 2 + ShortcutTag.ConsoleWidth : 0); // Pad two spaces before shortcut tag (which are also aligned right) /// /// Sets or gets whether the shows a check indicator or not. See . /// - public bool Checked { set; get; } + public bool? Checked { set; get; } + + /// + /// Used only if is of type. + /// If allows to be null, true or false. + /// If only allows to be true or false. + /// + public bool AllowNullChecked { + get => allowNullChecked; + set { + allowNullChecked = value; + if (Checked == null) { + Checked = false; + } + } + } /// /// Sets or gets the of a menu item where is set to . /// - public MenuItemCheckStyle CheckType { get; set; } + public MenuItemCheckStyle CheckType { + get => checkType; + set { + checkType = value; + if (checkType == MenuItemCheckStyle.Checked && !allowNullChecked && Checked == null) { + Checked = false; + } + } + } /// /// Gets the parent for this . @@ -199,6 +225,33 @@ namespace Terminal.Gui { return IsFromSubMenu; } + /// + /// Toggle the between three states if is + /// or between two states if is . + /// + public void ToggleChecked () + { + if (checkType != MenuItemCheckStyle.Checked) { + throw new InvalidOperationException ("This isn't a Checked MenuItemCheckStyle!"); + } + var previousChecked = Checked; + if (AllowNullChecked) { + switch (previousChecked) { + case null: + Checked = true; + break; + case true: + Checked = false; + break; + case false: + Checked = null; + break; + } + } else { + Checked = !Checked; + } + } + void GetHotKey () { bool nextIsHot = false; @@ -500,6 +553,7 @@ namespace Terminal.Gui { } ustring textToDraw; + var nullCheckedChar = Driver.NullChecked; var checkChar = Driver.Selected; var uncheckedChar = Driver.UnSelected; @@ -509,7 +563,9 @@ namespace Terminal.Gui { } // Support Checked even though CheckType wasn't set - if (item.Checked) { + if (item.CheckType == MenuItemCheckStyle.Checked && item.Checked == null) { + textToDraw = ustring.Make (new Rune [] { nullCheckedChar, ' ' }) + item.Title; + } else if (item.Checked == true) { textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title; } else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) { textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title; diff --git a/UICatalog/Scenarios/BackgroundWorkerCollection.cs b/UICatalog/Scenarios/BackgroundWorkerCollection.cs index 2e566ebe5..de83d5759 100644 --- a/UICatalog/Scenarios/BackgroundWorkerCollection.cs +++ b/UICatalog/Scenarios/BackgroundWorkerCollection.cs @@ -105,7 +105,7 @@ namespace UICatalog.Scenarios { } item.Action += () => { var top = Application.MdiChildes.Find ((x) => x.Data.ToString () == "WorkerApp"); - item.Checked = top.Visible = !item.Checked; + item.Checked = top.Visible = (bool)!item.Checked; if (top.Visible) { top.ShowChild (); } else { diff --git a/UICatalog/Scenarios/ClassExplorer.cs b/UICatalog/Scenarios/ClassExplorer.cs index 650bd099f..acbc6b539 100644 --- a/UICatalog/Scenarios/ClassExplorer.cs +++ b/UICatalog/Scenarios/ClassExplorer.cs @@ -78,7 +78,7 @@ namespace UICatalog.Scenarios { highlightModelTextOnly = new MenuItem ("_Highlight Model Text Only", "", () => OnCheckHighlightModelTextOnly()) { CheckType = MenuItemCheckStyle.Checked }, - }) + }) }); Application.Top.Add (menu); @@ -122,7 +122,7 @@ namespace UICatalog.Scenarios { private BindingFlags GetFlags () { - if (miShowPrivate.Checked) { + if (miShowPrivate.Checked == true) { return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; } diff --git a/UICatalog/Scenarios/CollectionNavigatorTester.cs b/UICatalog/Scenarios/CollectionNavigatorTester.cs index ba921dafa..102db475b 100644 --- a/UICatalog/Scenarios/CollectionNavigatorTester.cs +++ b/UICatalog/Scenarios/CollectionNavigatorTester.cs @@ -90,7 +90,7 @@ namespace UICatalog.Scenarios { Checked = false }; allowMultiSelection.Action = () => allowMultiSelection.Checked = _listView.AllowsMultipleSelection = !_listView.AllowsMultipleSelection; - allowMultiSelection.CanExecute = () => allowMarking.Checked; + allowMultiSelection.CanExecute = () => (bool)allowMarking.Checked; var menu = new MenuBar (new MenuBarItem [] { new MenuBarItem ("_Configure", new MenuItem [] { @@ -156,7 +156,7 @@ namespace UICatalog.Scenarios { TextAlignment = TextAlignment.Centered, X = Pos.Right (_listView) + 2, Y = 1, // for menu - Width = Dim.Percent (50), + Width = Dim.Percent (50), Height = 1, }; Application.Top.Add (label); diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index 897f75acc..e45c3e50d 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -106,7 +106,7 @@ namespace UICatalog.Scenarios { tfBottomRight.ContextMenu.ForceMinimumPosToZero = forceMinimumPosToZero; }) { CheckType = MenuItemCheckStyle.Checked, Checked = forceMinimumPosToZero }, miUseSubMenusSingleFrame = new MenuItem ("Use_SubMenusSingleFrame", "", - () => contextMenu.UseSubMenusSingleFrame = miUseSubMenusSingleFrame.Checked = useSubMenusSingleFrame = !useSubMenusSingleFrame) { + () => contextMenu.UseSubMenusSingleFrame = (bool)(miUseSubMenusSingleFrame.Checked = useSubMenusSingleFrame = !useSubMenusSingleFrame)) { CheckType = MenuItemCheckStyle.Checked, Checked = useSubMenusSingleFrame }, null, diff --git a/UICatalog/Scenarios/DynamicMenuBar.cs b/UICatalog/Scenarios/DynamicMenuBar.cs index 94db537ed..eca4ba9ff 100644 --- a/UICatalog/Scenarios/DynamicMenuBar.cs +++ b/UICatalog/Scenarios/DynamicMenuBar.cs @@ -42,6 +42,7 @@ namespace UICatalog.Scenarios { public bool hasSubMenu; public MenuItemCheckStyle checkStyle; public ustring shortcut; + public bool allowNullChecked; public DynamicMenuItem () { } @@ -51,7 +52,7 @@ namespace UICatalog.Scenarios { this.hasSubMenu = hasSubMenu; } - public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortcut = null) + public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortcut = null, bool allowNullChecked = false) { this.title = title; this.help = help; @@ -60,6 +61,7 @@ namespace UICatalog.Scenarios { this.hasSubMenu = hasSubMenu; this.checkStyle = checkStyle; this.shortcut = shortcut; + this.allowNullChecked = allowNullChecked; } } @@ -564,6 +566,7 @@ namespace UICatalog.Scenarios { newMenu.CheckType = item.checkStyle; newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item); newMenu.Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut); + newMenu.AllowNullChecked = item.allowNullChecked; } else if (item.isTopLevel) { newMenu = new MenuBarItem (item.title, item.help, null); newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item); @@ -635,6 +638,7 @@ namespace UICatalog.Scenarios { public TextView _txtAction; public CheckBox _ckbIsTopLevel; public CheckBox _ckbSubMenu; + public CheckBox _ckbNullCheck; public RadioGroup _rbChkStyle; public TextField _txtShortcut; @@ -700,6 +704,12 @@ namespace UICatalog.Scenarios { }; Add (_ckbSubMenu); + _ckbNullCheck = new CheckBox ("Allow null checked") { + X = Pos.Left (_lblTitle), + Y = Pos.Bottom (_ckbSubMenu) + }; + Add (_ckbNullCheck); + var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" }; _rbChkStyle = new RadioGroup (_rChkLabels) { X = Pos.Left (_lblTitle), @@ -825,6 +835,11 @@ namespace UICatalog.Scenarios { _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false; } }; + _ckbNullCheck.Toggled += (e) => { + if (_menuItem != null) { + _menuItem.AllowNullChecked = (bool)_ckbNullCheck.Checked; + } + }; //Add (_frmMenuDetails); @@ -842,6 +857,7 @@ namespace UICatalog.Scenarios { _txtAction.Text = m.action; _ckbIsTopLevel.Checked = false; _ckbSubMenu.Checked = !hasParent; + _ckbNullCheck.Checked = false; _txtHelp.Enabled = hasParent; _txtAction.Enabled = hasParent; _txtShortcut.Enabled = hasParent; @@ -880,7 +896,7 @@ namespace UICatalog.Scenarios { _ckbSubMenu != null ? (bool)_ckbSubMenu.Checked : false, _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck : _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio, - _txtShortcut.Text); + _txtShortcut.Text, (bool)_ckbNullCheck.Checked); } else { return null; } @@ -903,6 +919,7 @@ namespace UICatalog.Scenarios { _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : ustring.Empty; _ckbIsTopLevel.Checked = IsTopLevel (menuItem); _ckbSubMenu.Checked = HasSubMenus (menuItem); + _ckbNullCheck.Checked = menuItem.AllowNullChecked; _txtHelp.Enabled = (bool)!_ckbSubMenu.Checked; _txtAction.Enabled = (bool)!_ckbSubMenu.Checked; _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck); @@ -963,7 +980,7 @@ namespace UICatalog.Scenarios { case MenuItemCheckStyle.NoCheck: return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok")); case MenuItemCheckStyle.Checked: - return new Action (() => menuItem.Checked = !menuItem.Checked); + return new Action (menuItem.ToggleChecked); case MenuItemCheckStyle.Radio: break; } diff --git a/UICatalog/Scenarios/Editor.cs b/UICatalog/Scenarios/Editor.cs index d6b085a17..ec9774776 100644 --- a/UICatalog/Scenarios/Editor.cs +++ b/UICatalog/Scenarios/Editor.cs @@ -514,7 +514,7 @@ namespace UICatalog.Scenarios { item.Title = "Keep Content Always In Viewport"; item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = true; - item.Action += () => _scrollBar.KeepContentAlwaysInViewport = item.Checked = !item.Checked; + item.Action += () => _scrollBar.KeepContentAlwaysInViewport = (bool)(item.Checked = !item.Checked); return new MenuItem [] { item }; } @@ -527,7 +527,7 @@ namespace UICatalog.Scenarios { item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.WordWrap; item.Action += () => { - _textView.WordWrap = item.Checked = !item.Checked; + _textView.WordWrap = (bool)(item.Checked = !item.Checked); if (_textView.WordWrap) { _scrollBar.OtherScrollBarView.ShowScrollIndicator = false; _textView.BottomOffset = 0; @@ -546,7 +546,7 @@ namespace UICatalog.Scenarios { auto.CheckType |= MenuItemCheckStyle.Checked; auto.Checked = false; auto.Action += () => { - if (auto.Checked = !auto.Checked) { + if ((bool)(auto.Checked = !auto.Checked)) { // setup autocomplete with all words currently in the editor _textView.Autocomplete.AllSuggestions = @@ -570,7 +570,7 @@ namespace UICatalog.Scenarios { item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.AllowsTab; item.Action += () => { - _textView.AllowsTab = item.Checked = !item.Checked; + _textView.AllowsTab = (bool)(item.Checked = !item.Checked); }; return item; @@ -583,7 +583,7 @@ namespace UICatalog.Scenarios { }; item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.ReadOnly; - item.Action += () => _textView.ReadOnly = item.Checked = !item.Checked; + item.Action += () => _textView.ReadOnly = (bool)(item.Checked = !item.Checked); return item; } @@ -596,7 +596,7 @@ namespace UICatalog.Scenarios { item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.CanFocus; item.Action += () => { - _textView.CanFocus = item.Checked = !item.Checked; + _textView.CanFocus = (bool)(item.Checked = !item.Checked); if (_textView.CanFocus) { _textView.SetFocus (); } @@ -613,7 +613,7 @@ namespace UICatalog.Scenarios { item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.Enabled; item.Action += () => { - _textView.Enabled = item.Checked = !item.Checked; + _textView.Enabled = (bool)(item.Checked = !item.Checked); if (_textView.Enabled) { _textView.SetFocus (); } @@ -630,7 +630,7 @@ namespace UICatalog.Scenarios { item.CheckType |= MenuItemCheckStyle.Checked; item.Checked = _textView.Visible; item.Action += () => { - _textView.Visible = item.Checked = !item.Checked; + _textView.Visible = (bool)(item.Checked = !item.Checked); if (_textView.Visible) { _textView.SetFocus (); } diff --git a/UICatalog/Scenarios/HexEditor.cs b/UICatalog/Scenarios/HexEditor.cs index c83408ada..121a164ae 100644 --- a/UICatalog/Scenarios/HexEditor.cs +++ b/UICatalog/Scenarios/HexEditor.cs @@ -72,7 +72,7 @@ namespace UICatalog.Scenarios { private void ToggleAllowEdits () { - _hexView.AllowEdits = miAllowEdits.Checked = !miAllowEdits.Checked; + _hexView.AllowEdits = (bool)(miAllowEdits.Checked = !miAllowEdits.Checked); } private void _hexView_Edited (System.Collections.Generic.KeyValuePair obj) @@ -171,7 +171,7 @@ namespace UICatalog.Scenarios { sb.Append ("Hello world.\n"); sb.Append ("This is a test of the Emergency Broadcast System.\n"); - byte [] buffer = Encoding.Unicode.GetBytes (sb.ToString()); + byte [] buffer = Encoding.Unicode.GetBytes (sb.ToString ()); MemoryStream ms = new MemoryStream (buffer); FileStream file = new FileStream (fileName, FileMode.Create, FileAccess.Write); ms.WriteTo (file); diff --git a/UICatalog/Scenarios/SyntaxHighlighting.cs b/UICatalog/Scenarios/SyntaxHighlighting.cs index 3a696b14c..1885ff6ff 100644 --- a/UICatalog/Scenarios/SyntaxHighlighting.cs +++ b/UICatalog/Scenarios/SyntaxHighlighting.cs @@ -55,7 +55,7 @@ namespace UICatalog.Scenarios { private void WordWrap () { miWrap.Checked = !miWrap.Checked; - textView.WordWrap = miWrap.Checked; + textView.WordWrap = (bool)miWrap.Checked; } private void Quit () diff --git a/UICatalog/Scenarios/TabViewExample.cs b/UICatalog/Scenarios/TabViewExample.cs index 2bab75e70..0936cdc00 100644 --- a/UICatalog/Scenarios/TabViewExample.cs +++ b/UICatalog/Scenarios/TabViewExample.cs @@ -176,21 +176,21 @@ namespace UICatalog.Scenarios { { miShowTopLine.Checked = !miShowTopLine.Checked; - tabView.Style.ShowTopLine = miShowTopLine.Checked; + tabView.Style.ShowTopLine = (bool)miShowTopLine.Checked; tabView.ApplyStyleChanges (); } private void ShowBorder () { miShowBorder.Checked = !miShowBorder.Checked; - tabView.Style.ShowBorder = miShowBorder.Checked; + tabView.Style.ShowBorder = (bool)miShowBorder.Checked; tabView.ApplyStyleChanges (); } private void SetTabsOnBottom () { miTabsOnBottom.Checked = !miTabsOnBottom.Checked; - tabView.Style.TabsOnBottom = miTabsOnBottom.Checked; + tabView.Style.TabsOnBottom = (bool)miTabsOnBottom.Checked; tabView.ApplyStyleChanges (); } diff --git a/UICatalog/Scenarios/TableEditor.cs b/UICatalog/Scenarios/TableEditor.cs index 99200329e..9f50ccbad 100644 --- a/UICatalog/Scenarios/TableEditor.cs +++ b/UICatalog/Scenarios/TableEditor.cs @@ -14,8 +14,7 @@ namespace UICatalog.Scenarios { [ScenarioCategory ("Dialogs")] [ScenarioCategory ("Text and Formatting")] [ScenarioCategory ("Top Level Windows")] - public class TableEditor : Scenario - { + public class TableEditor : Scenario { TableView tableView; private MenuItem miAlwaysShowHeaders; private MenuItem miHeaderOverline; @@ -35,7 +34,7 @@ namespace UICatalog.Scenarios { public override void Setup () { - Win.Title = this.GetName(); + Win.Title = this.GetName (); Win.Y = 1; // menu Win.Height = Dim.Fill (1); // status bar Application.Top.LayoutSubviews (); @@ -93,43 +92,43 @@ namespace UICatalog.Scenarios { Win.Add (tableView); - var selectedCellLabel = new Label(){ + var selectedCellLabel = new Label () { X = 0, - Y = Pos.Bottom(tableView), + Y = Pos.Bottom (tableView), Text = "0,0", - Width = Dim.Fill(), + Width = Dim.Fill (), TextAlignment = TextAlignment.Right - + }; - Win.Add(selectedCellLabel); + Win.Add (selectedCellLabel); tableView.SelectedCellChanged += (e) => { selectedCellLabel.Text = $"{tableView.SelectedRow},{tableView.SelectedColumn}"; }; tableView.CellActivated += EditCurrentCell; tableView.KeyPress += TableViewKeyPress; - SetupScrollBar(); + SetupScrollBar (); - redColorScheme = new ColorScheme(){ + redColorScheme = new ColorScheme () { Disabled = Win.ColorScheme.Disabled, HotFocus = Win.ColorScheme.HotFocus, Focus = Win.ColorScheme.Focus, - Normal = Application.Driver.MakeAttribute(Color.Red,Win.ColorScheme.Normal.Background) + Normal = Application.Driver.MakeAttribute (Color.Red, Win.ColorScheme.Normal.Background) }; - alternatingColorScheme = new ColorScheme(){ + alternatingColorScheme = new ColorScheme () { Disabled = Win.ColorScheme.Disabled, HotFocus = Win.ColorScheme.HotFocus, Focus = Win.ColorScheme.Focus, - Normal = Application.Driver.MakeAttribute(Color.White,Color.BrightBlue) + Normal = Application.Driver.MakeAttribute (Color.White, Color.BrightBlue) }; - redColorSchemeAlt = new ColorScheme(){ + redColorSchemeAlt = new ColorScheme () { Disabled = Win.ColorScheme.Disabled, HotFocus = Win.ColorScheme.HotFocus, Focus = Win.ColorScheme.Focus, - Normal = Application.Driver.MakeAttribute(Color.Red,Color.BrightBlue) + Normal = Application.Driver.MakeAttribute (Color.Red, Color.BrightBlue) }; // if user clicks the mouse in TableView @@ -139,7 +138,7 @@ namespace UICatalog.Scenarios { if (clickedCol != null) { if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) { - + // left click in a header SortColumn (clickedCol); } else if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)) { @@ -153,7 +152,7 @@ namespace UICatalog.Scenarios { private void ShowAllColumns () { - foreach(var colStyle in tableView.Style.ColumnStyles) { + foreach (var colStyle in tableView.Style.ColumnStyles) { colStyle.Value.Visible = true; } tableView.Update (); @@ -181,7 +180,7 @@ namespace UICatalog.Scenarios { foreach (DataColumn col in tableView.Table.Columns) { // remove any lingering sort indicator - col.ColumnName = TrimArrows(col.ColumnName); + col.ColumnName = TrimArrows (col.ColumnName); // add a new one if this the one that is being sorted if (col == clickedCol) { @@ -250,8 +249,7 @@ namespace UICatalog.Scenarios { private void SetMinAcceptableWidthToOne () { - foreach (DataColumn c in tableView.Table.Columns) - { + foreach (DataColumn c in tableView.Table.Columns) { var style = tableView.Style.GetOrCreateColumnStyle (c); style.MinAcceptableWidth = 1; } @@ -259,7 +257,7 @@ namespace UICatalog.Scenarios { private void SetMinAcceptableWidth () { var col = GetColumn (); - RunColumnWidthDialog (col, "MinAcceptableWidth", (s,v)=>s.MinAcceptableWidth = v,(s)=>s.MinAcceptableWidth); + RunColumnWidthDialog (col, "MinAcceptableWidth", (s, v) => s.MinAcceptableWidth = v, (s) => s.MinAcceptableWidth); } private void SetMinWidth () @@ -274,7 +272,7 @@ namespace UICatalog.Scenarios { RunColumnWidthDialog (col, "MaxWidth", (s, v) => s.MaxWidth = v, (s) => s.MaxWidth); } - private void RunColumnWidthDialog (DataColumn col, string prompt, Action setter,Func getter) + private void RunColumnWidthDialog (DataColumn col, string prompt, Action setter, Func getter) { var accepted = false; var ok = new Button ("Ok", is_default: true); @@ -292,7 +290,7 @@ namespace UICatalog.Scenarios { }; var tf = new TextField () { - Text = getter(style).ToString (), + Text = getter (style).ToString (), X = 0, Y = 2, Width = Dim.Fill () @@ -306,7 +304,7 @@ namespace UICatalog.Scenarios { if (accepted) { try { - setter (style, int.Parse (tf.Text.ToString())); + setter (style, int.Parse (tf.Text.ToString ())); } catch (Exception ex) { MessageBox.ErrorQuery (60, 20, "Failed to set", ex.Message, "Ok"); } @@ -336,35 +334,32 @@ namespace UICatalog.Scenarios { };*/ tableView.DrawContent += (e) => { - _scrollBar.Size = tableView.Table?.Rows?.Count ??0; + _scrollBar.Size = tableView.Table?.Rows?.Count ?? 0; _scrollBar.Position = tableView.RowOffset; - // _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1; - // _scrollBar.OtherScrollBarView.Position = _listView.LeftItem; + // _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1; + // _scrollBar.OtherScrollBarView.Position = _listView.LeftItem; _scrollBar.Refresh (); }; - + } private void TableViewKeyPress (View.KeyEventEventArgs e) { - if(e.KeyEvent.Key == Key.DeleteChar){ + if (e.KeyEvent.Key == Key.DeleteChar) { - if(tableView.FullRowSelect) - { + if (tableView.FullRowSelect) { // Delete button deletes all rows when in full row mode - foreach(int toRemove in tableView.GetAllSelectedCells().Select(p=>p.Y).Distinct().OrderByDescending(i=>i)) - tableView.Table.Rows.RemoveAt(toRemove); - } - else{ + foreach (int toRemove in tableView.GetAllSelectedCells ().Select (p => p.Y).Distinct ().OrderByDescending (i => i)) + tableView.Table.Rows.RemoveAt (toRemove); + } else { // otherwise set all selected cells to null - foreach(var pt in tableView.GetAllSelectedCells()) - { - tableView.Table.Rows[pt.Y][pt.X] = DBNull.Value; + foreach (var pt in tableView.GetAllSelectedCells ()) { + tableView.Table.Rows [pt.Y] [pt.X] = DBNull.Value; } } - tableView.Update(); + tableView.Update (); e.Handled = true; } @@ -373,85 +368,85 @@ namespace UICatalog.Scenarios { private void ClearColumnStyles () { - tableView.Style.ColumnStyles.Clear(); - tableView.Update(); + tableView.Style.ColumnStyles.Clear (); + tableView.Update (); } private void ToggleAlwaysShowHeader () { miAlwaysShowHeaders.Checked = !miAlwaysShowHeaders.Checked; - tableView.Style.AlwaysShowHeaders = miAlwaysShowHeaders.Checked; - tableView.Update(); + tableView.Style.AlwaysShowHeaders = (bool)miAlwaysShowHeaders.Checked; + tableView.Update (); } private void ToggleOverline () { miHeaderOverline.Checked = !miHeaderOverline.Checked; - tableView.Style.ShowHorizontalHeaderOverline = miHeaderOverline.Checked; - tableView.Update(); + tableView.Style.ShowHorizontalHeaderOverline = (bool)miHeaderOverline.Checked; + tableView.Update (); } private void ToggleHeaderMidline () { miHeaderMidline.Checked = !miHeaderMidline.Checked; - tableView.Style.ShowVerticalHeaderLines = miHeaderMidline.Checked; - tableView.Update(); + tableView.Style.ShowVerticalHeaderLines = (bool)miHeaderMidline.Checked; + tableView.Update (); } private void ToggleUnderline () { miHeaderUnderline.Checked = !miHeaderUnderline.Checked; - tableView.Style.ShowHorizontalHeaderUnderline = miHeaderUnderline.Checked; - tableView.Update(); + tableView.Style.ShowHorizontalHeaderUnderline = (bool)miHeaderUnderline.Checked; + tableView.Update (); } private void ToggleHorizontalScrollIndicators () { miShowHorizontalScrollIndicators.Checked = !miShowHorizontalScrollIndicators.Checked; - tableView.Style.ShowHorizontalScrollIndicators = miShowHorizontalScrollIndicators.Checked; - tableView.Update(); + tableView.Style.ShowHorizontalScrollIndicators = (bool)miShowHorizontalScrollIndicators.Checked; + tableView.Update (); } private void ToggleFullRowSelect () { miFullRowSelect.Checked = !miFullRowSelect.Checked; - tableView.FullRowSelect= miFullRowSelect.Checked; - tableView.Update(); + tableView.FullRowSelect = (bool)miFullRowSelect.Checked; + tableView.Update (); } - private void ToggleExpandLastColumn() + private void ToggleExpandLastColumn () { miExpandLastColumn.Checked = !miExpandLastColumn.Checked; - tableView.Style.ExpandLastColumn = miExpandLastColumn.Checked; - - tableView.Update(); - - } - private void ToggleSmoothScrolling() - { - miSmoothScrolling.Checked = !miSmoothScrolling.Checked; - tableView.Style.SmoothHorizontalScrolling = miSmoothScrolling.Checked; + tableView.Style.ExpandLastColumn = (bool)miExpandLastColumn.Checked; tableView.Update (); } - private void ToggleCellLines() + private void ToggleSmoothScrolling () + { + miSmoothScrolling.Checked = !miSmoothScrolling.Checked; + tableView.Style.SmoothHorizontalScrolling = (bool)miSmoothScrolling.Checked; + + tableView.Update (); + + } + private void ToggleCellLines () { miCellLines.Checked = !miCellLines.Checked; - tableView.Style.ShowVerticalCellLines = miCellLines.Checked; - tableView.Update(); + tableView.Style.ShowVerticalCellLines = (bool)miCellLines.Checked; + tableView.Update (); } - private void ToggleAllCellLines() + private void ToggleAllCellLines () { tableView.Style.ShowHorizontalHeaderOverline = true; tableView.Style.ShowVerticalHeaderLines = true; tableView.Style.ShowHorizontalHeaderUnderline = true; tableView.Style.ShowVerticalCellLines = true; - + miHeaderOverline.Checked = true; miHeaderMidline.Checked = true; miHeaderUnderline.Checked = true; miCellLines.Checked = true; - tableView.Update(); + tableView.Update (); } - private void ToggleNoCellLines() + private void ToggleNoCellLines () { tableView.Style.ShowHorizontalHeaderOverline = false; tableView.Style.ShowVerticalHeaderLines = false; @@ -463,29 +458,27 @@ namespace UICatalog.Scenarios { miHeaderUnderline.Checked = false; miCellLines.Checked = false; - tableView.Update(); + tableView.Update (); } - private void ToggleAlternatingColors() + private void ToggleAlternatingColors () { //toggle menu item miAlternatingColors.Checked = !miAlternatingColors.Checked; - if(miAlternatingColors.Checked){ - tableView.Style.RowColorGetter = (a)=> {return a.RowIndex%2==0 ? alternatingColorScheme : null;}; - } - else - { + if (miAlternatingColors.Checked == true) { + tableView.Style.RowColorGetter = (a) => { return a.RowIndex % 2 == 0 ? alternatingColorScheme : null; }; + } else { tableView.Style.RowColorGetter = null; } - tableView.SetNeedsDisplay(); + tableView.SetNeedsDisplay (); } private void ToggleInvertSelectedCellFirstCharacter () { //toggle menu item miCursor.Checked = !miCursor.Checked; - tableView.Style.InvertSelectedCellFirstCharacter = miCursor.Checked; + tableView.Style.InvertSelectedCellFirstCharacter = (bool)miCursor.Checked; tableView.SetNeedsDisplay (); } private void CloseExample () @@ -500,11 +493,11 @@ namespace UICatalog.Scenarios { private void OpenExample (bool big) { - tableView.Table = BuildDemoDataTable(big ? 30 : 5, big ? 1000 : 5); - SetDemoTableStyles(); + tableView.Table = BuildDemoDataTable (big ? 30 : 5, big ? 1000 : 5); + SetDemoTableStyles (); } - private void OpenUnicodeMap() + private void OpenUnicodeMap () { tableView.Table = BuildUnicodeMap (); tableView.Update (); @@ -515,7 +508,7 @@ namespace UICatalog.Scenarios { var dt = new DataTable (); // add cols called 0 to 9 - for (int i = 0; i < 10;i++) { + for (int i = 0; i < 10; i++) { var col = dt.Columns.Add (i.ToString (), typeof (uint)); var style = tableView.Style.GetOrCreateColumnStyle (col); @@ -523,9 +516,9 @@ namespace UICatalog.Scenarios { } // add cols called a to z - for (int i = 'a'; i < 'a'+26; i++) { - - var col =dt.Columns.Add (((char)i).ToString (), typeof (uint)); + for (int i = 'a'; i < 'a' + 26; i++) { + + var col = dt.Columns.Add (((char)i).ToString (), typeof (uint)); var style = tableView.Style.GetOrCreateColumnStyle (col); style.RepresentationGetter = (o) => new Rune ((uint)o).ToString (); } @@ -533,19 +526,19 @@ namespace UICatalog.Scenarios { // now add table contents List runes = new List (); - foreach(var range in Ranges) { - for(uint i=range.Start;i<=range.End;i++) { + foreach (var range in Ranges) { + for (uint i = range.Start; i <= range.End; i++) { runes.Add (i); } } DataRow dr = null; - for(int i = 0; i v is DateTime d ? d.ToString("yyyy-MM-dd"):v.ToString() + RepresentationGetter = (v) => v is DateTime d ? d.ToString ("yyyy-MM-dd") : v.ToString () }; var negativeRight = new TableView.ColumnStyle () { - + Format = "0.##", MinWidth = 10, - AlignmentGetter = (v)=>v is double d ? + AlignmentGetter = (v) => v is double d ? // align negative values right - d < 0 ? TextAlignment.Right : + d < 0 ? TextAlignment.Right : // align positive values left - TextAlignment.Left: + TextAlignment.Left : // not a double TextAlignment.Left, - - ColorGetter = (a)=> a.CellValue is double d ? + + ColorGetter = (a) => a.CellValue is double d ? // color 0 and negative values red - d <= 0.0000001 ? a.RowIndex%2==0 && miAlternatingColors.Checked ? redColorSchemeAlt: redColorScheme : + d <= 0.0000001 ? a.RowIndex % 2 == 0 && miAlternatingColors.Checked == true ? redColorSchemeAlt : redColorScheme : // use normal scheme for positive values - null: + null : // not a double null }; - - tableView.Style.ColumnStyles.Add(tableView.Table.Columns["DateCol"],dateFormatStyle); - tableView.Style.ColumnStyles.Add(tableView.Table.Columns["DoubleCol"],negativeRight); - tableView.Style.ColumnStyles.Add(tableView.Table.Columns["NullsCol"],alignMid); - tableView.Style.ColumnStyles.Add(tableView.Table.Columns["IntCol"],alignRight); - - tableView.Update(); + + tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["DateCol"], dateFormatStyle); + tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["DoubleCol"], negativeRight); + tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["NullsCol"], alignMid); + tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["IntCol"], alignRight); + + tableView.Update (); } private void OpenSimple (bool big) { - tableView.Table = BuildSimpleDataTable(big ? 30 : 5, big ? 1000 : 5); + tableView.Table = BuildSimpleDataTable (big ? 30 : 5, big ? 1000 : 5); } private void EditCurrentCell (TableView.CellActivatedEventArgs e) { - if(e.Table == null) + if (e.Table == null) return; var o = e.Table.Rows [e.Row] [e.Col]; - var title = o is uint u ? GetUnicodeCategory(u) + $"(0x{o:X4})" : "Enter new value"; + var title = o is uint u ? GetUnicodeCategory (u) + $"(0x{o:X4})" : "Enter new value"; - var oldValue = e.Table.Rows[e.Row][e.Col].ToString(); + var oldValue = e.Table.Rows [e.Row] [e.Col].ToString (); bool okPressed = false; var ok = new Button ("Ok", is_default: true); @@ -770,35 +763,33 @@ namespace UICatalog.Scenarios { cancel.Clicked += () => { Application.RequestStop (); }; var d = new Dialog (title, 60, 20, ok, cancel); - var lbl = new Label() { + var lbl = new Label () { X = 0, Y = 1, - Text = e.Table.Columns[e.Col].ColumnName + Text = e.Table.Columns [e.Col].ColumnName }; - var tf = new TextField() - { - Text = oldValue, - X = 0, - Y = 2, - Width = Dim.Fill() - }; - - d.Add (lbl,tf); - tf.SetFocus(); + var tf = new TextField () { + Text = oldValue, + X = 0, + Y = 2, + Width = Dim.Fill () + }; + + d.Add (lbl, tf); + tf.SetFocus (); Application.Run (d); - if(okPressed) { + if (okPressed) { try { - e.Table.Rows[e.Row][e.Col] = string.IsNullOrWhiteSpace(tf.Text.ToString()) ? DBNull.Value : (object)tf.Text; + e.Table.Rows [e.Row] [e.Col] = string.IsNullOrWhiteSpace (tf.Text.ToString ()) ? DBNull.Value : (object)tf.Text; + } catch (Exception ex) { + MessageBox.ErrorQuery (60, 20, "Failed to set text", ex.Message, "Ok"); } - catch(Exception ex) { - MessageBox.ErrorQuery(60,20,"Failed to set text", ex.Message,"Ok"); - } - - tableView.Update(); + + tableView.Update (); } } @@ -813,27 +804,27 @@ namespace UICatalog.Scenarios { /// /// /// - public static DataTable BuildDemoDataTable(int cols, int rows) + public static DataTable BuildDemoDataTable (int cols, int rows) { - var dt = new DataTable(); + var dt = new DataTable (); int explicitCols = 6; - dt.Columns.Add(new DataColumn("StrCol",typeof(string))); - dt.Columns.Add(new DataColumn("DateCol",typeof(DateTime))); - dt.Columns.Add(new DataColumn("IntCol",typeof(int))); - dt.Columns.Add(new DataColumn("DoubleCol",typeof(double))); - dt.Columns.Add(new DataColumn("NullsCol",typeof(string))); - dt.Columns.Add(new DataColumn("Unicode",typeof(string))); + dt.Columns.Add (new DataColumn ("StrCol", typeof (string))); + dt.Columns.Add (new DataColumn ("DateCol", typeof (DateTime))); + dt.Columns.Add (new DataColumn ("IntCol", typeof (int))); + dt.Columns.Add (new DataColumn ("DoubleCol", typeof (double))); + dt.Columns.Add (new DataColumn ("NullsCol", typeof (string))); + dt.Columns.Add (new DataColumn ("Unicode", typeof (string))); - for(int i=0;i< cols -explicitCols; i++) { - dt.Columns.Add("Column" + (i+explicitCols)); + for (int i = 0; i < cols - explicitCols; i++) { + dt.Columns.Add ("Column" + (i + explicitCols)); } - - var r = new Random(100); - for(int i=0;i< rows;i++) { - - List row = new List(){ + var r = new Random (100); + + for (int i = 0; i < rows; i++) { + + List row = new List (){ "Some long text that is super cool", new DateTime(2000+i,12,25), r.Next(i), @@ -841,12 +832,12 @@ namespace UICatalog.Scenarios { DBNull.Value, "Les Mise" + Char.ConvertFromUtf32(Int32.Parse("0301", NumberStyles.HexNumber)) + "rables" }; - - for(int j=0;j< cols -explicitCols; j++) { - row.Add("SomeValue" + r.Next(100)); + + for (int j = 0; j < cols - explicitCols; j++) { + row.Add ("SomeValue" + r.Next (100)); } - dt.Rows.Add(row.ToArray()); + dt.Rows.Add (row.ToArray ()); } return dt; @@ -858,24 +849,24 @@ namespace UICatalog.Scenarios { /// /// /// - public static DataTable BuildSimpleDataTable(int cols, int rows) + public static DataTable BuildSimpleDataTable (int cols, int rows) { - var dt = new DataTable(); + var dt = new DataTable (); - for(int c = 0; c < cols; c++) { - dt.Columns.Add("Col"+c); + for (int c = 0; c < cols; c++) { + dt.Columns.Add ("Col" + c); } - - for(int r = 0; r < rows; r++) { - var newRow = dt.NewRow(); - for(int c = 0; c < cols; c++) { - newRow[c] = $"R{r}C{c}"; + for (int r = 0; r < rows; r++) { + var newRow = dt.NewRow (); + + for (int c = 0; c < cols; c++) { + newRow [c] = $"R{r}C{c}"; } - dt.Rows.Add(newRow); + dt.Rows.Add (newRow); } - + return dt; } } diff --git a/UICatalog/Scenarios/TextViewAutocompletePopup.cs b/UICatalog/Scenarios/TextViewAutocompletePopup.cs index 5907a6a25..cf740e712 100644 --- a/UICatalog/Scenarios/TextViewAutocompletePopup.cs +++ b/UICatalog/Scenarios/TextViewAutocompletePopup.cs @@ -101,7 +101,7 @@ namespace UICatalog.Scenarios { SetMultilineStatusText (); SetWrapStatusText (); - if (miMultiline.Checked) { + if (miMultiline.Checked == true) { height = 10; } else { height = 1; @@ -155,21 +155,21 @@ namespace UICatalog.Scenarios { { miMultiline.Checked = !miMultiline.Checked; SetMultilineStatusText (); - textViewTopLeft.Multiline = miMultiline.Checked; - textViewTopRight.Multiline = miMultiline.Checked; - textViewBottomLeft.Multiline = miMultiline.Checked; - textViewBottomRight.Multiline = miMultiline.Checked; - textViewCentered.Multiline = miMultiline.Checked; + textViewTopLeft.Multiline = (bool)miMultiline.Checked; + textViewTopRight.Multiline = (bool)miMultiline.Checked; + textViewBottomLeft.Multiline = (bool)miMultiline.Checked; + textViewBottomRight.Multiline = (bool)miMultiline.Checked; + textViewCentered.Multiline = (bool)miMultiline.Checked; } private void WordWrap () { miWrap.Checked = !miWrap.Checked; - textViewTopLeft.WordWrap = miWrap.Checked; - textViewTopRight.WordWrap = miWrap.Checked; - textViewBottomLeft.WordWrap = miWrap.Checked; - textViewBottomRight.WordWrap = miWrap.Checked; - textViewCentered.WordWrap = miWrap.Checked; + textViewTopLeft.WordWrap = (bool)miWrap.Checked; + textViewTopRight.WordWrap = (bool)miWrap.Checked; + textViewBottomLeft.WordWrap = (bool)miWrap.Checked; + textViewBottomRight.WordWrap = (bool)miWrap.Checked; + textViewCentered.WordWrap = (bool)miWrap.Checked; miWrap.Checked = textViewTopLeft.WordWrap; SetWrapStatusText (); } diff --git a/UICatalog/Scenarios/TreeViewFileSystem.cs b/UICatalog/Scenarios/TreeViewFileSystem.cs index 03441f2d4..be72c5bf8 100644 --- a/UICatalog/Scenarios/TreeViewFileSystem.cs +++ b/UICatalog/Scenarios/TreeViewFileSystem.cs @@ -164,7 +164,7 @@ namespace UICatalog.Scenarios { { Title = "Details"; Visible = true; - CanFocus = true; + CanFocus = true; } public FileSystemInfo FileInfo { @@ -251,7 +251,7 @@ namespace UICatalog.Scenarios { { miShowLines.Checked = !miShowLines.Checked; - treeViewFiles.Style.ShowBranchLines = miShowLines.Checked; + treeViewFiles.Style.ShowBranchLines = (bool)miShowLines.Checked; treeViewFiles.SetNeedsDisplay (); } @@ -270,14 +270,14 @@ namespace UICatalog.Scenarios { { miColoredSymbols.Checked = !miColoredSymbols.Checked; - treeViewFiles.Style.ColorExpandSymbol = miColoredSymbols.Checked; + treeViewFiles.Style.ColorExpandSymbol = (bool)miColoredSymbols.Checked; treeViewFiles.SetNeedsDisplay (); } private void InvertExpandableSymbols () { miInvertSymbols.Checked = !miInvertSymbols.Checked; - treeViewFiles.Style.InvertExpandSymbolColors = miInvertSymbols.Checked; + treeViewFiles.Style.InvertExpandSymbolColors = (bool)miInvertSymbols.Checked; treeViewFiles.SetNeedsDisplay (); } @@ -285,7 +285,7 @@ namespace UICatalog.Scenarios { { miFullPaths.Checked = !miFullPaths.Checked; - if (miFullPaths.Checked) { + if (miFullPaths.Checked == true) { treeViewFiles.AspectGetter = (f) => f.FullName; } else { treeViewFiles.AspectGetter = (f) => f.Name; @@ -296,17 +296,17 @@ namespace UICatalog.Scenarios { private void SetLeaveLastRow () { miLeaveLastRow.Checked = !miLeaveLastRow.Checked; - treeViewFiles.Style.LeaveLastRow = miLeaveLastRow.Checked; + treeViewFiles.Style.LeaveLastRow = (bool)miLeaveLastRow.Checked; } private void SetCursor () { miCursor.Checked = !miCursor.Checked; - treeViewFiles.DesiredCursorVisibility = miCursor.Checked ? CursorVisibility.Default : CursorVisibility.Invisible; + treeViewFiles.DesiredCursorVisibility = miCursor.Checked == true ? CursorVisibility.Default : CursorVisibility.Invisible; } private void SetMultiSelect () { miMultiSelect.Checked = !miMultiSelect.Checked; - treeViewFiles.MultiSelect = miMultiSelect.Checked; + treeViewFiles.MultiSelect = (bool)miMultiSelect.Checked; } @@ -319,7 +319,7 @@ namespace UICatalog.Scenarios { miCustomColors.Checked = !miCustomColors.Checked; - if (miCustomColors.Checked) { + if (miCustomColors.Checked == true) { treeViewFiles.ColorGetter = (m) => { if (m is DirectoryInfo && m.Attributes.HasFlag (FileAttributes.Hidden)) return hidden; if (m is FileInfo && m.Attributes.HasFlag (FileAttributes.Hidden)) return hidden; diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 14cd47ece..cdae57037 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -178,7 +178,7 @@ namespace UICatalog { "About UI Catalog", () => MessageBox.Query ("About UI Catalog", _aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A), }), }); - + Capslock = new StatusItem (Key.CharMask, "Caps", null); Numlock = new StatusItem (Key.CharMask, "Num", null); Scrolllock = new StatusItem (Key.CharMask, "Scroll", null); @@ -330,7 +330,7 @@ namespace UICatalog { miIsMouseDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMouseDisabled.Title.ToString ().Substring (1, 1) [0]; miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked; miIsMouseDisabled.Action += () => { - miIsMouseDisabled.Checked = Application.IsMouseDisabled = !miIsMouseDisabled.Checked; + miIsMouseDisabled.Checked = Application.IsMouseDisabled = (bool)!miIsMouseDisabled.Checked; }; menuItems.Add (miIsMouseDisabled); @@ -363,7 +363,7 @@ namespace UICatalog { miHeightAsBuffer.CheckType |= MenuItemCheckStyle.Checked; miHeightAsBuffer.Action += () => { miHeightAsBuffer.Checked = !miHeightAsBuffer.Checked; - Application.HeightAsBuffer = miHeightAsBuffer.Checked; + Application.HeightAsBuffer = (bool)miHeightAsBuffer.Checked; }; menuItems.Add (miHeightAsBuffer); @@ -392,10 +392,10 @@ namespace UICatalog { } item.Action += () => { var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off); - if (item.Title == t && !item.Checked) { + if (item.Title == t && item.Checked == false) { _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler); item.Checked = true; - } else if (item.Title == t && item.Checked) { + } else if (item.Title == t && item.Checked == true) { _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler); item.Checked = false; } else { diff --git a/UnitTests/Menus/MenuTests.cs b/UnitTests/Menus/MenuTests.cs index b82450c9c..1f822c2cc 100644 --- a/UnitTests/Menus/MenuTests.cs +++ b/UnitTests/Menus/MenuTests.cs @@ -1734,5 +1734,96 @@ Edit │ │ └──────────────────────────────────────┘", output); } + + [Fact, AutoInitShutdown] + public void AllowNullChecked_Get_Set () + { + MenuItem mi = new MenuItem ("Check this out 你", "", null) { + CheckType = MenuItemCheckStyle.Checked + }; + mi.Action = mi.ToggleChecked; + var menu = new MenuBar (new MenuBarItem [] { + new MenuBarItem("Nullable Checked",new MenuItem [] { + mi + }) + }); + new CheckBox (); + var top = Application.Top; + top.Add (menu); + Application.Begin (top); + + Assert.False (mi.Checked); + Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ()))); + Assert.True (menu.openMenu.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()))); + Application.MainLoop.MainIteration (); + Assert.True (mi.Checked); + Assert.True (menu.MouseEvent (new MouseEvent () { + X = 0, + Y = 0, + Flags = MouseFlags.Button1Pressed, + View = menu + })); + Assert.True (menu.openMenu.MouseEvent (new MouseEvent () { + X = 0, + Y = 1, + Flags = MouseFlags.Button1Clicked, + View = menu.openMenu + })); + Application.MainLoop.MainIteration (); + Assert.False (mi.Checked); + + mi.AllowNullChecked = true; + Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ()))); + Assert.True (menu.openMenu.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()))); + Application.MainLoop.MainIteration (); + Assert.Null (mi.Checked); + Assert.True (menu.MouseEvent (new MouseEvent () { + X = 0, + Y = 0, + Flags = MouseFlags.Button1Pressed, + View = menu + })); + Application.Refresh (); + TestHelpers.AssertDriverContentsWithFrameAre (@" + Nullable Checked +┌──────────────────────┐ +│ ⍰ Check this out 你 │ +└──────────────────────┘", output); + Assert.True (menu.openMenu.MouseEvent (new MouseEvent () { + X = 0, + Y = 1, + Flags = MouseFlags.Button1Clicked, + View = menu.openMenu + })); + Application.MainLoop.MainIteration (); + Assert.True (mi.Checked); + Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ()))); + Assert.True (menu.openMenu.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()))); + Application.MainLoop.MainIteration (); + Assert.False (mi.Checked); + Assert.True (menu.MouseEvent (new MouseEvent () { + X = 0, + Y = 0, + Flags = MouseFlags.Button1Pressed, + View = menu + })); + Assert.True (menu.openMenu.MouseEvent (new MouseEvent () { + X = 0, + Y = 1, + Flags = MouseFlags.Button1Clicked, + View = menu.openMenu + })); + Application.MainLoop.MainIteration (); + Assert.Null (mi.Checked); + + mi.AllowNullChecked = false; + Assert.False (mi.Checked); + + mi.CheckType = MenuItemCheckStyle.NoCheck; + Assert.Throws (mi.ToggleChecked); + + mi.CheckType = MenuItemCheckStyle.Radio; + Assert.Throws (mi.ToggleChecked); + } } }