Browse Source

Re-measure/render when window resized.

pull/4/head
grokys 13 years ago
parent
commit
c8df546e73
  1. 34
      Perspex.Windows/Window.cs
  2. 32
      Perspex/Controls/Control.cs
  3. 23
      Perspex/Layout/ILayoutManager.cs
  4. 15
      Perspex/Layout/ILayoutRoot.cs
  5. 23
      Perspex/Layout/ILayoutable.cs
  6. 48
      Perspex/Layout/LayoutManager.cs
  7. 4
      Perspex/Perspex.csproj
  8. 1
      TestApplication/Program.cs

34
Perspex.Windows/Window.cs

@ -5,11 +5,10 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Perspex.Controls; using Perspex.Controls;
using Perspex.Layout;
using Perspex.Windows.Interop; using Perspex.Windows.Interop;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
public class Window : ContentControl public class Window : ContentControl, ILayoutRoot
{ {
private UnmanagedMethods.WndProc wndProcDelegate; private UnmanagedMethods.WndProc wndProcDelegate;
@ -20,9 +19,15 @@
public Window() public Window()
{ {
this.CreateWindow(); this.CreateWindow();
Size clientSize = this.ClientSize; Size clientSize = this.ClientSize;
this.LayoutManager = new LayoutManager();
this.renderer = new Renderer(this.Handle, (int)clientSize.Width, (int)clientSize.Height); this.renderer = new Renderer(this.Handle, (int)clientSize.Width, (int)clientSize.Height);
this.LayoutManager.LayoutNeeded.Subscribe(x =>
{
this.LayoutManager.ExecuteLayoutPass();
this.renderer.Render(this);
});
} }
public Size ClientSize public Size ClientSize
@ -41,12 +46,15 @@
private set; private set;
} }
public ILayoutManager LayoutManager
{
get;
private set;
}
public void Show() public void Show()
{ {
UnmanagedMethods.ShowWindow(this.Handle, 4); UnmanagedMethods.ShowWindow(this.Handle, 4);
this.Measure(this.ClientSize);
this.Arrange(new Rect(this.ClientSize));
this.renderer.Render(this);
} }
protected override Visual DefaultTemplate() protected override Visual DefaultTemplate()
@ -133,14 +141,10 @@
//// InputManager.Current.ProcessInput(new RawMouseEventArgs(mouse, RawMouseEventType.Move)); //// InputManager.Current.ProcessInput(new RawMouseEventArgs(mouse, RawMouseEventType.Move));
//// break; //// break;
////case UnmanagedMethods.WindowsMessage.WM_SIZE: case UnmanagedMethods.WindowsMessage.WM_SIZE:
//// if (this.renderTarget != null) this.renderer.Resize((int)lParam & 0xffff, (int)lParam >> 16);
//// { this.InvalidateMeasure();
//// this.renderTarget.Resize(new SharpDX.DrawingSize((int)lParam & 0xffff, (int)lParam >> 16)); return IntPtr.Zero;
//// }
//// this.OnResized();
//// return IntPtr.Zero;
} }
return UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam); return UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam);

32
Perspex/Controls/Control.cs

