Created
March 28, 2018 21:55
-
-
Save cwensley/bd511e9a348529a36e81dd02f625d9ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Eto.Forms; | |
using Eto.Drawing; | |
namespace Eto.Forms | |
{ | |
/// <summary> | |
/// A dialog that sizes to its content automatically | |
/// </summary> | |
public class AutoSizingDialog<T> : Dialog<T> | |
{ | |
Panel contentPanel; | |
Scrollable scrollable; | |
bool isResizing; | |
bool hasCustomSize; | |
bool hasShown; | |
Size? lastContentSize; | |
public AutoSizingDialog() | |
{ | |
contentPanel = new Panel(); | |
contentPanel.SizeChanged += contentPanel_SizeChanged; | |
scrollable = new Scrollable | |
{ | |
Border = BorderType.None, | |
ExpandContentWidth = false, | |
ExpandContentHeight = false, | |
Content = contentPanel | |
}; | |
base.Content = scrollable; | |
} | |
public new Size ClientSize | |
{ | |
get { return contentPanel.Size; } | |
set { contentPanel.Size = value; } | |
} | |
public new Control Content | |
{ | |
get { return contentPanel.Content; } | |
set { contentPanel.Content = value; } | |
} | |
protected override void OnShown(EventArgs e) | |
{ | |
base.OnShown(e); | |
hasShown = true; | |
} | |
protected override void OnSizeChanged(EventArgs e) | |
{ | |
base.OnSizeChanged(e); | |
if (hasShown && !isResizing && Resizable) | |
{ | |
// User is changing the size manually, so we keep that size | |
hasCustomSize = true; | |
scrollable.ExpandContentWidth = true; | |
scrollable.ExpandContentHeight = true; | |
} | |
isResizing = false; | |
} | |
void contentPanel_SizeChanged(object sender, EventArgs e) | |
{ | |
if (!Loaded || hasCustomSize) | |
return; | |
var contentSize = scrollable.Content.Size; | |
if (contentSize != lastContentSize) | |
{ | |
isResizing = true; | |
base.ClientSize = contentSize; | |
lastContentSize = contentSize; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment