using System;
using System.Collections.Generic;
using Annulus;
using Azimuth;
using Glass;
namespace Clave.Glass
{
///
/// One action's part of a bindings screen: its name, then its chords in two columns of cells that wrap onto more
/// lines, with one empty cell after the last that adds a chord. A chord too wide for a cell takes a whole line. A
/// chord that clashes with another binding has a note on the line under it.
///
public class KeyBindingRow : Control
{
///
/// How many cells a line holds.
///
public const int Columns = 2;
private readonly KeyBindingsEditor editor;
private readonly KeyBindingsMaterial material;
// A slot per chord and one more after them, which adds a chord.
private readonly List slots = new List();
// A clash note per slot, in the same order, hidden when its chord clashes with nothing.
private readonly List notes = new List();
private Scalar nameColumnWidth;
private Scalar cellWidth;
// True while Refresh changes the slots, so a slot it shows doesn't lay the row out before it's done.
private bool refreshing;
public KeyBindingRow(KeyBindingsEditor editor, KeyBindings source, KeyAction action,
KeyBindingsMaterial material)
{
this.editor = editor ?? throw new ArgumentNullException(nameof(editor));
Source = source ?? throw new ArgumentNullException(nameof(source));
Action = action ?? throw new ArgumentNullException(nameof(action));
this.material = material;
SetMaterial(global::Glass.Material.Invisible);
NameLabel = new Label(material.ActionTextStyle ?? new TextMaterialStyle(), action.Name);
CanvasChildren.Add(NameLabel);
Refresh();
}
public KeyBindings Source { get; }
public KeyAction Action { get; }
public Label NameLabel { get; }
///
/// Raised after the row is laid out again, which may have changed its size.
///
public event Action Resized;
///
/// Gets a slot per chord, in order, and last the slot that adds one.
///
public IReadOnlyList Slots
{
get { return slots; }
}
///
/// Gets the note for each slot, in the same order, which says what its chord clashes with and is hidden when it
/// clashes with nothing.
///
public IReadOnlyList Notes
{
get { return notes; }
}
///
/// Gets the width the name needs, for a screen lining its rows up.
///
public Scalar NameWidth => NameLabel.PreferredSize(Constraints.Unbounded).X;
///
/// Gets the height of each line of cells: a binding's height, or a taller cell's. A binding counts even when
/// there is none, so every row's lines match.
///
public Scalar LineHeight
{
get
{
Scalar keyLine = (material.KeyTextStyle ?? new TextMaterialStyle()).SingleLineHeight;
Scalar returnMe = KeyBindingSlot.BoundsSizeFor(new Vector(0, keyLine), removable: true,
material.Binding, material).Y;
foreach (KeyBindingSlot slot in slots)
{
returnMe = Scalar.Max(returnMe, slot.PreferredBoundsSize.Y);
}
return returnMe;
}
}
///
/// Gets the width a cell needs: the material's sizing binding with its remove button, the add text and the
/// capture prompt, whichever is widest. A wider binding takes a whole line instead.
///
public Scalar CellWidth
{
get
{
KeyBindingsText text = material.TextOrEnglish;
Vector sizing = KeyBindingSlot.ContentSizeFor(material.SizingComboOrDefault, Source.Keys.KeyLabels,
material);
Scalar returnMe = KeyBindingSlot.BoundsSizeFor(sizing, removable: true, material.Binding, material).X;
returnMe = Scalar.Max(returnMe,
KeyBindingSlot.BoundsSizeFor(text.AddChord, material.EmptyBinding, material).X);
returnMe = Scalar.Max(returnMe,
KeyBindingSlot.BoundsSizeFor(text.CapturePrompt, material.CapturingBinding, material).X);
return returnMe;
}
}
///
/// Gets the width of the widest slot here, for a screen making sure a whole line can hold it.
///
public Scalar WidestSlot
{
get
{
Scalar returnMe = 0;
foreach (KeyBindingSlot slot in slots)
{
returnMe = Scalar.Max(returnMe, slot.PreferredBoundsSize.X);
}
return returnMe;
}
}
private void AddSlot(int index)
{
var slot = new KeyBindingSlot(editor, Source, Action, index, material);
// Refresh lays the row out once after showing every slot.
slot.Resized += _ =>
{
if (!refreshing)
{
Layout(nameColumnWidth, cellWidth);
}
};
var note = new Label(material.ClashTextStyle ?? new TextMaterialStyle(), "");
slots.Add(slot);
notes.Add(note);
CanvasChildren.Add(slot);
CanvasChildren.Add(note);
}
private void RemoveLastSlot()
{
int last = slots.Count - 1;
slots[last].CancelCapture();
CanvasChildren.Remove(slots[last]);
CanvasChildren.Remove(notes[last]);
slots.RemoveAt(last);
notes.RemoveAt(last);
}
private string ClashTextOf(Chord chord)
{
KeyBindingsText text = material.TextOrEnglish;
var clashes = new List();
foreach (KeyClash clash in editor.ClashesOf(Source, Action, chord))
{
string description = text.Describe(clash);
if (!clashes.Contains(description))
{
clashes.Add(description);
}
}
if (clashes.Count == 0)
{
return "";
}
string keys = Source.Keys.KeyLabels.LabelOf(chord);
return string.Format(text.ClashNoteFormat, keys, string.Join(text.ClashSeparator, clashes));
}
///
/// Shows the action's chords and clashes as they are now. The slot that adds a chord stays the same slot when
/// its capture fills it, so focus stays on it.
///
public void Refresh()
{
IReadOnlyList chords = Source.ChordsOf(Action);
refreshing = true;
try
{
while (slots.Count > chords.Count + 1)
{
RemoveLastSlot();
}
while (slots.Count < chords.Count + 1)
{
AddSlot(slots.Count);
}
for (int i = 0; i < slots.Count; ++i)
{
string note = i < chords.Count ? ClashTextOf(chords[i]) : "";
notes[i].Text = note;
notes[i].Visible = note.Length > 0;
slots[i].Clashing = note.Length > 0;
slots[i].Show();
}
}
finally
{
refreshing = false;
}
Layout(nameColumnWidth, cellWidth);
}
// Which line each slot goes on, and how many cells it takes: two for a slot wider than one cell.
private List> LinesFor(Scalar cell, out List spans)
{
var returnMe = new List>();
spans = new List();
int column = Columns;
for (int i = 0; i < slots.Count; ++i)
{
int span = slots[i].PreferredBoundsSize.X > cell ? Columns : 1;
spans.Add(span);
if (column + span > Columns)
{
returnMe.Add(new List());
column = 0;
}
returnMe[returnMe.Count - 1].Add(i);
column += span;
}
return returnMe;
}
///
/// Arranges the row with its cells starting in, or past the name if that is
/// wider, each wide or as wide as this row's cells need, and sizes the row to fit.
/// Keeps the row's position.
///
public void Layout(Scalar nameColumnWidth, Scalar cellWidth)
{
this.nameColumnWidth = nameColumnWidth;
this.cellWidth = cellWidth;
Scalar spacing = material.Spacing;
Scalar line = LineHeight;
Scalar cell = Scalar.Max(cellWidth, CellWidth);
Vector nameSize = NameLabel.PreferredSize(Constraints.Unbounded);
Scalar nameColumn = Scalar.Max(nameSize.X, nameColumnWidth);
List> lines = LinesFor(cell, out List spans);
// Measure: the name beside the lines of cells, each followed by the notes of its cells.
var cells = new RectAggregator(new Vector(spacing, spacing));
cells.NewCols(cell, Columns);
Scalar cellsWidth = cells.Rect.Width;
foreach (List slotsInLine in lines)
{
cells.NewRow(line);
foreach (int i in slotsInLine)
{
if (notes[i].Visible)
{
Vector noteSize = notes[i].PreferredSize(Constraints.Unbounded);
cells.NewRow(noteSize.Y);
cellsWidth = Scalar.Max(cellsWidth, noteSize.X);
}
}
}
var columns = new RectAggregator(new Vector(spacing, 0));
columns.NewCol(nameColumn);
columns.NewCol(cellsWidth);
Scalar height = Scalar.Max(nameSize.Y, cells.Rect.Height);
Bounds = Bounds.WithSize(new Vector(columns.Rect.Width, height));
// Arrange: the same parts in the space the row has.
Rect remainder = Rect.FromPositionAndSize(Vector.Zero, CanvasRect.Size);
Rect nameRect = remainder.NewCol(nameColumn, margin: spacing, shortfall: Shortfall.UseWhatIsLeft);
Rect nameLine = nameRect.NewRow(line, VerticalJustification.Top, 0, Shortfall.UseWhatIsLeft);
Rect nameSlot = nameLine.NewCol(nameSize.X, HorizontalJustification.Left, 0, Shortfall.UseWhatIsLeft);
nameSlot.Center(nameSize, Shortfall.UseWhatIsLeft);
NameLabel.Bounds = nameSlot;
var spanned = new RectAggregator(new Vector(spacing, 0));
spanned.NewCols(cell, Columns);
Scalar wholeLine = spanned.Rect.Width;
foreach (List slotsInLine in lines)
{
Rect lineRect = remainder.NewRow(line, margin: spacing, shortfall: Shortfall.UseWhatIsLeft);
foreach (int i in slotsInLine)
{
Scalar width = spans[i] == Columns ? wholeLine : cell;
slots[i].Arrange(lineRect.NewCol(width, margin: spacing, shortfall: Shortfall.UseWhatIsLeft));
}
foreach (int i in slotsInLine)
{
if (notes[i].Visible)
{
Vector noteSize = notes[i].PreferredSize(Constraints.Unbounded);
Rect noteRow = remainder.NewRow(noteSize.Y, margin: spacing,
shortfall: Shortfall.UseWhatIsLeft);
notes[i].Bounds = noteRow.NewCol(noteSize.X, shortfall: Shortfall.UseWhatIsLeft);
}
}
}
Resized?.Invoke(this);
}
}
}