From d407c971ee9884ee8082327e92df9ad49aa3e21e Mon Sep 17 00:00:00 2001 From: Marco Gavelli Date: Mon, 16 Jul 2018 16:29:46 +0200 Subject: [PATCH] Added cover page --- .../BookHtmlContent.cs | 18 ++++++-- .../EpubViewerControl.xaml.cs | 41 ++++++++++++------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/BookHtmlContent.cs b/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/BookHtmlContent.cs index e60dd22..4972f5f 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/BookHtmlContent.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/BookHtmlContent.cs @@ -38,10 +38,20 @@ namespace QuickLook.Plugin.EpubViewer protected override async void OnImageLoad(HtmlImageLoadEventArgs args) { - string imageFilePath = GetFullPath(ChapterRef.ContentFileName, args.Src); - if (EpubBook.Content.Images.TryGetValue(imageFilePath, out EpubByteContentFileRef imageContent)) + string imageFilePath = ChapterRef != null ? GetFullPath(ChapterRef.ContentFileName, args.Src) : null; + byte[] imageBytes = null; + if (args.Src == "COVER") { - using (MemoryStream imageStream = new MemoryStream(await imageContent.ReadContentAsBytesAsync())) + imageBytes = await EpubBook.ReadCoverAsync(); + } + else if (EpubBook.Content.Images.TryGetValue(imageFilePath, out EpubByteContentFileRef imageContent)) + { + imageBytes = await imageContent.ReadContentAsBytesAsync(); + } + + if (imageBytes != null) + { + using (MemoryStream imageStream = new MemoryStream(imageBytes)) { try { @@ -58,7 +68,7 @@ namespace QuickLook.Plugin.EpubViewer { Debug.WriteLine($"Failed to load image: {args.Src}"); } - } + } } } diff --git a/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/EpubViewerControl.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/EpubViewerControl.xaml.cs index 5b242eb..e57323f 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/EpubViewerControl.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.EpubViewer/EpubViewerControl.xaml.cs @@ -24,7 +24,7 @@ namespace QuickLook.Plugin.EpubViewer public event PropertyChangedEventHandler PropertyChanged; - public string Chapter => chapterRefs != null ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : ""; + public string Chapter => chapterRefs != null && currChapter >= 0 ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : ""; public EpubViewerControl() { @@ -42,7 +42,7 @@ namespace QuickLook.Plugin.EpubViewer this.chapterRefs = Flatten(epubBook.GetChapters()); this.currChapter = -1; this.pagePanel.EpubBook = epubBook; - this.NextChapter(); + this.UpdateChapter(); } private List Flatten(List list) @@ -60,16 +60,12 @@ namespace QuickLook.Plugin.EpubViewer try { this.currChapter = Math.Min(this.currChapter + 1, chapterRefs.Count - 1); - this.pagePanel.ChapterRef = chapterRefs[currChapter]; - if (chapterRefs[currChapter].Anchor != null) - { - this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor); - } + this.UpdateChapter(); } catch (Exception ex) { Debug.WriteLine(ex); - this.pagePanel.Text = "
Invalid chapter.
"; + this.pagePanel.Text = "
Invalid chapter.
"; } OnPropertyChanged("Chapter"); OnChapterChanged(); @@ -84,12 +80,8 @@ namespace QuickLook.Plugin.EpubViewer { try { - this.currChapter = Math.Max(this.currChapter - 1, 0); - this.pagePanel.ChapterRef = chapterRefs[currChapter]; - if (chapterRefs[currChapter].Anchor != null) - { - this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor); - } + this.currChapter = Math.Max(this.currChapter - 1, -1); + this.UpdateChapter(); } catch (Exception ex) { @@ -138,5 +130,26 @@ namespace QuickLook.Plugin.EpubViewer public int NewChapter { get; set; } } + + private void UpdateChapter() + { + if (currChapter < 0) + { + this.pagePanel.ChapterRef = null; + this.pagePanel.Text = string.Format(@" +
+ +
{0}
+
", epubBook.Title); + } + else + { + this.pagePanel.ChapterRef = chapterRefs[currChapter]; + if (chapterRefs[currChapter].Anchor != null) + { + this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor); + } + } + } } }