using System;
namespace Terminal.Gui;
///
/// The Label displays a string at a given position and supports multiple lines separated by newline
/// characters.
/// Multi-line Labels support word wrap.
///
///
/// The view is functionality identical to and is included for API backwards
/// compatibility.
///
public class Label : View {
///
public Label () => SetInitialProperties ();
///
public Label (Rect frame, bool autosize = false) : base (frame) => SetInitialProperties (autosize);
///
public Label (string text, bool autosize = true) : base (text) => SetInitialProperties (autosize);
///
public Label (Rect rect, string text, bool autosize = false) : base (rect, text) => SetInitialProperties (autosize);
///
public Label (int x, int y, string text, bool autosize = true) : base (x, y, text) => SetInitialProperties (autosize);
///
public Label (string text, TextDirection direction, bool autosize = true)
: base (text, direction) => SetInitialProperties (autosize);
void SetInitialProperties (bool autosize = true)
{
Height = 1;
AutoSize = autosize;
// Things this view knows how to do
AddCommand (Command.Default, () => {
// BUGBUG: This is a hack, but it does work.
var can = CanFocus;
CanFocus = true;
SetFocus ();
SuperView.FocusNext ();
CanFocus = can;
return true;
});
AddCommand (Command.Accept, () => AcceptKey ());
// Default key bindings for this view
KeyBindings.Add (KeyCode.Space, Command.Accept);
}
bool AcceptKey ()
{
if (!HasFocus) {
SetFocus ();
}
OnClicked ();
return true;
}
///
/// The event fired when the user clicks the primary mouse button within the Bounds of this
/// or if the user presses the action key while this view is focused. (TODO: IsDefault)
///
///
/// Client code can hook up to this event, it is
/// raised when the button is activated either with
/// the mouse or the keyboard.
///
public event EventHandler Clicked;
///
/// Method invoked when a mouse event is generated
///
///
/// true, if the event was handled, false otherwise.
public override bool OnMouseEvent (MouseEvent mouseEvent)
{
var args = new MouseEventEventArgs (mouseEvent);
if (OnMouseClick (args)) {
return true;
}
if (MouseEvent (mouseEvent)) {
return true;
}
if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
if (!HasFocus && SuperView != null) {
if (!SuperView.HasFocus) {
SuperView.SetFocus ();
}
SetFocus ();
SetNeedsDisplay ();
}
OnClicked ();
return true;
}
return false;
}
///
public override bool OnEnter (View view)
{
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
return base.OnEnter (view);
}
///
/// Virtual method to invoke the event.
///
public virtual void OnClicked () => Clicked?.Invoke (this, EventArgs.Empty);
}