Skip to content

Commit 21d52c2

Browse files
committed
feature: supports to switch 24-hours/12-hours (#2183)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 3a596bc commit 21d52c2

File tree

8 files changed

+96
-36
lines changed

8 files changed

+96
-36
lines changed

src/Models/DateTimeFormat.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,37 @@ namespace SourceGit.Models
55
{
66
public class DateTimeFormat
77
{
8-
public string DateOnly { get; set; }
9-
public string DateTime { get; set; }
8+
public string DateOnly
9+
{
10+
get;
11+
set;
12+
}
13+
14+
public string DateTime
15+
{
16+
get
17+
{
18+
if (_use24Hours != Use24Hours || string.IsNullOrEmpty(_dateTime))
19+
{
20+
_use24Hours = Use24Hours;
21+
_dateTime = _use24Hours ? $"{DateOnly} HH:mm:ss" : $"{DateOnly} hh:mm:ss tt";
22+
}
23+
24+
return _dateTime;
25+
}
26+
}
1027

1128
public string Example
1229
{
13-
get => _example.ToString(DateTime);
30+
get
31+
{
32+
return new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local).ToString(DateOnly);
33+
}
1434
}
1535

16-
public DateTimeFormat(string dateOnly, string dateTime)
36+
public DateTimeFormat(string date)
1737
{
18-
DateOnly = dateOnly;
19-
DateTime = dateTime;
38+
DateOnly = date;
2039
}
2140

2241
public static int ActiveIndex
@@ -25,26 +44,33 @@ public static int ActiveIndex
2544
set;
2645
} = 0;
2746

47+
public static bool Use24Hours
48+
{
49+
get;
50+
set;
51+
} = true;
52+
2853
public static DateTimeFormat Active
2954
{
3055
get => Supported[ActiveIndex];
3156
}
3257

3358
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
3459
{
35-
new DateTimeFormat("yyyy/MM/dd", "yyyy/MM/dd, HH:mm:ss"),
36-
new DateTimeFormat("yyyy.MM.dd", "yyyy.MM.dd, HH:mm:ss"),
37-
new DateTimeFormat("yyyy-MM-dd", "yyyy-MM-dd, HH:mm:ss"),
38-
new DateTimeFormat("MM/dd/yyyy", "MM/dd/yyyy, HH:mm:ss"),
39-
new DateTimeFormat("MM.dd.yyyy", "MM.dd.yyyy, HH:mm:ss"),
40-
new DateTimeFormat("MM-dd-yyyy", "MM-dd-yyyy, HH:mm:ss"),
41-
new DateTimeFormat("dd/MM/yyyy", "dd/MM/yyyy, HH:mm:ss"),
42-
new DateTimeFormat("dd.MM.yyyy", "dd.MM.yyyy, HH:mm:ss"),
43-
new DateTimeFormat("dd-MM-yyyy", "dd-MM-yyyy, HH:mm:ss"),
44-
new DateTimeFormat("MMM d yyyy", "MMM d yyyy, HH:mm:ss"),
45-
new DateTimeFormat("d MMM yyyy", "d MMM yyyy, HH:mm:ss"),
60+
new DateTimeFormat("yyyy/MM/dd"),
61+
new DateTimeFormat("yyyy.MM.dd"),
62+
new DateTimeFormat("yyyy-MM-dd"),
63+
new DateTimeFormat("MM/dd/yyyy"),
64+
new DateTimeFormat("MM.dd.yyyy"),
65+
new DateTimeFormat("MM-dd-yyyy"),
66+
new DateTimeFormat("dd/MM/yyyy"),
67+
new DateTimeFormat("dd.MM.yyyy"),
68+
new DateTimeFormat("dd-MM-yyyy"),
69+
new DateTimeFormat("MMM d yyyy"),
70+
new DateTimeFormat("d MMM yyyy"),
4671
};
4772

48-
private static readonly DateTime _example = new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local);
73+
private bool _use24Hours = true;
74+
private string _dateTime = null;
4975
}
5076
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@
649649
<x:String x:Key="Text.Preferences.General.ShowChildren" xml:space="preserve">Show children in the commit details</x:String>
650650
<x:String x:Key="Text.Preferences.General.ShowTagsInGraph" xml:space="preserve">Show tags in commit graph</x:String>
651651
<x:String x:Key="Text.Preferences.General.SubjectGuideLength" xml:space="preserve">Subject Guide Length</x:String>
652+
<x:String x:Key="Text.Preferences.General.Use24Hours" xml:space="preserve">24-Hours</x:String>
652653
<x:String x:Key="Text.Preferences.General.UseGitHubStyleAvatar" xml:space="preserve">Generate Github style default avatar</x:String>
653654
<x:String x:Key="Text.Preferences.Git" xml:space="preserve">GIT</x:String>
654655
<x:String x:Key="Text.Preferences.Git.CRLF" xml:space="preserve">Enable Auto CRLF</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@
653653
<x:String x:Key="Text.Preferences.General.ShowChildren" xml:space="preserve">在提交详情页中显示子提交列表</x:String>
654654
<x:String x:Key="Text.Preferences.General.ShowTagsInGraph" xml:space="preserve">在提交路线图中显示标签</x:String>
655655
<x:String x:Key="Text.Preferences.General.SubjectGuideLength" xml:space="preserve">SUBJECT字数检测</x:String>
656+
<x:String x:Key="Text.Preferences.General.Use24Hours" xml:space="preserve">24小时制</x:String>
656657
<x:String x:Key="Text.Preferences.General.UseGitHubStyleAvatar" xml:space="preserve">生成GitHub风格的默认头像</x:String>
657658
<x:String x:Key="Text.Preferences.Git" xml:space="preserve">GIT配置</x:String>
658659
<x:String x:Key="Text.Preferences.Git.CRLF" xml:space="preserve">自动换行转换</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@
653653
<x:String x:Key="Text.Preferences.General.ShowChildren" xml:space="preserve">在提交詳細資訊中顯示後續提交</x:String>
654654
<x:String x:Key="Text.Preferences.General.ShowTagsInGraph" xml:space="preserve">在路線圖中顯示標籤</x:String>
655655
<x:String x:Key="Text.Preferences.General.SubjectGuideLength" xml:space="preserve">提交標題字數偵測</x:String>
656+
<x:String x:Key="Text.Preferences.General.Use24Hours" xml:space="preserve">24小時制</x:String>
656657
<x:String x:Key="Text.Preferences.General.UseGitHubStyleAvatar" xml:space="preserve">產生 GitHub 風格的預設頭貼</x:String>
657658
<x:String x:Key="Text.Preferences.Git" xml:space="preserve">Git 設定</x:String>
658659
<x:String x:Key="Text.Preferences.Git.CRLF" xml:space="preserve">自動換行轉換</x:String>

