Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#7911 from robloo/add-template-parts
Browse files Browse the repository at this point in the history
Add Template Part Attributes to Controls
  • Loading branch information
maxkatz6 authored and grokys committed Apr 25, 2022
1 parent 2ad6d64 commit c877c1b
Show file tree
Hide file tree
Showing 27 changed files with 129 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Avalonia.Controls/AutoCompleteBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ public enum AutoCompleteFilterMode
/// drop-down that contains possible matches based on the input in the text
/// box.
/// </summary>
[TemplatePart(ElementPopup, typeof(Popup))]
[TemplatePart(ElementSelector, typeof(SelectingItemsControl))]
[TemplatePart(ElementSelectionAdapter, typeof(ISelectionAdapter))]
[TemplatePart(ElementTextBox, typeof(TextBox))]
[PseudoClasses(":dropdownopen")]
public class AutoCompleteBox : TemplatedControl
{
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/ButtonSpinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum Location
/// <summary>
/// Represents a spinner control that includes two Buttons.
/// </summary>
[TemplatePart("PART_DecreaseButton", typeof(Button))]
[TemplatePart("PART_IncreaseButton", typeof(Button))]
[PseudoClasses(":left", ":right")]
public class ButtonSpinner : Spinner
{
Expand Down
21 changes: 21 additions & 0 deletions src/Avalonia.Controls/Calendar/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input;
Expand Down Expand Up @@ -222,6 +223,8 @@ public CalendarModeChangedEventArgs(CalendarMode oldMode, CalendarMode newMode)
/// element in XAML.
/// </para>
/// </remarks>
[TemplatePart(PART_ElementMonth, typeof(CalendarItem))]
[TemplatePart(PART_ElementRoot, typeof(Panel))]
public class Calendar : TemplatedControl
{
internal const int RowsPerMonth = 7;
Expand Down Expand Up @@ -261,6 +264,7 @@ internal CalendarItem MonthControl
AvaloniaProperty.Register<Calendar, DayOfWeek>(
nameof(FirstDayOfWeek),
defaultValue: DateTimeHelper.GetCurrentDateFormat().FirstDayOfWeek);

/// <summary>
/// Gets or sets the day that is considered the beginning of the week.
/// </summary>
Expand All @@ -273,6 +277,7 @@ public DayOfWeek FirstDayOfWeek
get { return GetValue(FirstDayOfWeekProperty); }
set { SetValue(FirstDayOfWeekProperty, value); }
}

/// <summary>
/// FirstDayOfWeekProperty property changed handler.
/// </summary>
Expand All @@ -289,6 +294,7 @@ private void OnFirstDayOfWeekChanged(AvaloniaPropertyChangedEventArgs e)
throw new ArgumentOutOfRangeException("d", "Invalid DayOfWeek");
}
}

/// <summary>
/// Inherited code: Requires comment.
/// </summary>
Expand All @@ -311,6 +317,7 @@ private static bool IsValidFirstDayOfWeek(object value)
AvaloniaProperty.Register<Calendar, bool>(
nameof(IsTodayHighlighted),
defaultValue: true);

/// <summary>
/// Gets or sets a value indicating whether the current date is
/// highlighted.
Expand All @@ -324,6 +331,7 @@ public bool IsTodayHighlighted
get { return GetValue(IsTodayHighlightedProperty); }
set { SetValue(IsTodayHighlightedProperty, value); }
}

/// <summary>
/// IsTodayHighlightedProperty property changed handler.
/// </summary>
Expand All @@ -343,6 +351,7 @@ private void OnIsTodayHighlightedChanged(AvaloniaPropertyChangedEventArgs e)

public static readonly StyledProperty<IBrush> HeaderBackgroundProperty =
AvaloniaProperty.Register<Calendar, IBrush>(nameof(HeaderBackground));

public IBrush HeaderBackground
{
get { return GetValue(HeaderBackgroundProperty); }
Expand All @@ -367,6 +376,7 @@ public CalendarMode DisplayMode
get { return GetValue(DisplayModeProperty); }
set { SetValue(DisplayModeProperty, value); }
}

