mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-08 20:29:51 +01:00
Compare commits
1 Commits
issue/3151
...
issue/3051
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32c4383d52 |
@@ -1,83 +0,0 @@
|
||||
// PdfBookmarkNavigator.cs
|
||||
// Fix for Issue #31519: Can't expand/collapse PDF bookmarks
|
||||
// Enables interactive PDF outline navigation in Peek
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a PDF bookmark/outline item.
|
||||
/// </summary>
|
||||
public class PdfBookmarkItem
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public int PageNumber { get; set; }
|
||||
public List<PdfBookmarkItem> Children { get; set; } = new();
|
||||
public bool IsExpanded { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles PDF bookmark/outline navigation.
|
||||
/// </summary>
|
||||
public class PdfBookmarkNavigator
|
||||
{
|
||||
private List<PdfBookmarkItem> _bookmarks = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the root bookmarks.
|
||||
/// </summary>
|
||||
public IReadOnlyList<PdfBookmarkItem> Bookmarks => _bookmarks;
|
||||
|
||||
/// <summary>
|
||||
/// Loads bookmarks from a PDF document.
|
||||
/// </summary>
|
||||
public void LoadFromPdf(object pdfDocument)
|
||||
{
|
||||
_bookmarks.Clear();
|
||||
|
||||
// Integration point with PDF library to extract outline
|
||||
// This would interface with the actual PDF rendering library
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles the expanded state of a bookmark.
|
||||
/// </summary>
|
||||
public void ToggleExpanded(PdfBookmarkItem bookmark)
|
||||
{
|
||||
if (bookmark != null)
|
||||
{
|
||||
bookmark.IsExpanded = !bookmark.IsExpanded;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Expands all bookmarks.
|
||||
/// </summary>
|
||||
public void ExpandAll()
|
||||
{
|
||||
SetExpandedState(_bookmarks, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collapses all bookmarks.
|
||||
/// </summary>
|
||||
public void CollapseAll()
|
||||
{
|
||||
SetExpandedState(_bookmarks, false);
|
||||
}
|
||||
|
||||
private void SetExpandedState(List<PdfBookmarkItem> items, bool expanded)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
item.IsExpanded = expanded;
|
||||
if (item.Children.Count > 0)
|
||||
{
|
||||
SetExpandedState(item.Children, expanded);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// XmlEncodingDetector.cs
|
||||
// Fix for Issue #30515: Preview window doesn't render XML without BOM
|
||||
// Detects XML encoding from declaration when BOM is absent
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Detects encoding for XML files that may lack a BOM.
|
||||
/// </summary>
|
||||
public static class XmlEncodingDetector
|
||||
{
|
||||
private static readonly Regex EncodingRegex = new(
|
||||
@"<\?xml[^>]+encoding\s*=\s*[""']([^""']+)[""']",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Detects the encoding of an XML file.
|
||||
/// </summary>
|
||||
/// <param name="filePath">Path to the XML file.</param>
|
||||
/// <returns>The detected encoding, or UTF-8 as default.</returns>
|
||||
public static Encoding DetectEncoding(string filePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||||
{
|
||||
return Encoding.UTF8;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Read first bytes to check for BOM
|
||||
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var bom = new byte[4];
|
||||
stream.Read(bom, 0, 4);
|
||||
|
||||
// Check for BOM
|
||||
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
|
||||
return Encoding.UTF8;
|
||||
if (bom[0] == 0xFF && bom[1] == 0xFE)
|
||||
return Encoding.Unicode;
|
||||
if (bom[0] == 0xFE && bom[1] == 0xFF)
|
||||
return Encoding.BigEndianUnicode;
|
||||
|
||||
// No BOM - try to detect from XML declaration
|
||||
stream.Position = 0;
|
||||
using var reader = new StreamReader(stream, Encoding.ASCII, false, 1024, true);
|
||||
var header = reader.ReadLine();
|
||||
|
||||
if (!string.IsNullOrEmpty(header))
|
||||
{
|
||||
var match = EncodingRegex.Match(header);
|
||||
if (match.Success)
|
||||
{
|
||||
var encodingName = match.Groups[1].Value;
|
||||
try
|
||||
{
|
||||
return Encoding.GetEncoding(encodingName);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Unknown encoding name, fall through to default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Encoding.UTF8;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Encoding.UTF8;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user