Skip to content

Commit

Permalink
Merge pull request #6 from michaldivis/vNext
Browse files Browse the repository at this point in the history
v1.0.4
  • Loading branch information
michaldivis authored Feb 20, 2023
2 parents 5bb4bdb + 4263af9 commit a1ff49a
Show file tree
Hide file tree
Showing 18 changed files with 802 additions and 712 deletions.
8 changes: 8 additions & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
"type": "boolean",
"description": "Disables displaying the NUKE logo"
},
"NugetApiKey": {
"type": "string"
},
"NugetApiUrl": {
"type": "string"
},
"Partition": {
"type": "string",
"description": "Partition to use on CI"
Expand Down Expand Up @@ -67,6 +73,7 @@
"Clean",
"Compile",
"Pack",
"PushNuget",
"Restore"
]
}
Expand All @@ -84,6 +91,7 @@
"Clean",
"Compile",
"Pack",
"PushNuget",
"Restore"
]
}
Expand Down
56 changes: 34 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ A WPF user control for displaying in memory HTML. The control stores the loaded

### Basics
Add the namespace to your XAML
```XAML
xmlns:darkhtmlviewer="clr-namespace:DarkHtmlViewer;assembly=DarkHtmlViewer"
```XML
xmlns:dhv="clr-namespace:DarkHtmlViewer;assembly=DarkHtmlViewer"
```

And use the `HtmlViewer` control
```XAML
<darkhtmlviewer:HtmlViewer x:Name="htmlViewer" />
```XML
<dhv:HtmlViewer x:Name="htmlViewer" />
```

### Commands & methods
Expand All @@ -30,11 +30,11 @@ And use the `HtmlViewer` control

### Loading HTML content
To load content into the viewer, bind an HTML string to it's `HtmlContent` property
```XAML
<darkhtmlviewer:HtmlViewer x:Name="htmlViewer" HtmlContent="{Binding MyHtmlString}" />
```XML
<dhv:HtmlViewer x:Name="htmlViewer" HtmlContent="{Binding MyHtmlString}" />
```
or use the `LoadCommand` and pass the HTML string as the `CommandParameter`
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=LoadCommand}"
CommandParameter="{Binding MyHtmlString}"
Expand All @@ -45,17 +45,17 @@ or use the `LoadCommand` and pass the HTML string as the `CommandParameter`
Whenever a link is clicked in the loaded HTML file, the control fires the `LinkClickedCommand`. Bind you own command to that in order to handle link clicks, example:

View.cs
```XAML
<darkhtmlviewer:HtmlViewer
```XML
<dhv:HtmlViewer
x:Name="htmlViewer"
LinkClickedCommand="{Binding MyLinkClickedCommand}" />
```

ViewModel.cs
```Csharp
public ICommand MyLinkClickedCommand => new DarkCommand<string>(HandleLinkClick);
public ICommand MyLinkClickedCommand => new RelayCommand<string>(HandleLinkClick);

private void HandleLinkClick(string link)
private void HandleLinkClick(string? link)
{
Debug.WriteLine($"Link clicked: {link}");
}
Expand All @@ -65,15 +65,15 @@ private void HandleLinkClick(string link)
To scroll to a specific element id, you have several options.

`ScrollCommand`: tries to scroll to a specific element in the currently loaded HTML file
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=ScrollCommand}"
CommandParameter="elementId"
Content="Scroll to elementId" />
```

`ScrollOnNextLoadCommand`: will try to scroll to a specific element in the next loaded HTML file
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=ScrollOnNextLoadCommand}"
CommandParameter="elementId"
Expand All @@ -84,7 +84,7 @@ To scroll to a specific element id, you have several options.
Saves the current scroll position and tries to restore it next time HTML content is loaded. If `ScrollOnNextLoad` is used as well, this will be ignored

`SaveScrollPositionForNextLoadCommand`: will try to scroll to a specific element in the next loaded HTML file
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=SaveScrollPositionForNextLoadCommand}"
Content="Save scroll position for next load" />
Expand All @@ -93,15 +93,15 @@ Saves the current scroll position and tries to restore it next time HTML content
### Search

`SearchCommand`: finds a search term on the current page
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=SearchCommand}"
CommandParameter="search text"
Content="Search for text" />
```

`SearchOnNextLoadCommand`: finds a search term in the next loaded HTML file
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=SearchOnNextLoadCommand}"
CommandParameter="search text"
Expand All @@ -111,7 +111,7 @@ Saves the current scroll position and tries to restore it next time HTML content
### Printing

The `PrintCommand` can be used to bring up the default print dialog window.
```XAML
```XML
<Button
Command="{Binding ElementName=htmlViewer, Path=PrintCommand}"
Content="Show print dialog" />
Expand All @@ -120,21 +120,22 @@ The `PrintCommand` can be used to bring up the default print dialog window.
### Logging
Enable logging for the control by configuring an `ILoggerFactory` provider like so:
```csharp
using DarkHtmlViewer;
var loggerFactory = LoggerFactory.Create(c =>
{
c.SetMinimumLevel(LogLevel.Debug);
c.AddDebug();
});

