소스 검색

wpf: Added start and end hours to calendar control

Kenric Nugteren 1 개월 전
부모
커밋
aee290967e
1개의 변경된 파일37개의 추가작업 그리고 18개의 파일을 삭제
  1. 37 18
      inabox.wpf/Forms/CalendarControl/CalendarControl.cs

+ 37 - 18
inabox.wpf/Forms/CalendarControl/CalendarControl.cs

@@ -25,7 +25,7 @@ using System.Windows.Media;
 using System.Windows.Media.Animation;
 using System.Windows.Shapes;
 
-namespace InABox.WPF;
+namespace InABox.Wpf;
 
 public class CalendarBlockEventArgs(object? value, object column, DateTime date, TimeSpan start, TimeSpan end) : EventArgs
 {
@@ -102,6 +102,12 @@ public class CalendarRegion : INotifyPropertyChanged
 
 public class CalendarControl : ContentControl
 {
+    public static readonly DependencyProperty StartHourProperty = 
+        DependencyProperty.Register(nameof(StartHour), typeof(TimeSpan), typeof(CalendarControl), new(TimeSpan.Zero, Render_Changed));
+
+    public static readonly DependencyProperty EndHourProperty = 
+        DependencyProperty.Register(nameof(EndHour), typeof(TimeSpan), typeof(CalendarControl), new(TimeSpan.FromHours(24), Render_Changed));
+
     public static readonly DependencyProperty RowHeightProperty = 
         DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(CalendarControl), new(100.0, Render_Changed));
 
@@ -129,6 +135,17 @@ public class CalendarControl : ContentControl
     public static readonly DependencyProperty RegionsProperty = 
         DependencyProperty.Register(nameof(Regions), typeof(IEnumerable<CalendarRegion>), typeof(CalendarControl), new(Regions_Changed));
 
+    public TimeSpan StartHour
+    {
+        get => (TimeSpan)GetValue(StartHourProperty);
+        set => SetValue(StartHourProperty, value);
+    }
+
+    public TimeSpan EndHour
+    {
+        get => (TimeSpan)GetValue(EndHourProperty);
+        set => SetValue(EndHourProperty, value);
+    }
 
     public double MinimumColumnWidth
     {
@@ -758,7 +775,7 @@ public class CalendarControl : ContentControl
             }
         }
 
-        var nRows = (24 / RowInterval.TotalHours);
+        var nRows = ((EndHour - StartHour).TotalHours / RowInterval.TotalHours);
         var rowHeight = Math.Max(RowHeight, MainScroll.ActualHeight / nRows);
 
         MainCanvas.Children.Clear();
@@ -854,25 +871,25 @@ public class CalendarControl : ContentControl
                         var rectangle = new Rectangle
                         {
                             Width = colWidth * columnBlocks.Columns!.Count,
-                            Height = ((region.End - region.Start).TotalHours / RowInterval.TotalHours) * RowHeight,
+                            Height = ((region.End - region.Start).TotalHours / RowInterval.TotalHours) * rowHeight,
                         };
 
                         rectangle.Bind(Rectangle.FillProperty, region, x => x.Background);
 
                         Canvas.SetLeft(rectangle, colX);
-                        Canvas.SetTop(rectangle, ((region.End - region.Start).TotalHours / RowInterval.TotalHours) * RowHeight);
+                        Canvas.SetTop(rectangle, ((region.Start - StartHour).TotalHours / RowInterval.TotalHours) * rowHeight);
                         MainCanvas.Children.Add(rectangle);
                     }
                 }
 
                 // Add cell placeholders
                 var rowIdx = 0;
-                for(var time = TimeSpan.Zero; time < TimeSpan.FromHours(24); time += RowInterval)
+                for(var time = StartHour; time < EndHour; time += RowInterval)
                 {
                     var rectangle = new Rectangle
                     {
                         Width = colWidth * columnBlocks.Columns!.Count,
-                        Height = RowHeight,
+                        Height = rowHeight,
                         Fill = new SolidColorBrush(Colors.Transparent),
                     };
                     rectangle.MouseEnter += (o, e) =>
@@ -980,7 +997,7 @@ public class CalendarControl : ContentControl
         else
         {
             MainScroll.ScrollToHorizontalOffset(_currentScroll.x);
-            MainScroll.ScrollToVerticalOffset(Math.Max(minY - RowHeight / 2, _currentScroll.y));
+            MainScroll.ScrollToVerticalOffset(Math.Max(minY - rowHeight / 2, _currentScroll.y));
         }
 
         var lines = new List<FrameworkElement>();
@@ -989,7 +1006,7 @@ public class CalendarControl : ContentControl
         LabelCanvas.Height = MainCanvas.Height;
 
         var y = rowHeight;
-        for(var time = RowInterval; time < TimeSpan.FromHours(24); time += RowInterval)
+        for(var time = StartHour + RowInterval; time < EndHour; time += RowInterval)
         {
             var rectangle = new Rectangle
             {
@@ -1025,15 +1042,15 @@ public class CalendarControl : ContentControl
         var point = e.GetPosition(MainCanvas);
         var rowIdx = (int)Math.Floor(point.Y / _rowHeight);
 
-        start = RowInterval * rowIdx;
-        end = RowInterval * (rowIdx + 1);
-        if(start.TotalHours < 0)
+        start = StartHour + RowInterval * rowIdx;
+        end = StartHour + RowInterval * (rowIdx + 1);
+        if(start < StartHour)
         {
-            start = TimeSpan.Zero;
+            start = StartHour;
         }
-        if(end.TotalHours >= 24)
+        if(end > EndHour)
         {
-            end = TimeSpan.FromHours(24).Subtract(TimeSpan.FromTicks(1));
+            end = EndHour;
         }
         column = null;
         index = -1;
@@ -1147,9 +1164,11 @@ public class CalendarControl : ContentControl
                 MainCanvas.Children.Add(_heldSelection);
             }
 
-            var blockStart = start;
+            var blockStart = StartHour;
             var blockEnd = TimeSpan.MinValue;
 
+            start = StartHour + (pos.Y / _rowHeight) * RowInterval;
+
             var x = 0.0;
             foreach(var (columnDate, columnKey) in _columnList)
             {
@@ -1193,8 +1212,8 @@ public class CalendarControl : ContentControl
                 blockStart = start;
                 blockEnd = end;
             }
-            var top = (blockStart.TotalHours / RowInterval.TotalHours) * RowHeight;
-            var height = ((blockEnd - blockStart).TotalHours / RowInterval.TotalHours) * RowHeight;
+            var top = ((blockStart - StartHour).TotalHours / RowInterval.TotalHours) * _rowHeight;
+            var height = ((blockEnd - blockStart).TotalHours / RowInterval.TotalHours) * _rowHeight;
 
             var width = _colWidth;
 
@@ -1293,7 +1312,7 @@ public class CalendarControl : ContentControl
 
     private double GetRow(TimeSpan time)
     {
-        return time.TotalHours / RowInterval.TotalHours;
+        return (time - StartHour).TotalHours / RowInterval.TotalHours;
     }
 
     private static List<List<Block>> RecalculateBlockPositionsForDay(List<Block> dayBlocks)