Skip to content
This repository has been archived by the owner on May 28, 2022. It is now read-only.

Commit

Permalink
Ngs.Engine.UI: Set the window minimum/maximum size based on the measu…
Browse files Browse the repository at this point in the history
…rement

This is finally possible thanks to the PR rust-windowing/winit#548!
  • Loading branch information
yvt committed Jun 5, 2018
1 parent 391e672 commit e16b89f
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions Ngs.Engine.Framework/UI/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public class Window : ISynchronizeInvoke {
/// </summary>
Vector2? size;

/// <summary>
/// The minimum size of the client region. <c>null</c> if not computed or specified yet
/// </summary>
Vector2? minSize;

/// <summary>
/// The maximum size of the client region. <c>null</c> if not computed or specified yet
/// </summary>
Vector2? maxSize;

/// <summary>
/// Should be the value of <see cref="size" /> pushed to the compositor?
/// </summary>
Expand Down Expand Up @@ -114,18 +124,21 @@ public bool Visible {

internal void Render() {
// TODO: Minimize the update
Vector2 minSize;
Vector2 maxSize;

if (this.ContentsView is View view) {
view.BeforeLayout();
view.Measure();

var measurement = view.Measurement;
if (!size.HasValue) {
// The window size is not specified yet. Automatically derive it from the
// measurement result.
size = view.Measurement.PreferredSize;
size = measurement.PreferredSize;
shouldPushSize = true;
} else {
// Limit the size according to the measurement result.
var measurement = view.Measurement;
var newSize = Vector2.Clamp(size.Value,
measurement.MinimumSize,
measurement.MaximumSize);
Expand All @@ -135,6 +148,9 @@ internal void Render() {
}
}

minSize = measurement.MinimumSize;
maxSize = measurement.MaximumSize;

view.Bounds = new Box2(Vector2.Zero, size.Value);
view.Arrange();
view.Render();
Expand All @@ -144,6 +160,9 @@ internal void Render() {
size = Vector2.Zero;
shouldPushSize = true;
}

minSize = Vector2.Zero;
maxSize = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
}

if (!this.materialized) {
Expand All @@ -155,12 +174,8 @@ internal void Render() {
flags |= WindowFlags.Borderless | WindowFlags.Transparent;
}

// Deny resizing at all if the root view has the maximum size
if (this.ContentsView is View view2) {
var maxSize = view2.Measurement.MaximumSize;
if (!float.IsInfinity(maxSize.X) || !float.IsInfinity(maxSize.Y)) {
flags &= ~WindowFlags.Resizable;
}
if (maxSize.X <= minSize.X + 0.5 && maxSize.Y <= minSize.Y + 0.5) {
flags &= ~WindowFlags.Resizable;
}

this.pfWindow.Flags = flags;
Expand All @@ -171,13 +186,18 @@ internal void Render() {
shouldPushSize = false;
}

if (minSize != this.minSize || maxSize != this.maxSize) {
this.pfWindow.MinimumSize = minSize;
this.pfWindow.MaximumSize = maxSize;
this.minSize = minSize;
this.maxSize = maxSize;
}

if (shouldPushTitle) {
this.pfWindow.Title = title;
shouldPushTitle = false;
}

// TODO: Repond to the resize event and re-render accordingly

this.pfWindow.Child = this.ContentsView?.MainPFLayer;
this.materialized = true;
}
Expand Down

0 comments on commit e16b89f

Please sign in to comment.