From 56cb31fd2af03eaf2e18793bf6a04bd2323d2015 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sun, 6 Aug 2017 21:01:02 +0300 Subject: [PATCH] finish auto-hide --- .../ImagePanel.xaml | 2 +- .../ImagePanel.xaml.cs | 11 ++ .../QuickLook.Plugin.ImageViewer/Plugin.cs | 3 +- .../PdfViewerControl.xaml | 2 +- .../QuickLook.Plugin.TextViewer/Plugin.cs | 2 +- .../TextViewerPanel.xaml.cs | 5 +- .../ViewerPanel.xaml.cs | 2 +- QuickLook/Actions/ConditionalEventTrigger.cs | 103 ++++++++++++++++++ .../Converters/BooleanToKeyTimeConverter.cs | 45 ++++++++ QuickLook/Helpers/TranslationHelper.cs | 2 +- QuickLook/MainWindowTransparent.xaml | 60 ++++------ QuickLook/MainWindowTransparent.xaml.cs | 29 ++++- QuickLook/Plugin/ContextObject.cs | 28 +++-- QuickLook/QuickLook.csproj | 2 + .../Styles/ScrollBarStyleDictionary.xaml | 14 +-- QuickLook/TrayIconManager.cs | 2 +- 16 files changed, 241 insertions(+), 71 deletions(-) create mode 100644 QuickLook/Actions/ConditionalEventTrigger.cs create mode 100644 QuickLook/Converters/BooleanToKeyTimeConverter.cs diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml index 9f50929..ce622fa 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml @@ -9,7 +9,7 @@ x:Name="imagePanel" d:DesignHeight="300" d:DesignWidth="300"> - + diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs index 4b48d11..73665ad 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs @@ -45,6 +45,7 @@ namespace QuickLook.Plugin.ImageViewer private BitmapScalingMode _renderMode = BitmapScalingMode.HighQuality; private BitmapSource _source; private double _zoomFactor = 1d; + private Visibility _backgroundVisibility = Visibility.Visible; private bool _zoomToFit = true; @@ -88,6 +89,16 @@ namespace QuickLook.Plugin.ImageViewer } } + public Visibility BackgroundVisibility + { + get => _backgroundVisibility; + set + { + _backgroundVisibility = value; + OnPropertyChanged(); + } + } + public double MinZoomFactor { get => _minZoomFactor; diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index 8e2b891..517e4af 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -64,8 +64,7 @@ namespace QuickLook.Plugin.ImageViewer context.PreferredSize = new Size(1024, 768); Directory.SetCurrentDirectory(App.AppPath); - - context.AutoHideTitlebar = true; + context.TitlebarOverlap = true; } diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml index 4a455ad..854e710 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml @@ -70,7 +70,7 @@ - + \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index 7386afa..fe16a5c 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -64,7 +64,7 @@ namespace QuickLook.Plugin.TextViewer public void View(string path, ContextObject context) { - _tvp = new TextViewerPanel(path); + _tvp = new TextViewerPanel(path, context); context.ViewerContent = _tvp; context.Title = $"{Path.GetFileName(path)}"; diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs index 1617743..c77452d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs @@ -21,7 +21,6 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using ICSharpCode.AvalonEdit.Highlighting; -using QuickLook.Helpers; using UtfUnknown; namespace QuickLook.Plugin.TextViewer @@ -31,7 +30,7 @@ namespace QuickLook.Plugin.TextViewer /// public partial class TextViewerPanel : UserControl { - public TextViewerPanel(string path) + public TextViewerPanel(string path, ContextObject context) { InitializeComponent(); @@ -42,7 +41,7 @@ namespace QuickLook.Plugin.TextViewer viewer.PreviewMouseWheel += Viewer_MouseWheel; viewer.FontFamily = - new FontFamily(TranslationHelper.GetString("Editor_FontFamily")); + new FontFamily(context.GetString("Editor_FontFamily")); LoadFile(path); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs index 0cf461c..251ac66 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs @@ -266,7 +266,7 @@ namespace QuickLook.Plugin.VideoViewer [DebuggerNonUserCode] private void ShowErrorNotification(object sender, EventArgs e) { - TrayIconManager.GetInstance().ShowNotification("", "An error occurred while loading the video."); + _context.ShowNotification("", "An error occurred while loading the video."); mediaElement?.Stop(); Dispose(); diff --git a/QuickLook/Actions/ConditionalEventTrigger.cs b/QuickLook/Actions/ConditionalEventTrigger.cs new file mode 100644 index 0000000..72aa004 --- /dev/null +++ b/QuickLook/Actions/ConditionalEventTrigger.cs @@ -0,0 +1,103 @@ +// Copyright © 2017 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 . + +using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Markup; + +namespace QuickLook.Actions +{ + [ContentProperty("Actions")] + public class ConditionalEventTrigger : FrameworkContentElement + { + public static readonly DependencyProperty ConditionProperty = + DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalEventTrigger)); + + public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached("Triggers", + typeof(ConditionalEventTriggerCollection), typeof(ConditionalEventTrigger), new PropertyMetadata + { + PropertyChangedCallback = (obj, e) => + { + // When "Triggers" is set, register handlers for each trigger in the list + var element = (FrameworkElement) obj; + var triggers = (List) e.NewValue; + foreach (var trigger in triggers) + element.AddHandler(trigger.RoutedEvent, new RoutedEventHandler((obj2, e2) => + trigger.OnRoutedEvent(element))); + } + }); + + private static readonly RoutedEvent TriggerActionsEvent = EventManager.RegisterRoutedEvent("", + RoutingStrategy.Direct, + typeof(EventHandler), typeof(ConditionalEventTrigger)); + + public ConditionalEventTrigger() + { + Actions = new List(); + } + + public RoutedEvent RoutedEvent { get; set; } + public List Actions { get; set; } + + // Condition + public bool Condition + { + get => (bool) GetValue(ConditionProperty); + set => SetValue(ConditionProperty, value); + } + + // "Triggers" attached property + public static ConditionalEventTriggerCollection GetTriggers(DependencyObject obj) + { + return (ConditionalEventTriggerCollection) obj.GetValue(TriggersProperty); + } + + public static void SetTriggers(DependencyObject obj, ConditionalEventTriggerCollection value) + { + obj.SetValue(TriggersProperty, value); + } + + // When an event fires, check the condition and if it is true fire the actions + private void OnRoutedEvent(FrameworkElement element) + { + DataContext = element.DataContext; // Allow data binding to access element properties + if (Condition) + { + // Construct an EventTrigger containing the actions, then trigger it + var dummyTrigger = new EventTrigger {RoutedEvent = TriggerActionsEvent}; + foreach (var action in Actions) + dummyTrigger.Actions.Add(action); + + element.Triggers.Add(dummyTrigger); + try + { + element.RaiseEvent(new RoutedEventArgs(TriggerActionsEvent)); + } + finally + { + element.Triggers.Remove(dummyTrigger); + } + } + } + } + + // Create collection type visible to XAML - since it is attached we cannot construct it in code + public class ConditionalEventTriggerCollection : List + { + } +} \ No newline at end of file diff --git a/QuickLook/Converters/BooleanToKeyTimeConverter.cs b/QuickLook/Converters/BooleanToKeyTimeConverter.cs new file mode 100644 index 0000000..9526889 --- /dev/null +++ b/QuickLook/Converters/BooleanToKeyTimeConverter.cs @@ -0,0 +1,45 @@ +// Copyright © 2017 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 . + +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media.Animation; + +namespace QuickLook.Converters +{ + public sealed class BooleanToKeyTimeConverter : DependencyObject, IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value == null) + return KeyTime.FromTimeSpan(TimeSpan.MaxValue); + + var v = (bool) value; + + return v + ? KeyTime.FromTimeSpan(parameter as TimeSpan? ?? TimeSpan.MaxValue) + : KeyTime.FromTimeSpan(TimeSpan.MaxValue); + } + + object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/QuickLook/Helpers/TranslationHelper.cs b/QuickLook/Helpers/TranslationHelper.cs index 404dbbd..edfc31a 100644 --- a/QuickLook/Helpers/TranslationHelper.cs +++ b/QuickLook/Helpers/TranslationHelper.cs @@ -24,7 +24,7 @@ using System.Xml.XPath; namespace QuickLook.Helpers { - public class TranslationHelper + internal class TranslationHelper { private static readonly CultureInfo CurrentCultureInfo = CultureInfo.CurrentUICulture; //private static readonly CultureInfo CurrentCultureInfo = CultureInfo.GetCultureInfo("zh-CN"); diff --git a/QuickLook/MainWindowTransparent.xaml b/QuickLook/MainWindowTransparent.xaml index ff11544..5943b9d 100644 --- a/QuickLook/MainWindowTransparent.xaml +++ b/QuickLook/MainWindowTransparent.xaml @@ -8,6 +8,7 @@ xmlns:glassLayer="clr-namespace:QuickLook.Controls.GlassLayer" xmlns:converters="clr-namespace:QuickLook.Converters" xmlns:controls="clr-namespace:QuickLook.Controls" + xmlns:actions="clr-namespace:QuickLook.Actions" mc:Ignorable="d" x:Class="QuickLook.MainWindowTransparent" x:Name="mainWindow" UseLayoutRounding="True" d:DesignWidth="624" d:DesignHeight="700" @@ -23,6 +24,7 @@ + @@ -35,8 +37,8 @@ + ResizeBorderThickness="{Binding ContextObject.CanResize, Converter={StaticResource BooleanToResizeBorderThicknessConverter}, ElementName=mainWindow}" + UseAeroCaptionButtons="False" /> @@ -61,45 +63,29 @@ + + + + + + + + + + + + + + + - - - - - - - - - - -