2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
2024-02-18 23:30:10 -08:00
|
|
|
using Avalonia.Controls.Primitives;
|
2024-07-15 20:15:41 -07:00
|
|
|
using Avalonia.Interactivity;
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia.Media;
|
2024-07-15 20:15:41 -07:00
|
|
|
using Avalonia.Threading;
|
2024-02-18 23:30:10 -08:00
|
|
|
using Avalonia.VisualTree;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public class LayoutableGrid : Grid
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
public static readonly StyledProperty<bool> UseHorizontalProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<LayoutableGrid, bool>(nameof(UseHorizontal));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool UseHorizontal
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => GetValue(UseHorizontalProperty);
|
|
|
|
set => SetValue(UseHorizontalProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override Type StyleKeyOverride => typeof(Grid);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
static LayoutableGrid()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
UseHorizontalProperty.Changed.AddClassHandler<LayoutableGrid>((o, _) => o.RefreshLayout());
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void ApplyTemplate()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.ApplyTemplate();
|
|
|
|
RefreshLayout();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void RefreshLayout()
|
|
|
|
{
|
|
|
|
if (UseHorizontal)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var rowSpan = RowDefinitions.Count;
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < Children.Count; i++)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var child = Children[i];
|
|
|
|
child.SetValue(RowProperty, 0);
|
|
|
|
child.SetValue(RowSpanProperty, rowSpan);
|
|
|
|
child.SetValue(ColumnProperty, i);
|
|
|
|
child.SetValue(ColumnSpanProperty, 1);
|
2024-03-26 00:18:34 -07:00
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (child is GridSplitter splitter)
|
|
|
|
splitter.BorderThickness = new Thickness(1, 0, 0, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var colSpan = ColumnDefinitions.Count;
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < Children.Count; i++)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var child = Children[i];
|
|
|
|
child.SetValue(RowProperty, i);
|
|
|
|
child.SetValue(RowSpanProperty, 1);
|
|
|
|
child.SetValue(ColumnProperty, 0);
|
|
|
|
child.SetValue(ColumnSpanProperty, colSpan);
|
2024-03-26 00:18:34 -07:00
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (child is GridSplitter splitter)
|
|
|
|
splitter.BorderThickness = new Thickness(0, 1, 0, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 20:15:41 -07:00
|
|
|
public class CommitTimeTextBlock : TextBlock
|
|
|
|
{
|
|
|
|
public static readonly StyledProperty<bool> ShowAsDateTimeProperty =
|
|
|
|
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(ShowAsDateTime), true);
|
|
|
|
|
|
|
|
public bool ShowAsDateTime
|
|
|
|
{
|
|
|
|
get => GetValue(ShowAsDateTimeProperty);
|
|
|
|
set => SetValue(ShowAsDateTimeProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<ulong> TimestampProperty =
|
|
|
|
AvaloniaProperty.Register<CommitTimeTextBlock, ulong>(nameof(Timestamp));
|
|
|
|
|
|
|
|
public ulong Timestamp
|
|
|
|
{
|
|
|
|
get => GetValue(TimestampProperty);
|
|
|
|
set => SetValue(TimestampProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override Type StyleKeyOverride => typeof(TextBlock);
|
|
|
|
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
|
|
|
if (change.Property == TimestampProperty)
|
|
|
|
{
|
|
|
|
SetCurrentValue(TextProperty, GetDisplayText());
|
|
|
|
}
|
|
|
|
else if (change.Property == ShowAsDateTimeProperty)
|
|
|
|
{
|
|
|
|
SetCurrentValue(TextProperty, GetDisplayText());
|
|
|
|
|
|
|
|
if (ShowAsDateTime)
|
|
|
|
StopTimer();
|
|
|
|
else
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnLoaded(e);
|
|
|
|
|
|
|
|
if (!ShowAsDateTime)
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnUnloaded(e);
|
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartTimer()
|
|
|
|
{
|
|
|
|
if (_refreshTimer != null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_refreshTimer = DispatcherTimer.Run(() =>
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
{
|
|
|
|
var text = GetDisplayText();
|
|
|
|
if (!text.Equals(Text, StringComparison.Ordinal))
|
|
|
|
Text = text;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
2024-07-15 23:06:02 -07:00
|
|
|
}, TimeSpan.FromSeconds(10));
|
2024-07-15 20:15:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void StopTimer()
|
|
|
|
{
|
|
|
|
if (_refreshTimer != null)
|
|
|
|
{
|
|
|
|
_refreshTimer.Dispose();
|
|
|
|
_refreshTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDisplayText()
|
|
|
|
{
|
|
|
|
if (ShowAsDateTime)
|
|
|
|
return DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");
|
|
|
|
|
|
|
|
var today = DateTime.Today;
|
|
|
|
var committerTime = DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime();
|
|
|
|
|
|
|
|
if (committerTime >= today)
|
|
|
|
{
|
|
|
|
var now = DateTime.Now;
|
|
|
|
var timespan = now - committerTime;
|
|
|
|
if (timespan.TotalHours > 1)
|
2024-07-15 23:06:02 -07:00
|
|
|
return App.Text("Period.HoursAgo", (int)timespan.TotalHours);
|
2024-07-15 20:15:41 -07:00
|
|
|
|
2024-07-15 23:06:02 -07:00
|
|
|
return timespan.TotalMinutes < 1 ? App.Text("Period.JustNow") : App.Text("Period.MinutesAgo", (int)timespan.TotalMinutes);
|
2024-07-15 20:15:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var diffYear = today.Year - committerTime.Year;
|
|
|
|
if (diffYear == 0)
|
|
|
|
{
|
|
|
|
var diffMonth = today.Month - committerTime.Month;
|
|
|
|
if (diffMonth > 0)
|
2024-07-15 23:06:02 -07:00
|
|
|
return diffMonth == 1 ? App.Text("Period.LastMonth") : App.Text("Period.MonthsAgo", diffMonth);
|
2024-07-15 20:15:41 -07:00
|
|
|
|
|
|
|
var diffDay = today.Day - committerTime.Day;
|
2024-07-15 23:06:02 -07:00
|
|
|
return diffDay == 1 ? App.Text("Period.Yesterday") : App.Text("Period.DaysAgo", diffDay);
|
2024-07-15 20:15:41 -07:00
|
|
|
}
|
|
|
|
|
2024-07-15 23:06:02 -07:00
|
|
|
return diffYear == 1 ? App.Text("Period.LastYear") : App.Text("Period.YearsAgo", diffYear);
|
2024-07-15 20:15:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private IDisposable _refreshTimer = null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public class CommitGraph : Control
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
public static readonly StyledProperty<Models.CommitGraph> GraphProperty =
|
|
|
|
AvaloniaProperty.Register<CommitGraph, Models.CommitGraph>(nameof(Graph));
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Models.CommitGraph Graph
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => GetValue(GraphProperty);
|
|
|
|
set => SetValue(GraphProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-04-17 01:16:11 -07:00
|
|
|
public static readonly StyledProperty<IBrush> DotBrushProperty =
|
|
|
|
AvaloniaProperty.Register<CommitGraph, IBrush>(nameof(DotBrush), Brushes.Transparent);
|
|
|
|
|
|
|
|
public IBrush DotBrush
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-04-17 01:16:11 -07:00
|
|
|
get => GetValue(DotBrushProperty);
|
|
|
|
set => SetValue(DotBrushProperty, value);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-05-30 23:05:38 -07:00
|
|
|
static CommitGraph()
|
|
|
|
{
|
|
|
|
AffectsRender<CommitGraph>(GraphProperty, DotBrushProperty);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.Render(context);
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
var grid = this.FindAncestorOfType<Histories>()?.CommitDataGrid;
|
|
|
|
if (grid == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-18 23:30:10 -08:00
|
|
|
var graph = Graph;
|
2024-07-14 00:55:15 -07:00
|
|
|
if (graph == null)
|
2024-03-31 01:54:29 -07:00
|
|
|
return;
|
2024-02-18 23:30:10 -08:00
|
|
|
|
|
|
|
var rowsPresenter = grid.FindDescendantOfType<DataGridRowsPresenter>();
|
2024-03-31 01:54:29 -07:00
|
|
|
if (rowsPresenter == null)
|
|
|
|
return;
|
2024-02-18 23:30:10 -08:00
|
|
|
|
|
|
|
// Find the content display offset Y of binding DataGrid.
|
|
|
|
double rowHeight = grid.RowHeight;
|
|
|
|
double startY = 0;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var child in rowsPresenter.Children)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
if (child is DataGridRow { IsVisible: true, Bounds.Top: <= 0 } row && row.Bounds.Top > -rowHeight)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-18 23:30:10 -08:00
|
|
|
var test = rowHeight * row.GetIndex() - row.Bounds.Top;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (startY < test)
|
|
|
|
startY = test;
|
2024-02-18 23:30:10 -08:00
|
|
|
}
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-09 01:29:41 -07:00
|
|
|
var headerHeight = grid.ColumnHeaderHeight;
|
|
|
|
startY -= headerHeight;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
// Apply scroll offset.
|
2024-07-09 01:29:41 -07:00
|
|
|
context.PushClip(new Rect(Bounds.Left, Bounds.Top + headerHeight, grid.Columns[0].ActualWidth, Bounds.Height));
|
2024-02-18 23:30:10 -08:00
|
|
|
context.PushTransform(Matrix.CreateTranslation(0, -startY));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
// Calculate bounds.
|
2024-02-18 23:30:10 -08:00
|
|
|
var top = startY;
|
|
|
|
var bottom = startY + grid.Bounds.Height + rowHeight * 2;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
// Draw all curves
|
|
|
|
DrawCurves(context, top, bottom);
|
|
|
|
|
|
|
|
// Draw connect dots
|
2024-04-17 01:16:11 -07:00
|
|
|
IBrush dotFill = DotBrush;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var dot in graph.Dots)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (dot.Center.Y < top)
|
|
|
|
continue;
|
|
|
|
if (dot.Center.Y > bottom)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-06-06 05:25:16 -07:00
|
|
|
context.DrawEllipse(dotFill, Models.CommitGraph.Pens[dot.Color], dot.Center, 3, 3);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void DrawCurves(DrawingContext context, double top, double bottom)
|
|
|
|
{
|
|
|
|
foreach (var line in Graph.Paths)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var last = line.Points[0];
|
|
|
|
var size = line.Points.Count;
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (line.Points[size - 1].Y < top)
|
|
|
|
continue;
|
|
|
|
if (last.Y > bottom)
|
2024-07-15 06:16:55 -07:00
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var geo = new StreamGeometry();
|
2024-06-06 05:25:16 -07:00
|
|
|
var pen = Models.CommitGraph.Pens[line.Color];
|
2024-07-05 05:02:30 -07:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
using (var ctx = geo.Open())
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var started = false;
|
|
|
|
var ended = false;
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 1; i < size; i++)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var cur = line.Points[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (cur.Y < top)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
last = cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!started)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
ctx.BeginFigure(last, false);
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (cur.Y > bottom)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
cur = new Point(cur.X, bottom);
|
|
|
|
ended = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (cur.X > last.X)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
ctx.QuadraticBezierTo(new Point(cur.X, last.Y), cur);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (cur.X < last.X)
|
|
|
|
{
|
|
|
|
if (i < size - 1)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var midY = (last.Y + cur.Y) / 2;
|
2024-07-08 03:10:26 -07:00
|
|
|
ctx.CubicBezierTo(new Point(last.X, midY + 4), new Point(cur.X, midY - 4), cur);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
ctx.QuadraticBezierTo(new Point(last.X, cur.Y), cur);
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
ctx.LineTo(cur);
|
|
|
|
}
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (ended)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
last = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
context.DrawGeometry(null, pen, geo);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var link in Graph.Links)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (link.End.Y < top)
|
|
|
|
continue;
|
|
|
|
if (link.Start.Y > bottom)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var geo = new StreamGeometry();
|
2024-03-17 18:37:06 -07:00
|
|
|
using (var ctx = geo.Open())
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
ctx.BeginFigure(link.Start, false);
|
|
|
|
ctx.QuadraticBezierTo(link.Control, link.End);
|
|
|
|
}
|
|
|
|
|
2024-06-06 05:25:16 -07:00
|
|
|
context.DrawGeometry(null, Models.CommitGraph.Pens[link.Color], geo);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class Histories : UserControl
|
|
|
|
{
|
2024-04-07 06:19:02 -07:00
|
|
|
public static readonly StyledProperty<long> NavigationIdProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<Histories, long>(nameof(NavigationId));
|
2024-04-07 06:19:02 -07:00
|
|
|
|
|
|
|
public long NavigationId
|
|
|
|
{
|
|
|
|
get => GetValue(NavigationIdProperty);
|
|
|
|
set => SetValue(NavigationIdProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Histories()
|
|
|
|
{
|
|
|
|
NavigationIdProperty.Changed.AddClassHandler<Histories>((h, _) =>
|
|
|
|
{
|
|
|
|
// Force scroll selected item (current head) into view. see issue #58
|
2024-07-14 00:55:15 -07:00
|
|
|
var datagrid = h.CommitDataGrid;
|
2024-04-07 06:19:02 -07:00
|
|
|
if (datagrid != null && datagrid.SelectedItems.Count == 1)
|
|
|
|
datagrid.ScrollIntoView(datagrid.SelectedItems[0], null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Histories()
|
|
|
|
{
|
2024-02-19 01:40:36 -08:00
|
|
|
InitializeComponent();
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private void OnCommitDataGridLayoutUpdated(object _1, EventArgs _2)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
CommitGraph.InvalidateVisual();
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private void OnCommitDataGridSelectionChanged(object _, SelectionChangedEventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
if (DataContext is ViewModels.Histories histories)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
histories.Select(CommitDataGrid.SelectedItems);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnCommitDataGridContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-05-27 02:21:28 -07:00
|
|
|
if (DataContext is ViewModels.Histories histories && sender is DataGrid datagrid)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-05-27 02:21:28 -07:00
|
|
|
var menu = histories.MakeContextMenu(datagrid);
|
|
|
|
datagrid.OpenContextMenu(menu);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|