Skip to content

Commit

Permalink
Add ability to inspect XAML markup for gallery pages (#204)
Browse files Browse the repository at this point in the history
* Added ability to see Xaml for gallery page
  • Loading branch information
tomlm authored Dec 13, 2024
1 parent 09e3e08 commit 2b741da
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/Consolonia.Gallery/View/ControlsListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@
</Window.Resources>
<DockPanel>
<Grid RowDefinitions="* Auto" DockPanel.Dock="Left">
<DataGrid x:Name="Grid" Grid.Row="0"
<DataGrid x:Name="GalleryGrid" Grid.Row="0"
SelectedIndex="0"
SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="1" RowDefinitions="Auto Auto" ColumnDefinitions="Auto *">
<Grid Grid.Row="1" RowDefinitions="Auto Auto" ColumnDefinitions="Auto *" Margin="0 0 0 1">
<Label Content="Theme" />
<ComboBox Grid.Column="1" x:Name="ThemeCombo" SelectionChanged="ComboBox_SelectionChanged" IsTabStop="false" HorizontalContentAlignment="Stretch">
<ComboBox Grid.Column="1" x:Name="ThemeCombo" SelectionChanged="ComboBox_SelectionChanged" IsTabStop="false" HorizontalContentAlignment="Stretch" >
<ComboBoxItem Content="Material" IsSelected="True" />
<ComboBoxItem Content="Fluent"/>
<ComboBoxItem Content="TurboVision"/>
<ComboBoxItem Content="TurboVisionDark"/>
<ComboBoxItem Content="TurboVisionBlack"/>
</ComboBox>
<Button Grid.Row="1" Grid.ColumnSpan="2" Click="Exit_Click" IsTabStop="False">Exit</Button>
</Grid>

</Grid>
<Border Child="{Binding ElementName=Grid,Path=SelectedItem, Converter={StaticResource GalleryItemConverter}}"
BorderThickness="1"
BorderBrush="{DynamicResource ThemeBorderBrush}" />
<Grid RowDefinitions="* Auto">
<Border Child="{Binding ElementName=GalleryGrid,Path=SelectedItem, Converter={StaticResource GalleryItemConverter}}"
BorderThickness="1"
BorderBrush="{DynamicResource ThemeBorderBrush}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="1">
<Button Click="OnShowXaml" IsTabStop="False" HorizontalAlignment="Right">Show XAML</Button>
<Button Click="Exit_Click" IsTabStop="False">Exit</Button>
</StackPanel>
</Grid>
</DockPanel>
</Window>
23 changes: 19 additions & 4 deletions src/Consolonia.Gallery/View/ControlsListView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
Expand Down Expand Up @@ -33,7 +34,7 @@ public ControlsListView()
#if DEBUG
this.AttachDevTools();
#endif
this.Grid.ItemsSource = _items = GalleryItem.Enumerated.ToArray();
this.GalleryGrid.ItemsSource = _items = GalleryItem.Enumerated.ToArray();

var lifetime = Application.Current!.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
if (lifetime != null)
Expand All @@ -52,7 +53,7 @@ private void TrySetupSelected()
{
if (_commandLineArgs.Length is not 1 and not 2)
{
this.Grid.SelectedIndex = 0;
this.GalleryGrid.SelectedIndex = 0;
return;
}

Expand All @@ -72,8 +73,8 @@ private void TrySetupSelected()
$"Several gallery items found with provided name {itemToSelectName}");
}

this.Grid.SelectedItem = itemToSelect;
this.Grid.Focus();
this.GalleryGrid.SelectedItem = itemToSelect;
this.GalleryGrid.Focus();
}


Expand All @@ -88,6 +89,20 @@ private void Exit_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
this.Close();
}

private async void OnShowXaml(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var lifetime = (IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime;

var selectedItem = GalleryGrid.SelectedItem as GalleryItem;
var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "Gallery", "GalleryViews", $"{selectedItem.Type.Name}.axaml"));
var dialog = new XamlDialogWindow()
{
DataContext = File.ReadAllText(path)
};

await dialog.ShowDialogAsync(lifetime.MainWindow);
}

private void ComboBox_SelectionChanged(object sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if (ThemeCombo?.SelectedItem is not ComboBoxItem selectedItem ||
Expand Down
15 changes: 15 additions & 0 deletions src/Consolonia.Gallery/View/XamlDialogWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<controls:DialogWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:controls="clr-namespace:Consolonia.Core.Controls;assembly=Consolonia.Core"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Class="Consolonia.Gallery.View.XamlDialogWindow">
<ScrollViewer HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
Focusable="True">
<SelectableTextBlock Name="Xaml" Text="{Binding}" />
</ScrollViewer>
</controls:DialogWindow>
35 changes: 35 additions & 0 deletions src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using Avalonia;
using Avalonia.Input;
using Consolonia.Core.Controls;

namespace Consolonia.Gallery.View
{
public partial class XamlDialogWindow : DialogWindow
{
public XamlDialogWindow()
{
InitializeComponent();

AttachedToVisualTree += DialogWindowAttachedToVisualTree;
}

private void DialogWindowAttachedToVisualTree(object sender, Avalonia.VisualTreeAttachmentEventArgs e)
{
AttachedToVisualTree -= DialogWindowAttachedToVisualTree;

var child = this.LogicalChildren.FirstOrDefault();
if (child is InputElement input)
input.AttachedToVisualTree += OnChildAttachedToVisualTree;
}

private void OnChildAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
if (sender is InputElement input)
{
input.AttachedToVisualTree -= OnChildAttachedToVisualTree;
input.Focus();
}
}
}
}
2 changes: 1 addition & 1 deletion src/Tests/Consolonia.Gallery.Tests/ListBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task PerformSingleTest()
await UITest.KeyInput(Key.Tab, RawInputModifiers.Shift);
await UITest.KeyInput(Key.Tab, RawInputModifiers.Shift);
await UITest.KeyInput(Key.PageDown);
await UITest.AssertHasText("Item 53");
await UITest.AssertHasText("Item 49");
}
}
}

0 comments on commit 2b741da

Please sign in to comment.