Skip to content

Commit b0a40a2

Browse files
authored
Merge branch 'master' into fix-generateAvaloniaResources
2 parents 55de808 + ee69220 commit b0a40a2

File tree

26 files changed

+376
-148
lines changed

26 files changed

+376
-148
lines changed

src/Avalonia.Controls.DataGrid/DataGrid.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,12 @@ internal void UpdateStateOnCurrentChanged(object currentItem, int currentPositio
30393039
}
30403040
}
30413041

3042+
//TODO: Ensure right button is checked for
3043+
internal bool UpdateStateOnMouseRightButtonDown(PointerPressedEventArgs pointerPressedEventArgs, int columnIndex, int slot, bool allowEdit)
3044+
{
3045+
KeyboardHelper.GetMetaKeyState(pointerPressedEventArgs.KeyModifiers, out bool ctrl, out bool shift);
3046+
return UpdateStateOnMouseRightButtonDown(pointerPressedEventArgs, columnIndex, slot, allowEdit, shift, ctrl);
3047+
}
30423048
//TODO: Ensure left button is checked for
30433049
internal bool UpdateStateOnMouseLeftButtonDown(PointerPressedEventArgs pointerPressedEventArgs, int columnIndex, int slot, bool allowEdit)
30443050
{
@@ -4489,17 +4495,27 @@ private void PopulateCellContent(bool isCellEdited,
44894495
element = dataGridColumn.GenerateEditingElementInternal(dataGridCell, dataGridRow.DataContext);
44904496
if (element != null)
44914497
{
4492-
// Subscribe to the new element's events
4493-
element.Initialized += EditingElement_Initialized;
4498+
4499+
dataGridCell.Content = element;
4500+
if (element.IsInitialized)
4501+
{
4502+
PreparingCellForEditPrivate(element as Control);
4503+
}
4504+
else
4505+
{
4506+
// Subscribe to the new element's events
4507+
element.Initialized += EditingElement_Initialized;
4508+
}
44944509
}
44954510
}
44964511
else
44974512
{
44984513
// Generate Element and apply column style if available
44994514
element = dataGridColumn.GenerateElementInternal(dataGridCell, dataGridRow.DataContext);
4515+
dataGridCell.Content = element;
45004516
}
45014517

4502-
dataGridCell.Content = element;
4518+
45034519
}
45044520

45054521
private void PreparingCellForEditPrivate(Control editingElement)
@@ -5711,6 +5727,35 @@ private void VerticalScrollBar_Scroll(object sender, ScrollEventArgs e)
57115727
VerticalScroll?.Invoke(sender, e);
57125728
}
57135729

