Skip to content

Commit

Permalink
Better fit to content webview
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed Nov 10, 2024
1 parent 9b7a11f commit cf41bc3
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 46 deletions.
3 changes: 1 addition & 2 deletions LRReader.Shared/LRReader.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.1;uap10.0.17763</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<ExtrasUwpMetaPackageVersion>6.2.14</ExtrasUwpMetaPackageVersion>
<TargetPlatformVersion>10.0.22621.0</TargetPlatformVersion>
Expand All @@ -27,7 +27,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="RestSharp" Version="112.1.0" />
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="112.1.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions LRReader.Shared/Services/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AppCenter.Crashes;
#endif
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;
using System;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -33,7 +32,7 @@ public bool RefreshSettings(ServerProfile profile)
return false;
var options = new RestClientOptions(profile.ServerAddress) { UserAgent = "LRReader" };
Client?.Dispose();
Client = new RestClient(options, configureSerialization: s => s.UseNewtonsoftJson(), useClientFactory: true);
Client = new RestClient(options, useClientFactory: true);
if (!string.IsNullOrEmpty(profile.ServerApiKey))
{
var base64Key = Convert.ToBase64String(Encoding.UTF8.GetBytes(profile.ServerApiKey));
Expand Down
3 changes: 1 addition & 2 deletions LRReader.Shared/Services/Updates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using LRReader.Shared.Models;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

namespace LRReader.Shared.Services
{
Expand All @@ -29,7 +28,7 @@ public UpdatesService(PlatformService platform, ISettingsStorageService settings
var uri = new Uri("https://api.guerra24.net/");
#endif
var options = new RestClientOptions(uri) { UserAgent = "LRReader" };
client = new RestClient(options, configureSerialization: s => s.UseNewtonsoftJson());
client = new RestClient(options);
}

public abstract Task<CheckForUpdatesResult> CheckForUpdates();
Expand Down
20 changes: 0 additions & 20 deletions LRReader.Shared/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@
"System.Text.Json": "8.0.4"
}
},
"RestSharp.Serializers.NewtonsoftJson": {
"type": "Direct",
"requested": "[112.1.0, )",
"resolved": "112.1.0",
"contentHash": "d/Ro5KeCKw2p0XVQrAKG0CUiG6VO4YeXLxIm5sHj8fwaIk2IlrtMOKoJJprsEUyJfq1Z9Qv8E1yN2rPFPuRVzw==",
"dependencies": {
"Newtonsoft.Json": "13.0.3",
"RestSharp": "112.1.0"
}
},
"SixLabors.ImageSharp": {
"type": "Direct",
"requested": "[2.1.9, )",
Expand Down Expand Up @@ -546,16 +536,6 @@
"System.Text.Json": "8.0.4"
}
},
"RestSharp.Serializers.NewtonsoftJson": {
"type": "Direct",
"requested": "[112.1.0, )",
"resolved": "112.1.0",
"contentHash": "d/Ro5KeCKw2p0XVQrAKG0CUiG6VO4YeXLxIm5sHj8fwaIk2IlrtMOKoJJprsEUyJfq1Z9Qv8E1yN2rPFPuRVzw==",
"dependencies": {
"Newtonsoft.Json": "13.0.3",
"RestSharp": "112.1.0"
}
},
"SixLabors.ImageSharp": {
"type": "Direct",
"requested": "[2.1.9, )",
Expand Down
53 changes: 48 additions & 5 deletions LRReader.UWP/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,14 @@ public static class WebViewExt

private static readonly MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();

public static void SetMarkdown(WebView webView, string markdown)
public static void SetMarkdown(this WebView webView, string markdown)
{
webView.SetValue(MarkdownProperty, markdown);
if (!(bool)webView.GetValue(MarkdownReadyProperty))
{
webView.NavigationCompleted += async (sender, args) =>
webView.ScriptNotify += (sender, args) =>
{
var heightString = await webView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (int.TryParse(heightString, out var height))
if (double.TryParse(args.Value, out var height))
webView.Height = height;
};
webView.SetValue(MarkdownReadyProperty, true);
Expand All @@ -139,7 +138,51 @@ public static void SetMarkdownBase(this WebView webView, string markdown)
var color = (Color)Application.Current.Resources["TextFillColorPrimary"];
var selectedColor = (Color)Application.Current.Resources["TextOnAccentFillColorSelectedText"];
var selectedBg = (Color)Application.Current.Resources["SystemAccentColor"];
webView.NavigateToString($"<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>md</title><style>body{{font-family: \"Segoe UI\"; font-size: 14px; color: #{color.R:X2}{color.G:X2}{color.B:X2}; margin: 0;}} ::selection{{color: #{selectedColor.R:X2}{selectedColor.G:X2}{selectedColor.B:X2}; background-color: #{selectedBg.R:X2}{selectedBg.G:X2}{selectedBg.B:X2};}} img {{max-width: 80%;}}</style></head><body>{Markdown.ToHtml(markdown, pipeline)}</body></html>");
webView.NavigateToString($$"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>md</title>
<style>
body {
font-family: "Segoe UI";
font-size: 14px;
color: #{{color.R:X2}}{{color.G:X2}}{{color.B:X2}};
margin: 0;
}
::selection {
color: #{{selectedColor.R:X2}}{{selectedColor.G:X2}}{{selectedColor.B:X2}};
background-color: #{{selectedBg.R:X2}}{{selectedBg.G:X2}}{{selectedBg.B:X2}};
}
img {
max-width: 80%;
}
* {
box-sizing: border-box;
}
</style>
<script>
window.addEventListener("resize", () => {
const rect = document.querySelector('html').getBoundingClientRect();
window.external.notify(rect.height.toString());
});
document.addEventListener("DOMContentLoaded", (event) => {
const rect = document.querySelector('html').getBoundingClientRect();
window.external.notify(rect.height.toString());
requestAnimationFrame(() => {
const rect = document.querySelector('html').getBoundingClientRect();
window.external.notify(rect.height.toString());
});
});
</script>
</head>
<body>
{{Markdown.ToHtml(markdown, pipeline)}}
</body>
</html>
""");
webView.NavigationStarting += (sender, args) =>
{
args.Cancel = true;
Expand Down
4 changes: 2 additions & 2 deletions LRReader.UWP/LRReader.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>LRReader.UWP</AssemblyName>
<DefaultLanguage>en</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.26100.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand All @@ -30,7 +30,7 @@
<AppxPackageSigningTimestampServerUrl>http://timestamp.digicert.com</AppxPackageSigningTimestampServerUrl>
<Win32Resource>Resources\MiddleClickScrolling-CursorType.res</Win32Resource>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
Expand Down
4 changes: 2 additions & 2 deletions LRReader.UWP/Views/Dialogs/MarkdownDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
MinHeight="{ThemeResource ContentDialogMinHeight}"
MaxWidth="670"
MaxHeight="{ThemeResource ContentDialogMaxHeight}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
Background="{TemplateBinding Background}"
BackgroundSizing="InnerBorderEdge"
BorderBrush="{TemplateBinding BorderBrush}"
Expand Down Expand Up @@ -267,5 +267,5 @@
</Style>
</ContentDialog.Resources>

<WebView x:Name="WebView" DefaultBackgroundColor="Transparent" />
<WebView x:Name="WebView" MaxHeight="612" DefaultBackgroundColor="Transparent" />
</ContentDialog>
2 changes: 1 addition & 1 deletion LRReader.UWP/Views/Dialogs/MarkdownDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public MarkdownDialog(string title, string text)
{
this.InitializeComponent();
this.Title = title;
WebView.SetMarkdownBase(text);
WebView.SetMarkdown(text);
}
}
}
10 changes: 0 additions & 10 deletions LRReader.UWP/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,6 @@
"System.Text.Json": "8.0.4"
}
},
"RestSharp.Serializers.NewtonsoftJson": {
"type": "Transitive",
"resolved": "112.1.0",
"contentHash": "d/Ro5KeCKw2p0XVQrAKG0CUiG6VO4YeXLxIm5sHj8fwaIk2IlrtMOKoJJprsEUyJfq1Z9Qv8E1yN2rPFPuRVzw==",
"dependencies": {
"Newtonsoft.Json": "13.0.3",
"RestSharp": "112.1.0"
}
},
"runtime.win10-arm.Microsoft.Net.Native.Compiler": {
"type": "Transitive",
"resolved": "2.2.12-rel-31116-00",
Expand Down Expand Up @@ -628,7 +619,6 @@
"NReco.Logging.File": "[1.2.1, )",
"Newtonsoft.Json": "[13.0.3, )",
"RestSharp": "[112.1.0, )",
"RestSharp.Serializers.NewtonsoftJson": "[112.1.0, )",
"SixLabors.ImageSharp": "[2.1.9, )"
}
}
Expand Down

0 comments on commit cf41bc3

Please sign in to comment.