From e6aeada48004cad0d00242faa7dfc25fbe85c265 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 8 Dec 2024 17:37:28 -0800 Subject: [PATCH] Tweaked HexView --- Terminal.Gui/Input/Mouse/MouseBindings.cs | 4 +- Terminal.Gui/Views/ColorPicker.16.cs | 2 +- Terminal.Gui/Views/HexView.cs | 59 +++++++++-------------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/Terminal.Gui/Input/Mouse/MouseBindings.cs b/Terminal.Gui/Input/Mouse/MouseBindings.cs index 8b3f9814b..5dd5df04c 100644 --- a/Terminal.Gui/Input/Mouse/MouseBindings.cs +++ b/Terminal.Gui/Input/Mouse/MouseBindings.cs @@ -51,9 +51,9 @@ public class MouseBindings /// public void Add (MouseFlags mouseFlags, params Command [] commands) { - if (!Enum.IsDefined (typeof (MouseFlags), mouseFlags) || mouseFlags == MouseFlags.None) + if (mouseFlags == MouseFlags.None) { - throw new ArgumentException (@"Invalid MouseFlag", nameof (commands)); + throw new ArgumentException (@"Invalid MouseFlag", nameof (mouseFlags)); } if (commands.Length == 0) diff --git a/Terminal.Gui/Views/ColorPicker.16.cs b/Terminal.Gui/Views/ColorPicker.16.cs index 20420d26f..555ac29f7 100644 --- a/Terminal.Gui/Views/ColorPicker.16.cs +++ b/Terminal.Gui/Views/ColorPicker.16.cs @@ -188,7 +188,7 @@ public class ColorPicker16 : View AddCommand (Command.Select, (ctx) => { - bool set = false; + var set = false; if (ctx is CommandContext { Binding.MouseEventArgs: { } } mouseCommandContext) { diff --git a/Terminal.Gui/Views/HexView.cs b/Terminal.Gui/Views/HexView.cs index 1e47f2026..c2efbb7de 100644 --- a/Terminal.Gui/Views/HexView.cs +++ b/Terminal.Gui/Views/HexView.cs @@ -58,9 +58,7 @@ public class HexView : View, IDesignable _leftSideHasFocus = true; _firstNibble = true; - // PERF: Closure capture of 'this' creates a lot of overhead. - // BUG: Closure capture of 'this' may have unexpected results depending on how this is called. - // The above two comments apply to all the lambdas passed to all calls to AddCommand below. + AddCommand (Command.Select, HandleMouseClick); AddCommand (Command.Left, () => MoveLeft ()); AddCommand (Command.Right, () => MoveRight ()); AddCommand (Command.Down, () => MoveDown (BytesPerLine)); @@ -76,6 +74,8 @@ public class HexView : View, IDesignable Command.EndOfPage, () => MoveDown (BytesPerLine * (Viewport.Height - 1 - (int)(Address - Viewport.Y) / BytesPerLine)) ); + AddCommand (Command.ScrollDown, () => ScrollVertical (1)); + AddCommand (Command.ScrollUp, () => ScrollVertical (-1)); AddCommand (Command.DeleteCharLeft, () => true); AddCommand (Command.DeleteCharRight, () => true); AddCommand (Command.Insert, () => true); @@ -84,11 +84,8 @@ public class HexView : View, IDesignable KeyBindings.Add (Key.CursorRight, Command.Right); KeyBindings.Add (Key.CursorDown, Command.Down); KeyBindings.Add (Key.CursorUp, Command.Up); - KeyBindings.Add (Key.PageUp, Command.PageUp); - KeyBindings.Add (Key.PageDown, Command.PageDown); - KeyBindings.Add (Key.Home, Command.Start); KeyBindings.Add (Key.End, Command.End); KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.LeftStart); @@ -103,6 +100,12 @@ public class HexView : View, IDesignable KeyBindings.Remove (Key.Space); KeyBindings.Remove (Key.Enter); + // The Select handler deals with both single and double clicks + MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked, Command.Select); + MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Select); + MouseBindings.Add (MouseFlags.WheeledUp, Command.ScrollUp); + MouseBindings.Add (MouseFlags.WheeledDown, Command.ScrollDown); + SubviewsLaidOut += HexViewSubviewsLaidOut; } @@ -173,7 +176,7 @@ public class HexView : View, IDesignable if (offsetToNewCursor.X < 1) { - ScrollHorizontal(offsetToNewCursor.X); + ScrollHorizontal (offsetToNewCursor.X); } else if (offsetToNewCursor.X >= Viewport.Width) { @@ -347,20 +350,16 @@ public class HexView : View, IDesignable private int GetLeftSideStartColumn () { return AddressWidth == 0 ? 0 : AddressWidth + 1; } - /// - protected override bool OnMouseEvent (MouseEventArgs me) + private bool? HandleMouseClick (ICommandContext? commandContext) { - if (_source is null) + if (commandContext is not CommandContext { Binding.MouseEventArgs: { } } mouseCommandContext) { return false; } - if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) - && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) - && !me.Flags.HasFlag (MouseFlags.WheeledDown) - && !me.Flags.HasFlag (MouseFlags.WheeledUp)) + if (RaiseSelecting (commandContext) is true) { - return false; + return true; } if (!HasFocus) @@ -368,21 +367,7 @@ public class HexView : View, IDesignable SetFocus (); } - if (me.Flags == MouseFlags.WheeledDown) - { - ScrollVertical (1); - - return true; - } - - if (me.Flags == MouseFlags.WheeledUp) - { - ScrollVertical (-1); - - return true; - } - - if (me.Position.X < GetLeftSideStartColumn ()) + if (mouseCommandContext.Binding.MouseEventArgs.Position.X < GetLeftSideStartColumn ()) { return true; } @@ -391,14 +376,14 @@ public class HexView : View, IDesignable int blocksSize = blocks * HEX_COLUMN_WIDTH; int blocksRightOffset = GetLeftSideStartColumn () + blocksSize - 1; - if (me.Position.X > blocksRightOffset + BytesPerLine - 1) + if (mouseCommandContext.Binding.MouseEventArgs.Position.X > blocksRightOffset + BytesPerLine - 1) { return true; } - bool clickIsOnLeftSide = me.Position.X >= blocksRightOffset; - long lineStart = me.Position.Y * BytesPerLine + Viewport.Y * BytesPerLine; - int x = me.Position.X - GetLeftSideStartColumn () + 1; + bool clickIsOnLeftSide = mouseCommandContext.Binding.MouseEventArgs.Position.X >= blocksRightOffset; + long lineStart = mouseCommandContext.Binding.MouseEventArgs.Position.Y * BytesPerLine + Viewport.Y * BytesPerLine; + int x = mouseCommandContext.Binding.MouseEventArgs.Position.X - GetLeftSideStartColumn () + 1; int block = x / HEX_COLUMN_WIDTH; x -= block * 2; int empty = x % 3; @@ -413,14 +398,14 @@ public class HexView : View, IDesignable if (clickIsOnLeftSide) { - Address = Math.Min (lineStart + me.Position.X - blocksRightOffset, GetEditedSize ()); + Address = Math.Min (lineStart + mouseCommandContext.Binding.MouseEventArgs.Position.X - blocksRightOffset, GetEditedSize ()); } else { Address = Math.Min (lineStart + item, GetEditedSize ()); } - if (me.Flags == MouseFlags.Button1DoubleClicked) + if (mouseCommandContext.Binding.MouseEventArgs.Flags == MouseFlags.Button1DoubleClicked) { _leftSideHasFocus = !clickIsOnLeftSide; @@ -435,7 +420,7 @@ public class HexView : View, IDesignable SetNeedsDraw (); } - return true; + return false; } ///