-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Data.Converters; | ||
using Avalonia.Platform; | ||
using System.Globalization; | ||
using Turbulence.Discord; | ||
using Avalonia.Media.Imaging; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
|
||
namespace Turbulence.Desktop.Converters; | ||
|
||
public class ImageUrlConverter : IValueConverter | ||
{ | ||
private readonly IPlatformClient _client = Ioc.Default.GetService<IPlatformClient>()!; | ||
|
||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
if (value is not string url) | ||
throw new Exception("Not a string."); | ||
|
||
// Debug image | ||
if (Design.IsDesignMode) | ||
{ | ||
return new Bitmap(AssetLoader.Open(new Uri("resm:Avalonia.Skia.Assets.NoiseAsset_256X256_PNG.png?assembly=Avalonia.Skia"))); | ||
} | ||
|
||
var data = Task.Run(async () => await _client.GetImageAsync(url)).Result; | ||
var bmp = new Bitmap(new MemoryStream(data)); | ||
/*if (bmp.PixelSize.Height > 80) | ||
{ | ||
bmp = bmp.CreateScaledBitmap(new PixelSize(80, 80)); | ||
}*/ | ||
|
||
return bmp; | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Avalonia.Data.Converters; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using System.Globalization; | ||
using Turbulence.Discord; | ||
using Turbulence.Discord.Models.DiscordChannel; | ||
|
||
namespace Turbulence.Desktop.Converters; | ||
|
||
public class MessageVisibleConverter : IValueConverter | ||
{ | ||
private readonly IPlatformClient _client = Ioc.Default.GetService<IPlatformClient>()!; | ||
|
||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
if (value is not Message message) | ||
return null; | ||
|
||
//TODO: can we optimize this by not needing call this again or not needing a new converter | ||
return !string.IsNullOrEmpty(_client.GetMessageContent(message)); | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Turbulence.Desktop/DataTemplates/AttachmentTypeSelector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.Templates; | ||
using Avalonia.Metadata; | ||
using Turbulence.Discord.Models.DiscordChannel; | ||
|
||
namespace Turbulence.Desktop.DataTemplates; | ||
|
||
public class AttachmentTypeSelector : IDataTemplate | ||
{ | ||
[Content] | ||
public Dictionary<string, IDataTemplate> Templates { get; } = new Dictionary<string, IDataTemplate>(); | ||
|
||
public bool Match(object? data) => data is Attachment; | ||
|
||
Control? ITemplate<object?, Control?>.Build(object? param) | ||
{ | ||
var attachment = (Attachment)param!; | ||
var type = attachment.ContentType switch | ||
{ | ||
"image/png" or "image/jpeg" => "image", | ||
_ => "default", | ||
}; | ||
if (!Templates.TryGetValue(type, out var template)) | ||
return Templates["unknown"].Build(param); | ||
return template.Build(param); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters