Ability to set composer default font (#287)

* Added ability to set Composer font

* Added missing translations and refactoring

* Remove unused methods

* Small fixes
This commit is contained in:
Tiktack
2024-07-18 20:04:11 +02:00
committed by GitHub
parent 76375c9471
commit cf2f0ec936
30 changed files with 424 additions and 316 deletions

View File

@@ -1,50 +1,26 @@
using System.Collections.Generic;
using Serilog;
using Wino.Core.Domain.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using SkiaSharp;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Reader;
namespace Wino.Core.Services
namespace Wino.Core.Services;
public class FontService() : IFontService
{
public class FontService : IFontService
private static readonly Lazy<List<string>> _availableFonts = new(InitializeFonts);
private static readonly List<string> _defaultFonts = ["Arial", "Calibri", "Trebuchet MS", "Tahoma", "Verdana", "Courier New", "Georgia", "Times New Roman"];
private static List<string> InitializeFonts()
{
private readonly IPreferencesService _preferencesService;
private ILogger _logger = Log.ForContext<FontService>();
// TODO: Skia used to get system fonts. This is a temporary solution to support UWP and WinUI together.
// After migration to WinUI, this code should be replaced with lightweight solution.
var fontFamilies = SKFontManager.Default.FontFamilies;
private readonly List<ReaderFontModel> _availableFonts =
[
new ReaderFontModel(ReaderFont.Arial, "Arial"),
new ReaderFontModel(ReaderFont.Calibri, "Calibri"),
new ReaderFontModel(ReaderFont.TimesNewRoman, "Times New Roman"),
new ReaderFontModel(ReaderFont.TrebuchetMS, "Trebuchet MS"),
new ReaderFontModel(ReaderFont.Tahoma, "Tahoma"),
new ReaderFontModel(ReaderFont.Verdana, "Verdana"),
new ReaderFontModel(ReaderFont.Georgia, "Georgia"),
new ReaderFontModel(ReaderFont.CourierNew, "Courier New")
];
List<string> combinedFonts = [.. fontFamilies, .. _defaultFonts];
public FontService(IPreferencesService preferencesService)
{
_preferencesService = preferencesService;
}
public List<ReaderFontModel> GetReaderFonts() => _availableFonts;
public void ChangeReaderFont(ReaderFont font)
{
_preferencesService.ReaderFont = font;
_logger.Information("Default reader font is changed to {Font}", font);
}
public void ChangeReaderFontSize(int size)
{
_preferencesService.ReaderFontSize = size;
_logger.Information("Default reader font size is changed to {Size}", size);
}
public ReaderFontModel GetCurrentReaderFont() => _availableFonts.Find(f => f.Font == _preferencesService.ReaderFont);
public int GetCurrentReaderFontSize() => _preferencesService.ReaderFontSize;
return [.. combinedFonts.Distinct().OrderBy(x => x)];
}
public List<string> GetFonts() => _availableFonts.Value;
}

View File

@@ -28,6 +28,7 @@ namespace Wino.Core.Services
private readonly ISignatureService _signatureService;
private readonly IThreadingStrategyProvider _threadingStrategyProvider;
private readonly IMimeFileService _mimeFileService;
private readonly IPreferencesService _preferencesService;
private readonly ILogger _logger = Log.ForContext<MailService>();
@@ -38,7 +39,8 @@ namespace Wino.Core.Services
IAccountService accountService,
ISignatureService signatureService,
IThreadingStrategyProvider threadingStrategyProvider,
IMimeFileService mimeFileService) : base(databaseService)
IMimeFileService mimeFileService,
IPreferencesService preferencesService) : base(databaseService)
{
_folderService = folderService;
_contactService = contactService;
@@ -46,6 +48,7 @@ namespace Wino.Core.Services
_signatureService = signatureService;
_threadingStrategyProvider = threadingStrategyProvider;
_mimeFileService = mimeFileService;
_preferencesService = preferencesService;
}
public async Task<MailCopy> CreateDraftAsync(MailAccount composerAccount,
@@ -651,6 +654,9 @@ namespace Wino.Core.Services
message.From.Add(new MailboxAddress(account.SenderName, account.Address));
// It contains empty blocks with inlined font, to make sure when users starts typing,it will follow selected font.
var gapHtml = CreateHtmlGap();
// Manage "To"
if (reason == DraftCreationReason.Reply || reason == DraftCreationReason.ReplyAll)
{
@@ -682,8 +688,7 @@ namespace Wino.Core.Services
{
message.InReplyTo = referenceMessage.MessageId;
foreach (var id in referenceMessage.References)
message.References.Add(id);
message.References.AddRange(referenceMessage.References);
message.References.Add(referenceMessage.MessageId);
}
@@ -711,14 +716,18 @@ namespace Wino.Core.Services
if (string.IsNullOrWhiteSpace(builder.HtmlBody))
{
builder.HtmlBody = $"<br><br><br>{signature.HtmlBody}";
builder.HtmlBody = $"{gapHtml}{signature.HtmlBody}";
}
else
{
builder.HtmlBody = $"<br><br><br>{signature.HtmlBody}" + builder.HtmlBody;
builder.HtmlBody = $"{gapHtml}{signature.HtmlBody}{gapHtml}{builder.HtmlBody}";
}
}
}
else
{
builder.HtmlBody = $"{gapHtml}{builder.HtmlBody}";
}
// Manage Subject
if (reason == DraftCreationReason.Forward && !referenceMessage.Subject.StartsWith("FW: ", StringComparison.OrdinalIgnoreCase))
@@ -794,7 +803,7 @@ namespace Wino.Core.Services
{
var htmlMimeInfo = string.Empty;
// Separation Line
htmlMimeInfo += "<br><br><hr style='display:inline-block;width:100%' tabindex='-1'>";
htmlMimeInfo += "<hr style='display:inline-block;width:100%' tabindex='-1'>";
var visitor = _mimeFileService.CreateHTMLPreviewVisitor(referenceMessage, string.Empty);
visitor.Visit(referenceMessage);
@@ -816,6 +825,12 @@ namespace Wino.Core.Services
return htmlMimeInfo;
}
string CreateHtmlGap()
{
var template = $"""<div style="font-family: '{_preferencesService.ComposerFont}', Arial, sans-serif; font-size: {_preferencesService.ComposerFontSize}px"><br></div>""";
return string.Concat(Enumerable.Repeat(template, 5));
}
static string ParticipantsToHtml(InternetAddressList internetAddresses) =>
string.Join("; ", internetAddresses.Mailboxes
.Select(x => $"{x.Name ?? Translator.UnknownSender} &lt;<a href=\"mailto:{x.Address ?? Translator.UnknownAddress}\">{x.Address ?? Translator.UnknownAddress}</a>&gt;"));