55using System . Windows . Forms ;
66using Vocup . Util ;
77
8- #nullable disable
9-
108namespace Vocup . Controls ;
119
1210public partial class VocabularyListView : UserControl
1311{
1412 private Size _imageBaseSize = new Size ( 16 , 16 ) ;
1513 private readonly int initialWidthImage = 20 ;
16- private readonly int initialWidthLastPracticed = 120 ;
14+ private readonly int initialWidthLastPracticed = 100 ;
15+ private readonly int initialWidthCreationTime = 100 ;
1716
1817 private SizeF scalingFactor = new SizeF ( 1F , 1F ) ;
1918 private int scaledWidthImage ;
2019 private int scaledWidthLastPracticed ;
20+ private int scaledWidthCreationTime ;
2121
2222 public VocabularyListView ( )
2323 {
2424 scaledWidthImage = initialWidthImage ;
2525 scaledWidthLastPracticed = initialWidthLastPracticed ;
26+ scaledWidthCreationTime = initialWidthCreationTime ;
2627
2728 InitializeComponent ( ) ;
2829
@@ -46,7 +47,7 @@ public Size ImageBaseSize
4647
4748 public ListView . ListViewItemCollection Items => MainListView . Items ;
4849 public ListView . SelectedListViewItemCollection SelectedItems => MainListView . SelectedItems ;
49- public ListViewItem SelectedItem => SelectedItems . Count > 0 ? SelectedItems [ 0 ] : null ;
50+ public ListViewItem ? SelectedItem => SelectedItems . Count > 0 ? SelectedItems [ 0 ] : null ;
5051
5152 [ DefaultValue ( "" ) ]
5253 public string MotherTongue
@@ -62,19 +63,21 @@ public string ForeignLang
6263 set => foreignLangColumn . Text = value ;
6364 }
6465
65- public event ListViewItemSelectionChangedEventHandler ItemSelectionChanged ;
66+ public event ListViewItemSelectionChangedEventHandler ? ItemSelectionChanged ;
6667
6768 protected override void ScaleControl ( SizeF factor , BoundsSpecified specified )
6869 {
6970 scalingFactor = scalingFactor . Multiply ( factor ) ;
7071 scaledWidthImage = ( int ) Math . Round ( initialWidthImage * scalingFactor . Width ) ;
7172 scaledWidthLastPracticed = ( int ) Math . Round ( initialWidthLastPracticed * scalingFactor . Width ) ;
73+ scaledWidthCreationTime = ( int ) Math . Round ( initialWidthCreationTime * scalingFactor . Width ) ;
7274
7375 imageColumn . Width = scaledWidthImage ;
7476 // Here we don't save defaults and therefore directly scale with factor.
7577 motherTongueColumn . Width = ( int ) Math . Round ( motherTongueColumn . Width * factor . Width ) ;
7678 foreignLangColumn . Width = ( int ) Math . Round ( foreignLangColumn . Width * factor . Width ) ;
7779 lastPracticedColumn . Width = scaledWidthLastPracticed ;
80+ creationTimeColumn . Width = scaledWidthCreationTime ;
7881
7982 ScaleImageList ( ) ;
8083
@@ -91,14 +94,14 @@ protected override void OnLayout(LayoutEventArgs e)
9194
9295 private void ScaleImageList ( )
9396 {
94- ImageList old = MainListView . SmallImageList ;
97+ ImageList ? old = MainListView . SmallImageList ;
9598 MainListView . SmallImageList = IconImageList . Scale ( _imageBaseSize . Multiply ( scalingFactor ) . Rectify ( ) . Round ( ) ) ;
9699 old ? . Dispose ( ) ;
97100 }
98101
99102 private void MainListView_ColumnClick ( object sender , ColumnClickEventArgs e )
100103 {
101- Sorter sorter = ( Sorter ) MainListView . ListViewItemSorter ;
104+ Sorter sorter = ( Sorter ) MainListView . ListViewItemSorter ! ;
102105 if ( sorter . Column == e . Column )
103106 {
104107 if ( sorter . SortOrder == SortOrder . Ascending )
@@ -129,6 +132,11 @@ private void MainListView_ColumnWidthChanging(object sender, ColumnWidthChanging
129132 e . Cancel = true ;
130133 e . NewWidth = scaledWidthLastPracticed ;
131134 }
135+ else if ( e . ColumnIndex == 4 )
136+ {
137+ e . Cancel = true ;
138+ e . NewWidth = scaledWidthCreationTime ;
139+ }
132140 }
133141
134142 // Prevents the ListView component from changing these columns on resize
@@ -142,20 +150,32 @@ private void MainListView_ColumnWidthChanged(object sender, ColumnWidthChangedEv
142150 {
143151 lastPracticedColumn . Width = scaledWidthLastPracticed ;
144152 }
153+ else if ( e . ColumnIndex == 4 )
154+ {
155+ creationTimeColumn . Width = scaledWidthCreationTime ;
156+ }
145157 }
146158
147159 private void MainListView_Resize ( object sender , EventArgs e )
148160 {
149161 if ( Program . Settings . ColumnResize )
150162 {
151163 int include = SystemInformation . VerticalScrollBarWidth + MainListView . Columns . Count ;
152- int width = ( MainListView . Width - imageColumn . Width - lastPracticedColumn . Width - include ) / 2 ;
164+ int availableWidth = MainListView . Width - imageColumn . Width - include ;
165+
166+ float listViewWidth = MainListView . Width / scalingFactor . Width ;
167+ if ( listViewWidth > 400 )
168+ availableWidth -= lastPracticedColumn . Width ; // Keep last practiced column if enough space
169+ if ( listViewWidth > 500 )
170+ availableWidth -= creationTimeColumn . Width ; // Keep creation time column if enough space
171+
172+ int width = availableWidth / 2 ;
153173 motherTongueColumn . Width = width ;
154174 foreignLangColumn . Width = width ;
155175 }
156176 }
157177
158- private void MainListView_ItemSelectionChanged ( object sender , ListViewItemSelectionChangedEventArgs e )
178+ private void MainListView_ItemSelectionChanged ( object ? sender , ListViewItemSelectionChangedEventArgs e )
159179 {
160180 ItemSelectionChanged ? . Invoke ( sender , e ) ;
161181 }
@@ -166,9 +186,9 @@ public class Sorter : IComparer
166186
167187 public int Column { get ; set ; }
168188
169- public int Compare ( object x , object y )
189+ public int Compare ( object ? x , object ? y )
170190 {
171- if ( SortOrder == SortOrder . None )
191+ if ( SortOrder == SortOrder . None || x == null || y == null )
172192 return 0 ;
173193
174194 switch ( Column )
@@ -179,13 +199,9 @@ public int Compare(object x, object y)
179199 case 2 :
180200 return Inv ( ( ( ListViewItem ) x ) . SubItems [ Column ] . Text . CompareTo ( ( ( ListViewItem ) y ) . SubItems [ Column ] . Text ) ) ;
181201 case 3 :
182- string left = ( ( ListViewItem ) x ) . SubItems [ 3 ] . Text ;
183- string right = ( ( ListViewItem ) y ) . SubItems [ 3 ] . Text ;
184- if ( ! DateTime . TryParse ( left , out DateTime leftTime ) )
185- leftTime = DateTime . MinValue ;
186- if ( ! DateTime . TryParse ( right , out DateTime rightTime ) )
187- rightTime = DateTime . MinValue ;
188- return Inv ( leftTime . CompareTo ( rightTime ) ) ;
202+ return Inv ( CompareDates ( x , y , 3 ) ) ;
203+ case 4 :
204+ return Inv ( CompareDates ( x , y , 4 ) ) ;
189205 default :
190206 throw new NotImplementedException ( ) ;
191207 }
@@ -198,5 +214,17 @@ private int Inv(int ascending)
198214 else
199215 return - ascending ;
200216 }
217+
218+ private int CompareDates ( object x , object y , int index )
219+ {
220+ string left = ( ( ListViewItem ) x ) . SubItems [ index ] . Text ;
221+ string right = ( ( ListViewItem ) y ) . SubItems [ index ] . Text ;
222+
223+ if ( ! DateTime . TryParse ( left , out DateTime leftTime ) )
224+ leftTime = DateTime . MinValue ;
225+ if ( ! DateTime . TryParse ( right , out DateTime rightTime ) )
226+ rightTime = DateTime . MinValue ;
227+ return leftTime . CompareTo ( rightTime ) ;
228+ }
201229 }
202230}
0 commit comments