Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ColorPicker (true color) #298

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Design.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public IEnumerable<Design> GetSiblings()
{
yield break;
}

foreach (var v in this.View.SuperView.Subviews)
{
if (v == this.View)
Expand Down Expand Up @@ -763,6 +763,12 @@ private IEnumerable<Property> LoadDesignableProperties()
{
yield return this.CreateProperty(nameof(CheckBox.CheckedState));
}
if (this.View is ColorPicker cp)
{
yield return this.CreateSubProperty(nameof(ColorPickerStyle.ColorModel),nameof(ColorPicker.Style),cp.Style);
yield return this.CreateSubProperty(nameof(ColorPickerStyle.ShowColorName), nameof(ColorPicker.Style), cp.Style);
yield return this.CreateSubProperty(nameof(ColorPickerStyle.ShowTextFields), nameof(ColorPicker.Style), cp.Style);
}

if (this.View is ListView lv)
{
Expand Down
16 changes: 8 additions & 8 deletions src/TerminalGuiDesigner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.5" />
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.7.7" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0" />
<PackageReference Include="Terminal.Gui" Version="2.0.0-v2-develop.2189" />
<PackageReference Include="nlog" Version="5.2.7" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
<PackageReference Include="Terminal.Gui" Version="2.0.0-v2-develop.2203" />
<PackageReference Include="nlog" Version="5.3.3" />
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.7.7" />
<PackageReference Include="System.CodeDom" Version="8.0.0" />
</ItemGroup>

Expand Down
40 changes: 40 additions & 0 deletions src/ToCode/ColorPickerToCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.CodeDom;
using Terminal.Gui;

namespace TerminalGuiDesigner.ToCode
{
internal class ColorPickerToCode : ToCodeBase
{
private readonly Design design;
private readonly ColorPicker colorPicker;

/// <summary>
/// Initializes a new instance of the <see cref="ColorPickerToCode"/> class.
/// </summary>
/// <param name="design">Wrapper for a <see cref="ColorPicker"/>.</param>
public ColorPickerToCode(Design design)
{
this.design = design;

if (design.View is not ColorPicker cp)
{
throw new ArgumentException(nameof(design),
$"{nameof(ColorPickerToCode)} can only be used with {nameof(TerminalGuiDesigner.Design)} that wrap {nameof(ColorPicker)}");
}

this.colorPicker = cp;
}

/// <summary>
/// Adds code to .Designer.cs to call <see cref="ColorPicker.ApplyStyleChanges"/>
/// </summary>
/// <param name="args">State object for the .Designer.cs file being generated.</param>
public void ToCode(CodeDomArgs args)
{
var colorPickerFieldExpression = new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), design.FieldName);

AddMethodCall(args,colorPickerFieldExpression,nameof(ColorPicker.ApplyStyleChanges));
}
}
}
7 changes: 7 additions & 0 deletions src/ToCode/DesignToCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ internal void ToCode(CodeDomArgs args, CodeExpression parentView)
designItems.ToCode(args);
}

if (this.Design.View is ColorPicker)
{
var designItems = new ColorPickerToCode(this.Design);
designItems.ToCode(args);
}


// call this.Add(someView)
this.AddAddToViewStatement(args, this.Design, parentView);
}
Expand Down
4 changes: 4 additions & 0 deletions src/ToCode/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ private void CallRefreshMethodsIfAny()
{
t.Update();
}
if (this.Design.View is ColorPicker cp)
{
cp.ApplyStyleChanges();
}

this.Design.View.SetNeedsDisplay();
}
Expand Down
44 changes: 26 additions & 18 deletions src/UI/Windows/ColorPicker.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/UI/Windows/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ public ColorPicker(Attribute? currentValue)

if (currentValue != null)
{
// TODO: Enable again once true color nuget package available and designer supported
// see https://github.com/gui-cs/Terminal.Gui/pull/3604

// cpForeground.SelectedColor = currentValue.Value.Foreground;
// cpBackground.SelectedColor = currentValue.Value.Background;
cpForeground.SelectedColor = currentValue.Value.Foreground;
cpBackground.SelectedColor = currentValue.Value.Background;
}

