Fix being unable to open chat channel drop-down. (#22255)

This commit is contained in:
Pieter-Jan Briers
2023-12-09 03:15:06 +01:00
committed by GitHub
parent 8902591fb1
commit 995f506aaf

View File

@@ -2,16 +2,14 @@ using System.Numerics;
using Content.Shared.Chat; using Content.Shared.Chat;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input; using Robust.Shared.Timing;
namespace Content.Client.UserInterface.Systems.Chat.Controls; namespace Content.Client.UserInterface.Systems.Chat.Controls;
/// <summary>
/// Only needed to avoid the issue where right click on the button closes the popup
/// but leaves the button highlighted.
/// </summary>
public sealed class ChannelSelectorButton : Button public sealed class ChannelSelectorButton : Button
{ {
private readonly IGameTiming _gameTiming;
private readonly ChannelSelectorPopup _channelSelectorPopup; private readonly ChannelSelectorPopup _channelSelectorPopup;
public event Action<ChatSelectChannel>? OnChannelSelect; public event Action<ChatSelectChannel>? OnChannelSelect;
@@ -19,15 +17,16 @@ public sealed class ChannelSelectorButton : Button
private const int SelectorDropdownOffset = 38; private const int SelectorDropdownOffset = 38;
private uint _frameLastPopupChanged;
public ChannelSelectorButton() public ChannelSelectorButton()
{ {
// needed so the popup is untoggled regardless of which key is pressed when hovering this button. _gameTiming = IoCManager.Resolve<IGameTiming>();
// If we don't have this, then right clicking the button while it's toggled on will hide
// the popup but keep the button toggled on
Name = "ChannelSelector"; Name = "ChannelSelector";
Mode = ActionMode.Press;
EnableAllKeybinds = true;
ToggleMode = true; ToggleMode = true;
OnToggled += OnSelectorButtonToggled; OnToggled += OnSelectorButtonToggled;
_channelSelectorPopup = UserInterfaceManager.CreatePopup<ChannelSelectorPopup>(); _channelSelectorPopup = UserInterfaceManager.CreatePopup<ChannelSelectorPopup>();
_channelSelectorPopup.Selected += OnChannelSelected; _channelSelectorPopup.Selected += OnChannelSelected;
@@ -46,13 +45,25 @@ public sealed class ChannelSelectorButton : Button
private void OnPopupVisibilityChanged(Control control) 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; Pressed = control.Visible;
_frameLastPopupChanged = _gameTiming.CurFrame;
} }
protected override void KeyBindDown(GUIBoundKeyEventArgs args) protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{ {
// needed since we need EnableAllKeybinds - don't double-send both UI click and Use // If you try to close the popup by clicking on the button again the following would happen:
if (args.Function == EngineKeyFunctions.Use) return; // 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); base.KeyBindDown(args);
} }