Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit ea8c228

Browse files
committed
Added attachment template selector
1 parent 1202cd9 commit ea8c228

File tree

7 files changed

+91
-10
lines changed

7 files changed

+91
-10
lines changed

src/Quarrel.Client/Models/Messages/Embeds/Attachment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal Attachment(JsonAttachment jsonAttachment, QuarrelClient context) :
1515
{
1616
Id = jsonAttachment.Id;
1717
Filename = jsonAttachment.Filename;
18+
Size = jsonAttachment.Size;
1819
Url = jsonAttachment.Url;
1920
ProxyUrl = jsonAttachment.ProxyUrl;
2021
Height = jsonAttachment.Height;

src/Quarrel.ViewModels/Bindables/Messages/BindableMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Toolkit.Mvvm.Input;
55
using Microsoft.Toolkit.Mvvm.Messaging;
66
using Quarrel.Bindables.Abstract;
7-
using Quarrel.Bindables.Channels.Abstract;
87
using Quarrel.Bindables.Channels.Interfaces;
98
using Quarrel.Bindables.Messages.Embeds;
109
using Quarrel.Bindables.Users;

src/Quarrel/Converters/Common/Text/CaseConverter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class CaseConverter
1616
/// </summary>
1717
public CharacterCasing Case { get; set; }
1818

19-
2019
/// <summary>
2120
/// Initializes a new instance of the <see cref="CaseConverter"/> class.
2221
/// </summary>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Quarrel © 2022
2+
3+
using Humanizer;
4+
using Humanizer.Bytes;
5+
6+
namespace Quarrel.Converters.Discord.Messages.Attachments
7+
{
8+
public class HumanizeByteSizeConverter
9+
{
10+
public static string Convert(ulong bytes)
11+
{
12+
var byteSize = new ByteSize(bytes);
13+
return byteSize.Humanize("0.00");
14+
}
15+
}
16+
}

src/Quarrel/DataTemplates/MessageTemplates.xaml

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,44 @@
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:tc="using:Microsoft.Toolkit.Uwp.UI.Controls"
6+
xmlns:aconvert="using:Quarrel.Converters.Discord.Messages.Attachments"
67
xmlns:bconvert="using:Quarrel.Converters.Common.Boolean"
78
xmlns:tconvert="using:Quarrel.Converters.Common.Time"
89
xmlns:mconvert="using:Quarrel.Converters.Discord.Messages"
910
xmlns:qc="using:Quarrel.Controls"
1011
xmlns:mcontrols="using:Quarrel.Controls.Panels.Messages"
1112
xmlns:bindablemessages="using:Quarrel.Bindables.Messages"
1213
xmlns:bindableembeds="using:Quarrel.Bindables.Messages.Embeds"
14+
xmlns:mselector="using:Quarrel.Selectors.Messages"
1315
xmlns:markdown="using:Quarrel.Markdown">
16+
17+
<SolidColorBrush x:Key="AttachmentBackgroundBrush" Color="#1E1E1E"/>
18+
<SolidColorBrush x:Key="AttachmentBorderBrush" Color="#3C3C3C"/>
19+
20+
<DataTemplate x:Key="DefaultAttachmentTemplate" x:DataType="bindableembeds:BindableAttachment">
21+
<Grid VerticalAlignment="Top" HorizontalAlignment="Left"
22+
BorderThickness="1" CornerRadius="4" Padding="8,8,12,8"
23+
Background="{StaticResource AttachmentBackgroundBrush}"
24+
BorderBrush="{StaticResource AttachmentBorderBrush}">
25+
<Grid.ColumnDefinitions>
26+
<ColumnDefinition Width="Auto"/>
27+
<ColumnDefinition Width="Auto"/>
28+
</Grid.ColumnDefinitions>
29+
<FontIcon x:Name="FileIcon" FontSize="36" Glyph="" HorizontalAlignment="Left" Margin="0,0,6,0"/>
30+
<StackPanel Grid.Column="1">
31+
<HyperlinkButton Content="{x:Bind Attachment.Filename}" NavigateUri="{x:Bind Attachment.ProxyUrl}" FontSize="18" Padding="0" Style="{StaticResource PlainTextHyperlinkStyle}"/>
32+
<TextBlock Text="{x:Bind aconvert:HumanizeByteSizeConverter.Convert(Attachment.Size)}" FontSize="11" Opacity="0.5" Margin="0,-2,0,0"/>
33+
</StackPanel>
34+
</Grid>
35+
</DataTemplate>
36+
37+
<DataTemplate x:Key="ImageAttachmentTemplate" x:DataType="bindableembeds:BindableAttachment">
38+
<tc:ImageEx Source="{x:Bind Attachment.ProxyUrl}" MaxWidth="400" CornerRadius="2"/>
39+
</DataTemplate>
40+
41+
<mselector:AttachmentTemplateSelector x:Key="AttachmentTemplateSelector"
42+
DefaultAttachmentTemplate="{StaticResource DefaultAttachmentTemplate}"
43+
ImageAttachmentTemplate="{StaticResource ImageAttachmentTemplate}"/>
1444

1545
<DataTemplate x:Key="DefaultMessageTemplate" x:DataType="bindablemessages:BindableMessage">
1646
<!--Grid must be wrapped in a user control to enable the VisualStateManager-->
@@ -93,14 +123,9 @@
93123
<markdown:MessageRenderer Text="{x:Bind Message.Content, Mode=OneWay}" Context="{x:Bind Mode=OneWay}"
94124
HorizontalAlignment="Left"/>
95125

96-
<ItemsControl ItemsSource="{x:Bind Message.Attachments}"
97-
HorizontalAlignment="Left">
98-
<ItemsControl.ItemTemplate>
99-
<DataTemplate x:DataType="bindableembeds:BindableAttachment">
100-
<tc:ImageEx Source="{x:Bind Attachment.ProxyUrl}" MaxWidth="400" CornerRadius="2"/>
101-
</DataTemplate>
102-
</ItemsControl.ItemTemplate>
103-
</ItemsControl>
126+
<ItemsControl ItemsSource="{x:Bind Attachments}"
127+
ItemTemplateSelector="{StaticResource AttachmentTemplateSelector}"
128+
HorizontalAlignment="Left"/>
104129
</StackPanel>
105130
</Grid>
106131
</Grid>

src/Quarrel/Quarrel.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<Compile Include="Converters\Discord\Channels\IsPrivateChannelConverter.cs" />
150150
<Compile Include="Converters\Discord\Channels\IsPrivateChannelVisibleConverter.cs" />
151151
<Compile Include="Converters\Discord\Channels\MemberCountConverter.cs" />
152+
<Compile Include="Converters\Discord\Messages\Attachments\HumanizeByteSizeConverter.cs" />
152153
<Compile Include="Converters\Discord\Messages\InfoMessageColorConverter.cs" />
153154
<Compile Include="Converters\Discord\Messages\InfoMessageContentConverter.cs" />
154155
<Compile Include="Converters\Discord\Messages\InfoMessageIconConverter.cs" />
@@ -178,6 +179,7 @@
178179
<Compile Include="Selectors\Channels\ChannelEnabledStyleSelector.cs" />
179180
<Compile Include="Selectors\Guilds\GuildStyleSelector.cs" />
180181
<Compile Include="Selectors\Guilds\GuildTemplateSelector.cs" />
182+
<Compile Include="Selectors\Messages\AttachmentTemplateSelector.cs" />
181183
<Compile Include="Selectors\Messages\MessageTemplateSelector.cs" />
182184
<Compile Include="Selectors\SubPages\SubPageTemplateSelector.cs" />
183185
<Compile Include="Selectors\SubPages\UserSettings\UserSettingsMenuItemSelector.cs" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Quarrel © 2022
2+
3+
using Quarrel.Bindables.Messages.Embeds;
4+
using Windows.UI.Xaml;
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace Quarrel.Selectors.Messages
8+
{
9+
public class AttachmentTemplateSelector : DataTemplateSelector
10+
{
11+
public DataTemplate? DefaultAttachmentTemplate { get; set; }
12+
13+
public DataTemplate? ImageAttachmentTemplate { get; set; }
14+
15+
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
16+
{
17+
if (item is BindableAttachment attachment)
18+
{
19+
int index = attachment.Attachment.Filename.LastIndexOf('.');
20+
string filetype = attachment.Attachment.Filename.Substring(index + 1).ToLowerInvariant();
21+
return filetype switch
22+
{
23+
"png" or
24+
"jpg" or
25+
"jpeg" or
26+
"gif" => ImageAttachmentTemplate,
27+
28+
//"mp4" or
29+
//"mov" or
30+
//"wmv" => VideoAttachmentTemplate
31+
32+
_ => DefaultAttachmentTemplate,
33+
};
34+
}
35+
36+
return null;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)