lblPreview.ColorScheme = new ColorScheme();
Expand Down
8 changes: 8 additions & 0 deletions src/ViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ public static T Create<T>(int? width = null, int? height = null, string? text =
newView.Width = Dim.Auto();
break;
case ColorPicker:
SetDefaultDimensions(newView, width ?? 20, height ?? 4);
break;

// leave with default dimensions
case ColorPicker16:
break;
case TextView:
SetDefaultDimensions(newView, width ?? 10, height ?? 4);
break;
Expand Down Expand Up @@ -317,6 +323,8 @@ public static View Create( Type requestedType )
{ } t when t == typeof( TextValidateField ) => Create<TextValidateField>( ),
{ } t when t == typeof( ProgressBar ) => Create<ProgressBar>( ),
{ } t when t == typeof( View ) => Create<View>( ),
{ } t when t == typeof(ColorPicker) => Create<ColorPicker>(),
{ } t when t == typeof(ColorPicker16) => Create<ColorPicker16>(),
{ } t when t == typeof( Window ) => Create<Window>( ),
{ } t when t == typeof( TextField ) => Create<TextField>( ),
{ } t when t.IsAssignableTo( typeof( GraphView ) ) => Create<GraphView>( ),
Expand Down
41 changes: 41 additions & 0 deletions tests/ColorPickerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using static Terminal.Gui.SpinnerStyle;

namespace UnitTests;

[TestFixture]
[Category("Core")]
internal class ColorPickerTests : Tests
{
[Test]
public void ColorPickerHeightStaysAuto()
{
Assume.That(ViewFactory.SupportedViewTypes, Does.Contain(typeof(ColorPicker)));

using (var cp = ViewFactory.Create<ColorPicker>())
{
Assert.That(cp.Height?.IsAuto(out _,out _, out _),Is.True);
}
}


[Test]
[Category("Code Generation")]
public void ColorPickerCanSerializeStyle()
{
using var backIn = RoundTrip<View, ColorPicker>(static (d, v) =>
{
Assume.That(v.Style.ColorModel,Is.Not.EqualTo(ColorModel.RGB));

var prop = d.GetDesignableProperty(nameof(ColorPicker.Style) + "." + nameof(ColorPickerStyle.ColorModel))
?? throw new("Property was unexpectedly not designable");

// We change to RGB
prop.SetValue(ColorModel.RGB);

Assert.That(v.Style.ColorModel, Is.EqualTo(ColorModel.RGB));
}, out _);

// Reloaded code from .Designer.cs should match
Assert.That(backIn.Style.ColorModel, Is.EqualTo(ColorModel.RGB));
}
}
2 changes: 1 addition & 1 deletion tests/DimExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UnitTests;
[TestOf( typeof( DimExtensions ) )]
[Category( "Core" )]
[Category( "Terminal.Gui Extensions" )]
[Parallelizable( ParallelScope.Children )]
[NonParallelizable]
Copy link
Contributor

@BDisp BDisp Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead using [NonParallelizable] attribute everywhere, I think it's possible prevent parallel execution via .runsettings:

<RunSettings>
  <NUnit>
    <DefaultTestCaseParallelization>false</DefaultTestCaseParallelization>
  </NUnit>
</RunSettings>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right - I think this is the default so I could probably just have deleted the attributes.

internal class DimExtensionsTests
{
[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/KeyMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UnitTests;
[TestOf( typeof( KeyMap ) )]
[Category( "Core" )]
[Category( "UI" )]
[Parallelizable( ParallelScope.All )]
[NonParallelizable]
internal class KeyMapTests
{
private const string ExpectedKeysYamlContent =
Expand Down
2 changes: 1 addition & 1 deletion tests/KeyboardManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Backspace_WithDateFieldSelected( )
}

[Test]
[Parallelizable(ParallelScope.Self)]
[NonParallelizable]
public void ButtonRename( )
{
Button v = ViewFactory.Create<Button>( );
Expand Down
2 changes: 1 addition & 1 deletion tests/ObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace UnitTests;
[TestFixture]
[TestOf( typeof( ObjectExtensions ) )]
[Category( "Core" )]
[Parallelizable( ParallelScope.All )]
[NonParallelizable]
[Order( 10 )]
internal class ObjectExtensionsTests
{
Expand Down
2 changes: 1 addition & 1 deletion tests/StatusBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[TestFixture]
[Category( "Code Generation" )]
[Parallelizable(ParallelScope.All)]
[NonParallelizable]
internal class StatusBarTests : Tests
{
[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/StringExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UnitTests;
[TestFixture]
[TestOf( typeof( StringExtensions ) )]
[Category( "Core" )]
[Parallelizable( ParallelScope.All )]
[NonParallelizable]
[SuppressMessage( "Performance", "CA1861:Avoid constant arrays as arguments", Justification = "We don't really care for unit tests..." )]
internal class StringExtensionTests
{
Expand Down
10 changes: 5 additions & 5 deletions tests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="ReportGenerator" Version="5.2.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="NUnit" Version="4.2.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="ReportGenerator" Version="5.3.8" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading
Loading