Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Updated to handle VS themes
Browse files Browse the repository at this point in the history
  • Loading branch information
msawczyn committed Jan 24, 2022
1 parent 58c6646 commit ffb4465
Show file tree
Hide file tree
Showing 71 changed files with 2,554 additions and 310 deletions.
18 changes: 13 additions & 5 deletions src/Dsl/CustomCode/Extensions/ColorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ public static class ColorExtensions
/// </summary>
/// <param name="color">The background color</param>
public static Color LegibleTextColor(this Color color)
{
return color.IsLight() ? Color.Black : Color.White;
}

public static bool IsDark(this Color color)
{
// Counting the perceptive luminance - human eye favors green color...
double a = 1 - (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;

// bright colors (a < 0.5) - black font
// dark colors (a >= 0.5) - white font
// bright colors (a < 0.5)
// dark colors (a >= 0.5)

return a < 0.5
? Color.Black
: Color.White;
return a >= 0.5;
}

public static bool IsLight(this Color color)
{
return !color.IsDark();
}
}
}
4 changes: 1 addition & 3 deletions src/Dsl/CustomCode/ModelElement Interfaces/IHasStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling;

namespace Sawczyn.EFDesigner.EFModel {
public interface IHasStore
Expand Down
7 changes: 7 additions & 0 deletions src/Dsl/CustomCode/ModelElement Interfaces/IThemeable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Sawczyn.EFDesigner.EFModel
{
public interface IThemeable
{
void SetThemeColors(DiagramThemeColors diagramColors);
}
}
28 changes: 26 additions & 2 deletions src/Dsl/CustomCode/Partials/AssociationConnector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Sawczyn.EFDesigner.EFModel
{
public abstract partial class AssociationConnector : IHasStore
public abstract partial class AssociationConnector : IHasStore, IThemeable
{
/// <summary>
/// This method is called when a shape is inititially created, derived classes can
/// override to perform shape instance initialization. This method is always called within a transaction.
/// </summary>
public override void OnInitialize()
{
base.OnInitialize();
if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}

public void SetThemeColors(DiagramThemeColors diagramColors)
{
using (Transaction tx = Store.TransactionManager.BeginTransaction("Set diagram colors"))
{
Color = diagramColors.Background.LegibleTextColor();
TextColor = diagramColors.Text;
Invalidate();

tx.Commit();
}
}

/// <summary>
/// Initializes style set resources for this shape type
/// </summary>
Expand Down
332 changes: 245 additions & 87 deletions src/Dsl/CustomCode/Partials/ClassShape.cs

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion src/Dsl/CustomCode/Partials/CommentBoxShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@
using System.Collections.Generic;
using System.Drawing;

using Microsoft.VisualStudio.Modeling;

namespace Sawczyn.EFDesigner.EFModel
{
public partial class CommentBoxShape: IHasStore
public partial class CommentBoxShape: IHasStore, IThemeable
{
/// <summary>
/// Shape instance initialization.
/// </summary>
public override void OnInitialize()
{
base.OnInitialize();
if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}

public void SetThemeColors(DiagramThemeColors diagramColors)
{
using (Transaction tx = Store.TransactionManager.BeginTransaction("Set diagram colors"))
{
FillColor = diagramColors.Background;
TextColor = FillColor.LegibleTextColor();

Invalidate();

tx.Commit();
}
}

//Called once for each shape instance.
protected override void InitializeDecorators(IList<ShapeField> shapeFields, IList<Decorator> decorators)
{
Expand Down
29 changes: 27 additions & 2 deletions src/Dsl/CustomCode/Partials/CommentConnector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Sawczyn.EFDesigner.EFModel
{
public partial class CommentConnector: IHasStore
public partial class CommentConnector: IHasStore, IThemeable
{
/// <summary>
/// This method is called when a shape is inititially created, derived classes can
/// override to perform shape instance initialization. This method is always called within a transaction.
/// </summary>
public override void OnInitialize()
{
base.OnInitialize();
if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}

public void SetThemeColors(DiagramThemeColors diagramColors)
{
using (Transaction tx = Store.TransactionManager.BeginTransaction("Set diagram colors"))
{
Color = diagramColors.Background.LegibleTextColor();
TextColor = diagramColors.Text;

Invalidate();

tx.Commit();
}
}

protected override int ModifyLuminosity(int currentLuminosity, DiagramClientView view)
{
if (!view.HighlightedShapes.Contains(new DiagramItem(this)))
Expand Down
52 changes: 37 additions & 15 deletions src/Dsl/CustomCode/Partials/EFModelDiagram.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
Expand All @@ -14,7 +13,7 @@

namespace Sawczyn.EFDesigner.EFModel
{
public partial class EFModelDiagram : IHasStore
public partial class EFModelDiagram : IHasStore, IThemeable
{
public override void OnInitialize()
{
Expand All @@ -30,6 +29,32 @@ public override void OnInitialize()
ShowGrid = modelRoot?.ShowGrid ?? true;
GridColor = modelRoot?.GridColor ?? Color.Gainsboro;
SnapToGrid = modelRoot?.SnapToGrid ?? true;

if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}

public void SetThemeColors(DiagramThemeColors diagramColors)
{
Transaction tx = Store.TransactionManager.InTransaction
? null
: Store.TransactionManager.BeginTransaction("Set diagram colors");

try
{
FillColor = diagramColors.Background;
TextColor = diagramColors.Text;

Invalidate();
}
finally
{
if (tx != null)
{
tx.Commit();
tx.Dispose();
}
}
}

/// <summary>
Expand Down Expand Up @@ -84,25 +109,22 @@ public void Unhighlight(ShapeElement shape)
public override void OnDragOver(DiagramDragEventArgs diagramDragEventArgs)
{
base.OnDragOver(diagramDragEventArgs);

if (diagramDragEventArgs.Handled)
return;
diagramDragEventArgs.Handled = false;

List<BidirectionalConnector> bidirectionalConnectorsUnderShape = ClassShape.ClassShapeDragData?.GetBidirectionalConnectorsUnderShape(diagramDragEventArgs.MousePosition);

foreach (BidirectionalConnector connector in bidirectionalConnectorsUnderShape)
Highlight(connector);

bool isDroppingAssociationClass = bidirectionalConnectorsUnderShape?.Any() == true;

if (isDroppingAssociationClass)
diagramDragEventArgs.Effect = DragDropEffects.Link;
{
foreach (BidirectionalConnector connector in bidirectionalConnectorsUnderShape)
Highlight(connector);

diagramDragEventArgs.Effect = DragDropEffects.Copy;
}
else if (diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelEnum") is ModelEnum
|| diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelClass") is ModelClass
|| IsAcceptableDropItem(diagramDragEventArgs))
|| diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelClass") is ModelClass
|| IsAcceptableDropItem(diagramDragEventArgs))
diagramDragEventArgs.Effect = DragDropEffects.Copy;
else
diagramDragEventArgs.Effect = DragDropEffects.None;

diagramDragEventArgs.Handled = true;
}
Expand Down Expand Up @@ -380,5 +402,5 @@ public override void OnMouseDown(DiagramMouseEventArgs e)
}

public PointD MouseDownPosition;
}
}
}
69 changes: 61 additions & 8 deletions src/Dsl/CustomCode/Partials/EnumShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,48 @@ namespace Sawczyn.EFDesigner.EFModel
/// Override some methods of the compartment shape.
/// *** GenerateDoubleDerived must be set for this shape in DslDefinition.dsl. ****
/// </summary>
public partial class EnumShape : IHighlightFromModelExplorer, ICompartmentShapeMouseTarget
public partial class EnumShape : IHighlightFromModelExplorer, ICompartmentShapeMouseTarget, IThemeable
{
/// <summary>
/// Shape instance initialization.
/// </summary>
public override void OnInitialize()
{
base.OnInitialize();
if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}

private static bool UseInverseGlyphs
{
get
{
return ModelDisplay.GetDiagramColors?.Invoke().Background.IsDark() ?? false;
}
}

public void SetThemeColors(DiagramThemeColors diagramColors)
{
using (Transaction tx = Store.TransactionManager.BeginTransaction("Set diagram colors"))
{
TextColor = diagramColors.Text;
FillColor = diagramColors.Background;

foreach (ListCompartment compartment in NestedChildShapes.OfType<ListCompartment>())
{
compartment.CompartmentFillColor = diagramColors.Background;
compartment.ItemTextColor = diagramColors.Text;
compartment.TitleFillColor = diagramColors.HeaderBackground;
compartment.TitleTextColor = diagramColors.HeaderText;

compartment.Invalidate();
}

Invalidate();
tx.Commit();
}
}

/// <summary>
/// Exposes NodeShape Collapse() function to DSL's context menu
/// </summary>
Expand All @@ -36,22 +76,35 @@ protected override CompartmentMapping[] GetCompartmentMappings(Type melType)
{
CompartmentMapping[] mappings = base.GetCompartmentMappings(melType);

// Each item in the ValuesCompartment will call GetValueImage to determine its icon. Called any time the element's presentation element invalidates.
foreach (ElementListCompartmentMapping mapping in mappings.OfType<ElementListCompartmentMapping>()
.Where(m => m.CompartmentId == "ValuesCompartment"))
mapping.ImageGetter = GetValueImage;
// Each item in the ValuesCompartment will call GetValueGlyph to determine its icon. Called any time the element's presentation element invalidates.
foreach (ElementListCompartmentMapping mapping in mappings.OfType<ElementListCompartmentMapping>().Where(m => m.CompartmentId == "ValuesCompartment"))
mapping.ImageGetter = GetValueGlyph;

if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());

return mappings;
}

private Image GetValueImage(ModelElement element)
public static string GetExplorerNodeGlyphName(ModelEnum modelEnum)
{
string result = (modelEnum.IsVisible() ? nameof(Resources.Enumerator_16xVisible) : nameof(Resources.Enumerator_16x));

return UseInverseGlyphs
? result + "_i"
: result;
}

private Image GetValueGlyph(ModelElement element)
{
ModelRoot modelRoot = element.Store.ModelRoot();
if (element is ModelEnumValue enumValue)
{
Color background = ModelDisplay.GetDiagramColors?.Invoke().Background ?? Color.White;

return modelRoot.ShowWarningsInDesigner && enumValue.GetHasWarningValue()
? Resources.Warning
: Resources.EnumValue;
? (background.IsLight() ? Resources.Warning : Resources.Warning_i)
: (background.IsLight() ? Resources.EnumValue : Resources.EnumValue_i);
}

return null;
Expand Down
28 changes: 26 additions & 2 deletions src/Dsl/CustomCode/Partials/GeneralizationConnector.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Sawczyn.EFDesigner.EFModel
{
public partial class GeneralizationConnector: IHasStore
public partial class GeneralizationConnector: IHasStore, IThemeable
{
public override bool HasToolTip => true;

public void SetThemeColors(DiagramThemeColors diagramColors)
{
using (Transaction tx = Store.TransactionManager.BeginTransaction("Set diagram colors"))
{
Color = diagramColors.Background.LegibleTextColor();
TextColor = diagramColors.Text;
Invalidate();

tx.Commit();
}
}

public override string GetToolTipText(DiagramItem item)
{
return item.Shape.ModelElement is Generalization generalization
Expand All @@ -24,5 +37,16 @@ protected override int ModifyLuminosity(int currentLuminosity, DiagramClientView
// so if it's black we're highlighting, return 130, since that looks ok.
return baseCalculation == 40 ? 130 : baseCalculation;
}

/// <summary>
/// This method is called when a shape is inititially created, derived classes can
/// override to perform shape instance initialization. This method is always called within a transaction.
/// </summary>
public override void OnInitialize()
{
base.OnInitialize();
if (ModelDisplay.GetDiagramColors != null)
SetThemeColors(ModelDisplay.GetDiagramColors());
}
}
}
Loading

0 comments on commit ffb4465

Please sign in to comment.