Code reformat and UI tweaks

This commit is contained in:
Paddy Xu
2018-07-24 22:06:06 +03:00
parent c1733a39fd
commit e7aee219b3
59 changed files with 1490 additions and 703 deletions

View File

@@ -1,118 +1,120 @@
using System;
// Copyright © 2018 Marco Gavelli and Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using QuickLook.Common.Annotations;
using QuickLook.Common.Plugin;
using VersOne.Epub;
namespace QuickLook.Plugin.EpubViewer
{
/// <summary>
/// Logica di interazione per EpubViewerControl.xaml
/// </summary>
public partial class EpubViewerControl : UserControl, INotifyPropertyChanged
public partial class EpubViewerControl : IDisposable
{
public event EventHandler<ChapterChangedEventArgs> ChapterChanged;
private ContextObject _context;
private List<EpubChapterRef> _chapterRefs;
private int _currChapter;
private EpubBookRef epubBook;
private List<EpubChapterRef> chapterRefs;
private int currChapter;
private EpubBookRef _epubBook;
public event PropertyChangedEventHandler PropertyChanged;
public string Chapter => chapterRefs != null && currChapter >= 0 ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : "";
public EpubViewerControl()
public EpubViewerControl(ContextObject context)
{
_context = context;
InitializeComponent();
// design-time only
Resources.MergedDictionaries.Clear();
this.DataContext = this;
DataContext = this;
buttonPrevChapter.Click += (sender, e) => PrevChapter();
buttonNextChapter.Click += (sender, e) => NextChapter();
}
public string Chapter => _chapterRefs != null && _currChapter >= 0
? $"{_chapterRefs?[_currChapter].Title} ({_currChapter + 1}/{_chapterRefs?.Count})"
: "";
public void Dispose()
{
_chapterRefs.Clear();
_epubBook?.Dispose();
_epubBook = null;
}
internal void SetContent(EpubBookRef epubBook)
{
this.epubBook = epubBook;
this.chapterRefs = Flatten(epubBook.GetChapters());
this.currChapter = -1;
this.pagePanel.EpubBook = epubBook;
this.UpdateChapter();
_epubBook = epubBook;
_chapterRefs = Flatten(epubBook.GetChapters());
_currChapter = -1;
pagePanel.EpubBook = epubBook;
UpdateChapter();
}
private List<EpubChapterRef> Flatten(List<EpubChapterRef> list)
private static List<EpubChapterRef> Flatten(IEnumerable<EpubChapterRef> list)
{
return list.SelectMany(l => new EpubChapterRef[] { l }.Concat(Flatten(l.SubChapters))).ToList();
}
private void NextButton_Click(object sender, RoutedEventArgs e)
{
this.NextChapter();
return list.SelectMany(l => new[] {l}.Concat(Flatten(l.SubChapters))).ToList();
}
private void NextChapter()
{
try
{
this.currChapter = Math.Min(this.currChapter + 1, chapterRefs.Count - 1);
this.UpdateChapter();
_currChapter = Math.Min(_currChapter + 1, _chapterRefs.Count - 1);
UpdateChapter();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>";
pagePanel.Text = "<div>Invalid chapter.</div>";
}
OnPropertyChanged("Chapter");
OnChapterChanged();
}
private void PrevButton_Click(object sender, RoutedEventArgs e)
{
this.PrevChapter();
}
private void PrevChapter()
{
try
{
this.currChapter = Math.Max(this.currChapter - 1, -1);
this.UpdateChapter();
_currChapter = Math.Max(_currChapter - 1, -1);
UpdateChapter();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>";
pagePanel.Text = "<div>Invalid chapter.</div>";
}
OnPropertyChanged("Chapter");
OnChapterChanged();
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
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();
PrevChapter();
e.Handled = true;
}
else if (e.Key == Key.Right)
{
this.NextChapter();
NextChapter();
e.Handled = true;
}
else
@@ -121,35 +123,28 @@ namespace QuickLook.Plugin.EpubViewer
}
}
public class ChapterChangedEventArgs : EventArgs
{
public ChapterChangedEventArgs(int currChapter)
{
this.NewChapter = currChapter;
}
public int NewChapter { get; set; }
}
private void UpdateChapter()
{
if (currChapter < 0)
if (_currChapter < 0)
{
this.pagePanel.ChapterRef = null;
this.pagePanel.Text = string.Format(@"
pagePanel.ChapterRef = null;
pagePanel.Text = $@"
<div style=""margin:4pt; text-align: center;"">
<img src=""COVER"" alt=""COVER"" style=""height:500px;""/>
<div style=""word-wrap: break-word;"">{0}</div>
</div>", epubBook.Title);
<div style=""word-wrap: break-word;"">{_epubBook.Title}</div>
</div>";
_context.Title = _epubBook.Title;
}
else
{
this.pagePanel.ChapterRef = chapterRefs[currChapter];
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
}
pagePanel.ChapterRef = _chapterRefs[_currChapter];
if (_chapterRefs[_currChapter].Anchor != null)
pagePanel.ScrollToElement(_chapterRefs[_currChapter].Anchor);
_context.Title =
$"{_epubBook.Title}: {_chapterRefs[_currChapter].Title} ({_currChapter}/{_chapterRefs.Count})";
}
}
}
}
}