From e2b26754feab43ae664472ce32399c8e53b7133c Mon Sep 17 00:00:00 2001 From: Gustas Date: Tue, 6 Aug 2024 13:36:10 +0300 Subject: [PATCH] Fix switching tabs not yielding keyboard focus And some extra polish --- .../Logic/Editor/MapEditorTabsLogic.cs | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorTabsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorTabsLogic.cs index e32161b677..b31517d072 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorTabsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorTabsLogic.cs @@ -15,24 +15,24 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class MapEditorTabsLogic : ChromeLogic { - readonly Widget widget; + enum MenuType { Select, Tiles, Layers, Actors, Tools, History } + + readonly Widget panelContainer; + readonly Widget tabContainer; readonly EditorViewportControllerWidget editor; - protected enum MenuType { Select, Tiles, Layers, Actors, Tools, History } - protected MenuType menuType = MenuType.Tiles; - readonly Widget tabContainer; - + MenuType menuType = MenuType.Tiles; MenuType lastSelectedTab = MenuType.Tiles; [ObjectCreator.UseCtor] public MapEditorTabsLogic(Widget widget) { - this.widget = widget; + panelContainer = widget.Parent; + tabContainer = widget.Get("MAP_EDITOR_TAB_CONTAINER"); + editor = widget.Parent.Parent.Get("MAP_EDITOR"); editor.DefaultBrush.UpdateSelectedTab += HandleUpdateSelectedTab; - tabContainer = widget.Get("MAP_EDITOR_TAB_CONTAINER"); - SetupTab("SELECT_TAB", "SELECT_WIDGETS", MenuType.Select); SetupTab("TILES_TAB", "TILE_WIDGETS", MenuType.Tiles); SetupTab("OVERLAYS_TAB", "LAYER_WIDGETS", MenuType.Layers); @@ -50,28 +50,27 @@ namespace OpenRA.Mods.Common.Widgets.Logic void SetupTab(string buttonId, string tabId, MenuType tabType) { - if (buttonId != null) + var tab = tabContainer.Get(buttonId); + tab.IsHighlighted = () => menuType == tabType; + tab.OnClick = () => { - var tab = tabContainer.Get(buttonId); - tab.IsHighlighted = () => menuType == tabType; - tab.OnClick = () => menuType = SelectTab(tabType); + if (tabType != MenuType.Select) + lastSelectedTab = tabType; - if (tabType == MenuType.Select) - tab.IsDisabled = () => !editor.DefaultBrush.Selection.HasSelection; - } + menuType = tabType; - var container = widget.Parent.Get(tabId); + // Clear keyboard focus when switching tabs. + Ui.KeyboardFocusWidget = null; + }; + + // Selection tab is special, it can only be selected if a selection exists. + if (tabType == MenuType.Select) + tab.IsDisabled = () => !editor.DefaultBrush.Selection.HasSelection; + + var container = panelContainer.Get(tabId); container.IsVisible = () => menuType == tabType; } - MenuType SelectTab(MenuType newMenuType) - { - if (newMenuType != MenuType.Select) - lastSelectedTab = newMenuType; - - return newMenuType; - } - void HandleUpdateSelectedTab() { var hasSelection = editor.DefaultBrush.Selection.HasSelection;