Skip to content

Commit

Permalink
Merge pull request #2077 from cwensley/curtis/gtk-scrolled-window-sizes
Browse files Browse the repository at this point in the history
Gtk: Scrollable should respect preferred user sizes
  • Loading branch information
cwensley committed Nov 28, 2021
2 parents ac7660e + a0c5745 commit 2811397
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/Eto.Gtk/Forms/Controls/ScrollableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public BorderType Border
}
}

public class EtoScrolledWindow : Gtk.ScrolledWindow
public partial class EtoScrolledWindow : Eto.GtkSharp.Forms.EtoScrolledWindow
{
#if GTK3
// does this always work?
Expand All @@ -46,13 +46,23 @@ public class EtoScrolledWindow : Gtk.ScrolledWindow
protected override void OnAdjustSizeRequest(Gtk.Orientation orientation, out int minimum_size, out int natural_size)
{
base.OnAdjustSizeRequest(orientation, out minimum_size, out natural_size);

// the natural size of the scrolled window should be the size of the child viewport
if (Child != null)

var h = Handler;
if (h != null)
{
Child.GetPreferredSize(out var ms, out var ns);
var child_size = orientation == Gtk.Orientation.Horizontal ? ns.Width : ns.Height;
natural_size = Math.Max(natural_size, child_size + GetBorderSize());
var preferredSize = orientation == Gtk.Orientation.Horizontal ? h.UserPreferredSize.Width : h.UserPreferredSize.Height;

if (preferredSize > 0)
natural_size = preferredSize;
else if (Child != null)
{
Child.GetPreferredSize(out var ms, out var ns);
var child_size = orientation == Gtk.Orientation.Horizontal ? ns.Width : ns.Height;
natural_size = Math.Max(natural_size, child_size + GetBorderSize());
}

minimum_size = Math.Min(natural_size, minimum_size);
}
}
#endif
Expand All @@ -72,7 +82,7 @@ protected override void OnAdjustSizeRequest(Gtk.Orientation orientation, out int

public ScrollableHandler()
{
Control = new EtoScrolledWindow();
Control = new EtoScrolledWindow { Handler = this };
#if GTK3
// for some reason on mac it doesn't shrink past 47 pixels otherwise
// also, what is an appropriate size?
Expand Down
141 changes: 141 additions & 0 deletions src/Eto.Gtk/Forms/EtoControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ protected override void OnGetPreferredHeight(out int minimum_height, out int nat
minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
Expand Down Expand Up @@ -141,6 +155,20 @@ protected override void OnGetPreferredHeight(out int minimum_height, out int nat
minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
Expand Down Expand Up @@ -226,6 +254,119 @@ protected override void OnGetPreferredHeight(out int minimum_height, out int nat
minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
base.OnAdjustSizeAllocation(orientation, out minimum_size, out natural_size, out allocated_pos, out allocated_size);
var h = Handler;
if (h != null)
{
var preferredSize = orientation == Gtk.Orientation.Horizontal ? h.UserPreferredSize.Width : h.UserPreferredSize.Height;

if (preferredSize > 0)
natural_size = preferredSize;

minimum_size = Math.Min(natural_size, minimum_size);
}
}

protected override void OnAdjustSizeRequest(Gtk.Orientation orientation, out int minimum_size, out int natural_size)
{
base.OnAdjustSizeRequest(orientation, out minimum_size, out natural_size);
var h = Handler;
if (h != null)
{
var preferredSize = orientation == Gtk.Orientation.Horizontal ? h.UserPreferredSize.Width : h.UserPreferredSize.Height;

if (preferredSize > 0)
natural_size = preferredSize;

minimum_size = Math.Min(natural_size, minimum_size);
}
}
#endif

}

public partial class EtoScrolledWindow : Gtk.ScrolledWindow
{
WeakReference _handler;
public IGtkControl Handler
{
get => _handler?.Target as IGtkControl;
set => _handler = new WeakReference(value);
}

#if GTK3
protected override void OnGetPreferredWidth(out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidth(out minimum_width, out natural_width);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Width > 0)
natural_width = userPreferredSize.Width;

minimum_width = Math.Min(natural_width, minimum_width);
}
}

protected override void OnGetPreferredWidthForHeight(int height, out int minimum_width, out int natural_width)
{
base.OnGetPreferredWidthForHeight(height, out minimum_width, out natural_width);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Width > 0)
natural_width = userPreferredSize.Width;

minimum_width = Math.Min(natural_width, minimum_width);
}
}

protected override void OnGetPreferredHeight(out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeight(out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
Expand Down
16 changes: 15 additions & 1 deletion src/Eto.Gtk/Forms/EtoControls.tt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Eto.Drawing;
namespace Eto.GtkSharp.Forms
{
<#
var controls = new[] { "Fixed", "EventBox", "VBox" };
var controls = new[] { "Fixed", "EventBox", "VBox", "ScrolledWindow" };

foreach (var control in controls)
{
Expand Down Expand Up @@ -68,6 +68,20 @@ foreach (var control in controls)
minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnGetPreferredHeightForWidth(int width, out int minimum_height, out int natural_height)
{
base.OnGetPreferredHeightForWidth(width, out minimum_height, out natural_height);
var h = Handler;
if (h != null)
{
var userPreferredSize = h.UserPreferredSize;
if (userPreferredSize.Height > 0)
natural_height = userPreferredSize.Height;

minimum_height = Math.Min(natural_height, minimum_height);
}
}

protected override void OnAdjustSizeAllocation(Gtk.Orientation orientation, out int minimum_size, out int natural_size, out int allocated_pos, out int allocated_size)
{
Expand Down

0 comments on commit 2811397

Please sign in to comment.