HtmlViewer.ConfigureLogger(() => NullLoggerFactory.Instance);
HtmlViewer.ConfigureLogger(() => loggerFactory);
```

### Virtual host name to folder path mapping
See [this page](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.setvirtualhostnametofoldermapping?view=webview2-dotnet-1.0.1108.44) to learn more.

Enable virtual host name to folder path mapping like so:
```csharp
using DarkHtmlViewer;

HtmlViewer.ConfigureVirtualHostNameToFolderMappingSettings(new VirtualHostNameToFolderMappingSettings
{
IsEnabled = true,
Hostname = "myfiles.local",
FolderPath = @"C:\Resources\MyFiles",
AccessKind = Microsoft.Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow
Expand All @@ -150,4 +151,15 @@ You can then access your assets like this in HTML:
<body>
<img src="https://myfiles.local/my_image.jpg" />
</body>
```

### Default browser background color

Configure the default background color of the control like so:

```csharp
using System.Drawing;

var backgroundColor = Color.FromArgb(255, 24, 24, 24);
HtmlViewer.ConfigureDefaultBackgroundColor(backgroundColor);
```
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="6.1.1" />
<PackageReference Include="Nuke.Common" Version="6.3.0" />
</ItemGroup>

</Project>
15 changes: 13 additions & 2 deletions demos/DarkHtmlViewerBasicDemo/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
x:Class="DarkHtmlViewerBasicDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DarkHtmlViewerBasicDemo">
<Application.Resources />
xmlns:local="clr-namespace:DarkHtmlViewerBasicDemo"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Light"
PrimaryColor="Indigo"
SecondaryColor="Teal" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
46 changes: 12 additions & 34 deletions demos/DarkHtmlViewerBasicDemo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
using DarkHtmlViewer;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging;
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace DarkHtmlViewerBasicDemo;

public partial class App : Application
{
protected override async void OnStartup(StartupEventArgs e)
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

await CheckCompatibilityAsync();
CheckCompatibility();

//configure logger
HtmlViewer.ConfigureLogger(() => NullLoggerFactory.Instance);
HtmlViewer.ConfigureLogger(() => LoggerFactory.Create(c =>
{
c.SetMinimumLevel(LogLevel.Debug);
c.AddDebug();
}));

var appLocation = Assembly.GetExecutingAssembly().Location;
var htmlAssetsDir = Path.Combine(Path.GetDirectoryName(appLocation), "Files");
var appDirName = Path.GetDirectoryName(appLocation)!;
var htmlAssetsDir = Path.Combine(appDirName, "Files");

HtmlViewer.ConfigureVirtualHostNameToFolderMappingSettings(new VirtualHostNameToFolderMappingSettings
{
IsEnabled = true,
Hostname = "darkassets.local",
FolderPath = htmlAssetsDir,
AccessKind = Microsoft.Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow
});

HtmlViewer.ConfigureDefaultBackgroundColor(Color.Navy);
HtmlViewer.ConfigureDefaultBackgroundColor(Color.FromArgb(255, 24, 24, 24));

MainWindow = new DemoView();
MainWindow.Show();
}

private static async Task CheckCompatibilityAsync()
private static void CheckCompatibility()
{
var basicWatch = new System.Diagnostics.Stopwatch();
var fullWatch = new System.Diagnostics.Stopwatch();

//check compatibility - basic
try
{
basicWatch.Start();
HtmlViewer.CheckBasicCompatibility();
basicWatch.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return;

//check compatibility - full
try
{
fullWatch.Start();
await HtmlViewer.CheckFullCompatibilityAsync();
fullWatch.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

Console.WriteLine($"Basic: {basicWatch.ElapsedMilliseconds}ms");
Console.WriteLine($"Full: {fullWatch.ElapsedMilliseconds}ms");
Console.WriteLine();
}
}
9 changes: 8 additions & 1 deletion demos/DarkHtmlViewerBasicDemo/DarkHtmlViewerBasicDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,6 +25,12 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.7.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DarkHtmlViewer\DarkHtmlViewer.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions demos/DarkHtmlViewerBasicDemo/DemoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class DemoItem
{
public string Title { get; set; }
public string ItemCode { get; set; }
public string Html { get; set; }
public required string Title { get; init; }
public required string ItemCode { get; init; }
public required string Html { get; init; }
}
Loading

0 comments on commit a1ff49a

Please sign in to comment.