mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-25 22:25:00 +00:00
feature<Avatar>: supports gravatar.com and cravatar.cn (for China)
This commit is contained in:
parent
84e2c7b3a4
commit
49f6ad0407
6 changed files with 74 additions and 45 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue