-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve & use the new
FlagsEditor
for all flags
Turns out Scintilla had a custom editor for flags called `FlagsEnumConverter` before. Unaware of the first one, I wrote a new one called `FlagsEditor` for `ChangeHistory` enum. I may be biased but it turned out pretty good so think it would be cool to use it for all flags.
- Loading branch information
Showing
7 changed files
with
503 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace ScintillaNET; | ||
|
||
internal class FlagsConverter : TypeConverter | ||
{ | ||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | ||
{ | ||
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); | ||
} | ||
|
||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) | ||
{ | ||
if (value is Enum valueEnum && destinationType == typeof(string)) | ||
{ | ||
Type enumType = valueEnum.GetType(); | ||
ulong valueBits = Convert.ToUInt64(valueEnum); | ||
if (valueBits == 0) | ||
{ | ||
return Enum.ToObject(enumType, 0).ToString(); | ||
} | ||
ulong bits = 0; | ||
StringBuilder sb = new StringBuilder(); | ||
void Add(Enum item, ulong itemBits) | ||
{ | ||
if (itemBits != 0 && valueEnum.HasFlag(item)) | ||
{ | ||
bits |= itemBits; | ||
if (sb.Length > 0) | ||
sb.Append(" | "); | ||
sb.Append(item); | ||
} | ||
} | ||
foreach (Enum item in Enum.GetValues(enumType)) | ||
{ | ||
ulong itemBits = Convert.ToUInt64(item); | ||
if (Helpers.PopCount(itemBits) == 1) | ||
Add(item, itemBits); | ||
} | ||
foreach (Enum item in Enum.GetValues(enumType)) | ||
{ | ||
ulong itemBits = Convert.ToUInt64(item); | ||
if (Helpers.PopCount(itemBits) > 1 && (bits & itemBits) != itemBits) | ||
Add(item, itemBits); | ||
} | ||
return sb.ToString(); | ||
} | ||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
|
||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
Type t = context.PropertyDescriptor.PropertyType; | ||
ulong bits = 0; | ||
var nameList = str.Split('|').Select(x => x.Trim()); | ||
foreach (var name in nameList) | ||
{ | ||
bits |= Convert.ToUInt64(Enum.Parse(t, name)); | ||
} | ||
return Enum.ToObject(t, bits); | ||
} | ||
return base.ConvertFrom(context, culture, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.