From 10e39117bc3957fc9e1e748f704be5568a7b1299 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 25 Mar 2021 15:53:10 +0100 Subject: [PATCH] Table widget columns resize with mouse --- Crow/src/2d/Measure.cs | 42 +++++++++----------------------- Crow/src/Widgets/Label.cs | 2 +- Crow/src/Widgets/TableRow.cs | 47 ++++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/Crow/src/2d/Measure.cs b/Crow/src/2d/Measure.cs index 18ab62ce..b5f475f5 100644 --- a/Crow/src/2d/Measure.cs +++ b/Crow/src/2d/Measure.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2020 Jean-Philippe Bruyère +// Copyright (c) 2013-2021 Jean-Philippe Bruyère // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) @@ -13,7 +13,7 @@ namespace Crow /// /// Measure class allow proportional sizes as well as stretched and fit on content. /// - public struct Measure + public struct Measure : IEquatable { /// /// Integer value of the measure @@ -52,38 +52,20 @@ namespace Crow /// public bool IsRelativeToParent { get { return Value >= 0 && Units == Unit.Percent; }} #region Operators - public static implicit operator int(Measure m){ - return m.Value; - } - public static implicit operator Measure(int i){ - return new Measure(i); - } - public static implicit operator string(Measure m){ - return m.ToString(); - } - public static implicit operator Measure(string s){ - return Measure.Parse(s); - } + public static implicit operator int(Measure m) => m.Value; + public static implicit operator Measure(int i) => new Measure(i); + public static implicit operator string(Measure m) => m.ToString(); + public static implicit operator Measure(string s) => Measure.Parse(s); - public static bool operator ==(Measure m1, Measure m2){ - return m1.Value == m2.Value && m1.Units == m2.Units; - } - public static bool operator !=(Measure m1, Measure m2){ - return !(m1.Value == m2.Value && m1.Units == m2.Units); - } + public static bool operator ==(Measure m1, Measure m2) => m1.Equals (m2); + public static bool operator !=(Measure m1, Measure m2) => !m1.Equals (m2); #endregion + public bool Equals(Measure other) => Value == other.Value && Units == other.Units; + #region Object overrides - public override int GetHashCode () - { - return Value.GetHashCode (); - } - public override bool Equals (object obj) - { - return (obj == null || obj.GetType() != typeof(Measure)) ? - false : - this == (Measure)obj; - } + public override int GetHashCode () => HashCode.Combine (Value, Units); + public override bool Equals (object obj) => obj is Measure m ? Equals (m) : false; public override string ToString () { return Units == Unit.Inherit ? "Inherit" : diff --git a/Crow/src/Widgets/Label.cs b/Crow/src/Widgets/Label.cs index 9d6af8b8..f99908fb 100644 --- a/Crow/src/Widgets/Label.cs +++ b/Crow/src/Widgets/Label.cs @@ -675,7 +675,7 @@ namespace Crow { base.onMouseMove (sender, e); - updateHoverLocation (e.Position - ScreenCoordinates (Slot).TopLeft - ClientRectangle.TopLeft); + updateHoverLocation (ScreenPointToLocal (e.Position)); if (HasFocus && IFace.IsDown (MouseButton.Left)) { CurrentLoc = hoverLoc; diff --git a/Crow/src/Widgets/TableRow.cs b/Crow/src/Widgets/TableRow.cs index 27547718..a251b6f9 100644 --- a/Crow/src/Widgets/TableRow.cs +++ b/Crow/src/Widgets/TableRow.cs @@ -1,9 +1,11 @@ -using System.Linq; -// Copyright (c) 2019-2021 Jean-Philippe Bruyère +// Copyright (c) 2019-2021 Jean-Philippe Bruyère // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.ComponentModel; +using System.Linq; +using Crow.Cairo; +using Glfw; namespace Crow { @@ -142,5 +144,46 @@ namespace Crow base.OnChildLayoutChanges (sender, arg); }*/ + int splitIndex = -1; + const int minColumnSize = 10; + public override void onMouseMove(object sender, MouseMoveEventArgs e) + { + if (Spacing > 0 && Table != null && Table.Children.Count > 0) { + Point m = ScreenPointToLocal (e.Position); + if (IFace.IsDown (Glfw.MouseButton.Left) && splitIndex >= 0) { + TableRow firstRow = Table.Children[0] as TableRow; + Rectangle cb = ClientRectangle; + int splitPos = (int)(0.5 * Spacing + m.X); + if (splitPos > firstRow.Children[splitIndex].Slot.Left + minColumnSize && splitPos < firstRow.Children[splitIndex+1].Slot.Right - minColumnSize) { + Table.Columns[splitIndex+1].Width = firstRow.Children[splitIndex+1].Slot.Right - splitPos; + splitPos -= Spacing; + Table.Columns[splitIndex].Width = splitPos - firstRow.Children[splitIndex].Slot.Left; + Table.RegisterForLayouting (LayoutingType.Width); + } + //Console.WriteLine ($"left:{firstRow.Children[splitIndex].Slot.Left} right:{firstRow.Children[splitIndex+1].Slot.Right} cb.X:{cb.X} splitPos:{splitPos} m:{m}"); + } else { + splitIndex = -1; + for (int i = 0; i < Children.Count - 1; i++) + { + Rectangle r = Children[i].Slot; + if (m.X >= r.Right) { + r = Children[i+1].Slot; + if (m.X <= r.Left && Table.Columns.Count - 1 > i ) { + Console.WriteLine ($"Set cursor Table row on mouse move. {m}"); + IFace.MouseCursor = MouseCursor.sb_h_double_arrow; + splitIndex = i; + e.Handled = true; + break; + } + } + } + if (splitIndex < 0) { + IFace.MouseCursor = MouseCursor.top_left_arrow; + Console.WriteLine ($"RESet cursor Table row on mouse move. {m}"); + } + } + } + base.onMouseMove(sender, e); + } } } -- 2.47.3