Skip to content

Commit

Permalink
Fancy server info link buttons (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Visne authored Jan 3, 2024
1 parent d52953b commit 8501ac9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 24 deletions.
48 changes: 25 additions & 23 deletions SS14.Launcher/Views/MainWindowTabs/ServerEntryView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,33 @@

<Control DockPanel.Dock="Top" Height="8" />

<Grid DockPanel.Dock="Right" VerticalAlignment="Bottom" RowDefinitions="Auto,Auto">
<!-- Favorite add/remove button -->
<Button Grid.Row="0" HorizontalAlignment="Right"
Content="{Binding FavoriteButtonText}"
Command="{Binding FavoriteButtonPressed}" />
<views:RowSideAlignedPanel>
<ItemsControl VerticalAlignment="Bottom" ItemsSource="{Binding CacheData.Links}" Name="Links">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:ServerInfoLink">
<views:ServerInfoLinkControl DataContext="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

<!-- Raise to top button. -->
<Button Grid.Row="1" HorizontalAlignment="Right"
IsVisible="{Binding ViewedInFavoritesPane}" Classes="OpenRight"
Content="Raise to top"
Command="{Binding FavoriteRaiseButtonPressed}" />
</Grid>
<DockPanel Dock="Right">
<!-- Favorite add/remove button -->
<Button Classes="OpenRight"
Content="{Binding FavoriteButtonText}"
Command="{Binding FavoriteButtonPressed}" />

<ItemsControl VerticalAlignment="Bottom" ItemsSource="{Binding CacheData.Links}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:ServerInfoLink">
<views:ServerInfoLinkControl DataContext="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- Raise to top button. -->
<Button IsVisible="{Binding ViewedInFavoritesPane}"
Classes="OpenLeft"
Content="Raise to top"
Command="{Binding FavoriteRaiseButtonPressed}" />
</DockPanel>
</views:RowSideAlignedPanel>
</DockPanel>
</Expander>
</Panel>
Expand Down
32 changes: 31 additions & 1 deletion SS14.Launcher/Views/MainWindowTabs/ServerEntryView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Controls.Presenters;
using Avalonia.LogicalTree;
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace SS14.Launcher.Views.MainWindowTabs;
Expand All @@ -10,6 +13,33 @@ public partial class ServerEntryView : UserControl
public ServerEntryView()
{
InitializeComponent();

Links.LayoutUpdated += ApplyStyle;
}


// Sets the style for the link buttons correctly so that they look correct
private void ApplyStyle(object? _1, EventArgs _2)
{
for (var i = 0; i < Links.ItemCount; i++)
{
if (Links.ContainerFromIndex(i) is not ContentPresenter { Child: ServerInfoLinkControl control } presenter)
continue;

presenter.ApplyTemplate();

if (Links.ItemCount == 1)
return;

var style = i switch
{
0 => "OpenRight",
_ when i == Links.ItemCount - 1 => "OpenLeft",
_ => "OpenBoth",
};

control.GetLogicalChildren().OfType<Button>().FirstOrDefault()?.Classes.Add(style);
}
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
Expand Down
63 changes: 63 additions & 0 deletions SS14.Launcher/Views/RowSideAlignedPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using Avalonia;
using Avalonia.Controls;

namespace SS14.Launcher.Views;

public sealed class RowSideAlignedPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
if (Children.Count < 2)
return base.MeasureOverride(availableSize);

var left = Children[0];
var right = Children[1];

left.Measure(availableSize);
right.Measure(availableSize);

var leftSize = left.DesiredSize;
var rightSize = right.DesiredSize;

if (leftSize.Width + rightSize.Width <= availableSize.Width)
{
// They both fit on one row, easy.
return new Size(leftSize.Width + rightSize.Width, Math.Max(leftSize.Height, rightSize.Height));
}
else
{
// They don't fit on the same row, make two rows.
return new Size(Math.Max(leftSize.Width, rightSize.Width), leftSize.Height + rightSize.Height);
}
}

protected override Size ArrangeOverride(Size finalSize)
{
if (Children.Count < 2)
return base.MeasureOverride(finalSize);

var left = Children[0];
var right = Children[1];

var leftSize = left.DesiredSize;
var rightSize = right.DesiredSize;

if (leftSize.Width + rightSize.Width <= finalSize.Width)
{
// They both fit on one row, easy.
left.Arrange(new Rect(0, 0, leftSize.Width, finalSize.Height));
right.Arrange(new Rect(finalSize.Width - rightSize.Width, 0, rightSize.Width, finalSize.Height));

return finalSize;
}
else
{
// They don't fit on the same row, make two rows.
left.Arrange(new Rect(0, 0, leftSize.Width, leftSize.Height));
right.Arrange(new Rect(finalSize.Width - rightSize.Width, leftSize.Height, rightSize.Width, finalSize.Height - leftSize.Height));

return finalSize;
}
}
}

0 comments on commit 8501ac9

Please sign in to comment.