1+ namespace WinUI . TableView . Extensions ;
2+
3+ /// <summary>
4+ /// Provides extension methods for the TableViewCellSlotRange type.
5+ /// </summary>
6+ internal static class TableViewCellSlotRangeExtensions
7+ {
8+ /// <summary>
9+ /// Determines whether a specified cell slot is within the range.
10+ /// </summary>
11+ /// <param name="range">The TableViewCellSlotRange to check.</param>
12+ /// <param name="slot">The cell slot to check.</param>
13+ /// <returns>True if the slot is within the range; otherwise, false.</returns>
14+ public static bool IsInRange ( this TableViewCellSlotRange ? range , TableViewCellSlot slot )
15+ {
16+ if ( range is null || range . Length <= 0 ) return false ;
17+
18+ var minRow = Math . Min ( range . FirstRow , range . LastRow ) ;
19+ var maxRow = Math . Max ( range . FirstRow , range . LastRow ) ;
20+ var minColumn = Math . Min ( range . FirstColumn , range . LastColumn ) ;
21+ var maxColumn = Math . Max ( range . FirstColumn , range . LastColumn ) ;
22+
23+ return slot . Row >= minRow && slot . Row <= maxRow
24+ && slot . Column >= minColumn && slot . Column <= maxColumn ;
25+ }
26+
27+ /// <summary>
28+ /// Determines whether a specified row index is within the range.
29+ /// </summary>
30+ /// <param name="range">The TableViewCellSlotRange to check.</param>
31+ /// <param name="row">The row index to check.</param>
32+ /// <returns>True if the row index is within the range; otherwise, false.</returns>
33+ public static bool IsRowInRange ( this TableViewCellSlotRange ? range , int row )
34+ {
35+ return range ? . Length > 0
36+ && row >= Math . Min ( range . FirstRow , range . LastRow )
37+ && row <= Math . Max ( range . FirstRow , range . LastRow ) ;
38+ }
39+
40+ /// <summary>
41+ /// Determines whether a specified column index is within the range.
42+ /// </summary>
43+ /// <param name="range">The TableViewCellSlotRange to check.</param>
44+ /// <param name="column">The column index to check.</param>
45+ /// <returns>True if the column index is within the range; otherwise, false.</returns>
46+ public static bool IsColumnInRange ( this TableViewCellSlotRange ? range , int column )
47+ {
48+ return range ? . Length > 0
49+ && column >= Math . Min ( range . FirstColumn , range . LastColumn )
50+ && column <= Math . Max ( range . FirstColumn , range . LastColumn ) ;
51+ }
52+
53+ /// <summary>
54+ /// Determines whether the given cell slot range is valid within the TableView.
55+ /// </summary>
56+ /// <param name="range">The TableViewCellSlotRange to check.</param>
57+ /// <param name="tableView">The TableView to check against.</param>
58+ /// <returns>True if the cell slot range of TableView is valid; otherwise, false.</returns>
59+ public static bool IsValid ( this TableViewCellSlotRange range , TableView tableView )
60+ {
61+ return range . FirstSlot . IsValid ( tableView ) && range . LastSlot . IsValid ( tableView ) ;
62+ }
63+
64+ /// <summary>
65+ /// Returns all cell slots contained within this range, enumerated row by row.
66+ /// </summary>
67+ public static IEnumerable < TableViewCellSlot > GetSlots ( this TableViewCellSlotRange range )
68+ {
69+ for ( var row = range . FirstRow ; row <= range . LastRow ; row ++ )
70+ {
71+ for ( var col = range . FirstColumn ; col <= range . LastColumn ; col ++ )
72+ {
73+ yield return new TableViewCellSlot ( row , col ) ;
74+ }
75+ }
76+ }
77+
78+ /// <summary>
79+ /// Determines whether a specific cell slot falls within this range.
80+ /// </summary>
81+ public static bool Contains ( this TableViewCellSlotRange range , int rowIndex , int columnIndex )
82+ {
83+ return rowIndex >= range . FirstRow && rowIndex <= range . LastRow &&
84+ columnIndex >= range . FirstColumn && columnIndex <= range . LastColumn ;
85+ }
86+
87+ /// <summary>
88+ /// Determines whether another TableViewCellSlotRange is completely contained within this range.
89+ /// </summary>
90+ public static bool Contains ( this TableViewCellSlotRange ? range , TableViewCellSlotRange ? other )
91+ {
92+ if ( range == null || other == null ) return false ;
93+
94+ return range . Contains ( other . FirstRow , other . FirstColumn ) &&
95+ range . Contains ( other . LastRow , other . LastColumn ) ;
96+ }
97+
98+ /// <summary>
99+ /// Determines whether another range intersects with this range.
100+ /// </summary>
101+ public static bool IntersectsWith ( this TableViewCellSlotRange range , TableViewCellSlotRange other )
102+ {
103+ if ( other == null ) return false ;
104+
105+ return range . FirstRow <= other . LastRow && range . LastRow >= other . FirstRow &&
106+ range . FirstColumn <= other . LastColumn && range . LastColumn >= other . FirstColumn ;
107+ }
108+
109+ /// <summary>
110+ /// Subtracts another range from this range and returns the resulting ranges.
111+ /// </summary>
112+ /// <param name="range">The range to subtract from.</param>
113+ /// <param name="other">The range to subtract.</param>
114+ /// <returns>An enumerable of resulting ranges after subtraction.</returns>
115+ public static IEnumerable < TableViewCellSlotRange > Subtract ( this TableViewCellSlotRange range , TableViewCellSlotRange other )
116+ {
117+ // No overlap.
118+ if ( ! range . IntersectsWith ( other ) )
119+ {
120+ yield return range ;
121+ yield break ;
122+ }
123+
124+ // Intersection rectangle.
125+ var top = Math . Max ( range . FirstRow , other . FirstRow ) ;
126+ var left = Math . Max ( range . FirstColumn , other . FirstColumn ) ;
127+ var bottom = Math . Min ( range . LastRow , other . LastRow ) ;
128+ var right = Math . Min ( range . LastColumn , other . LastColumn ) ;
129+
130+ // Top strip.
131+ if ( range . FirstRow < top )
132+ {
133+ yield return new TableViewCellSlotRange (
134+ range . FirstRow ,
135+ range . FirstColumn ,
136+ top - range . FirstRow ,
137+ range . Columns ) ;
138+ }
139+
140+ // Bottom strip.
141+ if ( bottom < range . LastRow )
142+ {
143+ yield return new TableViewCellSlotRange (
144+ bottom + 1 ,
145+ range . FirstColumn ,
146+ range . LastRow - bottom ,
147+ range . Columns ) ;
148+ }
149+
150+ // Left strip.
151+ if ( range . FirstColumn < left )
152+ {
153+ yield return new TableViewCellSlotRange (
154+ top ,
155+ range . FirstColumn ,
156+ bottom - top + 1 ,
157+ left - range . FirstColumn ) ;
158+ }
159+
160+ // Right strip.
161+ if ( right < range . LastColumn )
162+ {
163+ yield return new TableViewCellSlotRange (
164+ top ,
165+ right + 1 ,
166+ bottom - top + 1 ,
167+ range . LastColumn - right ) ;
168+ }
169+ }
170+
171+ /// <summary>
172+ /// Merges two TableViewCellSlotRanges into a single range that encompasses both.
173+ /// </summary>
174+ public static TableViewCellSlotRange Merge ( this TableViewCellSlotRange range , TableViewCellSlotRange other )
175+ {
176+ var firstRow = Math . Min ( range . FirstRow , other . FirstRow ) ;
177+ var firstColumn = Math . Min ( range . FirstColumn , other . FirstColumn ) ;
178+ var lastRow = Math . Max ( range . LastRow , other . LastRow ) ;
179+ var lastColumn = Math . Max ( range . LastColumn , other . LastColumn ) ;
180+
181+ return TableViewCellSlotRange . FromCoordinates (
182+ firstRow ,
183+ firstColumn ,
184+ lastRow ,
185+ lastColumn ) ;
186+ }
187+ }
0 commit comments