Browse Source

Fixed tabs in Window.cs

pull/504/head
Jason Jarvis 11 years ago
parent
commit
2e9b80d3ff
  1. 637
      src/Perspex.Controls/Window.cs

637
src/Perspex.Controls/Window.cs

@ -14,327 +14,326 @@ using System.Collections.Generic;
namespace Perspex.Controls namespace Perspex.Controls
{ {
/// <summary> /// <summary>
/// Determines how a <see cref="Window"/> will size itself to fit its content. /// Determines how a <see cref="Window"/> will size itself to fit its content.
/// </summary> /// </summary>
public enum SizeToContent public enum SizeToContent
{ {
/// <summary> /// <summary>
/// The window will not automatically size itself to fit its content. /// The window will not automatically size itself to fit its content.
/// </summary> /// </summary>
Manual, Manual,
/// <summary> /// <summary>
/// The window will size itself horizontally to fit its content. /// The window will size itself horizontally to fit its content.
/// </summary> /// </summary>
Width, Width,
/// <summary> /// <summary>
/// The window will size itself vertically to fit its content. /// The window will size itself vertically to fit its content.
/// </summary> /// </summary>
Height, Height,
/// <summary> /// <summary>
/// The window will size itself horizontally and vertically to fit its content. /// The window will size itself horizontally and vertically to fit its content.
/// </summary> /// </summary>
WidthAndHeight, WidthAndHeight,
} }
/// <summary> /// <summary>
/// A top-level window. /// A top-level window.
/// </summary> /// </summary>
public class Window : TopLevel, IStyleable, IFocusScope, ILayoutRoot, INameScope public class Window : TopLevel, IStyleable, IFocusScope, ILayoutRoot, INameScope
{ {
private static IList<Window> s_windows = new List<Window>(); private static IList<Window> s_windows = new List<Window>();
/// <summary> /// <summary>
/// Retrieves an enumeration of all Windows in the currently running application. /// Retrieves an enumeration of all Windows in the currently running application.
/// </summary> /// </summary>
public static IList<Window> OpenWindows => s_windows; public static IList<Window> OpenWindows => s_windows;
/// <summary> /// <summary>
/// Defines the <see cref="SizeToContent"/> property. /// Defines the <see cref="SizeToContent"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<SizeToContent> SizeToContentProperty = public static readonly StyledProperty<SizeToContent> SizeToContentProperty =
PerspexProperty.Register<Window, SizeToContent>(nameof(SizeToContent)); PerspexProperty.Register<Window, SizeToContent>(nameof(SizeToContent));
/// <summary> /// <summary>
/// Enables of disables system window decorations (title bar, buttons, etc) /// Enables of disables system window decorations (title bar, buttons, etc)
/// </summary> /// </summary>
public static readonly StyledProperty<bool> HasSystemDecorationsProperty = public static readonly StyledProperty<bool> HasSystemDecorationsProperty =
PerspexProperty.Register<Window, bool>(nameof(HasSystemDecorations), true); PerspexProperty.Register<Window, bool>(nameof(HasSystemDecorations), true);
/// <summary> /// <summary>
/// Defines the <see cref="Title"/> property. /// Defines the <see cref="Title"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<string> TitleProperty = public static readonly StyledProperty<string> TitleProperty =
PerspexProperty.Register<Window, string>(nameof(Title), "Window"); PerspexProperty.Register<Window, string>(nameof(Title), "Window");
private readonly NameScope _nameScope = new NameScope(); private readonly NameScope _nameScope = new NameScope();
private object _dialogResult; private object _dialogResult;
private readonly Size _maxPlatformClientSize; private readonly Size _maxPlatformClientSize;
/// <summary> /// <summary>
/// Initializes static members of the <see cref="Window"/> class. /// Initializes static members of the <see cref="Window"/> class.
/// </summary> /// </summary>
static Window() static Window()
{ {
BackgroundProperty.OverrideDefaultValue(typeof(Window), Brushes.White); BackgroundProperty.OverrideDefaultValue(typeof(Window), Brushes.White);
TitleProperty.Changed.AddClassHandler<Window>((s, e) => s.PlatformImpl.SetTitle((string)e.NewValue)); TitleProperty.Changed.AddClassHandler<Window>((s, e) => s.PlatformImpl.SetTitle((string)e.NewValue));
HasSystemDecorationsProperty.Changed.AddClassHandler<Window>( HasSystemDecorationsProperty.Changed.AddClassHandler<Window>(
(s, e) => s.PlatformImpl.SetSystemDecorations((bool) e.NewValue)); (s, e) => s.PlatformImpl.SetSystemDecorations((bool) e.NewValue));
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Window"/> class. /// Initializes a new instance of the <see cref="Window"/> class.
/// </summary> /// </summary>
public Window() public Window()
: base(PlatformManager.CreateWindow()) : base(PlatformManager.CreateWindow())
{ {
_maxPlatformClientSize = this.PlatformImpl.MaxClientSize; _maxPlatformClientSize = this.PlatformImpl.MaxClientSize;
} }
/// <inheritdoc/> /// <inheritdoc/>
event EventHandler<NameScopeEventArgs> INameScope.Registered event EventHandler<NameScopeEventArgs> INameScope.Registered
{ {
add { _nameScope.Registered += value; } add { _nameScope.Registered += value; }
remove { _nameScope.Registered -= value; } remove { _nameScope.Registered -= value; }
} }
/// <inheritdoc/> /// <inheritdoc/>
event EventHandler<NameScopeEventArgs> INameScope.Unregistered event EventHandler<NameScopeEventArgs> INameScope.Unregistered
{ {
add { _nameScope.Unregistered += value; } add { _nameScope.Unregistered += value; }
remove { _nameScope.Unregistered -= value; } remove { _nameScope.Unregistered -= value; }
} }
/// <summary> /// <summary>
/// Gets the platform-specific window implementation. /// Gets the platform-specific window implementation.
/// </summary> /// </summary>
public new IWindowImpl PlatformImpl => (IWindowImpl)base.PlatformImpl; public new IWindowImpl PlatformImpl => (IWindowImpl)base.PlatformImpl;
/// <summary> /// <summary>
/// Gets or sets a value indicating how the window will size itself to fit its content. /// Gets or sets a value indicating how the window will size itself to fit its content.
/// </summary> /// </summary>
public SizeToContent SizeToContent public SizeToContent SizeToContent
{ {
get { return GetValue(SizeToContentProperty); } get { return GetValue(SizeToContentProperty); }
set { SetValue(SizeToContentProperty, value); } set { SetValue(SizeToContentProperty, value); }
} }
/// <summary> /// <summary>
/// Gets or sets the title of the window. /// Gets or sets the title of the window.
/// </summary> /// </summary>
public string Title public string Title
{ {
get { return GetValue(TitleProperty); } get { return GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); } set { SetValue(TitleProperty, value); }
} }
/// <summary> /// <summary>
/// Enables of disables system window decorations (title bar, buttons, etc) /// Enables of disables system window decorations (title bar, buttons, etc)
/// </summary> /// </summary>
/// ///
public bool HasSystemDecorations public bool HasSystemDecorations
{ {
get { return GetValue(HasSystemDecorationsProperty); } get { return GetValue(HasSystemDecorationsProperty); }
set { SetValue(HasSystemDecorationsProperty, value); } set { SetValue(HasSystemDecorationsProperty, value); }
} }
/// <summary> /// <summary>
/// Gets or sets the minimized/maximized state of the window. /// Gets or sets the minimized/maximized state of the window.
/// </summary> /// </summary>
public WindowState WindowState public WindowState WindowState
{ {
get { return this.PlatformImpl.WindowState; } get { return this.PlatformImpl.WindowState; }
set { this.PlatformImpl.WindowState = value; } set { this.PlatformImpl.WindowState = value; }
} }
/// <inheritdoc/> /// <inheritdoc/>
Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize; Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
/// <inheritdoc/> /// <inheritdoc/>
Type IStyleable.StyleKey => typeof(Window); Type IStyleable.StyleKey => typeof(Window);
/// <summary> /// <summary>
/// Closes the window. /// Closes the window.
/// </summary> /// </summary>
public void Close() public void Close()
{ {
s_windows.Remove(this); s_windows.Remove(this);
PlatformImpl.Dispose(); PlatformImpl.Dispose();
} }
protected override void HandleApplicationExiting() protected override void HandleApplicationExiting()
{ {
base.HandleApplicationExiting(); base.HandleApplicationExiting();
Close(); Close();
} }
/// <summary> /// <summary>
/// Closes a dialog window with the specified result. /// Closes a dialog window with the specified result.
/// </summary> /// </summary>
/// <param name="dialogResult">The dialog result.</param> /// <param name="dialogResult">The dialog result.</param>
/// <remarks> /// <remarks>
/// When the window is shown with the <see cref="ShowDialog{TResult}"/> method, the /// When the window is shown with the <see cref="ShowDialog{TResult}"/> method, the
/// resulting task will produce the <see cref="_dialogResult"/> value when the window /// resulting task will produce the <see cref="_dialogResult"/> value when the window
/// is closed. /// is closed.
/// </remarks> /// </remarks>
public void Close(object dialogResult) public void Close(object dialogResult)
{ {
_dialogResult = dialogResult; _dialogResult = dialogResult;
Close(); Close();
} }
/// <summary> /// <summary>
/// Hides the window but does not close it. /// Hides the window but does not close it.
/// </summary> /// </summary>
public void Hide() public void Hide()
{ {
using (BeginAutoSizing()) using (BeginAutoSizing())
{ {
PlatformImpl.Hide(); PlatformImpl.Hide();
} }
} }
/// <summary> /// <summary>
/// Shows the window. /// Shows the window.
/// </summary> /// </summary>
public void Show() public void Show()
{ {
s_windows.Add(this); s_windows.Add(this);
EnsureInitialized(); EnsureInitialized();
LayoutManager.Instance.ExecuteInitialLayoutPass(this); LayoutManager.Instance.ExecuteInitialLayoutPass(this);
using (BeginAutoSizing()) using (BeginAutoSizing())
{ {
PlatformImpl.Show(); PlatformImpl.Show();
} }
} }
/// <summary> /// <summary>
/// Shows the window as a dialog. /// Shows the window as a dialog.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A task that can be used to track the lifetime of the dialog. /// A task that can be used to track the lifetime of the dialog.
/// </returns> /// </returns>
public Task ShowDialog() public Task ShowDialog()
{ {
return ShowDialog<object>(); return ShowDialog<object>();
} }
/// <summary> /// <summary>
/// Shows the window as a dialog. /// Shows the window as a dialog.
/// </summary> /// </summary>
/// <typeparam name="TResult"> /// <typeparam name="TResult">
/// The type of the result produced by the dialog. /// The type of the result produced by the dialog.
/// </typeparam> /// </typeparam>
/// <returns>. /// <returns>.
/// A task that can be used to retrive the result of the dialog when it closes. /// A task that can be used to retrive the result of the dialog when it closes.
/// </returns> /// </returns>
public Task<TResult> ShowDialog<TResult>() public Task<TResult> ShowDialog<TResult>()
{ {
s_windows.Add(this); s_windows.Add(this);
EnsureInitialized(); EnsureInitialized();
LayoutManager.Instance.ExecuteInitialLayoutPass(this); LayoutManager.Instance.ExecuteInitialLayoutPass(this);
using (BeginAutoSizing()) using (BeginAutoSizing())
{ {
var modal = PlatformImpl.ShowDialog(); var modal = PlatformImpl.ShowDialog();
var result = new TaskCompletionSource<TResult>(); var result = new TaskCompletionSource<TResult>();
Observable.FromEventPattern(this, nameof(Closed)) Observable.FromEventPattern(this, nameof(Closed))
.Take(1) .Take(1)
.Subscribe(_ => .Subscribe(_ =>
{ {
modal.Dispose(); modal.Dispose();
result.SetResult((TResult)_dialogResult); result.SetResult((TResult)_dialogResult);
}); });
return result.Task; return result.Task;
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
void INameScope.Register(string name, object element) void INameScope.Register(string name, object element)
{ {
_nameScope.Register(name, element); _nameScope.Register(name, element);
} }
/// <inheritdoc/> /// <inheritdoc/>
object INameScope.Find(string name) object INameScope.Find(string name)
{ {
return _nameScope.Find(name); return _nameScope.Find(name);
} }
/// <inheritdoc/> /// <inheritdoc/>
void INameScope.Unregister(string name) void INameScope.Unregister(string name)
{ {
_nameScope.Unregister(name); _nameScope.Unregister(name);
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
var sizeToContent = SizeToContent; var sizeToContent = SizeToContent;
var size = ClientSize; var size = ClientSize;
var desired = base.MeasureOverride(availableSize.Constrain(_maxPlatformClientSize)); var desired = base.MeasureOverride(availableSize.Constrain(_maxPlatformClientSize));
switch (sizeToContent) switch (sizeToContent)
{ {
case SizeToContent.Width: case SizeToContent.Width:
size = new Size(desired.Width, ClientSize.Height); size = new Size(desired.Width, ClientSize.Height);
break; break;
case SizeToContent.Height: case SizeToContent.Height:
size = new Size(ClientSize.Width, desired.Height); size = new Size(ClientSize.Width, desired.Height);
break; break;
case SizeToContent.WidthAndHeight: case SizeToContent.WidthAndHeight:
size = new Size(desired.Width, desired.Height); size = new Size(desired.Width, desired.Height);
break; break;
case SizeToContent.Manual: case SizeToContent.Manual:
size = ClientSize; size = ClientSize;
break; break;
default: default:
throw new InvalidOperationException("Invalid value for SizeToContent."); throw new InvalidOperationException("Invalid value for SizeToContent.");
} }
return size; return size;
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override void HandleResized(Size clientSize) protected override void HandleResized(Size clientSize)
{ {
if (!AutoSizing) if (!AutoSizing)
{ {
SizeToContent = SizeToContent.Manual; SizeToContent = SizeToContent.Manual;
} }
base.HandleResized(clientSize); base.HandleResized(clientSize);
} }
private void EnsureInitialized() private void EnsureInitialized()
{ {
if (!this.IsInitialized) if (!this.IsInitialized)
{ {
var init = (ISupportInitialize)this; var init = (ISupportInitialize)this;
init.BeginInit(); init.BeginInit();
init.EndInit(); init.EndInit();
} }
} }
} }
} }
namespace Perspex namespace Perspex
{ {
public static class WindowApplicationExtensions public static class WindowApplicationExtensions
{ {
public static void RunWithMainWindow<WindowT>(this Application app) where WindowT : Perspex.Controls.Window, new() public static void RunWithMainWindow<WindowT>(this Application app) where WindowT : Perspex.Controls.Window, new()
{ {
var window = new WindowT(); var window = new WindowT();
window.Show(); window.Show();
app.Run(window); app.Run(window);
} }
} }
} }

Loading…
Cancel
Save