ColorScheme tweaks

This commit is contained in:
Tig
2025-04-03 06:09:46 -06:00
parent 5a26e60943
commit 01b526691a
3 changed files with 110 additions and 30 deletions

View File

@@ -1,4 +1,6 @@
#nullable enable
using System.ComponentModel;
namespace Terminal.Gui;
public partial class View
@@ -80,8 +82,17 @@ public partial class View
/// </returns>
public virtual Attribute GetNormalColor ()
{
ColorScheme? cs = ColorScheme ?? new ();
Attribute currAttribute = ColorScheme?.Normal ?? Attribute.Default;
Attribute newAttribute = new Attribute ();
CancelEventArgs<Attribute> args = new CancelEventArgs<Attribute> (in currAttribute, ref newAttribute);
GettingNormalColor?.Invoke (this, args);
if (args.Cancel)
{
return args.NewValue;
}
ColorScheme? cs = ColorScheme ?? new ();
Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
@@ -92,6 +103,8 @@ public partial class View
return Enabled ? GetColor (cs.Normal) : disabled;
}
public event EventHandler<CancelEventArgs<Attribute>>? GettingNormalColor;
private Attribute GetColor (Attribute inputAttribute)
{
Attribute attr = inputAttribute;

View File

@@ -302,13 +302,22 @@ public class FlagSelector : View, IOrientation, IDesignable
var checkbox = new CheckBox
{
CanFocus = false,
CanFocus = true,
Title = nameWithHotKey,
Id = name,
Data = flag,
HighlightStyle = HighlightStyle
// HighlightStyle = HighlightStyle
};
checkbox.GettingNormalColor += (_, e) =>
{
if (SuperView is {HasFocus: true})
{
e.Cancel = true;
e.NewValue = GetFocusColor ();
}
};
checkbox.Selecting += (sender, args) => { RaiseSelecting (args.Context); };
checkbox.CheckedStateChanged += (sender, args) =>

View File

@@ -90,11 +90,11 @@ public class Shortcut : View, IOrientation, IDesignable
Title = commandText ?? string.Empty;
HelpView.Id = "_helpView";
HelpView.CanFocus = false;
//HelpView.CanFocus = false;
HelpView.Text = helpText ?? string.Empty;
KeyView.Id = "_keyView";
KeyView.CanFocus = false;
//KeyView.CanFocus = false;
key ??= Key.Empty;
Key = key;
@@ -437,7 +437,7 @@ public class Shortcut : View, IOrientation, IDesignable
// The default behavior is for CommandView to not get focus. I
// If you want it to get focus, you need to set it.
_commandView.CanFocus = false;
// _commandView.CanFocus = false;
_commandView.HotKeyChanged += (s, e) =>
{
@@ -492,9 +492,19 @@ public class Shortcut : View, IOrientation, IDesignable
CommandView.VerticalTextAlignment = Alignment.Center;
CommandView.TextAlignment = Alignment.Start;
CommandView.TextFormatter.WordWrap = false;
CommandView.HighlightStyle = HighlightStyle.None;
//CommandView.HighlightStyle = HighlightStyle.None;
CommandView.GettingNormalColor += CommanandView_GettingNormalColor;
}
private void CommanandView_GettingNormalColor (object? sender, CancelEventArgs<Attribute>? e)
{
if (HasFocus)
{
e.Cancel = true;
e.NewValue = GetFocusColor ();
}
}
private void Shortcut_TitleChanged (object? sender, EventArgs<string> e)
{
// If the Title changes, update the CommandView text.
@@ -533,6 +543,17 @@ public class Shortcut : View, IOrientation, IDesignable
HelpView.TextAlignment = Alignment.Start;
HelpView.TextFormatter.WordWrap = false;
HelpView.HighlightStyle = HighlightStyle.None;
HelpView.GettingNormalColor += HelpView_GettingNormalColor;
}
private void HelpView_GettingNormalColor (object? sender, CancelEventArgs<Attribute>? e)
{
if (HasFocus)
{
e.Cancel = true;
e.NewValue = GetFocusColor ();
}
}
/// <summary>
@@ -619,10 +640,10 @@ public class Shortcut : View, IOrientation, IDesignable
}
/// <summary>
/// Gets the subview that displays the key. Internal for unit testing.
/// Gets the subview that displays the key. Is drawn with Normal and HotNormal colors reversed.
/// </summary>
public View KeyView { get; } = new ();
public KeyView KeyView { get; } = new ();
private int _minimumKeyTextSize;
@@ -698,16 +719,16 @@ public class Shortcut : View, IOrientation, IDesignable
#region Focus
/// <inheritdoc/>
public override ColorScheme? ColorScheme
{
get => base.ColorScheme;
set
{
base.ColorScheme = _nonFocusColorScheme = value;
SetColors ();
}
}
///// <inheritdoc/>
//public override ColorScheme? ColorScheme
//{
// get => base.ColorScheme;
// set
// {
// base.ColorScheme = _nonFocusColorScheme = value;
// SetColors ();
// }
//}
private bool _forceFocusColors;
@@ -731,6 +752,7 @@ public class Shortcut : View, IOrientation, IDesignable
/// </summary>
internal void SetColors (bool highlight = false)
{
return;
if (HasFocus || highlight || ForceFocusColors)
{
if (_nonFocusColorScheme is null)
@@ -762,17 +784,6 @@ public class Shortcut : View, IOrientation, IDesignable
}
}
// Set KeyView's colors to show "hot"
if (IsInitialized && base.ColorScheme is { })
{
var cs = new ColorScheme (base.ColorScheme)
{
Normal = GetHotNormalColor (),
HotNormal = GetNormalColor ()
};
KeyView.ColorScheme = cs;
}
if (CommandView.Margin is { })
{
CommandView.Margin.ColorScheme = base.ColorScheme;
@@ -788,6 +799,28 @@ public class Shortcut : View, IOrientation, IDesignable
}
}
public override Attribute GetNormalColor ()
{
if (HasFocus)
{
return base.GetFocusColor ();
}
return base.GetNormalColor ();
}
public override Attribute GetHotNormalColor ()
{
if (HasFocus)
{
return base.GetHotFocusColor ();
}
return base.GetHotNormalColor ();
}
/// <inheritdoc/>
protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
{
@@ -832,3 +865,28 @@ public class Shortcut : View, IOrientation, IDesignable
base.Dispose (disposing);
}
}
public class KeyView : View
{
// Normal = GetHotNormalColor (),
// HotNormal = GetNormalColor ()
/// <inheritdoc />
public override Attribute GetNormalColor ()
{
if (SuperView is { HasFocus: true})
{
return base.GetFocusColor ();
}
return base.GetHotNormalColor ();
}
/// <inheritdoc />
/// <inheritdoc />
public override Attribute GetHotNormalColor () { return base.GetNormalColor (); }
/// <inheritdoc />
public override Attribute GetFocusColor () { return base.GetFocusColor (); }
}