ChapterChanged event

This commit is contained in:
Marco Gavelli
2018-07-16 14:17:45 +02:00
parent 02911d9550
commit 3e251c35ec
5 changed files with 57 additions and 48 deletions

View File

@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using TheArtOfDev.HtmlRenderer.Core.Entities;
@@ -46,15 +43,22 @@ namespace QuickLook.Plugin.EpubViewer
{
using (MemoryStream imageStream = new MemoryStream(await imageContent.ReadContentAsBytesAsync()))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = imageStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
args.Callback(bitmapImage);
}
args.Handled = true;
try
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = imageStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
args.Callback(bitmapImage);
args.Handled = true;
}
catch
{
Debug.WriteLine($"Failed to load image: {args.Src}");
}
}
}
}

View File

@@ -39,7 +39,8 @@
Style="{DynamicResource CaptionTextButtonStyle}"/>
</StackPanel>
</Grid>
<local:BookHtmlContent x:Name="pagePanel"
Grid.Row="1"/>
<Grid Grid.Row="1">
<local:BookHtmlContent x:Name="pagePanel"/>
</Grid>
</Grid>
</UserControl>

View File

@@ -4,17 +4,9 @@ using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VersOne.Epub;
namespace QuickLook.Plugin.EpubViewer
@@ -24,13 +16,15 @@ namespace QuickLook.Plugin.EpubViewer
/// </summary>
public partial class EpubViewerControl : UserControl, INotifyPropertyChanged
{
public event EventHandler<ChapterChangedEventArgs> ChapterChanged;
private EpubBookRef epubBook;
private List<EpubChapterRef> chapterRefs;
private int currChapter;
public event PropertyChangedEventHandler PropertyChanged;
public string Chapter => $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})";
public string Chapter => chapterRefs != null ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : "";
public EpubViewerControl()
{
@@ -46,10 +40,9 @@ namespace QuickLook.Plugin.EpubViewer
{
this.epubBook = epubBook;
this.chapterRefs = Flatten(epubBook.GetChapters());
this.currChapter = 0;
this.currChapter = -1;
this.pagePanel.EpubBook = epubBook;
this.pagePanel.ChapterRef = chapterRefs[currChapter];
OnPropertyChanged("Chapter");
this.NextChapter();
}
private List<EpubChapterRef> Flatten(List<EpubChapterRef> list)
@@ -72,14 +65,14 @@ namespace QuickLook.Plugin.EpubViewer
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
OnPropertyChanged("Chapter");
}
catch (Exception ex)
{
this.pagePanel.Text = "<div>Invalid chapter.</div>";
OnPropertyChanged("Chapter");
{
Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>";
}
OnPropertyChanged("Chapter");
OnChapterChanged();
}
private void PrevButton_Click(object sender, RoutedEventArgs e)
@@ -97,14 +90,14 @@ namespace QuickLook.Plugin.EpubViewer
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
OnPropertyChanged("Chapter");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>";
OnPropertyChanged("Chapter");
this.pagePanel.Text = "<div>Invalid chapter.</div>";
}
OnPropertyChanged("Chapter");
OnChapterChanged();
}
// Create the OnPropertyChanged method to raise the event
@@ -113,9 +106,13 @@ namespace QuickLook.Plugin.EpubViewer
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
protected void OnChapterChanged()
{
ChapterChanged?.Invoke(this, new ChapterChangedEventArgs(currChapter));
}
private void Grid_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left)
{
this.PrevChapter();
@@ -131,5 +128,15 @@ namespace QuickLook.Plugin.EpubViewer
e.Handled = false;
}
}
public class ChapterChangedEventArgs : EventArgs
{
public ChapterChangedEventArgs(int currChapter)
{
this.NewChapter = currChapter;
}
public int NewChapter { get; set; }
}
}
}

View File

@@ -1,22 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.HtmlViewer;
using VersOne.Epub;
namespace QuickLook.Plugin.EpubViewer
{
public class Plugin : IViewer
{
private EpubViewerControl _panel;
private ContextObject _context;
private EpubViewerControl _epubControl;
public int Priority => int.MaxValue;
public void Init()
@@ -30,28 +26,30 @@ namespace QuickLook.Plugin.EpubViewer
public void Cleanup()
{
_epubControl = null;
_context = null;
}
public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size { Width = 1000, Height = 600 };
_context = context;
context.SetPreferredSizeFit(new Size { Width = 1000, Height = 600 }, 0.8);
}
public void View(string path, ContextObject context)
{
_panel = new EpubViewerControl();
context.ViewerContent = _panel;
_epubControl = new EpubViewerControl();
context.ViewerContent = _epubControl;
Exception exception = null;
_panel.Dispatcher.BeginInvoke(new Action(() =>
_epubControl.Dispatcher.BeginInvoke(new Action(() =>
{
try
{
// Opens a book
EpubBookRef epubBook = EpubReader.OpenBook(path);
context.Title = epubBook.Title;
_panel.SetContent(epubBook);
_epubControl.SetContent(epubBook);
context.IsBusy = false;
}
catch (Exception e)

View File

@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Le informazioni generali relative a un assembly sono controllate dal seguente