// 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.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using QuickLook.Common.Annotations; using QuickLook.Common.Helpers; namespace QuickLook.Common { /// /// A runtime object which allows interaction between this plugin and QuickLook. /// public class ContextObject : INotifyPropertyChanged { private bool _canResize = true; private bool _fullWindowDragging; private bool _isBusy = true; private string _title = string.Empty; private bool _titlebarAutoHide; private bool _titlebarBlurVisibility; private bool _titlebarColourVisibility = true; private bool _titlebarOverlap; private bool _useDarkTheme; private object _viewerContent; /// /// Get or set the title of Viewer window. /// public string Title { get => _title; set { _title = value; OnPropertyChanged(); } } /// /// Get or set the viewer content control. /// public object ViewerContent { get => _viewerContent; set { _viewerContent = value; OnPropertyChanged(); } } /// /// Show or hide the busy indicator icon. /// public bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged(); } } /// /// Set the exact size you want. /// public Size PreferredSize { get; set; } = new Size {Width = 800, Height = 600}; /// /// Set whether user are allowed to resize the viewer window. /// public bool CanResize { get => _canResize; set { _canResize = value; OnPropertyChanged(); } } /// /// Set whether the full viewer window can be used for mouse dragging. /// public bool FullWindowDragging { get => _fullWindowDragging; set { _fullWindowDragging = value; OnPropertyChanged(); } } /// /// Set whether the viewer content is overlapped by the title bar /// public bool TitlebarOverlap { get => _titlebarOverlap; set { _titlebarOverlap = value; OnPropertyChanged(); } } /// /// Set whether the title bar shows a blurred background /// public bool TitlebarBlurVisibility { get => _titlebarBlurVisibility; set { if (value == _titlebarBlurVisibility) return; _titlebarBlurVisibility = value; OnPropertyChanged(); } } /// /// Set whether the title bar shows a colour overlay /// public bool TitlebarColourVisibility { get => _titlebarColourVisibility; set { if (value == _titlebarColourVisibility) return; _titlebarColourVisibility = value; OnPropertyChanged(); } } /// /// Should the titlebar hides itself after a short period of inactivity? /// public bool TitlebarAutoHide { get => _titlebarAutoHide; set { if (value == _titlebarAutoHide) return; _titlebarAutoHide = value; OnPropertyChanged(); } } /// /// Switch to dark theme? /// public bool UseDarkTheme { get => _useDarkTheme; set { _useDarkTheme = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; /// /// Set the size of viewer window, scale or shrink to fit (to screen resolution). /// The window can take maximum (maxRatio*resolution) space. /// /// The desired size. /// The maximum percent (over screen resolution) it can take. public double SetPreferredSizeFit(Size size, double maxRatio) { if (maxRatio > 1) maxRatio = 1; var max = WindowHelper.GetCurrentWindowRect(); var widthRatio = max.Width * maxRatio / size.Width; var heightRatio = max.Height * maxRatio / size.Height; var ratio = Math.Min(widthRatio, heightRatio); if (ratio > 1) ratio = 1; PreferredSize = new Size {Width = size.Width * ratio, Height = size.Height * ratio}; return ratio; } public void Reset() { Title = string.Empty; IsBusy = true; PreferredSize = new Size(); CanResize = true; FullWindowDragging = false; UseDarkTheme = false; TitlebarOverlap = false; TitlebarAutoHide = false; TitlebarBlurVisibility = false; TitlebarColourVisibility = true; ViewerContent = null; } [NotifyPropertyChangedInvocator] protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }