More UI fixes (#22431)

* Fix chat filter button

Oh look, the popup code was copy pasted between chat filter and channel selector.

Hilarious.

Anyways same stuff as 995f506aaf. I pulled it all out into a base class so NO MORE COPY PASTE.

Fixes #22360

* Remove all further EnableAllKeybinds buttons.

Fixes #22346

Yeah none of these are valid use cases, why is this set...
This commit is contained in:
Pieter-Jan Briers
2023-12-13 03:02:19 +01:00
committed by GitHub
parent 93f8c4ef60
commit 16bd6802df
12 changed files with 113 additions and 121 deletions

View File

@@ -122,7 +122,7 @@ namespace Content.Client.Administration.UI.CustomControls
}
}
});
button.EnableAllKeybinds = true;
button.AddStyleClass(ListContainer.StyleClassListContainerButton);
}
}

View File

@@ -1,6 +1,5 @@
<ContainerButton xmlns="https://spacestation14.io"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
EnableAllKeybinds="True">
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls">
<PanelContainer Name="BackgroundColorPanel"/>
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"

View File

@@ -1,6 +1,5 @@
<ContainerButton xmlns="https://spacestation14.io"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
EnableAllKeybinds="True">
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls">
<PanelContainer Name="BackgroundColorPanel"/>
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"

View File

@@ -1,6 +1,5 @@
<ContainerButton xmlns="https://spacestation14.io"
xmlns:cc="clr-namespace:Content.Client.Administration.UI.CustomControls"
EnableAllKeybinds="True">
<Control xmlns="https://spacestation14.io"
xmlns:cc="clr-namespace:Content.Client.Administration.UI.CustomControls">
<PanelContainer Name="BackgroundColorPanel" Access="Public"/>
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"
@@ -39,4 +38,4 @@
Text="{Loc player-tab-playtime}"
MouseFilter="Pass"/>
</BoxContainer>
</ContainerButton>
</Control>

View File

@@ -7,7 +7,7 @@ using Robust.Shared.Input;
namespace Content.Client.Administration.UI.Tabs.PlayerTab;
[GenerateTypedNameReferences]
public sealed partial class PlayerTabHeader : ContainerButton
public sealed partial class PlayerTabHeader : Control
{
public event Action<Header>? OnHeaderClicked;

View File

@@ -1,6 +1,5 @@
<ContainerButton xmlns="https://spacestation14.io"
xmlns:customControls1="clr-namespace:Content.Client.Administration.UI.CustomControls"
EnableAllKeybinds="True">
xmlns:customControls1="clr-namespace:Content.Client.Administration.UI.CustomControls">
<PanelContainer Name="BackgroundColorPanel"/>
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"

View File

@@ -7,7 +7,7 @@ using Robust.Shared.Input;
namespace Content.Client.Info.PlaytimeStats;
[GenerateTypedNameReferences]
public sealed partial class PlaytimeStatsHeader : ContainerButton
public sealed partial class PlaytimeStatsHeader : Control
{
public event Action<Header, SortDirection>? OnHeaderClicked;
private SortDirection _roleDirection = SortDirection.Ascending;

View File

@@ -1,6 +1,5 @@
<ContainerButton xmlns="https://spacestation14.io"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
EnableAllKeybinds="True">
<Control xmlns="https://spacestation14.io"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls">
<PanelContainer Name="BackgroundColorPlaytimePanel" Access="Public"/>
<BoxContainer Orientation="Vertical"
HorizontalExpand="True">
@@ -26,4 +25,4 @@
<!-- Horizontal Separator -->
<customControls:HSeparator/>
</BoxContainer>
</ContainerButton>
</Control>

View File

@@ -1,20 +1,18 @@
using System.Numerics;
using Content.Client.Resources;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelFilterButton : ContainerButton
public sealed class ChannelFilterButton : ChatPopupButton<ChannelFilterPopup>
{
private static readonly Color ColorNormal = Color.FromHex("#7b7e9e");
private static readonly Color ColorHovered = Color.FromHex("#9699bb");
private static readonly Color ColorPressed = Color.FromHex("#789B8C");
private readonly TextureRect? _textureRect;
public readonly ChannelFilterPopup ChatFilterPopup;
private readonly ChatUIController _chatUIController;
private const int FilterDropdownOffset = 120;
public ChannelFilterButton()
@@ -23,10 +21,6 @@ public sealed class ChannelFilterButton : ContainerButton
var filterTexture = IoCManager.Resolve<IResourceCache>()
.GetTexture("/Textures/Interface/Nano/filter.svg.96dpi.png");
// needed for same reason as ChannelSelectorButton
Mode = ActionMode.Press;
EnableAllKeybinds = true;
AddChild(
(_textureRect = new TextureRect
{
@@ -35,42 +29,19 @@ public sealed class ChannelFilterButton : ContainerButton
VerticalAlignment = VAlignment.Center
})
);
ToggleMode = true;
OnToggled += OnFilterButtonToggled;
ChatFilterPopup = UserInterfaceManager.CreatePopup<ChannelFilterPopup>();
ChatFilterPopup.OnVisibilityChanged += PopupVisibilityChanged;
_chatUIController.FilterableChannelsChanged += ChatFilterPopup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated += ChatFilterPopup.UpdateUnread;
ChatFilterPopup.SetChannels(_chatUIController.FilterableChannels);
_chatUIController.FilterableChannelsChanged += Popup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated += Popup.UpdateUnread;
Popup.SetChannels(_chatUIController.FilterableChannels);
}
private void PopupVisibilityChanged(Control control)
{
Pressed = control.Visible;
}
private void OnFilterButtonToggled(ButtonToggledEventArgs args)
{
if (args.Pressed)
protected override UIBox2 GetPopupPosition()
{
var globalPos = GlobalPosition;
var (minX, minY) = ChatFilterPopup.MinSize;
var box = UIBox2.FromDimensions(globalPos - new Vector2(FilterDropdownOffset, 0),
new Vector2(Math.Max(minX, ChatFilterPopup.MinWidth), minY));
ChatFilterPopup.Open(box);
}
else
{
ChatFilterPopup.Close();
}
}
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{
// needed since we need EnableAllKeybinds - don't double-send both UI click and Use
if (args.Function == EngineKeyFunctions.Use) return;
base.KeyBindDown(args);
var (minX, minY) = Popup.MinSize;
return UIBox2.FromDimensions(
globalPos - new Vector2(FilterDropdownOffset, 0),
new Vector2(Math.Max(minX, Popup.MinWidth), minY));
}
private void UpdateChildColors()
@@ -114,7 +85,7 @@ public sealed class ChannelFilterButton : ContainerButton
if (!disposing)
return;
_chatUIController.FilterableChannelsChanged -= ChatFilterPopup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated -= ChatFilterPopup.UpdateUnread;
_chatUIController.FilterableChannelsChanged -= Popup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated -= Popup.UpdateUnread;
}
}

View File

@@ -1,80 +1,51 @@
using System.Numerics;
using Content.Shared.Chat;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Timing;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelSelectorButton : Button
public sealed class ChannelSelectorButton : ChatPopupButton<ChannelSelectorPopup>
{
private readonly IGameTiming _gameTiming;
private readonly ChannelSelectorPopup _channelSelectorPopup;
public event Action<ChatSelectChannel>? OnChannelSelect;
public ChatSelectChannel SelectedChannel { get; private set; }
private const int SelectorDropdownOffset = 38;
private uint _frameLastPopupChanged;
public ChannelSelectorButton()
{
_gameTiming = IoCManager.Resolve<IGameTiming>();
Name = "ChannelSelector";
ToggleMode = true;
Popup.Selected += OnChannelSelected;
OnToggled += OnSelectorButtonToggled;
_channelSelectorPopup = UserInterfaceManager.CreatePopup<ChannelSelectorPopup>();
_channelSelectorPopup.Selected += OnChannelSelected;
_channelSelectorPopup.OnVisibilityChanged += OnPopupVisibilityChanged;
if (_channelSelectorPopup.FirstChannel is { } firstSelector)
if (Popup.FirstChannel is { } firstSelector)
{
Select(firstSelector);
}
}
protected override UIBox2 GetPopupPosition()
{
var globalLeft = GlobalPosition.X;
var globalBot = GlobalPosition.Y + Height;
return UIBox2.FromDimensions(
new Vector2(globalLeft, globalBot),
new Vector2(SizeBox.Width, SelectorDropdownOffset));
}
private void OnChannelSelected(ChatSelectChannel channel)
{
Select(channel);
}
private void OnPopupVisibilityChanged(Control control)
{
// If the popup gets closed (e.g. by clicking anywhere else on the screen)
// We clear the button pressed state.
Pressed = control.Visible;
_frameLastPopupChanged = _gameTiming.CurFrame;
}
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{
// If you try to close the popup by clicking on the button again the following would happen:
// The UI system would see that you clicked outside a popup, and would close it.
// Because of the above logic, that sets the button to UNPRESSED.
// THEN, it would propagate the keyboard event to us, the chat selector...
// And we would become pressed again.
// As a workaround, we check the frame the popup was last dismissed (above)
// and don't allow changing it again this frame.
if (_frameLastPopupChanged == _gameTiming.CurFrame)
return;
base.KeyBindDown(args);
}
public void Select(ChatSelectChannel channel)
{
if (_channelSelectorPopup.Visible)
if (Popup.Visible)
{
_channelSelectorPopup.Close();
Popup.Close();
}
if (SelectedChannel == channel) return;
if (SelectedChannel == channel)
return;
SelectedChannel = channel;
OnChannelSelect?.Invoke(channel);
}
@@ -102,19 +73,4 @@ public sealed class ChannelSelectorButton : Button
Text = radio != null ? Loc.GetString(radio.Name) : ChannelSelectorName(channel);
Modulate = radio?.Color ?? ChannelSelectColor(channel);
}
private void OnSelectorButtonToggled(ButtonToggledEventArgs args)
{
if (args.Pressed)
{
var globalLeft = GlobalPosition.X;
var globalBot = GlobalPosition.Y + Height;
var box = UIBox2.FromDimensions(new Vector2(globalLeft, globalBot), new Vector2(SizeBox.Width, SelectorDropdownOffset));
_channelSelectorPopup.Open(box);
}
else
{
_channelSelectorPopup.Close();
}
}
}