@ -1,6 +1,8 @@
namespace Perspex.Controls namespace Perspex.Controls
{ {
using System;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using Perspex.Layout;
public enum HorizontalAlignment public enum HorizontalAlignment
{ {
@ -18,7 +20,7 @@
Bottom, Bottom,
} }
public abstract class Control : Visual public abstract class Control : Visual, ILayoutable
{ {
public static readonly PerspexProperty<HorizontalAlignment> HorizontalAlignmentProperty = public static readonly PerspexProperty<HorizontalAlignment> HorizontalAlignmentProperty =
PerspexProperty.Register<Control, HorizontalAlignment>("HorizontalAlignment"); PerspexProperty.Register<Control, HorizontalAlignment>("HorizontalAlignment");
@ -53,6 +55,24 @@
set { this.SetValue(MarginProperty, value); } set { this.SetValue(MarginProperty, value); }
} }
public Control Parent
{
get;
internal set;
}
public ILayoutRoot GetLayoutRoot()
{
Control c = this;
while (c != null && !(c is ILayoutRoot))
{
c = c.Parent;
}
return (ILayoutRoot)c;
}
public void Arrange(Rect rect) public void Arrange(Rect rect)
{ {
this.Bounds = new Rect( this.Bounds = new Rect(
@ -66,6 +86,16 @@
this.DesiredSize = this.MeasureContent(availableSize).Constrain(availableSize); this.DesiredSize = this.MeasureContent(availableSize).Constrain(availableSize);
} }
public void InvalidateArrange()
{
this.GetLayoutRoot().LayoutManager.InvalidateArrange(this);
}
public void InvalidateMeasure()
{
this.GetLayoutRoot().LayoutManager.InvalidateMeasure(this);
}
protected virtual Size ArrangeContent(Size finalSize) protected virtual Size ArrangeContent(Size finalSize)
{ {
return finalSize; return finalSize;

23
Perspex/Layout/ILayoutManager.cs

@ -0,0 +1,23 @@
// -----------------------------------------------------------------------
// <copyright file="ILayoutManager.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Layout
{
using System;
using System.Reactive;
using Perspex.Controls;
public interface ILayoutManager
{
IObservable<Unit> LayoutNeeded { get; }
void ExecuteLayoutPass();
void InvalidateMeasure(ILayoutable item);
void InvalidateArrange(ILayoutable item);
}
}

15
Perspex/Layout/ILayoutRoot.cs

@ -0,0 +1,15 @@
// -----------------------------------------------------------------------
// <copyright file="ILayoutRoot.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Layout
{
public interface ILayoutRoot : ILayoutable
{
Size ClientSize { get; }
ILayoutManager LayoutManager { get; }
}
}

23
Perspex/Layout/ILayoutable.cs

@ -0,0 +1,23 @@
// -----------------------------------------------------------------------
// <copyright file="ILayoutable.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Layout
{
public interface ILayoutable
{
Size? DesiredSize { get; }
ILayoutRoot GetLayoutRoot();
void Arrange(Rect rect);
void Measure(Size availableSize);
void InvalidateArrange();
void InvalidateMeasure();
}
}

48
Perspex/Layout/LayoutManager.cs

@ -0,0 +1,48 @@
namespace Perspex.Layout
{
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Subjects;
using Perspex.Controls;
public class LayoutManager : ILayoutManager
{
private ILayoutRoot root;
private Subject<Unit> layoutNeeded;
public LayoutManager()
{
this.layoutNeeded = new Subject<Unit>();
}
public IObservable<Unit> LayoutNeeded
{
get { return this.layoutNeeded; }
}
public void ExecuteLayoutPass()
{
if (root != null)
{
root.Measure(root.ClientSize);
root.Arrange(new Rect(root.ClientSize));
}
root = null;
}
public void InvalidateMeasure(ILayoutable item)
{
this.root = item.GetLayoutRoot();
this.layoutNeeded.OnNext(Unit.Default);
}
public void InvalidateArrange(ILayoutable item)
{
this.root = item.GetLayoutRoot();
this.layoutNeeded.OnNext(Unit.Default);
}
}
}

4
Perspex/Perspex.csproj

@ -74,6 +74,10 @@
<Compile Include="Controls\ContentControl.cs" /> <Compile Include="Controls\ContentControl.cs" />
<Compile Include="Controls\Control.cs" /> <Compile Include="Controls\Control.cs" />
<Compile Include="Controls\TextBlock.cs" /> <Compile Include="Controls\TextBlock.cs" />
<Compile Include="Layout\ILayoutable.cs" />
<Compile Include="Layout\ILayoutManager.cs" />
<Compile Include="Layout\ILayoutRoot.cs" />
<Compile Include="Layout\LayoutManager.cs" />
<Compile Include="Media\FormattedText.cs" /> <Compile Include="Media\FormattedText.cs" />
<Compile Include="Media\IDrawingContext.cs" /> <Compile Include="Media\IDrawingContext.cs" />
<Compile Include="Media\Brush.cs" /> <Compile Include="Media\Brush.cs" />

1
TestApplication/Program.cs

@ -9,6 +9,7 @@ using Perspex.Media;
using Perspex.Windows; using Perspex.Windows;
using Perspex.Windows.Media; using Perspex.Windows.Media;
using Perspex.Windows.Threading; using Perspex.Windows.Threading;
namespace TestApplication namespace TestApplication
{ {
class Program class Program

Loading…
Cancel
Save