//
// StatusBar.cs: a statusbar for an application
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// TODO:
// Add mouse support
using System;
using NStack;
namespace Terminal.Gui {
///
/// objects are contained by s.
/// Each has a title, a shortcut (hotkey), and an that will be invoked when the
/// is pressed.
/// The will be a global hotkey for the application in the current context of the screen.
/// The colour of the will be changed after each ~.
/// A set to `~F1~ Help` will render as *F1* using and
/// *Help* as .
///
public class StatusItem {
///
/// Initializes a new .
///
/// Shortcut to activate the .
/// Title for the .
/// Action to invoke when the is activated.
public StatusItem (Key shortcut, ustring title, Action action)
{
Title = title ?? "";
Shortcut = shortcut;
Action = action;
}
///
/// Gets the global shortcut to invoke the action on the menu.
///
public Key Shortcut { get; }
///
/// Gets or sets the title.
///
/// The title.
///
/// The colour of the will be changed after each ~.
/// A set to `~F1~ Help` will render as *F1* using and
/// *Help* as .
///
public ustring Title { get; set; }
///
/// Gets or sets the action to be invoked when the statusbar item is triggered
///
/// Action to invoke.
public Action Action { get; }
};
///
/// A status bar is a that snaps to the bottom of a displaying set of s.
/// The should be context sensitive. This means, if the main menu and an open text editor are visible, the items probably shown will
/// be ~F1~ Help ~F2~ Save ~F3~ Load. While a dialog to ask a file to load is executed, the remaining commands will probably be ~F1~ Help.
/// So for each context must be a new instance of a statusbar.
///
public class StatusBar : View {
bool disposedValue;
///
/// The items that compose the
///
public StatusItem [] Items { get; set; }
///
/// Initializes a new instance of the class.
///
public StatusBar () : this (items: new StatusItem [] { }) { }
///
/// Initializes a new instance of the class with the specified set of s.
/// The will be drawn on the lowest line of the terminal or (if not null).
///
/// A list of statusbar items.
public StatusBar (StatusItem [] items) : base ()
{
Width = Dim.Fill ();
Height = 1;
Items = items;
CanFocus = false;
ColorScheme = Colors.Menu;
X = 0;
Y = SuperView != null ? SuperView.Frame.Height - 1 : Pos.AnchorEnd (1);
Width = Dim.Fill ();
Height = 1;
Application.Resized += Application_Resized ();
}
private Action Application_Resized ()
{
return delegate {
X = 0;
Height = 1;
if (SuperView == null || SuperView is Toplevel) {
Y = SuperView.Frame.Height - 1;
} else {
//Y = Pos.Bottom (SuperView);
}
};
}
Attribute ToggleScheme (Attribute scheme)
{
var result = scheme == ColorScheme.Normal ? ColorScheme.HotNormal : ColorScheme.Normal;
Driver.SetAttribute (result);
return result;
}
///
public override void Redraw (Rect bounds)
{
//if (Frame.Y != Driver.Rows - 1) {
// Frame = new Rect (Frame.X, Driver.Rows - 1, Frame.Width, Frame.Height);
// Y = Driver.Rows - 1;
// SetNeedsDisplay ();
//}
Move (0, 0);
Driver.SetAttribute (Colors.Menu.Normal);
for (int i = 0; i < Frame.Width; i++)
Driver.AddRune (' ');
Move (1, 0);
var scheme = ColorScheme.Normal;
Driver.SetAttribute (scheme);
for (int i = 0; i < Items.Length; i++) {
var title = Items [i].Title;
for (int n = 0; n < title.RuneCount; n++) {
if (title [n] == '~') {
scheme = ToggleScheme (scheme);
continue;
}
Driver.AddRune (title [n]);
}
if (i + 1 < Items.Length) {
Driver.AddRune (' ');
Driver.AddRune (Driver.VLine);
Driver.AddRune (' ');
}
}
}
///
public override bool ProcessHotKey (KeyEvent kb)
{
foreach (var item in Items) {
if (kb.Key == item.Shortcut) {
item.Action?.Invoke ();
return true;
}
}
return false;
}
///
public override bool MouseEvent (MouseEvent me)
{
if (me.Flags != MouseFlags.Button1Clicked)
return false;
int pos = 1;
for (int i = 0; i < Items.Length; i++) {
if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
Run (Items [i].Action);
}
pos += GetItemTitleLength (Items [i].Title) + 3;
}
return true;
}
int GetItemTitleLength (ustring title)
{
int len = 0;
foreach (var ch in title) {
if (ch == '~')
continue;
len++;
}
return len;
}
void Run (Action action)
{
if (action == null)
return;
Application.MainLoop.AddIdle (() => {
action ();
return false;
});
}
///
protected override void Dispose (bool disposing)
{
if (!disposedValue) {
if (disposing) {
Application.Resized -= Application_Resized ();
}
disposedValue = true;
}
}
}
}