Open maxkatz6 opened 2 years ago
@kekekeks @worldbeater if you have any thoughts.
I think code generation is a good option here and like the ideas. I'll have to read more into what WinUI is doing when I first noticed this (https://github.com/AvaloniaUI/Avalonia/issues/7432#issuecomment-1066183609). They are also generating an enum so there are no magic strings anywhere.
There is also a pattern that is used for UWP that removes strings -- it uses nameof() of the fields instead. This wouldn't work for these ideas because someone still has to define the name whether it's a field or a string so this is just FYI:
[TemplatePart(Name = nameof(ColorPicker.AlphaChannelSlider), Type = typeof(ColorPickerSlider))]
[TemplatePart(Name = nameof(ColorPicker.AlphaChannelTextBox), Type = typeof(TextBox))]
public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
{
private TextBox AlphaChannelTextBox;
private ColorPickerSlider AlphaChannelSlider;
protected override void OnApplyTemplate()
{
this.AlphaChannelTextBox = this.GetTemplateChild<TextBox>(nameof(AlphaChannelTextBox));
this.AlphaChannelSlider = this.GetTemplateChild<ColorPickerSlider>(nameof(AlphaChannelSlider));
}
}
How exactly would custom OnApplyTemplate
logic work with such generated code? Should we generate a partial method to be called from OnApplyTemplate
?
How exactly would custom OnApplyTemplate logic work with such generated code? Should we generate a partial method to be called from OnApplyTemplate?
This is the best solution I can think of as well. Just do it like InitializeComponent() in a separate partial class file. It might be better actually because it is opt-in for each control. They have to explicitly call the new method from OnApplyTemplate().
add yet another way to read namescope after template was applied.
Just pass the arguments.
protected void InitializeTemplateParts(TemplateAppliedEventArgs e)
{
}
This method can be added to the avalonia base control class, and called just before OnApplyTemplate. As an option. Probably with some "hide from intellisense" attributes. A bit hacky, but transparent for user.
protected virtual void InitializeTemplateParts(TemplateAppliedEventArgs e);
This method can be added to the avalonia base control class, and called just before OnApplyTemplate.
Good idea! That would be totally transparent as you said. I think we have a working solution all ideas together.
Regarding the implementation details of the new generator, I guess it is worth introducing a new class similar to AvaloniaNameGenerator
named e.g. TemplatePartGenerator
, then adding a new option to GeneratorOptions
that would control if the new generator is enabled, and then invoking the generator in the composition root.
The code that scans the assembly and finds all classes annotated with the TemplatePart
attribute can be found here https://github.com/reactivemarbles/ObservableEvents/blob/main/src/ReactiveMarbles.ObservableEvents.SourceGenerator/SyntaxReceiver.cs#L17 Most likely we'll need to implement ISyntaxReceiver
that receives all relevant classes with TemplatePart
attributes.
To sum up, for the following annotated class:
[TemplatePart("PART_Text", typeof(TextPresenter))]
[TemplatePart("PART_AcceptButton", typeof(Button))]
public partial class TextBox
{
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
this.InitializeTemplateParts(e);
// Text.ShowCaret();
// AcceptButton.IsEnabled = false;
}
}
We are going to generate the following partial class:
public partial class TextBox
{
private global::Avalonia.Controls.Presenters.TextPresenter Text;
private global::Avalonia.Controls.Button AcceptButton;
private InitializeTemplateParts(global::Avalonia.Controls.Primitives.TemplateAppliedEventArgs e)
{
Text = e.NameScope.Get<global::Avalonia.Controls.Presenters.TextPresenter>("PART_Text");
AcceptButton = e.NameScope.Get<global::Avalonia.Controls.Button>("PART_AcceptButton");
}
}
There was a lot of further discussion in https://github.com/AvaloniaUI/Avalonia/issues/9093 related to how to define the string constants and then enforce them statically in XAML. I think that discussion isn't fully closed yet and it may influence what is done here. Feel free to jump into that conversation as well. That said, the TemplatePart attribute isn't going to change so however the string name gets into the attribute is irrelevant for you.
Concerning the code generation:
TextPresenter
the control should be named just Text
. Not all template parts currently have good names...Edit:
There is a new attribute ported from WPF. It will be useful to have optional code generation for "parts" of templates. As an example, user code:
Additional generated code, adds new property and sets value to it:
Problems:
Which means, we most likely will need to modify Avalonia to add yet another way to read namescope after template was applied.