1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-29 21:07:27 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/FocusedTextBox.cs
Dean Herbert 34ffc51c51 Avoid clearing chat overlay textbox when pressing "back" key binding
Generally this is expected behaviour for usages of focused text boxes
(ie. to clear search content), but not so much here.

Addresses https://github.com/ppy/osu/discussions/19403#discussioncomment-3230395.
2022-08-02 13:56:03 +09:00

105 lines
3.0 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osuTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using osu.Game.Input.Bindings;
using osuTK.Input;
using osu.Framework.Input.Bindings;
using osu.Game.Overlays;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// A textbox which holds focus eagerly.
/// </summary>
public class FocusedTextBox : OsuTextBox, IKeyBindingHandler<GlobalAction>
{
private bool focus;
private bool allowImmediateFocus => host?.OnScreenKeyboardOverlapsGameWindow != true;
/// <summary>
/// Whether the content of the text box should be cleared on the first "back" key press.
/// </summary>
protected virtual bool ClearTextOnBackKey => true;
public void TakeFocus()
{
if (!allowImmediateFocus)
return;
Scheduler.Add(() => GetContainingInputManager().ChangeFocus(this));
}
public new void KillFocus() => base.KillFocus();
public bool HoldFocus
{
get => allowImmediateFocus && focus;
set
{
focus = value;
if (!focus && HasFocus)
base.KillFocus();
}
}
[Resolved]
private GameHost? host { get; set; }
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider)
{
BackgroundUnfocused = colourProvider?.Background5 ?? new Color4(10, 10, 10, 255);
BackgroundFocused = colourProvider?.Background5 ?? new Color4(10, 10, 10, 255);
}
// We may not be focused yet, but we need to handle keyboard input to be able to request focus
public override bool HandleNonPositionalInput => HoldFocus || base.HandleNonPositionalInput;
protected override void OnFocus(FocusEvent e)
{
base.OnFocus(e);
BorderThickness = 0;
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (!HasFocus) return false;
if (e.Key == Key.Escape)
return false; // disable the framework-level handling of escape key for conformity (we use GlobalAction.Back).
return base.OnKeyDown(e);
}
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
if (!HasFocus) return false;
if (ClearTextOnBackKey && e.Action == GlobalAction.Back)
{
if (Text.Length > 0)
{
Text = string.Empty;
return true;
}
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
public override bool RequestsFocus => HoldFocus;
}
}