mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Add comprehensive unit tests for WindowsKeyConverter - Implement 118 parallelizable unit tests for WindowsKeyConverter - Cover ToKey and ToKeyInfo methods with full bidirectional testing - Test basic characters, modifiers, special keys, function keys - Test VK_PACKET Unicode/IME input - Test OEM keys, NumPad keys, and lock states - Include round-trip conversion tests - All tests passing successfully Fixes #4389 * Rename `start` parameter to `viewportXOffset` for clarity The `start` parameter in several methods and interfaces has been renamed to `viewportXOffset` to better reflect its purpose as the horizontal offset of the viewport during string rendering. - Updated method signatures in `ListViewWithSelection` to use `viewportXOffset` instead of `start`, including default values. - Modified the `RenderUstr` method in `ListViewWithSelection` to use `viewportXOffset` for calculating the starting index. - Renamed the `start` parameter to `viewportXOffset` in the `IListDataSource` interface and updated its documentation. - Replaced all occurrences of `start` with `viewportXOffset` in the `ListWrapper<T>` class, including method calls and logic. - Updated the `RenderUstr` method in `ListWrapper<T>` to use `viewportXOffset` for substring calculations. - Adjusted the test method in `ListViewTests.cs` to reflect the parameter name change. These changes improve code readability and make the parameter's role in rendering logic more explicit. * Remove WindowsKeyConverterTests class that was added by mistake * Modernized ListView and IListDataSource - Tons of new unit tests Refactored `ListView` and `IListDataSource` to improve readability, maintainability, and functionality. Introduced `ListWrapper<T>` as a default implementation of `IListDataSource` for easier integration with standard collections. Enhanced `ListView` with better handling of marking, selection, and scrolling. Replaced `viewportXOffset` with `viewportX` for horizontal scrolling. Added `EnsureSelectedItemVisible` to maintain visibility of the selected item. Updated `IListDataSource` with detailed XML documentation and added `SuspendCollectionChangedEvent` for bulk updates. Improved null safety with nullable reference types. Added comprehensive unit tests for `ListWrapper<T>` and `IListDataSource` to ensure robustness. Modernized the codebase with C# features like expression-bodied members and pattern matching. Fixed bugs related to `SelectedItem` validation and rendering artifacts. * Improve index validation in ComboBox and ListView Enhance robustness by adding stricter checks for valid indices in ComboBox and ListView. Updated conditions in the `_listview.SelectedItemChanged` event handler to ensure `e.Item` is non-negative before accessing `_searchSet`. Modified the `SetValue` method to use `e.Item` instead of `_listview.SelectedItem`. In ListView, updated the `OnSelectedChanged` method to validate that `SelectedItem` is non-negative (`>= 0`) before accessing the `Source` list. These changes prevent potential out-of-range errors and improve code safety. * Refactor and enhance test coverage across modules Refactored and added new tests to improve coverage, readability, and consistency across multiple test files. Key changes include: - **ShortcutTests.cs**: Added tests for `BindKeyToApplication` and removed redundant tests. - **SourcesManagerTests.cs**: Renamed `Update_*` tests to `Load_*` for clarity. - **ArrangementTests.cs**: Reintroduced `MouseGrabHandler` tests, added `ViewArrangement` flag tests, and improved structure. - **NeedsDrawTests.cs**: Replaced `Application.Screen.Size` with fixed dimensions for better isolation. - **DimAutoTests.cs**: Updated layout tests to use fixed dimensions. - **FrameTests.cs**: Standardized object initialization and validated frame behavior. - **SubViewTests.cs**: Improved formatting and modernized event handling. - **NumericUpDownTests.cs**: Decoupled layout tests from screen size. General improvements: - Enhanced formatting and removed redundant tests. - Added comments for clarity. - Introduced `ITestOutputHelper` for better debugging in `ArrangementTests`. * Refactor to use nullable types for better null safety Enabled nullable reference types across the codebase to improve null safety and prevent potential null reference issues. Refactored `SelectedItem` and related properties from `int` to `int?` to represent no selection with `null` instead of `-1`. Updated logic, event arguments, and method signatures to handle nullable values consistently. Simplified object initialization using modern C# syntax and improved code readability with interpolated strings. Added null checks and early returns to prevent runtime errors. Enhanced error handling by throwing `ArgumentOutOfRangeException` for invalid values. Updated tests to reflect the changes, replacing assertions for `-1` with `null` and ensuring proper handling of nullable values. Cleaned up redundant code and improved formatting for better maintainability. * on` functionality has been deprecated, refactored, or removed from the `Shortcut` class. * Refactor: Transition to instance-based architecture Updated `Run-LocalCoverage.ps1` to increase `--blame-hang-timeout` from 10s to 60s. Improved null safety in `GuiTestContext` by adding null-conditional operators. Commented out problematic code in `SetupFakeApplicationAttribute.cs` to prevent test hangs. Excluded `ViewBase` files from `UnitTests.Parallelizable.csproj` and removed redundant folder declarations. Simplified event handling in `IListDataSourceTests.cs` and updated `ListViewTests.cs` to use nullable reference types. Enhanced documentation to emphasize the transition to an instance-based application architecture. Updated examples in `application.md`, `multitasking.md`, and `navigation.md` to reflect the use of `Application.Create()` and `View.App`. Clarified the obsolescence of the static `Application` class. Revised table of contents in `toc.yml` to include new sections like "Application Deep Dive" and "Scheme Deep Dive." Added `dotnet-tools.json` for tool configuration. These changes improve maintainability, testability, and alignment with modern C# practices. * Refactor ListViewTests to use Terminal.Gui framework The `ListViewTests` class has been refactored to replace the `AutoInitShutdown` attribute with explicit application lifecycle management using `IApplication` and `app.Init()` from the `Terminal.Gui` framework. Key changes include: - Rewriting tests to use `Terminal.Gui`'s application lifecycle. - Adding a private `_output` field for logging test output via `ITestOutputHelper`. - Updating `DriverAssert.AssertDriverContentsWithFrameAre` to include `app.Driver` for UI verification. - Rewriting tests like `Clicking_On_Border_Is_Ignored`, `EnsureSelectedItemVisible_SelectedItem`, and others to align with the new framework. - Adding explicit calls to `app.Shutdown()` for proper cleanup. - Enabling nullable reference types with `#nullable enable`. - Updating `using` directives and `namespace` to reflect the new structure. These changes improve test maintainability, compatibility, and diagnostics. * Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Examples/UICatalog/UICatalogTop.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/Views/ListWrapper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/Views/ListWrapper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Updated the `SetMark` method to return `Source.IsMarked(SelectedItem.Value)` for consistency and removed an outdated comment questioning its correctness. Enhanced the exception message in the `SelectedItem` property setter to provide clearer guidance when the value is out of range. * Add comprehensive ListView behavior test coverage Added multiple test methods to validate `ListView` behavior: - `Vertical_ScrollBar_Hides_And_Shows_As_Needed`: Ensures the vertical scrollbar auto-hides/shows based on content height. - `Mouse_Wheel_Scrolls`: Verifies vertical scrolling with the mouse wheel updates `TopItem`. - `SelectedItem_With_Source_Null_Does_Nothing`: Confirms no exceptions occur when setting `SelectedItem` with a `null` source. - `Horizontal_Scroll`: Tests horizontal scrolling, including programmatic and mouse wheel interactions, ensuring `LeftItem` updates correctly. - `SetSourceAsync_SetsSource`: Validates the asynchronous `SetSourceAsync` method updates the source and item count. - `AllowsMultipleSelection_Set_To_False_Unmarks_All_But_Selected`: Ensures disabling multiple selection unmarks all but the selected item. - `Source_CollectionChanged_Remove`: Confirms `SelectedItem` and source count update correctly when items are removed from the source collection. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
677 lines
26 KiB
C#
677 lines
26 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
|
|
namespace UICatalog.Scenarios;
|
|
|
|
[ScenarioMetadata ("Dynamic StatusBar", "Demonstrates how to add and remove a StatusBar and change items dynamically.")]
|
|
[ScenarioCategory ("Arrangement")]
|
|
public class DynamicStatusBar : Scenario
|
|
{
|
|
public override void Main ()
|
|
{
|
|
Application.Init ();
|
|
Application.Run<DynamicStatusBarSample> ().Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
public class Binding
|
|
{
|
|
private readonly PropertyInfo _sourceBindingProperty;
|
|
private readonly object _sourceDataContext;
|
|
private readonly IValueConverter _valueConverter;
|
|
|
|
public Binding (
|
|
View source,
|
|
string sourcePropertyName,
|
|
View target,
|
|
string targetPropertyName,
|
|
IValueConverter valueConverter = null
|
|
)
|
|
{
|
|
Target = target;
|
|
Source = source;
|
|
SourcePropertyName = sourcePropertyName;
|
|
TargetPropertyName = targetPropertyName;
|
|
_sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
|
|
_sourceBindingProperty = _sourceDataContext.GetType ().GetProperty (SourcePropertyName);
|
|
_valueConverter = valueConverter;
|
|
UpdateTarget ();
|
|
|
|
var notifier = (INotifyPropertyChanged)_sourceDataContext;
|
|
|
|
if (notifier != null)
|
|
{
|
|
notifier.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == SourcePropertyName)
|
|
{
|
|
UpdateTarget ();
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
public View Source { get; }
|
|
public string SourcePropertyName { get; }
|
|
public View Target { get; }
|
|
public string TargetPropertyName { get; }
|
|
|
|
private void UpdateTarget ()
|
|
{
|
|
try
|
|
{
|
|
object sourceValue = _sourceBindingProperty.GetValue (_sourceDataContext);
|
|
|
|
if (sourceValue == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
object finalValue = _valueConverter?.Convert (sourceValue) ?? sourceValue;
|
|
|
|
PropertyInfo targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
|
|
targetProperty.SetValue (Target, finalValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
|
|
}
|
|
}
|
|
}
|
|
|
|
public class DynamicStatusBarDetails : FrameView
|
|
{
|
|
private Shortcut _statusItem;
|
|
|
|
public DynamicStatusBarDetails (Shortcut statusItem = null) : this ()
|
|
{
|
|
_statusItem = statusItem;
|
|
Title = statusItem == null ? "Adding New StatusBar Item." : "Editing StatusBar Item.";
|
|
}
|
|
|
|
public DynamicStatusBarDetails ()
|
|
{
|
|
var lblTitle = new Label { Y = 1, Text = "Title:" };
|
|
base.Add (lblTitle);
|
|
|
|
TextTitle = new TextField { X = Pos.Right (lblTitle) + 4, Y = Pos.Top (lblTitle), Width = Dim.Fill () };
|
|
base.Add (TextTitle);
|
|
|
|
var lblAction = new Label { X = Pos.Left (lblTitle), Y = Pos.Bottom (lblTitle) + 1, Text = "Action:" };
|
|
base.Add (lblAction);
|
|
|
|
TextAction = new TextView
|
|
{
|
|
X = Pos.Left (TextTitle), Y = Pos.Top (lblAction), Width = Dim.Fill (), Height = 5
|
|
};
|
|
base.Add (TextAction);
|
|
|
|
var lblShortcut = new Label
|
|
{
|
|
X = Pos.Left (lblTitle), Y = Pos.Bottom (TextAction) + 1, Text = "Shortcut:"
|
|
};
|
|
base.Add (lblShortcut);
|
|
|
|
TextShortcut = new TextField
|
|
{
|
|
X = Pos.X (TextAction), Y = Pos.Y (lblShortcut), Width = Dim.Fill (), ReadOnly = true
|
|
};
|
|
|
|
TextShortcut.KeyDown += (s, e) =>
|
|
{
|
|
TextShortcut.Text = e.ToString ();
|
|
|
|
};
|
|
base.Add (TextShortcut);
|
|
|
|
var btnShortcut = new Button
|
|
{
|
|
X = Pos.X (lblShortcut), Y = Pos.Bottom (TextShortcut) + 1, Text = "Clear Shortcut"
|
|
};
|
|
btnShortcut.Accepting += (s, e) => { TextShortcut.Text = ""; };
|
|
base.Add (btnShortcut);
|
|
}
|
|
|
|
public TextView TextAction { get; }
|
|
public TextField TextShortcut { get; }
|
|
public TextField TextTitle { get; }
|
|
public Action CreateAction (DynamicStatusItem item) { return () => MessageBox.ErrorQuery (item.Title, item.Action, "Ok"); }
|
|
|
|
public void EditStatusItem (Shortcut statusItem)
|
|
{
|
|
if (statusItem == null)
|
|
{
|
|
Enabled = false;
|
|
CleanEditStatusItem ();
|
|
|
|
return;
|
|
}
|
|
|
|
Enabled = true;
|
|
_statusItem = statusItem;
|
|
TextTitle.Text = statusItem?.Title ?? "";
|
|
|
|
TextAction.Text = statusItem != null && statusItem.Action != null
|
|
? GetTargetAction (statusItem.Action)
|
|
: string.Empty;
|
|
|
|
TextShortcut.Text = statusItem.Key;
|
|
}
|
|
|
|
public DynamicStatusItem EnterStatusItem ()
|
|
{
|
|
var valid = false;
|
|
|
|
if (_statusItem == null)
|
|
{
|
|
var m = new DynamicStatusItem ();
|
|
TextTitle.Text = m.Title;
|
|
TextAction.Text = m.Action;
|
|
}
|
|
else
|
|
{
|
|
EditStatusItem (_statusItem);
|
|
}
|
|
|
|
var btnOk = new Button { IsDefault = true, Text = "OK" };
|
|
|
|
btnOk.Accepting += (s, e) =>
|
|
{
|
|
if (string.IsNullOrEmpty (TextTitle.Text))
|
|
{
|
|
MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
|
|
}
|
|
else
|
|
{
|
|
|
|
valid = true;
|
|
Application.RequestStop ();
|
|
}
|
|
};
|
|
var btnCancel = new Button { Text = "Cancel" };
|
|
|
|
btnCancel.Accepting += (s, e) =>
|
|
{
|
|
TextTitle.Text = string.Empty;
|
|
Application.RequestStop ();
|
|
};
|
|
var dialog = new Dialog { Title = "Enter the menu details.", Buttons = [btnOk, btnCancel], Height = Dim.Auto (DimAutoStyle.Content, 17, Application.Screen.Height) };
|
|
|
|
Width = Dim.Fill ();
|
|
Height = Dim.Fill () - 2;
|
|
dialog.Add (this);
|
|
TextTitle.SetFocus ();
|
|
TextTitle.CursorPosition = TextTitle.Text.Length;
|
|
Application.Run (dialog);
|
|
dialog.Dispose ();
|
|
|
|
return valid
|
|
? new DynamicStatusItem
|
|
{
|
|
Title = TextTitle.Text, Action = TextAction.Text, Shortcut = TextShortcut.Text
|
|
}
|
|
: null;
|
|
}
|
|
|
|
private void CleanEditStatusItem ()
|
|
{
|
|
TextTitle.Text = "";
|
|
TextAction.Text = "";
|
|
TextShortcut.Text = "";
|
|
}
|
|
|
|
private string GetTargetAction (Action action)
|
|
{
|
|
object me = action.Target;
|
|
|
|
if (me == null)
|
|
{
|
|
throw new ArgumentException ();
|
|
}
|
|
|
|
var v = new object ();
|
|
|
|
foreach (FieldInfo field in me.GetType ().GetFields ())
|
|
{
|
|
if (field.Name == "item")
|
|
{
|
|
v = field.GetValue (me);
|
|
}
|
|
}
|
|
|
|
return v == null || !(v is DynamicStatusItem item) ? string.Empty : item.Action;
|
|
}
|
|
}
|
|
|
|
public class DynamicStatusBarSample : Window
|
|
{
|
|
private readonly ListView _lstItems;
|
|
private Shortcut _currentEditStatusItem;
|
|
private int _currentSelectedStatusBar = -1;
|
|
private Shortcut _currentStatusItem;
|
|
private StatusBar _statusBar;
|
|
|
|
public DynamicStatusBarSample ()
|
|
{
|
|
DataContext = new DynamicStatusItemModel ();
|
|
|
|
Title = $"{Application.QuitKey} to Quit - Scenario: Dynamic StatusBar";
|
|
|
|
var frmStatusBar = new FrameView
|
|
{
|
|
Y = 5, Width = Dim.Percent (50), Height = Dim.Fill (2), Title = "Items:"
|
|
};
|
|
|
|
var btnAddStatusBar = new Button { Y = 1, Text = "Add a StatusBar" };
|
|
frmStatusBar.Add (btnAddStatusBar);
|
|
|
|
var btnRemoveStatusBar = new Button { Y = 1, Text = "Remove a StatusBar" };
|
|
|
|
btnRemoveStatusBar.X = Pos.AnchorEnd ();
|
|
frmStatusBar.Add (btnRemoveStatusBar);
|
|
|
|
var btnAdd = new Button { Y = Pos.Top (btnRemoveStatusBar) + 2, Text = " Add " };
|
|
btnAdd.X = Pos.AnchorEnd ();
|
|
frmStatusBar.Add (btnAdd);
|
|
|
|
_lstItems = new ListView
|
|
{
|
|
SchemeName = "Dialog",
|
|
Y = Pos.Top (btnAddStatusBar) + 2,
|
|
Width = Dim.Fill () - Dim.Width (btnAdd) - 1,
|
|
Height = Dim.Fill (),
|
|
Source = new ListWrapper<DynamicStatusItemList> ([])
|
|
};
|
|
frmStatusBar.Add (_lstItems);
|
|
|
|
var btnRemove = new Button { X = Pos.Left (btnAdd), Y = Pos.Top (btnAdd) + 1, Text = "Remove" };
|
|
frmStatusBar.Add (btnRemove);
|
|
|
|
var btnUp = new Button { X = Pos.Right (_lstItems) + 2, Y = Pos.Top (btnRemove) + 2, Text = Glyphs.UpArrow.ToString () };
|
|
frmStatusBar.Add (btnUp);
|
|
|
|
var btnDown = new Button { X = Pos.Right (_lstItems) + 2, Y = Pos.Top (btnUp) + 1, Text = Glyphs.DownArrow.ToString () };
|
|
frmStatusBar.Add (btnDown);
|
|
|
|
Add (frmStatusBar);
|
|
|
|
var frmStatusBarDetails = new DynamicStatusBarDetails
|
|
{
|
|
X = Pos.Right (frmStatusBar),
|
|
Y = Pos.Top (frmStatusBar),
|
|
Width = Dim.Fill (),
|
|
Height = Dim.Fill (4),
|
|
Title = "Shortcut Details:"
|
|
};
|
|
Add (frmStatusBarDetails);
|
|
|
|
btnUp.Accepting += (s, e) =>
|
|
{
|
|
if (_lstItems.SelectedItem is null)
|
|
{
|
|
return;
|
|
}
|
|
int i = _lstItems.SelectedItem.Value;
|
|
|
|
Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
|
|
|
|
if (statusItem != null)
|
|
{
|
|
Shortcut [] items = _statusBar.SubViews.OfType<Shortcut> ().ToArray ();
|
|
|
|
if (i > 0)
|
|
{
|
|
items [i] = items [i - 1];
|
|
items [i - 1] = statusItem;
|
|
DataContext.Items [i] = DataContext.Items [i - 1];
|
|
|
|
DataContext.Items [i - 1] =
|
|
new DynamicStatusItemList (statusItem.Title, statusItem);
|
|
_lstItems.SelectedItem = i - 1;
|
|
_statusBar.SetNeedsDraw ();
|
|
}
|
|
}
|
|
};
|
|
|
|
btnDown.Accepting += (s, e) =>
|
|
{
|
|
if (_lstItems.SelectedItem is null)
|
|
{
|
|
return;
|
|
}
|
|
int i = _lstItems.SelectedItem.Value;
|
|
|
|
Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
|
|
|
|
if (statusItem != null)
|
|
{
|
|
Shortcut [] items = _statusBar.SubViews.OfType<Shortcut> ().ToArray ();
|
|
|
|
if (i < items.Length - 1)
|
|
{
|
|
items [i] = items [i + 1];
|
|
items [i + 1] = statusItem;
|
|
DataContext.Items [i] = DataContext.Items [i + 1];
|
|
|
|
DataContext.Items [i + 1] =
|
|
new DynamicStatusItemList (statusItem.Title, statusItem);
|
|
_lstItems.SelectedItem = i + 1;
|
|
_statusBar.SetNeedsDraw ();
|
|
}
|
|
}
|
|
};
|
|
|
|
var btnOk = new Button
|
|
{
|
|
X = Pos.Right (frmStatusBar) + 20, Y = Pos.Bottom (frmStatusBarDetails), Text = "Ok"
|
|
};
|
|
Add (btnOk);
|
|
|
|
var btnCancel = new Button { X = Pos.Right (btnOk) + 3, Y = Pos.Top (btnOk), Text = "Cancel" };
|
|
btnCancel.Accepting += (s, e) => { SetFrameDetails (_currentEditStatusItem); };
|
|
Add (btnCancel);
|
|
|
|
_lstItems.SelectedItemChanged += (s, e) => { SetFrameDetails (); };
|
|
|
|
btnOk.Accepting += (s, e) =>
|
|
{
|
|
if (string.IsNullOrEmpty (frmStatusBarDetails.TextTitle.Text) && _currentEditStatusItem != null)
|
|
{
|
|
MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
|
|
}
|
|
else if (_currentEditStatusItem != null)
|
|
{
|
|
var statusItem = new DynamicStatusItem
|
|
{
|
|
Title = frmStatusBarDetails.TextTitle.Text,
|
|
Action = frmStatusBarDetails.TextAction.Text,
|
|
Shortcut = frmStatusBarDetails.TextShortcut.Text
|
|
};
|
|
|
|
if (_lstItems.SelectedItem is { } selectedItem)
|
|
{
|
|
UpdateStatusItem (_currentEditStatusItem, statusItem, selectedItem);
|
|
}
|
|
}
|
|
};
|
|
|
|
btnAdd.Accepting += (s, e) =>
|
|
{
|
|
//if (StatusBar == null)
|
|
//{
|
|
// MessageBox.ErrorQuery (
|
|
// "StatusBar Bar Error",
|
|
// "Must add a StatusBar first!",
|
|
// "Ok"
|
|
// );
|
|
// _btnAddStatusBar.SetFocus ();
|
|
|
|
// return;
|
|
//}
|
|
|
|
var frameDetails = new DynamicStatusBarDetails ();
|
|
DynamicStatusItem item = frameDetails.EnterStatusItem ();
|
|
|
|
if (item == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Shortcut newStatusItem = CreateNewStatusBar (item);
|
|
_currentSelectedStatusBar++;
|
|
_statusBar.AddShortcutAt (_currentSelectedStatusBar, newStatusItem);
|
|
DataContext.Items.Add (new DynamicStatusItemList (newStatusItem.Title, newStatusItem));
|
|
_lstItems.MoveDown ();
|
|
SetFrameDetails ();
|
|
};
|
|
|
|
btnRemove.Accepting += (s, e) =>
|
|
{
|
|
Shortcut statusItem = DataContext.Items.Count > 0
|
|
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
|
|
: null;
|
|
|
|
if (statusItem != null)
|
|
{
|
|
_statusBar.RemoveShortcut (_currentSelectedStatusBar);
|
|
statusItem.Dispose ();
|
|
DataContext.Items.RemoveAt (_lstItems.SelectedItem.Value);
|
|
|
|
if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1)
|
|
{
|
|
_lstItems.SelectedItem = _lstItems.Source.Count - 1;
|
|
}
|
|
|
|
_lstItems.SetNeedsDraw ();
|
|
SetFrameDetails ();
|
|
}
|
|
};
|
|
|
|
_lstItems.HasFocusChanging += (s, e) =>
|
|
{
|
|
Shortcut statusItem = DataContext.Items.Count > 0
|
|
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
|
|
: null;
|
|
SetFrameDetails (statusItem);
|
|
};
|
|
|
|
btnAddStatusBar.Accepting += (s, e) =>
|
|
{
|
|
if (_statusBar != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_statusBar = new StatusBar ();
|
|
Add (_statusBar);
|
|
};
|
|
|
|
btnRemoveStatusBar.Accepting += (s, e) =>
|
|
{
|
|
if (_statusBar == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Remove (_statusBar);
|
|
_statusBar.Dispose ();
|
|
_statusBar = null;
|
|
DataContext.Items = [];
|
|
_currentStatusItem = null;
|
|
_currentSelectedStatusBar = -1;
|
|
SetListViewSource (_currentStatusItem, true);
|
|
SetFrameDetails ();
|
|
};
|
|
|
|
SetFrameDetails ();
|
|
|
|
var ustringConverter = new UStringValueConverter ();
|
|
var listWrapperConverter = new ListWrapperConverter<DynamicStatusItemList> ();
|
|
|
|
var lstItems = new Binding (this, "Items", _lstItems, "Source", listWrapperConverter);
|
|
|
|
void SetFrameDetails (Shortcut statusItem = null)
|
|
{
|
|
Shortcut newStatusItem;
|
|
|
|
if (statusItem == null)
|
|
{
|
|
newStatusItem = DataContext.Items.Count > 0
|
|
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
|
|
: null;
|
|
}
|
|
else
|
|
{
|
|
newStatusItem = statusItem;
|
|
}
|
|
|
|
_currentEditStatusItem = newStatusItem;
|
|
frmStatusBarDetails.EditStatusItem (newStatusItem);
|
|
bool f = btnOk.Enabled == frmStatusBarDetails.Enabled;
|
|
|
|
if (!f)
|
|
{
|
|
btnOk.Enabled = frmStatusBarDetails.Enabled;
|
|
btnCancel.Enabled = frmStatusBarDetails.Enabled;
|
|
}
|
|
}
|
|
|
|
void SetListViewSource (Shortcut currentStatusItem, bool fill = false)
|
|
{
|
|
DataContext.Items = [];
|
|
Shortcut statusItem = currentStatusItem;
|
|
|
|
if (!fill)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (statusItem != null)
|
|
{
|
|
foreach (Shortcut si in _statusBar.SubViews.OfType<Shortcut> ())
|
|
{
|
|
DataContext.Items.Add (new DynamicStatusItemList (si.Title, si));
|
|
}
|
|
}
|
|
}
|
|
|
|
Shortcut CreateNewStatusBar (DynamicStatusItem item)
|
|
{
|
|
var newStatusItem = new Shortcut (item.Shortcut, item.Title, frmStatusBarDetails.CreateAction (item));
|
|
|
|
return newStatusItem;
|
|
}
|
|
|
|
void UpdateStatusItem (
|
|
Shortcut currentEditStatusItem,
|
|
DynamicStatusItem statusItem,
|
|
int index
|
|
)
|
|
{
|
|
_statusBar.SubViews.ElementAt (index).Title = statusItem.Title;
|
|
((Shortcut)_statusBar.SubViews.ElementAt (index)).Action = frmStatusBarDetails.CreateAction (statusItem);
|
|
((Shortcut)_statusBar.SubViews.ElementAt (index)).Key = statusItem.Shortcut;
|
|
|
|
if (DataContext.Items.Count == 0)
|
|
{
|
|
DataContext.Items.Add (
|
|
new DynamicStatusItemList (
|
|
currentEditStatusItem.Title,
|
|
currentEditStatusItem
|
|
)
|
|
);
|
|
}
|
|
|
|
DataContext.Items [index] = new DynamicStatusItemList (
|
|
currentEditStatusItem.Title,
|
|
currentEditStatusItem
|
|
);
|
|
SetFrameDetails (currentEditStatusItem);
|
|
}
|
|
|
|
//_frmStatusBarDetails.Initialized += (s, e) => _frmStatusBarDetails.Enabled = false;
|
|
}
|
|
|
|
public DynamicStatusItemModel DataContext { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public class DynamicStatusItem
|
|
{
|
|
public string Action { get; set; } = "";
|
|
public string Shortcut { get; set; }
|
|
public string Title { get; set; } = "New";
|
|
}
|
|
|
|
public class DynamicStatusItemList
|
|
{
|
|
public DynamicStatusItemList () { }
|
|
|
|
public DynamicStatusItemList (string title, Shortcut statusItem)
|
|
{
|
|
Title = title;
|
|
Shortcut = statusItem;
|
|
}
|
|
|
|
public Shortcut Shortcut { get; set; }
|
|
public string Title { get; set; }
|
|
public override string ToString () { return $"{Title}, {Shortcut.Key}"; }
|
|
}
|
|
|
|
public class DynamicStatusItemModel : INotifyPropertyChanged
|
|
{
|
|
private ObservableCollection<DynamicStatusItemList> _items;
|
|
private string _statusBar;
|
|
public DynamicStatusItemModel () { Items = []; }
|
|
|
|
public ObservableCollection<DynamicStatusItemList> Items
|
|
{
|
|
get => _items;
|
|
set
|
|
{
|
|
if (value == _items)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_items = value;
|
|
|
|
PropertyChanged?.Invoke (
|
|
this,
|
|
new PropertyChangedEventArgs (GetPropertyName ())
|
|
);
|
|
}
|
|
}
|
|
|
|
public string StatusBar
|
|
{
|
|
get => _statusBar;
|
|
set
|
|
{
|
|
if (value == _statusBar)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_statusBar = value;
|
|
|
|
PropertyChanged?.Invoke (
|
|
this,
|
|
new PropertyChangedEventArgs (GetPropertyName ())
|
|
);
|
|
}
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
public string GetPropertyName ([CallerMemberName] string propertyName = null) { return propertyName; }
|
|
}
|
|
|
|
public interface IValueConverter
|
|
{
|
|
object Convert (object value, object parameter = null);
|
|
}
|
|
|
|
public class ListWrapperConverter<T> : IValueConverter
|
|
{
|
|
public object Convert (object value, object parameter = null) { return new ListWrapper<T> ((ObservableCollection<T>)value); }
|
|
}
|
|
|
|
public class UStringValueConverter : IValueConverter
|
|
{
|
|
public object Convert (object value, object parameter = null)
|
|
{
|
|
byte [] data = Encoding.ASCII.GetBytes (value.ToString () ?? string.Empty);
|
|
|
|
return StringExtensions.ToString (data);
|
|
}
|
|
}
|
|
}
|