View File

@@ -0,0 +1,70 @@
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Timing;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
// NO MORE FUCKING COPY PASTING THIS SHIT
/// <summary>
/// Base class for button that toggles a popup-window.
/// Base type of <see cref="ChannelFilterButton"/> and <see cref="ChannelSelectorButton"/>.
/// </summary>
public abstract class ChatPopupButton<TPopup> : Button
where TPopup : Popup, new()
{
private readonly IGameTiming _gameTiming;
public readonly TPopup Popup;
private uint _frameLastPopupChanged;
protected ChatPopupButton()
{
_gameTiming = IoCManager.Resolve<IGameTiming>();
ToggleMode = true;
OnToggled += OnButtonToggled;
Popup = UserInterfaceManager.CreatePopup<TPopup>();
Popup.OnVisibilityChanged += OnPopupVisibilityChanged;
}
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{
// If you try to close the popup by clicking on the button again the following would happen:
// The UI system would see that you clicked outside a popup, and would close it.
// Because of the above logic, that sets the button to UNPRESSED.
// THEN, it would propagate the keyboard event to us, the chat selector...
// And we would become pressed again.
// As a workaround, we check the frame the popup was last dismissed (above)
// and don't allow changing it again this frame.
if (_frameLastPopupChanged == _gameTiming.CurFrame)
return;
base.KeyBindDown(args);
}
protected abstract UIBox2 GetPopupPosition();
private void OnButtonToggled(ButtonToggledEventArgs args)
{
if (args.Pressed)
{
Popup.Open(GetPopupPosition());
}
else
{
Popup.Close();
}
}
private void OnPopupVisibilityChanged(Control control)
{
// If the popup gets closed (e.g. by clicking anywhere else on the screen)
// We clear the button pressed state.
Pressed = control.Visible;
_frameLastPopupChanged = _gameTiming.CurFrame;
}
}

View File

@@ -36,7 +36,7 @@ public partial class ChatBox : UIWidget
ChatInput.Input.OnKeyBindDown += OnKeyBindDown;
ChatInput.Input.OnTextChanged += OnTextChanged;
ChatInput.ChannelSelector.OnChannelSelect += OnChannelSelect;
ChatInput.FilterButton.ChatFilterPopup.OnChannelFilter += OnChannelFilter;
ChatInput.FilterButton.Popup.OnChannelFilter += OnChannelFilter;
_controller = UserInterfaceManager.GetUIController<ChatUIController>();
_controller.MessageAdded += OnMessageAdded;
@@ -51,7 +51,7 @@ public partial class ChatBox : UIWidget
private void OnMessageAdded(ChatMessage msg)
{
Logger.DebugS("chat", $"{msg.Channel}: {msg.Message}");
if (!ChatInput.FilterButton.ChatFilterPopup.IsActive(msg.Channel))
if (!ChatInput.FilterButton.Popup.IsActive(msg.Channel))
{
return;
}