using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace MediaBrowser.UI.Controls
{
///
/// Provides a base class for all Windows
///
public abstract class BaseWindow : Window, INotifyPropertyChanged
{
///
/// Occurs when [property changed].
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// Called when [property changed].
///
/// The info.
public void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
///
/// The _content scale
///
private double _contentScale = 1;
///
/// Gets the content scale.
///
/// The content scale.
public double ContentScale
{
get { return _contentScale; }
private set
{
_contentScale = value;
OnPropertyChanged("ContentScale");
}
}
///
/// Initializes a new instance of the class.
///
protected BaseWindow()
: base()
{
SizeChanged += MainWindow_SizeChanged;
Loaded += BaseWindowLoaded;
}
///
/// Bases the window loaded.
///
/// The sender.
/// The instance containing the event data.
void BaseWindowLoaded(object sender, RoutedEventArgs e)
{
OnLoaded();
}
///
/// Called when [loaded].
///
protected virtual void OnLoaded()
{
MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
}
///
/// Handles the SizeChanged event of the MainWindow control.
///
/// The source of the event.
/// The instance containing the event data.
void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
ContentScale = e.NewSize.Height / 1080;
}
///
/// Called when [browser back].
///
protected virtual void OnBrowserBack()
{
}
///
/// Called when [browser forward].
///
protected virtual void OnBrowserForward()
{
}
///
/// Invoked when an unhandled attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
///
/// The that contains the event data.
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (IsBackPress(e))
{
e.Handled = true;
if (!e.IsRepeat)
{
OnBrowserBack();
}
}
else if (IsForwardPress(e))
{
e.Handled = true;
if (!e.IsRepeat)
{
OnBrowserForward();
}
}
base.OnPreviewKeyDown(e);
}
///
/// Determines if a keypress should be treated as a backward press
///
/// The instance containing the event data.
/// true if [is back press] [the specified e]; otherwise, false.
private bool IsBackPress(KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
return true;
}
if (e.Key == Key.BrowserBack || e.Key == Key.Back)
{
return true;
}
if (e.SystemKey == Key.Left && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
{
return true;
}
return false;
}
///
/// Determines if a keypress should be treated as a forward press
///
/// The instance containing the event data.
/// true if [is forward press] [the specified e]; otherwise, false.
private bool IsForwardPress(KeyEventArgs e)
{
if (e.Key == Key.BrowserForward)
{
return true;
}
if (e.SystemKey == Key.RightAlt && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
{
return true;
}
return false;
}
}
}