Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SvgPreviewControl: Add Background so that white .svg are visible (#25397
) * SvgPreviewControl: Add checkered background so that white .svg are visible * SvgPreviewControl: Move preview generation logic into own class * SvgPreviewControl: Add possibility to configure background of svg preview pane via the settings ui * SvgPreviewControl: Take user configuration into consideration when generating svg preview * SvgPreviewControl: Do not generate preview file, if the actual size is under the 2mb limiation of WebView2 * SvgPreviewControl: Introduce SvgPreviewColorMode enumeration instead of using magic values * SvgPreviewControl: Add additional checkered pattern shades * SvgPreviewControl: Use newly introduced enums as default values
- Loading branch information
Showing
11 changed files
with
328 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.PowerToys.Settings.UI.Library; | ||
|
||
namespace SvgPreviewHandler | ||
{ | ||
internal sealed class Settings | ||
{ | ||
private static readonly SettingsUtils ModuleSettings = new SettingsUtils(); | ||
|
||
public int ColorMode | ||
{ | ||
get | ||
{ | ||
try | ||
{ | ||
return ModuleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.SvgBackgroundColorMode.Value; | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return PowerPreviewProperties.DefaultSvgBackgroundColorMode; | ||
} | ||
} | ||
} | ||
|
||
public Color SolidColor | ||
{ | ||
get | ||
{ | ||
try | ||
{ | ||
var colorString = ModuleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.SvgBackgroundSolidColor.Value; | ||
return ColorTranslator.FromHtml(colorString); | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return ColorTranslator.FromHtml(PowerPreviewProperties.DefaultSvgBackgroundSolidColor); | ||
} | ||
} | ||
} | ||
|
||
public Color ThemeColor | ||
{ | ||
get | ||
{ | ||
if (Common.UI.ThemeManager.GetWindowsBaseColor().ToLowerInvariant() == "dark") | ||
{ | ||
return ColorTranslator.FromHtml("#1e1e1e"); | ||
} | ||
else | ||
{ | ||
return Color.White; | ||
} | ||
} | ||
} | ||
|
||
public int CheckeredShade | ||
{ | ||
get | ||
{ | ||
try | ||
{ | ||
return ModuleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.SvgBackgroundCheckeredShade.Value; | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return PowerPreviewProperties.DefaultSvgBackgroundCheckeredShade; | ||
} | ||
} | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/modules/previewpane/SvgPreviewHandler/SvgHTMLPreviewGenerator.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,64 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Globalization; | ||
using Settings.UI.Library.Enumerations; | ||
|
||
namespace SvgPreviewHandler | ||
{ | ||
internal sealed class SvgHTMLPreviewGenerator | ||
{ | ||
private const string CheckeredBackgroundShadeLight = """ | ||
url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TiiIVBzOIOGSogmBBVMRRqlgEC6Wt0KqDyaVf0KQhSXFxFFwLDn4sVh1cnHV1cBUEwQ8QVxcnRRcp8X9JoUWMB8f9eHfvcfcOEBoVplmhCUDTbTMVj0nZ3KrU/YoQRIQxBlFmlpFIL2bgO77uEeDrXZRn+Z/7c/SpeYsBAYl4jhmmTbxBPLNpG5z3iUVWklXic+Jxky5I/Mh1xeM3zkWXBZ4pmpnUPLFILBU7WOlgVjI14mniiKrplC9kPVY5b3HWKjXWuid/YTivr6S5TnMYcSwhgSQkKKihjApsRGnVSbGQov2Yj3/I9SfJpZCrDEaOBVShQXb94H/wu1urMDXpJYVjQNeL43yMAN27QLPuON/HjtM8AYLPwJXe9lcbwOwn6fW2FjkC+reBi+u2puwBlzvA4JMhm7IrBWkKhQLwfkbflAMGboHeNa+31j5OH4AMdbV8AxwcAqNFyl73eXdPZ2//nmn19wOEPHKuuso0oQAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+cEFAwrEI+z8/sAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAL0lEQVQoz2O8e/cuAwzcv38fzlZUVMQqzsRAIqC9BhZi3I0sPhj9QIy7R+OB5hoACxUaWr81wGUAAAAASUVORK5CYII='); | ||
"""; | ||
|
||
private const string CheckeredBackgroundShadeMedium = """ | ||
url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TiiIVBzOIOGSogmBBVMRRqlgEC6Wt0KqDyaVf0KQhSXFxFFwLDn4sVh1cnHV1cBUEwQ8QVxcnRRcp8X9JoUWMB8f9eHfvcfcOEBoVplmhCUDTbTMVj0nZ3KrU/YoQRIQxBlFmlpFIL2bgO77uEeDrXZRn+Z/7c/SpeYsBAYl4jhmmTbxBPLNpG5z3iUVWklXic+Jxky5I/Mh1xeM3zkWXBZ4pmpnUPLFILBU7WOlgVjI14mniiKrplC9kPVY5b3HWKjXWuid/YTivr6S5TnMYcSwhgSQkKKihjApsRGnVSbGQov2Yj3/I9SfJpZCrDEaOBVShQXb94H/wu1urMDXpJYVjQNeL43yMAN27QLPuON/HjtM8AYLPwJXe9lcbwOwn6fW2FjkC+reBi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XwEKMu5HFB6MfiHH3aDzQXAMACHkZChhWp4QAAAAASUVORK5CYII='); | ||
"""; | ||
|
||
private const string HtmlTemplateSolidColor = """ | ||
<html> | ||
<body style="background-color: {0}"> | ||
{1} | ||
</body> | ||
</html> | ||
"""; | ||
|
||
private const string HtmlTemplateCheckered = """ | ||
<html> | ||
<body style="background-image: {0}"> | ||
{1} | ||
</body> | ||
</html> | ||
"""; | ||
|
||
private readonly Settings settings = new(); | ||
|
||
public string GeneratePreview(string svgData) | ||
{ | ||
var colorMode = (SvgPreviewColorMode)settings.ColorMode; | ||
return colorMode switch | ||
{ | ||
SvgPreviewColorMode.SolidColor => string.Format(CultureInfo.InvariantCulture, HtmlTemplateSolidColor, ColorTranslator.ToHtml(settings.SolidColor), svgData), | ||
SvgPreviewColorMode.Checkered => string.Format(CultureInfo.InvariantCulture, HtmlTemplateCheckered, GetConfiguredCheckeredShadeImage(), svgData), | ||
SvgPreviewColorMode.Default or _ => string.Format(CultureInfo.InvariantCulture, HtmlTemplateSolidColor, ColorTranslator.ToHtml(settings.ThemeColor), svgData), | ||
}; | ||
} | ||
|
||
private string GetConfiguredCheckeredShadeImage() | ||
{ | ||
var checkeredShade = (SvgPreviewCheckeredShade)settings.CheckeredShade; | ||
return checkeredShade switch | ||
{ | ||
SvgPreviewCheckeredShade.Light=> CheckeredBackgroundShadeLight, | ||
SvgPreviewCheckeredShade.Medium => CheckeredBackgroundShadeMedium, | ||
SvgPreviewCheckeredShade.Dark or _ => CheckeredBackgroundShadeDark, | ||
}; | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/settings-ui/Settings.UI.Library/Enumerations/SvgPreviewCheckeredShade.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,13 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Settings.UI.Library.Enumerations | ||
{ | ||
public enum SvgPreviewCheckeredShade | ||
{ | ||
Light, | ||
Medium, | ||
Dark, | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/settings-ui/Settings.UI.Library/Enumerations/SvgPreviewColorMode.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,13 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Settings.UI.Library.Enumerations | ||
{ | ||
public enum SvgPreviewColorMode | ||
{ | ||
Default, | ||
SolidColor, | ||
Checkered, | ||
} | ||
} |
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
Oops, something went wrong.