123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713 |
- using Microsoft.VisualBasic.FileIO;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using Unix.Terminal;
- namespace Terminal.Gui;
- /// <summary>
- /// <see cref="EventArgs"/> for <see cref="Slider{T}"/> <see cref="SliderOption{T}"/> events.
- /// </summary>
- public class SliderOptionEventArgs : EventArgs {
- /// <summary>
- /// Gets whether the option is set or not.
- /// </summary>
- public bool IsSet { get; }
- /// <summary>
- /// Initializes a new instance of <see cref="SliderOptionEventArgs"/>
- /// </summary>
- /// <param name="isSet"> indicates whether the option is set</param>
- public SliderOptionEventArgs (bool isSet)
- {
- IsSet = isSet;
- }
- }
- /// <summary>
- /// Represents an option in a <see cref="Slider{T}"/> .
- /// </summary>
- /// <typeparam name="T">Datatype of the option.</typeparam>
- public class SliderOption<T> {
- /// <summary>
- /// Legend of the option.
- /// </summary>
- public string Legend { get; set; }
- /// <summary>
- /// Abbreviation of the Legend. When the <see cref="Slider{T}.InnerSpacing"/> too small to fit <see cref="Legend"/>.
- /// </summary>
- public Rune LegendAbbr { get; set; }
- /// <summary>
- /// Custom data of the option.
- /// </summary>
- public T Data { get; set; }
- /// <summary>
- /// To Raise the <see cref="Set"/> event from the Slider.
- /// </summary>
- internal void OnSet ()
- {
- Set?.Invoke (this, new SliderOptionEventArgs (true));
- }
- /// <summary>
- /// To Raise the <see cref="UnSet"/> event from the Slider.
- /// </summary>
- internal void OnUnSet ()
- {
- UnSet?.Invoke (this, new SliderOptionEventArgs (false));
- }
- /// <summary>
- /// To Raise the <see cref="Changed"/> event from the Slider.
- /// </summary>
- internal void OnChanged (bool isSet)
- {
- Changed?.Invoke (this, new SliderOptionEventArgs (isSet));
- }
- /// <summary>
- /// Event Raised when this option is set.
- /// </summary>
- public event EventHandler<SliderOptionEventArgs> Set;
- /// <summary>
- /// Event Raised when this option is unset.
- /// </summary>
- public event EventHandler<SliderOptionEventArgs> UnSet;
- /// <summary>
- /// Event fired when the an option has changed.
- /// </summary>
- public event EventHandler<SliderOptionEventArgs> Changed;
- }
- /// <summary>
- /// <see cref="Slider{T}"/> Types
- /// </summary>
- public enum SliderType {
- /// <summary>
- /// <code>
- /// ├─┼─┼─┼─┼─█─┼─┼─┼─┼─┼─┼─┤
- /// </code>
- /// </summary>
- Single,
- /// <summary>
- /// <code>
- /// ├─┼─█─┼─┼─█─┼─┼─┼─┼─█─┼─┤
- /// </code>
- /// </summary>
- Multiple,
- /// <summary>
- /// <code>
- /// ├▒▒▒▒▒▒▒▒▒█─┼─┼─┼─┼─┼─┼─┤
- /// </code>
- /// </summary>
- LeftRange,
- /// <summary>
- /// <code>
- /// ├─┼─┼─┼─┼─█▒▒▒▒▒▒▒▒▒▒▒▒▒┤
- /// </code>
- /// </summary>
- RightRange,
- /// <summary>
- /// <code>
- /// ├─┼─┼─┼─┼─█▒▒▒▒▒▒▒█─┼─┼─┤
- /// </code>
- /// </summary>
- Range
- }
- /// <summary>
- /// <see cref="Slider{T}"/> Legend Style
- /// </summary>
- public class SliderAttributes {
- /// <summary>
- /// Attribute for when the respective Option is NOT Set.
- /// </summary>
- public Attribute? NormalAttribute { get; set; }
- /// <summary>
- /// Attribute for when the respective Option is Set.
- /// </summary>
- public Attribute? SetAttribute { get; set; }
- /// <summary>
- /// Attribute for the Legends Container.
- /// </summary>
- public Attribute? EmptyAttribute { get; set; }
- }
- /// <summary>
- /// <see cref="Slider{T}"/> Style
- /// </summary>
- public class SliderStyle {
- /// <summary>
- /// Legend attributes
- /// </summary>
- public SliderAttributes LegendAttributes { get; set; }
- /// <summary>
- /// The glyph and the attribute used for empty spaces on the slider.
- /// </summary>
- public Cell EmptyChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for each option (tick) on the slider.
- /// </summary>
- public Cell OptionChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for options (ticks) that are set on the slider.
- /// </summary>
- public Cell SetChar { get; set; }
- /// <summary>
- /// The glyph and the attribute to indicate mouse dragging.
- /// </summary>
- public Cell DragChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for spaces between options (ticks) on the slider.
- /// </summary>
- public Cell SpaceChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for filling in ranges on the slider.
- /// </summary>
- public Cell RangeChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for the start of ranges on the slider.
- /// </summary>
- public Cell StartRangeChar { get; set; }
- /// <summary>
- /// The glyph and the attribute used for the end of ranges on the slider.
- /// </summary>
- public Cell EndRangeChar { get; set; }
- /// <summary>
- /// Constructs a new instance.
- /// </summary>
- public SliderStyle ()
- {
- LegendAttributes = new SliderAttributes { };
- }
- }
- /// <summary>
- /// All <see cref="Slider{T}"/> configuration are grouped in this class.
- /// </summary>
- internal class SliderConfiguration {
- internal bool _rangeAllowSingle;
- internal bool _allowEmpty;
- internal int _mouseClickXOptionThreshold;
- internal bool _autoSize;
- internal int _startSpacing;
- internal int _endSpacing;
- internal int _innerSpacing;
- internal bool _showSpacing;
- internal bool _showLegends;
- internal bool _showLegendsAbbr;
- internal SliderType _type = SliderType.Single;
- internal Orientation _sliderOrientation = Orientation.Horizontal;
- internal Orientation _legendsOrientation = Orientation.Horizontal;
- }
- /// <summary>
- /// <see cref="EventArgs"/> for <see cref="Slider{T}"/> events.
- /// </summary>
- public class SliderEventArgs<T> : EventArgs {
- /// <summary>
- /// Gets/sets whether the option is set or not.
- /// </summary>
- public Dictionary<int, SliderOption<T>> Options { get; set; }
- /// <summary>
- /// Gets or sets the index of the option that is focused.
- /// </summary>
- public int Focused { get; set; }
- /// <summary>
- /// If set to true, the focus operation will be canceled, if applicable.
- /// </summary>
- public bool Cancel { get; set; }
- /// <summary>
- /// Initializes a new instance of <see cref="SliderEventArgs{T}"/>
- /// </summary>
- /// <param name="options">The current options.</param>
- /// <param name="focused">Index of the option that is focused. -1 if no option has the focus.</param>
- public SliderEventArgs (Dictionary<int, SliderOption<T>> options, int focused = -1)
- {
- Options = options;
- Focused = focused;
- Cancel = false;
- }
- }
- /// <summary>
- /// <see cref="EventArgs"/> for <see cref="Orientation"/> events.
- /// </summary>
- public class OrientationEventArgs : EventArgs {
- /// <summary>
- /// The new orientation.
- /// </summary>
- public Orientation Orientation { get; set; }
- /// <summary>
- /// If set to true, the orientation change operation will be canceled, if applicable.
- /// </summary>
- public bool Cancel { get; set; }
- /// <summary>
- /// Constructs a new instance.
- /// </summary>
- /// <param name="orientation">the new orientation</param>
- public OrientationEventArgs (Orientation orientation)
- {
- Orientation = orientation;
- Cancel = false;
- }
- }
- /// <summary>
- /// Slider control.
- /// </summary>
- public class Slider : Slider<object> {
- /// <summary>
- /// Initializes a new instance of the <see cref="Slider"/> class.
- /// </summary>
- public Slider () : base () { }
- /// <summary>
- /// Initializes a new instance of the <see cref="Slider"/> class.
- /// </summary>
- /// <param name="options">Initial slider options.</param>
- /// <param name="orientation">Initial slider options.</param>
- public Slider (List<object> options, Orientation orientation = Orientation.Horizontal) : base (options, orientation) { }
- }
- /// <summary>
- /// Provides a slider control letting the user navigate from a set of typed options in a linear manner using the keyboard or mouse.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class Slider<T> : View {
- SliderConfiguration _config = new SliderConfiguration ();
- SliderStyle _style = new SliderStyle ();
- // Options
- List<SliderOption<T>> _options;
- // List of the current set options.
- List<int> _setOptions = new List<int> ();
- /// <summary>
- /// The focused option (has the cursor).
- /// </summary>
- public int FocusedOption { get; set; }
- #region Events
- /// <summary>
- /// Event raised when the slider option/s changed.
- /// The dictionary contains: key = option index, value = T
- /// </summary>
- public event EventHandler<SliderEventArgs<T>> OptionsChanged;
- /// <summary>
- /// Overridable method called when the slider options have changed. Raises the <see cref="OptionsChanged"/> event.
- /// </summary>
- public virtual void OnOptionsChanged ()
- {
- OptionsChanged?.Invoke (this, new SliderEventArgs<T> (GetSetOptionDictionary ()));
- SetNeedsDisplay ();
- }
- /// <summary>
- /// Event raised When the option is hovered with the keys or the mouse.
- /// </summary>
- public event EventHandler<SliderEventArgs<T>> OptionFocused;
- int _lastFocusedOption; // for Range type; the most recently focused option. Used to determine shrink direction
- /// <summary>
- /// Overridable function that fires the <see cref="OptionFocused"/> event.
- /// </summary>
- /// <param name="args"></param>
- /// <returns><see langword="true"/> if the focus change was cancelled.</returns>
- /// <param name="newFocusedOption"></param>
- public virtual bool OnOptionFocused (int newFocusedOption, SliderEventArgs<T> args)
- {
- if (newFocusedOption > _options.Count - 1 || newFocusedOption < 0) {
- return true;
- }
- OptionFocused?.Invoke (this, args);
- if (!args.Cancel) {
- _lastFocusedOption = FocusedOption;
- FocusedOption = newFocusedOption;
- PositionCursor ();
- }
- return args.Cancel;
- }
- #endregion
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="Slider"/> class.
- /// </summary>
- public Slider () : this (new List<T> ())
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Slider"/> class.
- /// </summary>
- /// <param name="options">Initial slider options.</param>
- /// <param name="orientation">Initial slider orientation.</param>
- public Slider (List<T> options, Orientation orientation = Orientation.Horizontal)
- {
- if (options == null) {
- SetInitialProperties (null, orientation);
- } else {
- SetInitialProperties (options.Select (e => {
- var legend = e.ToString ();
- return new SliderOption<T> {
- Data = e,
- Legend = legend,
- LegendAbbr = (Rune)(legend?.Length > 0 ? legend [0] : ' '),
- };
- }).ToList (), orientation);
- }
- }
- #endregion
- #region Initialize
- void SetInitialProperties (List<SliderOption<T>> options, Orientation orientation = Orientation.Horizontal)
- {
- CanFocus = true;
- this._options = options ?? new List<SliderOption<T>> ();
- _config._sliderOrientation = orientation;
- _config._showLegends = true;
- SetDefaultStyle ();
- SetCommands ();
- // When we lose focus of the View(Slider), if we are range selecting we stop it.
- Leave += (object s, FocusEventArgs e) => {
- //if (_settingRange == true) {
- // _settingRange = false;
- //}
- Driver.SetCursorVisibility (CursorVisibility.Invisible);
- };
- Enter += (object s, FocusEventArgs e) => {
- };
- LayoutComplete += (s, e) => {
- CalcSpacingConfig ();
- AdjustBestHeight ();
- AdjustBestWidth ();
- };
- }
- #endregion
- #region Properties
- /// <summary>
- /// Allow no selection.
- /// </summary>
- public bool AllowEmpty {
- get => _config._allowEmpty;
- set {
- _config._allowEmpty = value;
- if (!value && _options.Count > 0 && _setOptions.Count == 0) {
- SetOption (0);
- }
- }
- }
- /// <summary>
- /// If <see langword="true"/> the slider will be sized to fit the available space (the Bounds of the the SuperView).
- /// </summary>
- /// <remarks>
- /// For testing, if there is no SuperView, the slider will be sized based on what <see cref="InnerSpacing"/> is
- /// set to.
- /// </remarks>
- public override bool AutoSize {
- get => _config._autoSize;
- set => _config._autoSize = value;
- }
- /// <summary>
- /// Gets or sets the number of rows/columns between <see cref="Options"/>
- /// </summary>
- public int InnerSpacing {
- get => _config._innerSpacing;
- set {
- _config._innerSpacing = value;
- CalcSpacingConfig ();
- Adjust ();
- SetNeedsDisplay ();
- SuperView?.SetNeedsLayout ();
- }
- }
- /// <summary>
- /// Slider Type. <see cref="SliderType"></see>
- /// </summary>
- public SliderType Type {
- get => _config._type;
- set {
- _config._type = value;
- // Todo: Custom logic to preserve options.
- _setOptions.Clear ();
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Slider Orientation. <see cref="Gui.Orientation"></see>
- /// </summary>
- public Orientation Orientation {
- get => _config._sliderOrientation;
- set => OnOrientationChanged (value);
- }
- /// <summary>
- /// Fired when the slider orientation has changed. Can be cancelled by setting <see cref="OrientationEventArgs.Cancel"/> to true.
- /// </summary>
- public event EventHandler<OrientationEventArgs> OrientationChanged;
- /// <summary>
- /// Called when the slider orientation has changed. Invokes the <see cref="OrientationChanged"/> event.
- /// </summary>
- /// <param name="newOrientation"></param>
- /// <returns>True of the event was cancelled.</returns>
- public virtual bool OnOrientationChanged (Orientation newOrientation)
- {
- var args = new OrientationEventArgs (newOrientation);
- OrientationChanged?.Invoke (this, args);
- if (!args.Cancel) {
- _config._sliderOrientation = newOrientation;
- SetKeyBindings ();
- if (IsInitialized) {
- CalcSpacingConfig ();
- Adjust ();
- SetNeedsDisplay ();
- SuperView?.SetNeedsLayout ();
- }
- }
- return args.Cancel;
- }
- /// <summary>
- /// Legends Orientation. <see cref="Gui.Orientation"></see>
- /// </summary>
- public Orientation LegendsOrientation {
- get => _config._legendsOrientation;
- set {
- _config._legendsOrientation = value;
- CalcSpacingConfig ();
- Adjust ();
- SetNeedsDisplay ();
- SuperView?.SetNeedsLayout ();
- }
- }
- /// <summary>
- /// Slider styles. <see cref="SliderStyle"></see>
- /// </summary>
- public SliderStyle Style {
- get {
- // Note(jmperricone): Maybe SliderStyle should be a struct so we return a copy ???
- // Or SetStyle() and ( GetStyle() || Style getter copy )
- return _style;
- }
- set {
- // Note(jmperricone): If the user change a style, he/she must call SetNeedsDisplay(). OK ???
- _style = value;
- }
- }
- /// <summary>
- /// Set the slider options.
- /// </summary>
- public List<SliderOption<T>> Options {
- get {
- // Note(jmperricone): Maybe SliderOption should be a struct so we return a copy ???
- // Events will be preserved ? Need a test.
- // Or SetOptions() and ( GetOptions() || Options getter copy )
- return _options;
- }
- set {
- _options = value;
- if (_options == null || _options.Count == 0) {
- return;
- }
- CalcSpacingConfig ();
- Adjust ();
- SetNeedsDisplay ();
- SuperView?.SetNeedsLayout ();
- }
- }
- /// <summary>
- /// Allow range start and end be in the same option, as a single option.
- /// </summary>
- public bool RangeAllowSingle {
- get => _config._rangeAllowSingle;
- set {
- _config._rangeAllowSingle = value;
- }
- }
- /// <summary>
- /// Show/Hide spacing before and after the first and last option.
- /// </summary>
- public bool ShowSpacing {
- get => _config._showSpacing;
- set {
- _config._showSpacing = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Show/Hide the options legends.
- /// </summary>
- public bool ShowLegends {
- get => _config._showLegends;
- set {
- _config._showLegends = value;
- Adjust ();
- }
- }
- /// <summary>
- /// Causes the specified option to be set and be focused.
- /// </summary>
- public bool SetOption (int optionIndex)
- {
- // TODO: Handle range type.
- // Note: Maybe return false only when optionIndex doesn't exist, otherwise true.
- if (!_setOptions.Contains (optionIndex) && optionIndex >= 0 && optionIndex < _options.Count) {
- FocusedOption = optionIndex;
- SetFocusedOption ();
- return true;
- }
- return false;
- }
- /// <summary>
- /// Causes the specified option to be un-set and be focused.
- /// </summary>
- public bool UnSetOption (int optionIndex)
- {
- // TODO: Handle range type.
- if ((!AllowEmpty && _setOptions.Count > 2) && _setOptions.Contains (optionIndex)) {
- FocusedOption = optionIndex;
- SetFocusedOption ();
- return true;
- }
- return false;
- }
- /// <summary>
- /// Get the indexes of the set options.
- /// </summary>
- public List<int> GetSetOptions ()
- {
- // Copy
- return _setOptions.OrderBy (e => e).ToList ();
- }
- #endregion
- #region Helpers
- void MoveAndAdd (int x, int y, Rune rune)
- {
- Move (x, y);
- Driver?.AddRune (rune);
- }
- void MoveAndAdd (int x, int y, string str)
- {
- Move (x, y);
- Driver?.AddStr (str);
- }
- // TODO: Make configurable via ConfigurationManager
- void SetDefaultStyle ()
- {
- switch (_config._sliderOrientation) {
- case Orientation.Horizontal:
- _style.SpaceChar = new Cell () { Runes = { CM.Glyphs.HLine } }; // '─'
- _style.OptionChar = new Cell () { Runes = { CM.Glyphs.BlackCircle } }; // '┼●🗹□⏹'
- break;
- case Orientation.Vertical:
- _style.SpaceChar = new Cell () { Runes = { CM.Glyphs.VLine } };
- _style.OptionChar = new Cell () { Runes = { CM.Glyphs.BlackCircle } };
- break;
- }
- // TODO(jmperricone) Wide Vertical ???
- /*
- │
- │
- ┼─ 40
- │
- │
- ███ 30
- ▒▒▒
- ▒▒▒
- ▒▒▒ 20
- ▒▒▒
- ▒▒▒
- ███ 10
- │
- │
- ─●─ 0
- */
- _config._legendsOrientation = _config._sliderOrientation;
- _style.EmptyChar = new Cell () { Runes = { new Rune (' ') } };
- _style.SetChar = new Cell () { Runes = { CM.Glyphs.ContinuousMeterSegment } }; // ■
- _style.RangeChar = new Cell () { Runes = { CM.Glyphs.Stipple } }; // ░ ▒ ▓ // Medium shade not blinking on curses.
- _style.StartRangeChar = new Cell () { Runes = { CM.Glyphs.ContinuousMeterSegment } };
- _style.EndRangeChar = new Cell () { Runes = { CM.Glyphs.ContinuousMeterSegment } };
- _style.DragChar = new Cell () { Runes = { CM.Glyphs.Diamond } };
- // TODO: Support left & right (top/bottom)
- // First = '├',
- // Last = '┤',
- }
- /// <summary>
- /// Calculates the spacing configuration (start, inner, end) as well as turning on/off legend abbreviation
- /// if needed. Behaves differently based on <see cref="AutoSize"/> and <see cref="View.IsInitialized"/>.
- /// </summary>
- internal void CalcSpacingConfig ()
- {
- int size = 0;
- if (_options.Count == 0) {
- return;
- }
- if (_config._autoSize || !IsInitialized) {
- if (IsInitialized && SuperView != null) {
- // Calculate the size of the slider based on the size of the SuperView's Bounds.
- // TODO:
- } else {
- // Use the config values
- size = CalcLength ();
- return;
- }
- } else {
- // Fit Slider to the actual width and height.
- if (_config._sliderOrientation == Orientation.Horizontal) {
- size = Bounds.Width;
- } else {
- size = Bounds.Height;
- }
- }
- int max_legend;
- if (_config._sliderOrientation == _config._legendsOrientation) {
- max_legend = _options.Max (e => e.Legend.ToString ().Length);
- } else {
- max_legend = 1;
- }
- var min = (size - max_legend) / (_options.Count - 1);
- string first;
- string last;
- if (max_legend >= min) {
- if (_config._sliderOrientation == _config._legendsOrientation) {
- _config._showLegendsAbbr = true;
- }
- first = "x";
- last = "x";
- } else {
- _config._showLegendsAbbr = false;
- first = _options.First ().Legend;
- last = _options.Last ().Legend;
- }
- // --o--
- // Hello
- // Left = He
- // Right = lo
- var first_left = (first.Length - 1) / 2; // Chars count of the first option to the left.
- var last_right = (last.Length) / 2; // Chars count of the last option to the right.
- if (_config._sliderOrientation != _config._legendsOrientation) {
- first_left = 0;
- last_right = 0;
- }
- var width = size - first_left - last_right - 1;
- _config._startSpacing = first_left;
- _config._innerSpacing = Math.Max (0, (int)Math.Floor ((double)width / (_options.Count - 1)) - 1);
- _config._endSpacing = last_right;
- }
- /// <summary>
- /// Adjust the height of the Slider to the best value.
- ///</summary>
- public void AdjustBestHeight ()
- {
- // Hack??? Otherwise we can't go back to Dim.Absolute.
- LayoutStyle = LayoutStyle.Absolute;
- if (_config._sliderOrientation == Orientation.Horizontal) {
- Bounds = new Rect (Bounds.Location, new Size (Bounds.Width, CalcThickness ()));
- } else {
- Bounds = new Rect (Bounds.Location, new Size (Bounds.Width, CalcLength ()));
- }
- LayoutStyle = LayoutStyle.Computed;
- }
- /// <summary>
- /// Adjust the height of the Slider to the best value. (Only works if height is DimAbsolute).
- ///</summary>
- public void AdjustBestWidth ()
- {
- LayoutStyle = LayoutStyle.Absolute;
- if (_config._sliderOrientation == Orientation.Horizontal) {
- Bounds = new Rect (Bounds.Location, new Size (CalcLength (), Bounds.Height));
- } else {
- Bounds = new Rect (Bounds.Location, new Size (CalcThickness (), Bounds.Height));
- }
- LayoutStyle = LayoutStyle.Computed;
- }
- void Adjust ()
- {
- if (Width is Dim.DimAbsolute) {
- AdjustBestWidth ();
- }
- if (Height is Dim.DimAbsolute) {
- AdjustBestHeight ();
- }
- }
- internal int CalcLength ()
- {
- if (_options.Count == 0) {
- return 0;
- }
- var length = 0;
- length += _config._startSpacing + _config._endSpacing;
- length += _options.Count;
- length += (_options.Count - 1) * _config._innerSpacing;
- return length;
- }
- int CalcThickness ()
- {
- var thickness = 1; // Always show the slider.
- if (_config._showLegends) {
- if (_config._legendsOrientation != _config._sliderOrientation && _options.Count > 0) {
- thickness += _options.Max (s => s.Legend.Length);
- } else {
- thickness += 1;
- }
- }
- return thickness;
- }
- internal bool TryGetPositionByOption (int option, out (int x, int y) position)
- {
- position = (-1, -1);
- if (option < 0 || option >= _options.Count ()) {
- return false;
- }
- var offset = 0;
- offset += _config._startSpacing;
- offset += option * (_config._innerSpacing + 1);
- if (_config._sliderOrientation == Orientation.Vertical) {
- position = (0, offset);
- } else {
- position = (offset, 0);
- }
- return true;
- }
- /// <summary>
- /// Tries to get the option index by the position.
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="threshold"></param>
- /// <param name="option_idx"></param>
- /// <returns></returns>
- internal bool TryGetOptionByPosition (int x, int y, int threshold, out int option_idx)
- {
- // Fix(jmperricone): Not working.
- option_idx = -1;
- if (Orientation == Orientation.Horizontal) {
- if (y != 0) {
- return false;
- }
- for (int xx = (x - threshold); xx < (x + threshold + 1); xx++) {
- var cx = xx;
- cx -= _config._startSpacing;
- var option = cx / (_config._innerSpacing + 1);
- var valid = cx % (_config._innerSpacing + 1) == 0;
- if (!valid || option < 0 || option > _options.Count - 1) {
- continue;
- }
- option_idx = option;
- return true;
- }
- } else {
- if (x != 0) {
- return false;
- }
- for (int yy = (y - threshold); yy < (y + threshold + 1); yy++) {
- var cy = yy;
- cy -= _config._startSpacing;
- var option = cy / (_config._innerSpacing + 1);
- var valid = cy % (_config._innerSpacing + 1) == 0;
- if (!valid || option < 0 || option > _options.Count - 1) {
- continue;
- }
- option_idx = option;
- return true;
- }
- }
- return false;
- }
- #endregion
- #region Cursor and Drawing
- /// <inheritdoc/>
- public override void PositionCursor ()
- {
- //base.PositionCursor ();
- if (HasFocus) {
- Driver?.SetCursorVisibility (CursorVisibility.Default);
- } else {
- Driver?.SetCursorVisibility (CursorVisibility.Invisible);
- }
- if (TryGetPositionByOption (FocusedOption, out (int x, int y) position)) {
- if (IsInitialized && Bounds.Contains (position.x, position.y)) {
- Move (position.x, position.y);
- }
- }
- }
- /// <inheritdoc/>
- public override void OnDrawContent (Rect contentArea)
- {
- // TODO: make this more surgical to reduce repaint
- var normalScheme = ColorScheme?.Normal ?? Application.Current.ColorScheme.Normal;
- if (this._options == null && this._options.Count > 0) {
- return;
- }
- // Debug
- #if (DEBUG)
- Driver.SetAttribute (new Attribute (Color.White, Color.Red));
- for (var y = 0; y < Bounds.Height; y++) {
- for (var x = 0; x < Bounds.Width; x++) {
- // MoveAndAdd (x, y, '·');
- }
- }
- #endif
- // Draw Slider
- DrawSlider ();
- // Draw Legends.
- if (_config._showLegends) {
- DrawLegends ();
- }
- if (_dragPosition.HasValue && _moveRenderPosition.HasValue) {
- AddRune (_moveRenderPosition.Value.X, _moveRenderPosition.Value.Y, _style.DragChar.Runes [0]);
- }
- }
- string AlignText (string text, int width, TextAlignment textAlignment)
- {
- if (text == null) {
- return "";
- }
- if (text.Length > width) {
- text = text [0..width];
- }
- var w = width - text.Length;
- var s1 = new string (' ', w / 2);
- var s2 = new string (' ', w % 2);
- // Note: The formatter doesn't handle all of this ???
- switch (textAlignment) {
- case TextAlignment.Justified:
- return TextFormatter.Justify (text, width);
- case TextAlignment.Left:
- return text + s1 + s1 + s2;
- case TextAlignment.Centered:
- if (text.Length % 2 != 0) {
- return s1 + text + s1 + s2;
- } else {
- return s1 + s2 + text + s1;
- }
- case TextAlignment.Right:
- return s1 + s1 + s2 + text;
- default:
- return text;
- }
- }
- void DrawSlider ()
- {
- // TODO: be more surgical on clear
- Clear ();
- // Attributes
- var normalScheme = ColorScheme?.Normal ?? Application.Current.ColorScheme.Normal;
- //var normalScheme = style.LegendStyle.NormalAttribute ?? ColorScheme.Disabled;
- var setScheme = _style.SetChar.Attribute ?? ColorScheme.HotNormal;// ColorScheme?.Focus ?? Application.Current.ColorScheme.Focus;
- var isVertical = _config._sliderOrientation == Orientation.Vertical;
- var isLegendsVertical = _config._legendsOrientation == Orientation.Vertical;
- var isReverse = _config._sliderOrientation != _config._legendsOrientation;
- var x = 0;
- var y = 0;
- var isSet = _setOptions.Count > 0;
- // Left Spacing
- if (_config._showSpacing && _config._startSpacing > 0) {
- Driver.SetAttribute (isSet && _config._type == SliderType.LeftRange ? _style.RangeChar.Attribute ?? normalScheme : _style.SpaceChar.Attribute ?? normalScheme);
- var rune = isSet && _config._type == SliderType.LeftRange ? _style.RangeChar.Runes [0] : _style.SpaceChar.Runes [0];
- for (var i = 0; i < this._config._startSpacing; i++) {
- MoveAndAdd (x, y, rune);
- if (isVertical) y++; else x++;
- }
- } else {
- Driver.SetAttribute (_style.EmptyChar.Attribute ?? normalScheme);
- // for (int i = 0; i < this.config.StartSpacing + ((this.config.StartSpacing + this.config.EndSpacing) % 2 == 0 ? 1 : 2); i++) {
- for (var i = 0; i < this._config._startSpacing; i++) {
- MoveAndAdd (x, y, _style.EmptyChar.Runes [0]);
- if (isVertical) y++; else x++;
- }
- }
- // Slider
- if (_options.Count > 0) {
- for (var i = 0; i < _options.Count; i++) {
- var drawRange = false;
- if (isSet) {
- switch (_config._type) {
- case SliderType.LeftRange when i <= _setOptions [0]:
- drawRange = i < _setOptions [0];
- break;
- case SliderType.RightRange when i >= _setOptions [0]:
- drawRange = i >= _setOptions [0];
- break;
- case SliderType.Range when _setOptions.Count == 1:
- drawRange = false;
- break;
- case SliderType.Range when _setOptions.Count == 2:
- if ((i >= _setOptions [0] && i <= _setOptions [1]) || (i >= _setOptions [1] && i <= _setOptions [0])) {
- drawRange = (i >= _setOptions [0] && i < _setOptions [1]) || (i >= _setOptions [1] && i < _setOptions [0]);
- }
- break;
- default:
- // Is Not a Range.
- break;
- }
- }
- // Draw Option
- Driver.SetAttribute (isSet && _setOptions.Contains (i) ? _style.SetChar.Attribute ?? setScheme : drawRange ? _style.RangeChar.Attribute ?? setScheme : _style.OptionChar.Attribute ?? normalScheme);
- // Note(jmperricone): Maybe only for curses, windows inverts actual colors, while curses inverts bg with fg.
- //if (Application.Driver is CursesDriver) {
- // if (FocusedOption == i && HasFocus) {
- // Driver.SetAttribute (ColorScheme.Focus);
- // }
- //}
- Rune rune = drawRange ? _style.RangeChar.Runes [0] : _style.OptionChar.Runes [0];
- if (isSet) {
- if (_setOptions [0] == i) {
- rune = _style.StartRangeChar.Runes [0];
- } else if (_setOptions.Count > 1 && _setOptions [1] == i) {
- rune = _style.EndRangeChar.Runes [0];
- } else if (_setOptions.Contains (i)) {
- rune = _style.SetChar.Runes [0];
- }
- }
- MoveAndAdd (x, y, rune);
- if (isVertical) y++; else x++;
- // Draw Spacing
- if (_config._showSpacing || i < _options.Count - 1) { // Skip if is the Last Spacing.
- Driver.SetAttribute (drawRange && isSet ? _style.RangeChar.Attribute ?? setScheme : _style.SpaceChar.Attribute ?? normalScheme);
- for (var s = 0; s < _config._innerSpacing; s++) {
- MoveAndAdd (x, y, drawRange && isSet ? _style.RangeChar.Runes [0] : _style.SpaceChar.Runes [0]);
- if (isVertical) y++; else x++;
- }
- }
- }
- }
- var remaining = isVertical ? Bounds.Height - y : Bounds.Width - x;
- // Right Spacing
- if (_config._showSpacing) {
- Driver.SetAttribute (isSet && _config._type == SliderType.RightRange ? _style.RangeChar.Attribute ?? normalScheme : _style.SpaceChar.Attribute ?? normalScheme);
- var rune = isSet && _config._type == SliderType.RightRange ? _style.RangeChar.Runes [0] : _style.SpaceChar.Runes [0];
- for (var i = 0; i < remaining; i++) {
- MoveAndAdd (x, y, rune);
- if (isVertical) y++; else x++;
- }
- } else {
- Driver.SetAttribute (_style.EmptyChar.Attribute ?? normalScheme);
- for (var i = 0; i < remaining; i++) {
- MoveAndAdd (x, y, _style.EmptyChar.Runes [0]);
- if (isVertical) y++; else x++;
- }
- }
- }
- void DrawLegends ()
- {
- // Attributes
- var normalScheme = _style.LegendAttributes.NormalAttribute ?? ColorScheme?.Normal ?? ColorScheme.Disabled;
- var setScheme = _style.LegendAttributes.SetAttribute ?? ColorScheme?.HotNormal ?? ColorScheme.Normal;
- var spaceScheme = normalScheme;// style.LegendStyle.EmptyAttribute ?? normalScheme;
- var isTextVertical = _config._legendsOrientation == Orientation.Vertical;
- var isSet = _setOptions.Count > 0;
- var x = 0;
- var y = 0;
- Move (x, y);
- if (_config._sliderOrientation == Orientation.Horizontal && _config._legendsOrientation == Orientation.Vertical) {
- x += _config._startSpacing;
- }
- if (_config._sliderOrientation == Orientation.Vertical && _config._legendsOrientation == Orientation.Horizontal) {
- y += _config._startSpacing;
- }
- if (_config._sliderOrientation == Orientation.Horizontal) {
- y += 1;
- } else { // Vertical
- x += 1;
- }
- for (int i = 0; i < _options.Count; i++) {
- bool isOptionSet = false;
- // Check if the Option is Set.
- switch (_config._type) {
- case SliderType.Single:
- case SliderType.Multiple:
- if (isSet && _setOptions.Contains (i))
- isOptionSet = true;
- break;
- case SliderType.LeftRange:
- if (isSet && i <= _setOptions [0])
- isOptionSet = true;
- break;
- case SliderType.RightRange:
- if (isSet && i >= _setOptions [0])
- isOptionSet = true;
- break;
- case SliderType.Range when _setOptions.Count == 1:
- if (isSet && i == _setOptions [0])
- isOptionSet = true;
- break;
- case SliderType.Range:
- if (isSet && ((i >= _setOptions [0] && i <= _setOptions [1]) || (i >= _setOptions [1] && i <= _setOptions [0]))) {
- isOptionSet = true;
- }
- break;
- }
- // Text || Abbreviation
- string text = string.Empty;
- if (_config._showLegendsAbbr) {
- text = _options [i].LegendAbbr.ToString () ?? new Rune (_options [i].Legend.First ()).ToString ();
- } else {
- text = _options [i].Legend;
- }
- switch (_config._sliderOrientation) {
- case Orientation.Horizontal:
- switch (_config._legendsOrientation) {
- case Orientation.Horizontal:
- text = AlignText (text, _config._innerSpacing + 1, TextAlignment.Centered);
- break;
- case Orientation.Vertical:
- y = 1;
- break;
- }
- break;
- case Orientation.Vertical:
- switch (_config._legendsOrientation) {
- case Orientation.Horizontal:
- x = 1;
- break;
- case Orientation.Vertical:
- text = AlignText (text, _config._innerSpacing + 1, TextAlignment.Centered);
- break;
- }
- break;
- }
- // Text
- var legend_left_spaces_count = text.TakeWhile (e => e == ' ').Count ();
- var legend_right_spaces_count = text.Reverse ().TakeWhile (e => e == ' ').Count ();
- text = text.Trim ();
- // TODO(jmperricone): Improve the Orientation check.
- // Calculate Start Spacing
- if (_config._sliderOrientation == _config._legendsOrientation) {
- if (i == 0) {
- // The spacing for the slider use the StartSpacing but...
- // The spacing for the legends is the StartSpacing MINUS the total chars to the left of the first options.
- // ●────●────●
- // Hello Bye World
- //
- // chars_left is 2 for Hello => (5 - 1) / 2
- //
- // then the spacing is 2 for the slider but 0 for the legends.
- var chars_left = (text.Length - 1) / 2;
- legend_left_spaces_count = _config._startSpacing - chars_left;
- }
- // Option Left Spacing
- if (isTextVertical) y += legend_left_spaces_count;
- else x += legend_left_spaces_count;
- //Move (x, y);
- }
- // Legend
- Driver.SetAttribute (isOptionSet ? setScheme : normalScheme);
- foreach (var c in text.EnumerateRunes ()) {
- MoveAndAdd (x, y, c);
- //Driver.AddRune (c);
- if (isTextVertical) y += 1;
- else x += 1;
- }
- // Calculate End Spacing
- if (i == _options.Count () - 1) {
- // See Start Spacing explanation.
- var chars_right = text.Length / 2;
- legend_right_spaces_count = _config._endSpacing - chars_right;
- }
- // Option Right Spacing of Option
- Driver.SetAttribute (spaceScheme);
- if (isTextVertical) y += legend_right_spaces_count;
- else x += legend_right_spaces_count;
- if (_config._sliderOrientation == Orientation.Horizontal && _config._legendsOrientation == Orientation.Vertical) {
- x += _config._innerSpacing + 1;
- } else if (_config._sliderOrientation == Orientation.Vertical && _config._legendsOrientation == Orientation.Horizontal) {
- y += _config._innerSpacing + 1;
- }
- }
- }
- #endregion
- #region Keys and Mouse
- // Mouse coordinates of current drag
- Point? _dragPosition;
- // Coordinates of where the "move cursor" is drawn (in OnDrawContent)
- Point? _moveRenderPosition;
- /// <inheritdoc/>
- public override bool MouseEvent (MouseEvent mouseEvent)
- {
- // Note(jmperricone): Maybe we click to focus the cursor, and on next click we set the option.
- // That will makes OptionFocused Event more relevant.
- // (tig: I don't think so. Maybe an option if someone really wants it, but for now that
- // adss to much friction to UI.
- // TODO(jmperricone): Make Range Type work with mouse.
- if (!(mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked) ||
- mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) ||
- mouseEvent.Flags.HasFlag (MouseFlags.ReportMousePosition) ||
- mouseEvent.Flags.HasFlag (MouseFlags.Button1Released))) {
- return false;
- }
- Point ClampMovePosition (Point position)
- {
- int Clamp (int value, int min, int max) => Math.Max (min, Math.Min (max, value));
- if (Orientation == Orientation.Horizontal) {
- var left = _config._startSpacing;
- var width = _options.Count + (_options.Count - 1) * _config._innerSpacing;
- var right = (left + width - 1);
- var clampedX = Clamp (position.X, left, right);
- position = new Point (clampedX, 0);
- } else {
- var top = _config._startSpacing;
- var height = _options.Count + (_options.Count - 1) * _config._innerSpacing;
- var bottom = (top + height - 1);
- var clampedY = Clamp (position.Y, top, bottom);
- position = new Point (0, clampedY);
- }
- return position;
- }
- SetFocus ();
- if (!_dragPosition.HasValue && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))) {
- if (mouseEvent.Flags.HasFlag (MouseFlags.ReportMousePosition)) {
- _dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
- _moveRenderPosition = ClampMovePosition ((Point)_dragPosition);
- Application.GrabMouse (this);
- }
- SetNeedsDisplay ();
- return true;
- }
- if (_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.ReportMousePosition) && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)) {
- // Continue Drag
- _dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
- _moveRenderPosition = ClampMovePosition ((Point)_dragPosition);
- var success = false;
- var option = 0;
- // how far has user dragged from original location?
- if (Orientation == Orientation.Horizontal) {
- success = TryGetOptionByPosition (mouseEvent.X, 0, Math.Max (0, _config._innerSpacing / 2), out option);
- } else {
- success = TryGetOptionByPosition (0, mouseEvent.Y, Math.Max (0, _config._innerSpacing / 2), out option);
- }
- if (!_config._allowEmpty && success) {
- if (!OnOptionFocused (option, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- SetFocusedOption ();
- }
- }
- SetNeedsDisplay ();
- return true;
- }
- if ((_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)) || mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) {
- // End Drag
- Application.UngrabMouse ();
- _dragPosition = null;
- _moveRenderPosition = null;
- // TODO: Add func to calc distance between options to use as the MouseClickXOptionThreshold
- var success = false;
- var option = 0;
- if (Orientation == Orientation.Horizontal) {
- success = TryGetOptionByPosition (mouseEvent.X, 0, Math.Max (0, _config._innerSpacing / 2), out option);
- } else {
- success = TryGetOptionByPosition (0, mouseEvent.Y, Math.Max (0, _config._innerSpacing / 2), out option);
- }
- if (success) {
- if (!OnOptionFocused (option, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- SetFocusedOption ();
- }
- }
- SetNeedsDisplay ();
- return true;
- }
- return false;
- }
- void SetCommands ()
- {
- AddCommand (Command.Right, () => MovePlus ());
- AddCommand (Command.LineDown, () => MovePlus ());
- AddCommand (Command.Left, () => MoveMinus ());
- AddCommand (Command.LineUp, () => MoveMinus ());
- AddCommand (Command.LeftHome, () => MoveStart ());
- AddCommand (Command.RightEnd, () => MoveEnd ());
- AddCommand (Command.RightExtend, () => ExtendPlus ());
- AddCommand (Command.LeftExtend, () => ExtendMinus ());
- AddCommand (Command.Accept, () => Set ());
- SetKeyBindings ();
- }
- // This is called during initialization and anytime orientation changes
- void SetKeyBindings ()
- {
- if (_config._sliderOrientation == Orientation.Horizontal) {
- AddKeyBinding (Key.CursorRight, Command.Right);
- ClearKeyBinding (Key.CursorDown);
- AddKeyBinding (Key.CursorLeft, Command.Left);
- ClearKeyBinding (Key.CursorUp);
- AddKeyBinding (Key.CursorRight | Key.CtrlMask, Command.RightExtend);
- ClearKeyBinding (Key.CursorDown | Key.CtrlMask);
- AddKeyBinding (Key.CursorLeft | Key.CtrlMask, Command.LeftExtend);
- ClearKeyBinding (Key.CursorUp | Key.CtrlMask);
- } else {
- ClearKeyBinding (Key.CursorRight);
- AddKeyBinding (Key.CursorDown, Command.LineDown);
- ClearKeyBinding (Key.CursorLeft);
- AddKeyBinding (Key.CursorUp, Command.LineUp);
- ClearKeyBinding (Key.CursorRight | Key.CtrlMask);
- AddKeyBinding (Key.CursorDown | Key.CtrlMask, Command.RightExtend);
- ClearKeyBinding (Key.CursorLeft | Key.CtrlMask);
- AddKeyBinding (Key.CursorUp | Key.CtrlMask, Command.LeftExtend);
- }
- AddKeyBinding (Key.Home, Command.LeftHome);
- AddKeyBinding (Key.End, Command.RightEnd);
- AddKeyBinding (Key.Enter, Command.Accept);
- AddKeyBinding (Key.Space, Command.Accept);
- }
- /// <inheritdoc/>
- public override bool ProcessKey (KeyEvent keyEvent)
- {
- if (!CanFocus || !HasFocus) {
- return base.ProcessKey (keyEvent);
- }
- var result = InvokeKeybindings (keyEvent);
- if (result != null) {
- return (bool)result;
- }
- return base.ProcessKey (keyEvent);
- }
- Dictionary<int, SliderOption<T>> GetSetOptionDictionary () => _setOptions.ToDictionary (e => e, e => _options [e]);
- void SetFocusedOption ()
- {
- switch (_config._type) {
- case SliderType.Single:
- case SliderType.LeftRange:
- case SliderType.RightRange:
- if (_setOptions.Count == 1) {
- var prev = _setOptions [0];
- if (!_config._allowEmpty && prev == FocusedOption) {
- break;
- }
- _setOptions.Clear ();
- _options [FocusedOption].OnUnSet ();
- if (FocusedOption != prev) {
- _setOptions.Add (FocusedOption);
- _options [FocusedOption].OnSet ();
- }
- } else {
- _setOptions.Add (FocusedOption);
- _options [FocusedOption].OnSet ();
- }
- // Raise slider changed event.
- OnOptionsChanged ();
- break;
- case SliderType.Multiple:
- if (_setOptions.Contains (FocusedOption)) {
- if (!_config._allowEmpty && _setOptions.Count () == 1) {
- break;
- }
- _setOptions.Remove (FocusedOption);
- _options [FocusedOption].OnUnSet ();
- } else {
- _setOptions.Add (FocusedOption);
- _options [FocusedOption].OnSet ();
- }
- OnOptionsChanged ();
- break;
- case SliderType.Range:
- if (_config._rangeAllowSingle) {
- if (_setOptions.Count == 1) {
- var prev = _setOptions [0];
- if (!_config._allowEmpty && prev == FocusedOption) {
- break;
- }
- if (FocusedOption == prev) {
- // un-set
- _setOptions.Clear ();
- _options [FocusedOption].OnUnSet ();
- } else {
- _setOptions [0] = FocusedOption;
- _setOptions.Add (prev);
- _setOptions.Sort ();
- _options [FocusedOption].OnSet ();
- }
- } else if (_setOptions.Count == 0) {
- _setOptions.Add (FocusedOption);
- _options [FocusedOption].OnSet ();
- } else {
- // Extend/Shrink
- if (FocusedOption < _setOptions [0]) {
- // extend left
- _options [_setOptions [0]].OnUnSet ();
- _setOptions [0] = FocusedOption;
- } else if (FocusedOption > _setOptions [1]) {
- // extend right
- _options [_setOptions [1]].OnUnSet ();
- _setOptions [1] = FocusedOption;
- } else if (FocusedOption >= _setOptions [0] && FocusedOption <= _setOptions [1]) {
- if (FocusedOption < _lastFocusedOption) {
- // shrink to the left
- _options [_setOptions [1]].OnUnSet ();
- _setOptions [1] = FocusedOption;
- } else if (FocusedOption > _lastFocusedOption) {
- // shrink to the right
- _options [_setOptions [0]].OnUnSet ();
- _setOptions [0] = FocusedOption;
- }
- if (_setOptions.Count > 1 && _setOptions [0] == _setOptions [1]) {
- _setOptions.Clear ();
- _setOptions.Add (FocusedOption);
- }
- }
- }
- } else {
- if (_setOptions.Count == 1) {
- var prev = _setOptions [0];
- if (!_config._allowEmpty && prev == FocusedOption) {
- break;
- }
- _setOptions [0] = FocusedOption;
- _setOptions.Add (prev);
- _setOptions.Sort ();
- _options [FocusedOption].OnSet ();
- } else if (_setOptions.Count == 0) {
- _setOptions.Add (FocusedOption);
- _options [FocusedOption].OnSet ();
- var next = FocusedOption < _options.Count - 1 ? FocusedOption + 1 : FocusedOption - 1;
- _setOptions.Add (next);
- _options [next].OnSet ();
- } else {
- // Extend/Shrink
- if (FocusedOption < _setOptions [0]) {
- // extend left
- _options [_setOptions [0]].OnUnSet ();
- _setOptions [0] = FocusedOption;
- } else if (FocusedOption > _setOptions [1]) {
- // extend right
- _options [_setOptions [1]].OnUnSet ();
- _setOptions [1] = FocusedOption;
- } else if (FocusedOption >= _setOptions [0] && FocusedOption <= _setOptions [1] && (_setOptions [1] - _setOptions [0] > 1)) {
- if (FocusedOption < _lastFocusedOption) {
- // shrink to the left
- _options [_setOptions [1]].OnUnSet ();
- _setOptions [1] = FocusedOption;
- } else if (FocusedOption > _lastFocusedOption) {
- // shrink to the right
- _options [_setOptions [0]].OnUnSet ();
- _setOptions [0] = FocusedOption;
- }
- }
- //if (_setOptions.Count > 1 && _setOptions [0] == _setOptions [1]) {
- // SetFocusedOption ();
- //}
- }
- }
- // Raise Slider Option Changed Event.
- OnOptionsChanged ();
- break;
- default:
- throw new ArgumentOutOfRangeException (_config._type.ToString ());
- }
- }
- internal bool ExtendPlus ()
- {
- var next = FocusedOption < _options.Count - 1 ? FocusedOption + 1 : FocusedOption;
- if (next != FocusedOption && !OnOptionFocused (next, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- SetFocusedOption ();
- }
- return true;
- //// TODO: Support RangeMultiple
- //if (_setOptions.Contains (FocusedOption)) {
- // var next = FocusedOption < _options.Count - 1 ? FocusedOption + 1 : FocusedOption;
- // if (!_setOptions.Contains (next)) {
- // if (_config._type == SliderType.Range) {
- // if (_setOptions.Count == 1) {
- // if (!OnOptionFocused (next, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- // _setOptions.Add (FocusedOption);
- // _setOptions.Sort (); // Range Type
- // OnOptionsChanged ();
- // }
- // } else if (_setOptions.Count == 2) {
- // if (!OnOptionFocused (next, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- // _setOptions [1] = FocusedOption;
- // _setOptions.Sort (); // Range Type
- // OnOptionsChanged ();
- // }
- // }
- // } else {
- // _setOptions.Remove (FocusedOption);
- // // Note(jmperricone): We are setting the option here, do we send the OptionFocused Event too ?
- // if (!OnOptionFocused (next, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- // _setOptions.Add (FocusedOption);
- // _setOptions.Sort (); // Range Type
- // OnOptionsChanged ();
- // }
- // }
- // } else {
- // if (_config._type == SliderType.Range) {
- // if (!OnOptionFocused (next, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- // _setOptions.Clear();
- // _setOptions.Add (FocusedOption);
- // OnOptionsChanged ();
- // }
- // } else if (/*_settingRange == true ||*/ !AllowEmpty) {
- // SetFocusedOption ();
- // }
- // }
- //}
- //return true;
- }
- internal bool ExtendMinus ()
- {
- var prev = FocusedOption > 0 ? FocusedOption - 1 : FocusedOption;
- if (prev != FocusedOption && !OnOptionFocused (prev, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- SetFocusedOption ();
- }
- return true;
- }
- internal bool Set ()
- {
- SetFocusedOption ();
- return true;
- }
- internal bool MovePlus ()
- {
- var cancelled = OnOptionFocused (FocusedOption + 1, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption));
- if (cancelled) {
- return false;
- }
- if (!AllowEmpty) {
- SetFocusedOption ();
- }
- return true;
- }
- internal bool MoveMinus ()
- {
- var cancelled = OnOptionFocused (FocusedOption - 1, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption));
- if (cancelled) {
- return false;
- }
- if (!AllowEmpty) {
- SetFocusedOption ();
- }
- return true;
- }
- internal bool MoveStart ()
- {
- if (OnOptionFocused (0, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- return false;
- }
- if (!AllowEmpty) {
- SetFocusedOption ();
- }
- return true;
- }
- internal bool MoveEnd ()
- {
- if (OnOptionFocused (_options.Count - 1, new SliderEventArgs<T> (GetSetOptionDictionary (), FocusedOption))) {
- return false;
- }
- if (!AllowEmpty) {
- SetFocusedOption ();
- }
- return true;
- }
- #endregion
- }
|