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

@@ -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();
}
}
}