/// <summary>
/// DisplayModeProperty property changed handler.
/// </summary>
Expand Down Expand Up @@ -424,6 +434,7 @@ private static bool IsValidDisplayMode(CalendarMode mode)
|| mode == CalendarMode.Year
|| mode == CalendarMode.Decade;
}

private void OnDisplayModeChanged(CalendarModeChangedEventArgs args)
{
DisplayModeChanged?.Invoke(this, args);
Expand All @@ -433,6 +444,7 @@ private void OnDisplayModeChanged(CalendarModeChangedEventArgs args)
AvaloniaProperty.Register<Calendar, CalendarSelectionMode>(
nameof(SelectionMode),
defaultValue: CalendarSelectionMode.SingleDate);

/// <summary>
/// Gets or sets a value that indicates what kind of selections are
/// allowed.
Expand All @@ -457,6 +469,7 @@ public CalendarSelectionMode SelectionMode
get { return GetValue(SelectionModeProperty); }
set { SetValue(SelectionModeProperty, value); }
}

private void OnSelectionModeChanged(AvaloniaPropertyChangedEventArgs e)
{
if (IsValidSelectionMode(e.NewValue))
Expand All @@ -471,6 +484,7 @@ private void OnSelectionModeChanged(AvaloniaPropertyChangedEventArgs e)
throw new ArgumentOutOfRangeException("d", "Invalid SelectionMode");
}
}

/// <summary>
/// Inherited code: Requires comment.
/// </summary>
Expand All @@ -492,6 +506,7 @@ private static bool IsValidSelectionMode(object value)
o => o.SelectedDate,
(o, v) => o.SelectedDate = v,
defaultBindingMode: BindingMode.TwoWay);

/// <summary>
/// Gets or sets the currently selected date.
/// </summary>
Expand Down Expand Up @@ -720,6 +735,7 @@ internal DateTime SelectedYear
o => o.DisplayDate,
(o, v) => o.DisplayDate = v,
defaultBindingMode: BindingMode.TwoWay);

/// <summary>
/// Gets or sets the date to display.
/// </summary>
Expand Down Expand Up @@ -1959,13 +1975,15 @@ internal void ProcessPageUpKey(bool shift)
}
}
}

private void Calendar_KeyUp(KeyEventArgs e)
{
if (!e.Handled && (e.Key == Key.LeftShift || e.Key == Key.RightShift))
{
ProcessShiftKeyUp();
}
}

internal void ProcessShiftKeyUp()
{
if (_isShiftPressed && (SelectionMode == CalendarSelectionMode.SingleRange || SelectionMode == CalendarSelectionMode.MultipleRange))
Expand Down Expand Up @@ -2014,6 +2032,7 @@ protected override void OnGotFocus(GotFocusEventArgs e)
}
}
}

protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
Expand All @@ -2040,6 +2059,7 @@ protected override void OnLostFocus(RoutedEventArgs e)
}
}
}

/// <summary>
/// Called when the IsEnabled property changes.
/// </summary>
Expand Down Expand Up @@ -2084,6 +2104,7 @@ public Calendar()

private const string PART_ElementRoot = "Root";
private const string PART_ElementMonth = "CalendarItem";

/// <summary>
/// Builds the visual tree for the
/// <see cref="T:System.Windows.Controls.Calendar" /> when a new
Expand Down
5 changes: 5 additions & 0 deletions src/Avalonia.Controls/Calendar/CalendarDatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input;
Expand Down Expand Up @@ -116,6 +117,10 @@ public enum CalendarDatePickerFormat
Custom = 2
}

