From 53329b7068c5eee13e8aea2ee456f134a5a24786 Mon Sep 17 00:00:00 2001 From: deniz1a Date: Sun, 2 Aug 2015 22:13:21 +0300 Subject: [PATCH] Makes dropdown menus auto-scroll to selected item. --- .../Widgets/DropDownButtonWidget.cs | 4 +++ .../Widgets/ScrollPanelWidget.cs | 35 +++++++++++++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/DropDownButtonWidget.cs b/OpenRA.Mods.Common/Widgets/DropDownButtonWidget.cs index 7cb6fe03ca..a679a5c4ed 100644 --- a/OpenRA.Mods.Common/Widgets/DropDownButtonWidget.cs +++ b/OpenRA.Mods.Common/Widgets/DropDownButtonWidget.cs @@ -94,6 +94,10 @@ namespace OpenRA.Mods.Common.Widgets oldBounds.Width, oldBounds.Height); panelRoot.AddChild(panel); + + var scrollPanel = panel as ScrollPanelWidget; + if (scrollPanel != null) + scrollPanel.ScrollToSelectedItem(); } public void ShowDropDown(string panelTemplate, int maxHeight, IEnumerable options, Func setupItem) diff --git a/OpenRA.Mods.Common/Widgets/ScrollPanelWidget.cs b/OpenRA.Mods.Common/Widgets/ScrollPanelWidget.cs index 02ef449ff7..9658a736b9 100644 --- a/OpenRA.Mods.Common/Widgets/ScrollPanelWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ScrollPanelWidget.cs @@ -214,17 +214,8 @@ namespace OpenRA.Mods.Common.Widgets get { return targetListOffset == Math.Min(0, Bounds.Height - ContentHeight) || ContentHeight <= Bounds.Height; } } - public void ScrollToItem(string itemKey, bool smooth = false) + void ScrollToItem(Widget item, bool smooth = false) { - var item = Children.FirstOrDefault(c => - { - var si = c as ScrollItemWidget; - return si != null && si.ItemKey == itemKey; - }); - - if (item == null) - return; - // Scroll the item to be visible float? newOffset = null; if (item.Bounds.Top + currentListOffset < 0) @@ -237,6 +228,30 @@ namespace OpenRA.Mods.Common.Widgets SetListOffset(newOffset.Value, smooth); } + public void ScrollToItem(string itemKey, bool smooth = false) + { + var item = Children.FirstOrDefault(c => + { + var si = c as ScrollItemWidget; + return si != null && si.ItemKey == itemKey; + }); + + if (item != null) + ScrollToItem(item, smooth); + } + + public void ScrollToSelectedItem() + { + var item = Children.FirstOrDefault(c => + { + var si = c as ScrollItemWidget; + return si != null && si.IsSelected(); + }); + + if (item != null) + ScrollToItem(item); + } + public override void Tick() { if (upPressed)