namespace Terminal.Gui.Views;
///
/// Displays text that describes the View next in the . When
/// the user presses a hotkey that matches the of the Label, the next in
/// will be activated.
///
///
///
/// Title and Text are the same property. When Title is set Text s also set. When Text is set Title is also set.
///
///
/// If is and the use clicks on the Label,
/// the will be invoked on the next in
/// .
///
///
public class Label : View, IDesignable
{
///
public Label ()
{
Height = Dim.Auto (DimAutoStyle.Text);
Width = Dim.Auto (DimAutoStyle.Text);
// On HoKey, pass it to the next view
AddCommand (Command.HotKey, InvokeHotKeyOnNextPeer);
TitleChanged += Label_TitleChanged;
MouseClick += Label_MouseClick;
}
private void Label_MouseClick (object sender, MouseEventArgs e)
{
if (!CanFocus)
{
e.Handled = InvokeCommand (Command.HotKey, new ([Command.HotKey], this, this)) == true;
}
}
private void Label_TitleChanged (object sender, EventArgs e)
{
base.Text = e.Value;
TextFormatter.HotKeySpecifier = HotKeySpecifier;
}
///
public override string Text
{
get => Title;
set => base.Text = Title = value;
}
///
public override Rune HotKeySpecifier
{
get => base.HotKeySpecifier;
set => TextFormatter.HotKeySpecifier = base.HotKeySpecifier = value;
}
private bool? InvokeHotKeyOnNextPeer (ICommandContext commandContext)
{
if (RaiseHandlingHotKey () == true)
{
return true;
}
if (CanFocus)
{
SetFocus ();
// Always return true on hotkey, even if SetFocus fails because
// hotkeys are always handled by the View (unless RaiseHandlingHotKey cancels).
// This is the same behavior as the base (View).
return true;
}
if (HotKey.IsValid)
{
// If the Label has a hotkey, we need to find the next view in the subview list
int me = SuperView?.SubViews.IndexOf (this) ?? -1;
if (me != -1 && me < SuperView?.SubViews.Count - 1)
{
return SuperView?.SubViews.ElementAt (me + 1).InvokeCommand (Command.HotKey) == true;
}
}
return false;
}
///
bool IDesignable.EnableForDesign ()
{
Text = "_Label";
return true;
}
}