Skip to content

Commit

Permalink
Add ElementGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Jan 2, 2025
1 parent a2d6e9b commit f0adbe0
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Install-Package DevWinUI
## 🔥 DevWinUI.Controls 🔥
### ⚡ What’s Inside? ⚡

- ✨ ElementGroup
- ✨ CompareSlider
- ✨ TransitioningContentControl
- ✨ DateTimePicker
Expand Down Expand Up @@ -257,6 +258,9 @@ Install-Package DevWinUI.ContextMenu

## 🕰️ History 🕰️

### ElementGroup
![DevWinUI](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/ElementGroup.gif)

### Hatch
![DevWinUI](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/Hatch.gif)

Expand Down
164 changes: 164 additions & 0 deletions dev/DevWinUI.Controls/Controls/Others/ElementGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using Microsoft.UI.Xaml.Markup;

namespace DevWinUI;
public partial class ElementGroup : ItemsControl
{
private Dictionary<Control, long> _visibilityCallbacks = new();

public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register(nameof(Orientation), typeof(Orientation), typeof(ElementGroup), new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged));
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (ElementGroup)d;
if (ctl != null)
{
ctl.UpdateOrientation();
}
}

private void UpdateOrientation()
{
if (Orientation == Orientation.Horizontal)
{
ItemsPanel = (ItemsPanelTemplate)XamlReader.Load("<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><StackPanel Orientation='Horizontal'/></ItemsPanelTemplate>");
}
else
{
ItemsPanel = (ItemsPanelTemplate)XamlReader.Load("<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><StackPanel Orientation='Vertical'/></ItemsPanelTemplate>");
}
UpdateElements();
}

public ElementGroup()
{
UpdateOrientation();
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

UpdateElements();
Loaded -= OnLoaded;
Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
UpdateVisibilityEventHandlers();
UpdateElements();
}

protected override void OnItemsChanged(object e)
{
base.OnItemsChanged(e);

UpdateVisibilityEventHandlers();
UpdateElements();
}
private void UpdateVisibilityEventHandlers()
{
if (ItemsPanelRoot is Panel panel)
{
foreach (var entry in _visibilityCallbacks)
{
entry.Key.UnregisterPropertyChangedCallback(VisibilityProperty, entry.Value);
}
_visibilityCallbacks.Clear();

foreach (var child in panel.Children)
{
if (child is Control element)
{
long token = element.RegisterPropertyChangedCallback(VisibilityProperty, OnChildVisibilityChanged);
_visibilityCallbacks[element] = token;
}
}
}
}
private void OnChildVisibilityChanged(DependencyObject sender, DependencyProperty dp)
{
if (dp == VisibilityProperty)
{
UpdateElements();
}
}
private void UpdateElements()
{
if (ItemsPanelRoot is Panel panel)
{
var visibleChildren = panel.Children
.OfType<Control>()
.Where(c => c.Visibility == Visibility.Visible)
.ToList();

int totalItems = visibleChildren.Count;

if (totalItems == 1)
{
if (visibleChildren[0] is Control element)
{
element.CornerRadius = new CornerRadius(4);
element.BorderThickness = new Thickness(1);
}
return;
}

for (int i = 0; i < totalItems; i++)
{
if (visibleChildren[i] is Control element)
{
if (Orientation == Orientation.Horizontal)
{
SetStackPanelHorizontalItems(i, totalItems, element);
}
else
{
SetStackPanelVerticalItems(i, totalItems, element);
}
}
}
}
}