5730+
//TODO: Ensure right button is checked for
5731+
private bool UpdateStateOnMouseRightButtonDown(PointerPressedEventArgs pointerPressedEventArgs, int columnIndex, int slot, bool allowEdit, bool shift, bool ctrl)
5732+
{
5733+
Debug.Assert(slot >= 0);
5734+
5735+
if (shift || ctrl)
5736+
{
5737+
return true;
5738+
}
5739+
if (IsSlotOutOfBounds(slot))
5740+
{
5741+
return true;
5742+
}
5743+
if (GetRowSelection(slot))
5744+
{
5745+
return true;
5746+
}
5747+
// Unselect everything except the row that was clicked on
5748+
try
5749+
{
5750+
UpdateSelectionAndCurrency(columnIndex, slot, DataGridSelectionAction.SelectCurrent, scrollIntoView: false);
5751+
}
5752+
finally
5753+
{
5754+
NoSelectionChangeCount--;
5755+
}
5756+
return true;
5757+
}
5758+
57145759
//TODO: Ensure left button is checked for
57155760
private bool UpdateStateOnMouseLeftButtonDown(PointerPressedEventArgs pointerPressedEventArgs, int columnIndex, int slot, bool allowEdit, bool shift, bool ctrl)
57165761
{

src/Avalonia.Controls.DataGrid/DataGridCell.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,42 @@ protected override void OnPointerLeave(PointerEventArgs e)
161161
private void DataGridCell_PointerPressed(PointerPressedEventArgs e)
162162
{
163163
// OwningGrid is null for TopLeftHeaderCell and TopRightHeaderCell because they have no OwningRow
164-
if (OwningGrid != null)
164+
if (OwningGrid == null)
165165
{
166-
OwningGrid.OnCellPointerPressed(new DataGridCellPointerPressedEventArgs(this, OwningRow, OwningColumn, e));
167-
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
166+
return;
167+
}
168+
OwningGrid.OnCellPointerPressed(new DataGridCellPointerPressedEventArgs(this, OwningRow, OwningColumn, e));
169+
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
170+
{
171+
if (!e.Handled)
172+
//if (!e.Handled && OwningGrid.IsTabStop)
173+
{
174+
OwningGrid.Focus();
175+
}
176+
if (OwningRow != null)
168177
{
169-
if (!e.Handled)
170-
//if (!e.Handled && OwningGrid.IsTabStop)
178+
var handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, ColumnIndex, OwningRow.Slot, !e.Handled);
179+
180+
// Do not handle PointerPressed with touch,
181+
// so we can start scroll gesture on the same event.
182+
if (e.Pointer.Type != PointerType.Touch)
171183
{
172-
OwningGrid.Focus();
184+
e.Handled = handled;
173185
}
174-
if (OwningRow != null)
175-
{
176-
var handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, ColumnIndex, OwningRow.Slot, !e.Handled);
177-
178-
// Do not handle PointerPressed with touch,
179-
// so we can start scroll gesture on the same event.
180-
if (e.Pointer.Type != PointerType.Touch)
181-
{
182-
e.Handled = handled;
183-
}
184186

185-
OwningGrid.UpdatedStateOnMouseLeftButtonDown = true;
186-
}
187+
OwningGrid.UpdatedStateOnMouseLeftButtonDown = true;
188+
}
189+
}
190+
else if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
191+
{
192+
if (!e.Handled)
193+
//if (!e.Handled && OwningGrid.IsTabStop)
194+
{
195+
OwningGrid.Focus();
196+
}
197+
if (OwningRow != null)
198+
{
199+
e.Handled = OwningGrid.UpdateStateOnMouseRightButtonDown(e, ColumnIndex, OwningRow.Slot, !e.Handled);
187200
}
188201
}
189202
}

src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public bool BeginEdit(object dataItem)
233233
else
234234
{
235235
editableCollectionView.EditItem(dataItem);
236-
return editableCollectionView.IsEditingItem;
236+
return editableCollectionView.IsEditingItem || editableCollectionView.IsAddingNew;
237237
}
238238
}
239239

@@ -314,7 +314,14 @@ public bool EndEdit(object dataItem)
314314
CommittingEdit = true;
315315
try
316316
{
317-
editableCollectionView.CommitEdit();
317+
if (editableCollectionView.IsAddingNew)
318+
{
319+
editableCollectionView.CommitNew();
320+
}
321+
else
322+
{
323+
editableCollectionView.CommitEdit();
324+
}
318325
}
319326
finally
320327
{

src/Avalonia.Controls.DataGrid/DataGridRow.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,13 @@ internal int? MouseOverColumnIndex
378378
}
379379
}
380380
}
381-
}
381+
}
382382

383383
internal Panel RootElement
384384
{
385385
get;
386386
private set;
387-
}
387+
}
388388

389389
internal int Slot
390390
{
@@ -638,7 +638,7 @@ internal void UpdatePseudoClasses()
638638
PseudoClasses.Set(":editing", IsEditing);
639639
PseudoClasses.Set(":invalid", !IsValid);
640640
ApplyHeaderStatus();
641-
}
641+
}
642642
}
643643

644644
//TODO Animation
@@ -896,7 +896,7 @@ internal void EnsureDetailsContentHeight()
896896
_detailsElement.ContentHeight = _detailsDesiredHeight;
897897
}
898898
}
899-
}
899+
}
900900

