feature<Avatar>: supports gravatar.com and cravatar.cn (for China)

This commit is contained in:
leo 2024-02-23 11:39:05 +08:00
parent 84e2c7b3a4
commit 49f6ad0407
6 changed files with 74 additions and 45 deletions

View file

@ -9,10 +9,15 @@ using System.Threading.Tasks;
namespace SourceGit.Models {
public interface IAvatarHost {
void OnAvatarResourceReady(string md5, Bitmap bitmap);
void OnAvatarResourceChanged(string md5);
}
public static class AvatarManager {
public static string SelectedServer {
get;
set;
} = "https://www.gravatar.com/avatar/";
static AvatarManager() {
_storePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit", "avatars");
if (!Directory.Exists(_storePath)) Directory.CreateDirectory(_storePath);
@ -37,7 +42,7 @@ namespace SourceGit.Models {
var img = null as Bitmap;
try {
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) };
var task = client.GetAsync($"https://cravatar.cn/avatar/{md5}?d=404");
var task = client.GetAsync($"{SelectedServer}{md5}?d=404");
task.Wait();
var rsp = task.Result;
@ -61,19 +66,28 @@ namespace SourceGit.Models {
Dispatcher.UIThread.InvokeAsync(() => {
if (_resources.ContainsKey(md5)) _resources[md5] = img;
else _resources.Add(md5, img);
if (img != null) NotifyResourceReady(md5, img);
NotifyResourceChanged(md5);
});
}
});
}
public static void Subscribe(IAvatarHost host) {
_avatars.Add(new WeakReference<IAvatarHost>(host));
_avatars.Add(host);
}
public static void Unsubscribe(IAvatarHost host) {
_avatars.Remove(host);
}
public static Bitmap Request(string md5, bool forceRefetch = false) {
if (forceRefetch) {
if (_resources.ContainsKey(md5)) _resources.Remove(md5);
if (_resources.ContainsKey(md5)) _resources.Remove(md5);
var localFile = Path.Combine(_storePath, md5);
if (File.Exists(localFile)) File.Delete(localFile);
NotifyResourceChanged(md5);
} else {
if (_resources.ContainsKey(md5)) return _resources[md5];
@ -96,24 +110,15 @@ namespace SourceGit.Models {
return null;
}
private static void NotifyResourceReady(string md5, Bitmap bitmap) {
List<WeakReference<IAvatarHost>> invalids = new List<WeakReference<IAvatarHost>>();
private static void NotifyResourceChanged(string md5) {
foreach (var avatar in _avatars) {
IAvatarHost retrived = null;
if (avatar.TryGetTarget(out retrived)) {
retrived.OnAvatarResourceReady(md5, bitmap);
break;
} else {
invalids.Add(avatar);
}
avatar.OnAvatarResourceChanged(md5);
}
foreach (var invalid in invalids) _avatars.Remove(invalid);
}
private static object _synclock = new object();
private static string _storePath = string.Empty;
private static List<WeakReference<IAvatarHost>> _avatars = new List<WeakReference<IAvatarHost>>();
private static List<IAvatarHost> _avatars = new List<IAvatarHost>();
private static Dictionary<string, Bitmap> _resources = new Dictionary<string, Bitmap>();
private static HashSet<string> _requesting = new HashSet<string>();
}

View file

@ -271,16 +271,12 @@
<sys:String x:Key="Text.FastForwardWithoutCheck">Fast-Forward (without checkout)</sys:String>
<sys:String x:Key="Text.FileHistory">File History</sys:String>
<sys:String x:Key="Text.FileHistory.UseThisVersion">USE THIS VERSION</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode">CHANGE DISPLAY MODE</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.Grid">Show as Grid</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.List">Show as List</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.Tree">Show as Tree</sys:String>
<sys:String x:Key="Text.FolderDialog">SELECT FOLDER</sys:String>
<sys:String x:Key="Text.FolderDialog.Selected">SELECTED :</sys:String>
<sys:String x:Key="Text.Histories">Histories</sys:String>
<sys:String x:Key="Text.Histories.Search">SEARCH SHA/SUBJECT/AUTHOR. PRESS ENTER TO SEARCH, ESC TO QUIT</sys:String>
<sys:String x:Key="Text.Histories.SearchClear">CLEAR</sys:String>
@ -380,6 +376,7 @@
<sys:String x:Key="Text.Preference">Preference</sys:String>
<sys:String x:Key="Text.Preference.General">GENERAL</sys:String>
<sys:String x:Key="Text.Preference.General.Locale">Language</sys:String>
<sys:String x:Key="Text.Preference.General.AvatarServer">Avatar Server</sys:String>
<sys:String x:Key="Text.Preference.General.Theme">Theme</sys:String>
<sys:String x:Key="Text.Preference.General.MaxHistoryCommits">History Commits</sys:String>
<sys:String x:Key="Text.Preference.General.RestoreTabs">Restore windows</sys:String>

View file

@ -270,16 +270,12 @@
<sys:String x:Key="Text.FastForwardWithoutCheck">快进无需Checkout</sys:String>
<sys:String x:Key="Text.FileHistory">文件历史</sys:String>
<sys:String x:Key="Text.FileHistory.UseThisVersion">使用该版本</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode">切换变更显示模式</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.Grid">网格模式</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.List">列表模式</sys:String>
<sys:String x:Key="Text.ChangeDisplayMode.Tree">树形模式</sys:String>
<sys:String x:Key="Text.FolderDialog">选择目录...</sys:String>
<sys:String x:Key="Text.FolderDialog.Selected">当前选择 </sys:String>
<sys:String x:Key="Text.Histories">历史记录</sys:String>
<sys:String x:Key="Text.Histories.Search">查询提交指纹、信息、作者。回车键开始ESC键取消</sys:String>
<sys:String x:Key="Text.Histories.SearchClear">清空</sys:String>
@ -379,6 +375,7 @@
<sys:String x:Key="Text.Preference">偏好设置</sys:String>
<sys:String x:Key="Text.Preference.General">通用配置</sys:String>
<sys:String x:Key="Text.Preference.General.Locale">显示语言</sys:String>
<sys:String x:Key="Text.Preference.General.AvatarServer">头像服务</sys:String>
<sys:String x:Key="Text.Preference.General.Theme">主题</sys:String>
<sys:String x:Key="Text.Preference.General.MaxHistoryCommits">最大历史提交数</sys:String>
<sys:String x:Key="Text.Preference.General.RestoreTabs">启动时恢复上次打开的仓库</sys:String>

View file

@ -51,6 +51,16 @@ namespace SourceGit.ViewModels {
}
}
public string AvatarServer {
get => Models.AvatarManager.SelectedServer;
set {
if (Models.AvatarManager.SelectedServer != value) {
Models.AvatarManager.SelectedServer = value;
OnPropertyChanged(nameof(AvatarServer));
}
}
}
public int MaxHistoryCommits {
get => _maxHistoryCommits;
set => SetProperty(ref _maxHistoryCommits, value);

View file

@ -1,7 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using System;
using System.Globalization;
using System.Security.Cryptography;
@ -42,25 +42,24 @@ namespace SourceGit.Views {
var refetch = new MenuItem() { Header = App.Text("RefetchAvatar") };
refetch.Click += (o, e) => {
if (User != null) {
_image = Models.AvatarManager.Request(_emailMD5, true);
Models.AvatarManager.Request(_emailMD5, true);
InvalidateVisual();
}
};
ContextMenu = new ContextMenu();
ContextMenu.Items.Add(refetch);
Models.AvatarManager.Subscribe(this);
}
public override void Render(DrawingContext context) {
if (User == null) return;
float corner = (float)Math.Max(2, Bounds.Width / 16);
if (_image != null) {
var corner = (float)Math.Max(2, Bounds.Width / 16);
var img = Models.AvatarManager.Request(_emailMD5, false);
if (img != null) {
var rect = new Rect(0, 0, Bounds.Width, Bounds.Height);
context.PushClip(new RoundedRect(rect, corner));
context.DrawImage(_image, rect);
context.DrawImage(img, rect);
} else {
Point textOrigin = new Point((Bounds.Width - _fallbackLabel.Width) * 0.5, (Bounds.Height - _fallbackLabel.Height) * 0.5);
context.DrawRectangle(_fallbackBrush, null, new Rect(0, 0, Bounds.Width, Bounds.Height), corner, corner);
@ -68,13 +67,22 @@ namespace SourceGit.Views {
}
}
public void OnAvatarResourceReady(string md5, Bitmap bitmap) {
public void OnAvatarResourceChanged(string md5) {
if (_emailMD5 == md5) {
_image = bitmap;
InvalidateVisual();
}
}
protected override void OnLoaded(RoutedEventArgs e) {
base.OnLoaded(e);
Models.AvatarManager.Subscribe(this);
}
protected override void OnUnloaded(RoutedEventArgs e) {
base.OnUnloaded(e);
Models.AvatarManager.Unsubscribe(this);
}
private static void OnUserPropertyChanged(Avatar avatar, AvaloniaPropertyChangedEventArgs e) {
if (avatar.User == null) {
avatar._emailMD5 = null;
@ -90,10 +98,7 @@ namespace SourceGit.Views {
var builder = new StringBuilder();
foreach (var c in hash) builder.Append(c.ToString("x2"));
var md5 = builder.ToString();
if (avatar._emailMD5 != md5) {
avatar._emailMD5 = md5;
avatar._image = Models.AvatarManager.Request(md5, false);
}
if (avatar._emailMD5 != md5) avatar._emailMD5 = md5;
avatar._fallbackBrush = new LinearGradientBrush {
GradientStops = FALLBACK_GRADIENTS[sum % FALLBACK_GRADIENTS.Length],
@ -117,6 +122,5 @@ namespace SourceGit.Views {
private FormattedText _fallbackLabel = null;
private LinearGradientBrush _fallbackBrush = null;
private string _emailMD5 = null;
private Bitmap _image = null;
}
}

View file

@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:m="using:SourceGit.Models"
xmlns:c="using:SourceGit.Converters"
xmlns:vm="using:SourceGit.ViewModels"
@ -56,7 +57,7 @@
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Preference.General}"/>
</TabItem.Header>
<Grid Margin="8" RowDefinitions="32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<Grid Margin="8" RowDefinitions="32,32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Preference.General.Locale}"
HorizontalAlignment="Right"
@ -70,10 +71,25 @@
SelectedItem="{Binding Locale, Mode=TwoWay, Converter={x:Static c:StringConverters.ToLocale}}"/>
<TextBlock Grid.Row="1" Grid.Column="0"
Text="{DynamicResource Text.Preference.General.Theme}"
Text="{DynamicResource Text.Preference.General.AvatarServer}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<ComboBox Grid.Row="1" Grid.Column="1"
MinHeight="28"
Padding="8,0"
HorizontalAlignment="Stretch"
SelectedItem="{Binding AvatarServer, Mode=TwoWay}">
<ComboBox.Items>
<sys:String>https://www.gravatar.com/avatar/</sys:String>
<sys:String>https://cravatar.cn/avatar/</sys:String>
</ComboBox.Items>
</ComboBox>
<TextBlock Grid.Row="2" Grid.Column="0"
Text="{DynamicResource Text.Preference.General.Theme}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<ComboBox Grid.Row="2" Grid.Column="1"
MinHeight="28"
Padding="8,0"
HorizontalAlignment="Stretch"
@ -86,11 +102,11 @@
</ComboBox.Items>
</ComboBox>
<TextBlock Grid.Row="2" Grid.Column="0"
<TextBlock Grid.Row="3" Grid.Column="0"
Text="{DynamicResource Text.Preference.General.MaxHistoryCommits}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="*,64">
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="*,64">
<Slider Grid.Column="0"
Minimum="20000" Maximum="100000"
TickPlacement="BottomRight" TickFrequency="5000"
@ -114,11 +130,11 @@
Text="{Binding MaxHistoryCommits}"/>
</Grid>
<CheckBox Grid.Row="3" Grid.Column="1"
<CheckBox Grid.Row="4" Grid.Column="1"
Content="{DynamicResource Text.Preference.General.RestoreTabs}"
IsChecked="{Binding RestoreTabs, Mode=TwoWay}"/>
<CheckBox Grid.Row="4" Grid.Column="1"
<CheckBox Grid.Row="5" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preference.General.UseMacOSStyle}"
IsChecked="{Binding Source={x:Static vm:Preference.Instance}, Path=UseMacOSStyle, Mode=TwoWay}"