[TemplatePart(ElementButton, typeof(Button))]
[TemplatePart(ElementCalendar, typeof(Calendar))]
[TemplatePart(ElementPopup, typeof(Popup))]
[TemplatePart(ElementTextBox, typeof(TextBox))]
public class CalendarDatePicker : TemplatedControl
{
private const string ElementTextBox = "PART_TextBox";
Expand Down
5 changes: 5 additions & 0 deletions src/Avalonia.Controls/Calendar/CalendarItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace Avalonia.Controls.Primitives
/// Represents the currently displayed month or year on a
/// <see cref="T:Avalonia.Controls.Calendar" />.
/// </summary>
[TemplatePart(PART_ElementHeaderButton, typeof(Button))]
[TemplatePart(PART_ElementMonthView, typeof(Grid))]
[TemplatePart(PART_ElementNextButton, typeof(Button))]
[TemplatePart(PART_ElementPreviousButton, typeof(Button))]
[TemplatePart(PART_ElementYearView, typeof(Grid))]
[PseudoClasses(":calendardisabled")]
public sealed class CalendarItem : TemplatedControl
{
Expand Down
4 changes: 4 additions & 0 deletions src/Avalonia.Controls/Chrome/CaptionButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace Avalonia.Controls.Chrome
/// <summary>
/// Draws window minimize / maximize / close buttons in a <see cref="TitleBar"/> when managed client decorations are enabled.
/// </summary>
[TemplatePart("PART_CloseButton", typeof(Panel))]
[TemplatePart("PART_RestoreButton", typeof(Panel))]
[TemplatePart("PART_MinimiseButton", typeof(Panel))]
[TemplatePart("PART_FullScreenButton", typeof(Panel))]
[PseudoClasses(":minimized", ":normal", ":maximized", ":fullscreen")]
public class CaptionButtons : TemplatedControl
{
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Controls/Chrome/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Avalonia.Controls.Chrome
/// <summary>
/// Draws a titlebar when managed client decorations are enabled.
/// </summary>
[TemplatePart("PART_CaptionButtons", typeof(CaptionButtons))]
[PseudoClasses(":minimized", ":normal", ":maximized", ":fullscreen")]
public class TitleBar : TemplatedControl
{
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
using Avalonia.Media;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Avalonia.Controls.Metadata;

namespace Avalonia.Controls
{
/// <summary>
/// A drop-down list control.
/// </summary>
[TemplatePart("PART_Popup", typeof(Popup))]
public class ComboBox : SelectingItemsControl
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/ContentControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Collections;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
Expand All @@ -12,6 +13,7 @@ namespace Avalonia.Controls
/// <summary>
/// Displays <see cref="Content"/> according to a <see cref="FuncDataTemplate"/>.
/// </summary>
[TemplatePart("PART_ContentPresenter", typeof(IContentPresenter))]
public class ContentControl : TemplatedControl, IContentControl, IContentPresenterHost
{
/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Avalonia.Controls/DateTimePickers/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace Avalonia.Controls
/// <summary>
/// A control to allow the user to select a date
/// </summary>
[TemplatePart("ButtonContentGrid", typeof(Grid))]
[TemplatePart("DayText", typeof(TextBlock))]
[TemplatePart("FirstSpacer", typeof(Rectangle))]
[TemplatePart("FlyoutButton", typeof(Button))]
[TemplatePart("MonthText", typeof(TextBlock))]
[TemplatePart("PickerPresenter", typeof(DatePickerPresenter))]
[TemplatePart("Popup", typeof(Popup))]
[TemplatePart("SecondSpacer", typeof(Rectangle))]
[TemplatePart("YearText", typeof(TextBlock))]
[PseudoClasses(":hasnodate")]
public class DatePicker : TemplatedControl
{
Expand Down
20 changes: 19 additions & 1 deletion src/Avalonia.Controls/DateTimePickers/DatePickerPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Interactivity;
Expand All @@ -12,6 +13,23 @@ namespace Avalonia.Controls
/// Defines the presenter used for selecting a date for a
/// <see cref="DatePicker"/>
/// </summary>
[TemplatePart("AcceptButton", typeof(Button))]
[TemplatePart("DayDownButton", typeof(RepeatButton))]
[TemplatePart("DayHost", typeof(Panel))]
[TemplatePart("DaySelector", typeof(DateTimePickerPanel))]
[TemplatePart("DayUpButton", typeof(RepeatButton))]
[TemplatePart("DismissButton", typeof(Button))]
[TemplatePart("FirstSpacer", typeof(Rectangle))]
[TemplatePart("MonthDownButton", typeof(RepeatButton))]
[TemplatePart("MonthHost", typeof(Panel))]
[TemplatePart("MonthSelector", typeof(DateTimePickerPanel))]
[TemplatePart("MonthUpButton", typeof(RepeatButton))]
[TemplatePart("PickerContainer", typeof(Grid))]
[TemplatePart("SecondSpacer", typeof(Rectangle))]
[TemplatePart("YearDownButton", typeof(RepeatButton))]
[TemplatePart("YearHost", typeof(Panel))]
[TemplatePart("YearSelector", typeof(DateTimePickerPanel))]
[TemplatePart("YearUpButton", typeof(RepeatButton))]
public class DatePickerPresenter : PickerPresenterBase
{
/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Avalonia.Controls/DateTimePickers/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ namespace Avalonia.Controls
/// <summary>
/// A control to allow the user to select a time.
/// </summary>
[TemplatePart("FirstColumnDivider", typeof(Rectangle))]
[TemplatePart("FirstPickerHost", typeof(Border))]
[TemplatePart("FlyoutButton", typeof(Button))]
[TemplatePart("FlyoutButtonContentGrid", typeof(Grid))]
[TemplatePart("HourTextBlock", typeof(TextBlock))]
[TemplatePart("MinuteTextBlock", typeof(TextBlock))]
[TemplatePart("PeriodTextBlock", typeof(TextBlock))]
[TemplatePart("PickerPresenter", typeof(TimePickerPresenter))]
[TemplatePart("Popup", typeof(Popup))]
[TemplatePart("SecondColumnDivider", typeof(Rectangle))]
[TemplatePart("SecondPickerHost", typeof(Border))]
[TemplatePart("ThirdPickerHost", typeof(Border))]
[PseudoClasses(":hasnotime")]
public class TimePicker : TemplatedControl
{
Expand Down
17 changes: 16 additions & 1 deletion src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Interactivity;
Expand All @@ -10,6 +11,20 @@ namespace Avalonia.Controls
/// Defines the presenter used for selecting a time. Intended for use with
/// <see cref="TimePicker"/> but can be used independently
/// </summary>
[TemplatePart("AcceptButton", typeof(Button))]
[TemplatePart("DismissButton", typeof(Button))]
[TemplatePart("HourDownButton", typeof(RepeatButton))]
[TemplatePart("HourSelector", typeof(DateTimePickerPanel))]
[TemplatePart("HourUpButton", typeof(RepeatButton))]
[TemplatePart("MinuteDownButton", typeof(RepeatButton))]
[TemplatePart("MinuteSelector", typeof(DateTimePickerPanel))]
[TemplatePart("MinuteUpButton", typeof(RepeatButton))]
[TemplatePart("PeriodDownButton", typeof(RepeatButton))]
[TemplatePart("PeriodHost", typeof(Panel))]
[TemplatePart("PeriodSelector", typeof(DateTimePickerPanel))]
[TemplatePart("PeriodUpButton", typeof(RepeatButton))]
[TemplatePart("PickerContainer", typeof(Grid))]
[TemplatePart("SecondSpacer", typeof(Rectangle))]
public class TimePickerPresenter : PickerPresenterBase
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/ListBox.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
Expand All @@ -12,6 +13,7 @@ namespace Avalonia.Controls
/// <summary>
/// An <see cref="ItemsControl"/> in which individual items can be selected.
/// </summary>
[TemplatePart("PART_ScrollViewer", typeof(IScrollable))]
public class ListBox : SelectingItemsControl
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Controls/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Avalonia.Controls
/// <summary>
/// A menu item control.
/// </summary>
[TemplatePart("PART_Popup", typeof(Popup))]
[PseudoClasses(":separator", ":icon", ":open", ":pressed", ":selected")]
public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable, ICommandSource
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Avalonia.Controls.Notifications
/// <summary>
/// An <see cref="INotificationManager"/> that displays notifications in a <see cref="Window"/>.
/// </summary>
[TemplatePart("PART_Items", typeof(Panel))]
[PseudoClasses(":topleft", ":topright", ":bottomleft", ":bottomright")]
public class WindowNotificationManager : TemplatedControl, IManagedNotificationManager, ICustomSimpleHitTest
{
Expand Down
Loading

0 comments on commit c877c1b

Please sign in to comment.