901901
// Makes sure the _detailsDesiredHeight is initialized. We need to measure it to know what
902902
// height we want to animate to. Subsequently, we just update that height in response to SizeChanged
@@ -919,7 +919,7 @@ private void EnsureDetailsDesiredHeight()
919919

920920
//TODO Cleanup
921921
double? _previousDetailsHeight = null;
922-
922+
923923
//TODO Animation
924924
private void DetailsContent_HeightChanged(double newValue)
925925
{
@@ -1022,7 +1022,7 @@ internal void SetDetailsVisibilityInternal(bool isVisible, bool raiseNotificatio
10221022
}
10231023
}
10241024
}
1025-
1025+
10261026
internal void ApplyDetailsTemplate(bool initializeDetailsPreferredHeight)
10271027
{
10281028
if (_detailsElement != null && AreDetailsVisible)
@@ -1066,7 +1066,7 @@ internal void ApplyDetailsTemplate(bool initializeDetailsPreferredHeight)
10661066
.Subscribe(DetailsContent_MarginChanged);
10671067

10681068
}
1069-
1069+
10701070
_detailsElement.Children.Add(_detailsContent);
10711071
}
10721072
}
@@ -1090,6 +1090,28 @@ internal void ApplyDetailsTemplate(bool initializeDetailsPreferredHeight)
10901090
}
10911091
}
10921092
}
1093+
1094+
1095+
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
1096+
{
1097+
if (change.Property == DataContextProperty)
1098+
{
1099+
var owner = OwningGrid;
1100+
if (owner != null && this.IsRecycled)
1101+
{
1102+
var columns = owner.ColumnsItemsInternal;
1103+
var nc = columns.Count;
1104+
for (int ci = 0; ci < nc; ci++)
1105+
{
1106+
if (columns[ci] is DataGridTemplateColumn column)
1107+
{
1108+
column.RefreshCellContent((Control)this.Cells[column.Index].Content, nameof(DataGridTemplateColumn.CellTemplate));
1109+
}
1110+
}
1111+
}
1112+
}
1113+
base.OnPropertyChanged(change);
1114+
}
10931115

10941116
}
10951117

src/Avalonia.Controls.DataGrid/DataGridRowGroupHeader.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,11 @@ internal void ClearFrozenStates()
283283
//TODO TabStop
284284
private void DataGridRowGroupHeader_PointerPressed(PointerPressedEventArgs e)
285285
{
286-
if (OwningGrid != null && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
286+
if (OwningGrid == null)
287+
{
288+
return;
289+
}
290+
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
287291
{
288292
if (OwningGrid.IsDoubleClickRecordsClickOnCall(this) && !e.Handled)
289293
{
@@ -300,6 +304,15 @@ private void DataGridRowGroupHeader_PointerPressed(PointerPressedEventArgs e)
300304
e.Handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, OwningGrid.CurrentColumnIndex, RowGroupInfo.Slot, allowEdit: false);
301305
}
302306
}
307+
else if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
308+
{
309+
if (!e.Handled)
310+
{
311+
OwningGrid.Focus();
312+
}
313+
e.Handled = OwningGrid.UpdateStateOnMouseRightButtonDown(e, OwningGrid.CurrentColumnIndex, RowGroupInfo.Slot, allowEdit: false);
314+
}
315+
303316
}
304317

305318
private void EnsureChildClip(Visual child, double frozenLeftEdge)