private void SetStackPanelVerticalItems(int index, int totalItems, Control element)
{
if (index == 0)
{
element.CornerRadius = new CornerRadius(4, 4, 0, 0);
element.BorderThickness = new Thickness(1, 1, 1, 0);
}
else if (index == totalItems - 1)
{
element.CornerRadius = new CornerRadius(0, 0, 4, 4);
element.BorderThickness = new Thickness(1, 1, 1, 1);
}
else
{
element.CornerRadius = new CornerRadius(0);
element.BorderThickness = new Thickness(1, 1, 1, 0);
}
}
private void SetStackPanelHorizontalItems(int index, int totalItems, Control element)
{
if (index == 0)
{
element.CornerRadius = new CornerRadius(4, 0, 0, 4);
element.BorderThickness = new Thickness(1, 1, 0, 1);
}
else if (index == totalItems - 1)
{
element.CornerRadius = new CornerRadius(0, 4, 4, 0);
element.BorderThickness = new Thickness(1, 1, 1, 1);
}
else
{
element.CornerRadius = new CornerRadius(0);
element.BorderThickness = new Thickness(1, 1, 0, 1);
}
}
}
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions dev/DevWinUI.Gallery/Assets/NavViewMenu/AppData.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
"ImagePath": "ms-appx:///Assets/Fluent/RatingControl.png",
"IsSpecialSection": false,
"Items": [
{
"UniqueId": "DevWinUIGallery.Views.ElementGroupPage",
"Title": "ElementGroup",
"Subtitle": "Group any Element",
"IsNew": true,
"ImagePath": "ms-appx:///Assets/Fluent/VariableSizedWrapGrid.png"
},
{
"UniqueId": "DevWinUIGallery.Views.CompareSliderPage",
"Title": "CompareSlider",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<dev:ElementGroup>
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
</dev:ElementGroup>
1 change: 1 addition & 0 deletions dev/DevWinUI.Gallery/T4Templates/NavigationPageMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class NavigationPageMappings
{"DevWinUIGallery.Views.WaveCirclePage", typeof(DevWinUIGallery.Views.WaveCirclePage)},
{"DevWinUIGallery.Views.BubblePage", typeof(DevWinUIGallery.Views.BubblePage)},
{"DevWinUIGallery.Views.GooeyPage", typeof(DevWinUIGallery.Views.GooeyPage)},
{"DevWinUIGallery.Views.ElementGroupPage", typeof(DevWinUIGallery.Views.ElementGroupPage)},
{"DevWinUIGallery.Views.CompareSliderPage", typeof(DevWinUIGallery.Views.CompareSliderPage)},
{"DevWinUIGallery.Views.TransitioningContentControlPage", typeof(DevWinUIGallery.Views.TransitioningContentControlPage)},
{"DevWinUIGallery.Views.CirclePanelPage", typeof(DevWinUIGallery.Views.CirclePanelPage)},
Expand Down
69 changes: 69 additions & 0 deletions dev/DevWinUI.Gallery/Views/Pages/Features/ElementGroupPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8" ?>
<Page x:Class="DevWinUIGallery.Views.ElementGroupPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dev="using:DevWinUI"
xmlns:local="using:DevWinUIGallery"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<ScrollViewer>
<StackPanel Margin="10"
dev:PanelAttach.ChildrenTransitions="Default"
Spacing="10">
<local:ControlExample DocPage="controls/elementGroup"
XamlSource="Features/ElementGroup_xaml.txt">
<local:ControlExample.Pane>
<ComboBox x:Name="CmbOrientation"
Header="Orientation"
SelectedIndex="0"
SelectionChanged="CmbOrientation_SelectionChanged">
<ComboBoxItem Content="Horizontal"
Tag="Horizontal" />
<ComboBoxItem Content="Vertical"
Tag="Vertical" />
</ComboBox>
</local:ControlExample.Pane>
<StackPanel Spacing="10">
<dev:ElementGroup x:Name="ElementGroupSample">
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
</dev:ElementGroup>

<dev:ElementGroup x:Name="ElementGroupSample2">
<Button Height="32"
HorizontalAlignment="Stretch">
<SymbolIcon Symbol="Clock" />
</Button>
<TextBox Height="30"
HorizontalAlignment="Stretch"
BorderBrush="{ThemeResource ControlElevationBorderBrush}"
PlaceholderText="TextBox" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
</dev:ElementGroup>

<dev:ElementGroup x:Name="ElementGroupSample3">
<TextBox Height="30"
HorizontalAlignment="Stretch"
BorderBrush="{ThemeResource ControlElevationBorderBrush}"
PlaceholderText="TextBox" />
<Button HorizontalAlignment="Stretch"
Content="Button" />
<ToggleButton HorizontalAlignment="Stretch"
Content="ToggleButton" />
</dev:ElementGroup>
</StackPanel>
</local:ControlExample>
</StackPanel>
</ScrollViewer>
</Page>
21 changes: 21 additions & 0 deletions dev/DevWinUI.Gallery/Views/Pages/Features/ElementGroupPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DevWinUIGallery.Views;

public sealed partial class ElementGroupPage : Page
{
public ElementGroupPage()
{
this.InitializeComponent();
}

private void CmbOrientation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ElementGroupSample != null && ElementGroupSample2 != null && ElementGroupSample3 != null)
{
var item = CmbOrientation.SelectedItem as ComboBoxItem;
var orientation = GeneralHelper.GetEnum<Orientation>(item.Tag.ToString());
ElementGroupSample.Orientation = orientation;
ElementGroupSample2.Orientation = orientation;
ElementGroupSample3.Orientation = orientation;
}
}
}

0 comments on commit f0adbe0

Please sign in to comment.