src/ViewModels/Preferences.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ public int DateTimeFormat
155155
}
156156
}
157157

158+
public bool Use24Hours
159+
{
160+
get => Models.DateTimeFormat.Use24Hours;
161+
set
162+
{
163+
if (value != Models.DateTimeFormat.Use24Hours)
164+
{
165+
Models.DateTimeFormat.Use24Hours = value;
166+
OnPropertyChanged();
167+
}
168+
}
169+
}
170+
158171
public bool UseFixedTabWidth
159172
{
160173
get => _useFixedTabWidth;

src/Views/CommitTimeTextBlock.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ public bool ShowAsDateTime
1919
set => SetValue(ShowAsDateTimeProperty, value);
2020
}
2121

22+
public static readonly StyledProperty<bool> Use24HoursProperty =
23+
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(Use24Hours), true);
24+
25+
public bool Use24Hours
26+
{
27+
get => GetValue(Use24HoursProperty);
28+
set => SetValue(Use24HoursProperty, value);
29+
}
30+
2231
public static readonly StyledProperty<int> DateTimeFormatProperty =
2332
AvaloniaProperty.Register<CommitTimeTextBlock, int>(nameof(DateTimeFormat));
2433

@@ -62,7 +71,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
6271
HorizontalAlignment = HorizontalAlignment.Center;
6372
}
6473
}
65-
else if (change.Property == DateTimeFormatProperty)
74+
else if (change.Property == DateTimeFormatProperty || change.Property == Use24HoursProperty)
6675
{
6776
if (ShowAsDateTime)
6877
SetCurrentValue(TextProperty, GetDisplayText());

src/Views/Histories.axaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@
241241
<v:CommitTimeTextBlock FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
242242
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
243243
UseAuthorTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
244-
ShowAsDateTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
245-
DateTimeFormat="{Binding Source={x:Static vm:Preferences.Instance}, Path=DateTimeFormat}"/>
244+
ShowAsDateTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=!DisplayTimeAsPeriodInHistories, Mode=OneWay}"
245+
DateTimeFormat="{Binding Source={x:Static vm:Preferences.Instance}, Path=DateTimeFormat, Mode=OneWay}"
246+
Use24Hours="{Binding Source={x:Static vm:Preferences.Instance}, Path=Use24Hours, Mode=OneWay}"/>
246247
</Border>
247248
</DataTemplate>
248249
</DataGridTemplateColumn.CellTemplate>

src/Views/Preferences.axaml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,29 @@
6363
Text="{DynamicResource Text.Preferences.General.DateFormat}"
6464
HorizontalAlignment="Right"
6565
Margin="0,0,16,0"/>
66-
<ComboBox Grid.Row="1" Grid.Column="1"
67-
MinHeight="28"
68-
Padding="8,0"
69-
HorizontalAlignment="Stretch"
70-
ItemsSource="{Binding Source={x:Static m:DateTimeFormat.Supported}}"
71-
SelectedIndex="{Binding DateTimeFormat, Mode=TwoWay}">
72-
<ComboBox.ItemTemplate>
73-
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
74-
<Grid ColumnDefinitions="*,8,*">
75-
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
76-
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateTime}" Foreground="{DynamicResource Brush.FG2}"/>
77-
</Grid>
78-
</DataTemplate>
79-
</ComboBox.ItemTemplate>
80-
</ComboBox>
66+
<Grid Grid.Row="1" Grid.Column="1" ColumnDefinitions="*,Auto">
67+
<ComboBox Grid.Column="0"
68+
MinHeight="28"
69+
Padding="8,0"
70+
HorizontalAlignment="Stretch"
71+
ItemsSource="{Binding Source={x:Static m:DateTimeFormat.Supported}}"
72+
SelectedIndex="{Binding DateTimeFormat, Mode=TwoWay}">
73+
<ComboBox.ItemTemplate>
74+
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
75+
<Grid ColumnDefinitions="*,8,*">
76+
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
77+
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateOnly}" Foreground="{DynamicResource Brush.FG2}"/>
78+
</Grid>
79+
</DataTemplate>
80+
</ComboBox.ItemTemplate>
81+
</ComboBox>
82+
83+
<CheckBox Grid.Column="1"
84+
Height="32"
85+
Margin="8,0,0,0"
86+
Content="{DynamicResource Text.Preferences.General.Use24Hours}"
87+
IsChecked="{Binding Use24Hours, Mode=TwoWay}"/>
88+
</Grid>
8189

8290
<TextBlock Grid.Row="2" Grid.Column="0"
8391
Text="{DynamicResource Text.Preferences.Git.DefaultCloneDir}"

0 commit comments

Comments
 (0)