src/Avalonia.Controls.DataGrid/DataGridRowHeader.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ protected override void OnPointerLeave(PointerEventArgs e)
179179
//TODO TabStop
180180
private void DataGridRowHeader_PointerPressed(object sender, PointerPressedEventArgs e)
181181
{
182-
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
182+
if (OwningGrid == null)
183183
{
184184
return;
185185
}
186186

187-
if (OwningGrid != null)
187+
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
188188
{
189189
if (!e.Handled)
190190
//if (!e.Handled && OwningGrid.IsTabStop)
@@ -199,6 +199,19 @@ private void DataGridRowHeader_PointerPressed(object sender, PointerPressedEvent
199199
OwningGrid.UpdatedStateOnMouseLeftButtonDown = true;
200200
}
201201
}
202+
else if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
203+
{
204+
if (!e.Handled)
205+
{
206+
OwningGrid.Focus();
207+
}
208+
if (OwningRow != null)
209+
{
210+
Debug.Assert(sender is DataGridRowHeader);
211+
Debug.Assert(sender == this);
212+
e.Handled = OwningGrid.UpdateStateOnMouseRightButtonDown(e, -1, Slot, false);
213+
}
214+
}
202215
}
203216

204217
}

src/Avalonia.Controls/Design.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public static Control GetPreviewWith(AvaloniaObject target)
6060
return target.GetValue(PreviewWithProperty);
6161
}
6262

63+
public static readonly AttachedProperty<IStyle> DesignStyleProperty = AvaloniaProperty
64+
.RegisterAttached<Control, IStyle>("DesignStyle", typeof(Design));
65+
66+
public static void SetDesignStyle(Control control, IStyle value)
67+
{
68+
control.SetValue(DesignStyleProperty, value);
69+
}
70+
71+
public static IStyle GetDesignStyle(Control control)
72+
{
73+
return control.GetValue(DesignStyleProperty);
74+
}
75+
6376
public static void ApplyDesignModeProperties(Control target, Control source)
6477
{
6578
if (source.IsSet(WidthProperty))
@@ -68,6 +81,8 @@ public static void ApplyDesignModeProperties(Control target, Control source)
6881
target.Height = source.GetValue(HeightProperty);
6982
if (source.IsSet(DataContextProperty))
7083
target.DataContext = source.GetValue(DataContextProperty);
84+
if (source.IsSet(DesignStyleProperty))
85+
target.Styles.Add(source.GetValue(DesignStyleProperty));
7186
}
7287
}
7388
}

src/Avalonia.Controls/Primitives/Popup.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ static Popup()
145145
{
146146
IsHitTestVisibleProperty.OverrideDefaultValue<Popup>(false);
147147
ChildProperty.Changed.AddClassHandler<Popup>((x, e) => x.ChildChanged(e));
148-
IsOpenProperty.Changed.AddClassHandler<Popup>((x, e) => x.IsOpenChanged((AvaloniaPropertyChangedEventArgs<bool>)e));
148+
IsOpenProperty.Changed.AddClassHandler<Popup>((x, e) => x.IsOpenChanged((AvaloniaPropertyChangedEventArgs<bool>)e));
149+
VerticalOffsetProperty.Changed.AddClassHandler<Popup>((x, _) => x.HandlePositionChange());
150+
HorizontalOffsetProperty.Changed.AddClassHandler<Popup>((x, _) => x.HandlePositionChange());
149151
}
150152

151153
/// <summary>
@@ -519,6 +521,24 @@ protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs
519521
base.OnDetachedFromLogicalTree(e);
520522
Close();
521523
}
524+
525+
private void HandlePositionChange()
526+
{
527+
if (_openState != null)
528+
{
529+
var placementTarget = PlacementTarget ?? this.FindLogicalAncestorOfType<IControl>();
530+
if (placementTarget == null)
531+
return;
532+
_openState.PopupHost.ConfigurePosition(
533+
placementTarget,
534+
PlacementMode,
535+
new Point(HorizontalOffset, VerticalOffset),
536+
PlacementAnchor,
537+
PlacementGravity,
538+
PlacementConstraintAdjustment,
539+
PlacementRect);
540+
}
541+
}
522542

523543
private static IDisposable SubscribeToEventHandler<T, TEventHandler>(T target, TEventHandler handler, Action<T, TEventHandler> subscribe, Action<T, TEventHandler> unsubscribe)
524544
{

0 commit comments

Comments
 (0)