refactor: date time format

- generate example dynamically
- update commit/author time in histories immediately after data time format changed
This commit is contained in:
leo 2024-12-31 10:43:00 +08:00
parent a6420aff9b
commit 87e7b792bb
No known key found for this signature in database
4 changed files with 40 additions and 20 deletions

View file

@ -191,7 +191,8 @@
FontWeight="{Binding FontWeight}"
Opacity="{Binding Opacity}"
UseAuthorTime="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"/>
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
DateTimeFormat="{Binding Source={x:Static vm:Preference.Instance}, Path=DateTimeFormat}"/>
</Border>
</Grid>
</DataTemplate>

View file

@ -343,6 +343,15 @@ namespace SourceGit.Views
set => SetValue(ShowAsDateTimeProperty, value);
}
public static readonly StyledProperty<int> DateTimeFormatProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, int>(nameof(DateTimeFormat), 0);
public int DateTimeFormat
{
get => GetValue(DateTimeFormatProperty);
set => SetValue(DateTimeFormatProperty, value);
}
public static readonly StyledProperty<bool> UseAuthorTimeProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(UseAuthorTime), true);
@ -371,6 +380,11 @@ namespace SourceGit.Views
else
StartTimer();
}
else if (change.Property == DateTimeFormatProperty)
{
if (ShowAsDateTime)
SetCurrentValue(TextProperty, GetDisplayText());
}
}
protected override void OnLoaded(RoutedEventArgs e)
@ -426,10 +440,10 @@ namespace SourceGit.Views
if (commit == null)
return string.Empty;
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
if (ShowAsDateTime)
return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(Models.DateTimeFormat.Actived.DateTime);
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
var now = DateTime.Now;
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
var span = now - localTime;

View file

@ -72,7 +72,7 @@
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
<Grid ColumnDefinitions="*,8,*">
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DisplayText}"/>
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateTime}" Foreground="{DynamicResource Brush.FG2}"/>
</Grid>
</DataTemplate>