This commit is contained in:
Wiesław Šoltés 2022-01-29 13:11:45 +01:00
Родитель 115e763809
Коммит 9864875485
6 изменённых файлов: 226 добавлений и 31 удалений

Просмотреть файл

@ -0,0 +1,158 @@
<Panel xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Panel.Styles>
<Style Selector="ExperimentalAcrylicBorder">
<Setter Property="CornerRadius" Value="5" />
<Setter Property="MaxWidth" Value="660" />
</Style>
<Style Selector="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Foreground" Value="Black" />
</Style>
<Style Selector="Slider">
<Setter Property="Margin" Value="8,0" />
<Setter Property="Minimum" Value="0" />
<Setter Property="Maximum" Value="1" />
<Setter Property="LargeChange" Value="0.2" />
<Setter Property="SmallChange" Value="0.1" />
</Style>
</Panel.Styles>
<StackPanel Spacing="20">
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger" TintColor="White" />
</ExperimentalAcrylicBorder.Material>
<Grid Margin="20,10"
ColumnDefinitions="Auto,*,Auto"
RowDefinitions="Auto,Auto">
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="TintOpacity" />
<Slider Name="TintOpacitySlider"
Grid.Row="0"
Grid.Column="1"
Value="0.9" />
<TextBlock Grid.Row="0"
Grid.Column="2"
Text="{Binding #TintOpacitySlider.Value, StringFormat=\{0:0.#\}}" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="MaterialOpacity" />
<Slider Name="MaterialOpacitySlider"
Grid.Row="1"
Grid.Column="1"
Value="0.8" />
<TextBlock Grid.Row="1"
Grid.Column="2"
Text="{Binding #MaterialOpacitySlider.Value, StringFormat=\{0:0.#\}}" />
</Grid>
</ExperimentalAcrylicBorder>
<UniformGrid x:Name="BordersGrid"
HorizontalAlignment="Stretch"
MaxWidth="660"
Columns="3">
<UniformGrid.Styles>
<Style Selector="ExperimentalAcrylicBorder">
<Setter Property="Height" Value="{Binding $self.Bounds.Width}" />
<Setter Property="Margin" Value="10" />
<Setter Property="MaxWidth" Value="200" />
</Style>
</UniformGrid.Styles>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#FF0000"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#00FF00"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#000000"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#0000FF"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#FFFF00"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#000000"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="White"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="#3c3c3c"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder>
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="White"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
</UniformGrid>
<ExperimentalAcrylicBorder Width="{Binding #BordersGrid.Bounds.Width}"
Height="160">
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial BackgroundSource="Digger"
MaterialOpacity="{Binding #MaterialOpacitySlider.Value}"
TintColor="Red"
TintOpacity="{Binding #TintOpacitySlider.Value}" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
</StackPanel>
</Panel>

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -2,6 +2,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Input;
using Avalonia.Controls;
using ReactiveUI;
@ -13,14 +14,15 @@ namespace XamlToy.ViewModels
{
private ObservableCollection<SampleViewModel> _samples;
private SampleViewModel? _selectedSample;
private string? _xaml;
private IDisposable? _selectedSampleXamlDisposable;
private IControl? _control;
private bool _enableAutoRun;
public MainViewModel()
{
_samples = GetSamples();
_samples = GetSamples(".xml");
_selectedSample = _samples.FirstOrDefault();
_xaml = _selectedSample?.Xaml;
_enableAutoRun = true;
RunCommand = ReactiveCommand.Create(Run);
@ -28,9 +30,19 @@ namespace XamlToy.ViewModels
.WhereNotNull()
.Subscribe(x =>
{
Xaml = x.Xaml;
AutoRun(x, _enableAutoRun);
Control = null;
});
this.WhenAnyValue(x => x.EnableAutoRun)
.DistinctUntilChanged()
.Subscribe(x =>
{
if (_selectedSample is { })
{
AutoRun(_selectedSample, x);
}
});
}
public ObservableCollection<SampleViewModel> Samples
@ -45,21 +57,21 @@ namespace XamlToy.ViewModels
set => this.RaiseAndSetIfChanged(ref _selectedSample, value);
}
public string? Xaml
{
get => _xaml;
set => this.RaiseAndSetIfChanged(ref _xaml, value);
}
public IControl? Control
{
get => _control;
set => this.RaiseAndSetIfChanged(ref _control, value);
}
public bool EnableAutoRun
{
get => _enableAutoRun;
set => this.RaiseAndSetIfChanged(ref _enableAutoRun, value);
}
public ICommand RunCommand { get; }
private string? GetSampleName(string resourceName)
private static string? GetSampleName(string resourceName)
{
var parts = resourceName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2)
@ -70,7 +82,7 @@ namespace XamlToy.ViewModels
return null;
}
private string? LoadResourceString(string name)
private static string? LoadResourceString(string name)
{
var assembly = typeof(MainViewModel).Assembly;
using var stream = assembly.GetManifestResourceStream(name);
@ -82,9 +94,8 @@ namespace XamlToy.ViewModels
return reader.ReadToEnd();
}
private ObservableCollection<SampleViewModel> GetSamples()
private static ObservableCollection<SampleViewModel> GetSamples(string sampleExtension)
{
var sampleExtension = ".txt";
var samples = new ObservableCollection<SampleViewModel>();
var assembly = typeof(MainViewModel).Assembly;
var resourceNames = assembly.GetManifestResourceNames();
@ -108,14 +119,29 @@ namespace XamlToy.ViewModels
return samples;
}
private void AutoRun(SampleViewModel sample, bool enableAutoRun)
{
_selectedSampleXamlDisposable?.Dispose();
if (enableAutoRun)
{
_selectedSampleXamlDisposable = sample.WhenAnyValue(s => s.Xaml)
.WhereNotNull()
.Subscribe(_ => Run());
}
}
private void Run()
{
try
{
var control = AvaloniaRuntimeXamlLoader.Parse<IControl?>(_xaml);
if (control is { })
if (_selectedSample?.Xaml is { } xaml)
{
Control = control;
var control = AvaloniaRuntimeXamlLoader.Parse<IControl?>(xaml);
if (control is { })
{
Control = control;
}
}
}
catch (Exception e)

Просмотреть файл

@ -48,28 +48,39 @@
ShowLineNumbers="True"
FontWeight="Normal"
FontSize="14"
WordWrap="False">
WordWrap="False"
DataContext="{Binding SelectedSample}">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding Xaml, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</ae:TextEditor>
<Button Grid.Column="0"
Grid.Row="1"
Background="#2fa6e2"
Foreground="White"
Content="Run"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="0,10,25,0"
Command="{Binding RunCommand}" />
<DockPanel Grid.Column="0"
Grid.Row="1"
HorizontalAlignment="Right"
VerticalAlignment="Top"
LastChildFill="False">
<CheckBox DockPanel.Dock="Left"
Margin="0,10,10,0"
Content="Auto"
IsChecked="{Binding EnableAutoRun}" />
<Button DockPanel.Dock="Left"
Margin="0,10,25,0"
Background="#2fa6e2"
Foreground="White"
Content="Run"
Command="{Binding RunCommand}" />
</DockPanel>
<GridSplitter Grid.Column="1"
Grid.Row="1"
Width="6"
ResizeBehavior="PreviousAndNext"
ResizeDirection="Columns"
Background="Transparent"/>
<ContentControl Grid.Column="2"
Grid.Row="1"
Content="{Binding Control}" />
<ScrollViewer Grid.Column="2"
Grid.Row="1"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<ContentControl Content="{Binding Control}" />
</ScrollViewer>
</Grid>
</UserControl>

Просмотреть файл

@ -20,6 +20,6 @@
<PackageReference Include="Avalonia.AvaloniaEdit" Version="$(AvaloniaEditVersion)" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Samples\*.txt" />
<EmbeddedResource Include="Samples\*.xml" />
</ItemGroup>
</Project>