-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
989 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
samples/Avalonia.Labs.Catalog/Converters/FlexDemoNumberToThicknessConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
using Avalonia.Data.Converters; | ||
|
||
namespace Avalonia.Labs.Catalog.Converters | ||
{ | ||
internal sealed class FlexDemoNumberToThicknessConverter : IValueConverter | ||
{ | ||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
if (value is int x && targetType.IsAssignableFrom(typeof(Thickness))) | ||
{ | ||
var y = 16 + 2 * ((x * 5) % 9); | ||
return new Thickness(2 * y, y); | ||
} | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => | ||
throw new NotSupportedException(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
samples/Avalonia.Labs.Catalog/ViewModels/FlexItemViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Reactive.Linq; | ||
|
||
using Avalonia.Labs.Panels; | ||
|
||
using ReactiveUI; | ||
|
||
namespace Avalonia.Labs.Catalog.ViewModels | ||
{ | ||
public sealed class FlexItemViewModel : ReactiveObject | ||
{ | ||
internal const AlignItems AlignSelfAuto = (AlignItems)(-1); | ||
|
||
private readonly ObservableAsPropertyHelper<AlignItems?> _alignSelf; | ||
|
||
private bool _isSelected; | ||
private bool _isVisible = true; | ||
|
||
private AlignItems _alignSelfItem = AlignSelfAuto; | ||
private int _order; | ||
|
||
public FlexItemViewModel(int value) | ||
{ | ||
Value = value; | ||
|
||
_alignSelf = (from item in this.WhenAnyValue(vm => vm.AlignSelfItem) | ||
select item == AlignSelfAuto ? default(AlignItems?) : item).ToProperty(this, nameof(AlignSelf)); | ||
} | ||
|
||
public int Value { get; } | ||
|
||
public bool IsSelected | ||
{ | ||
get => _isSelected; | ||
set => this.RaiseAndSetIfChanged(ref _isSelected, value); | ||
} | ||
|
||
public bool IsVisible | ||
{ | ||
get => _isVisible; | ||
set => this.RaiseAndSetIfChanged(ref _isVisible, value); | ||
} | ||
|
||
public AlignItems AlignSelfItem | ||
{ | ||
get => _alignSelfItem; | ||
set => this.RaiseAndSetIfChanged(ref _alignSelfItem, value); | ||
} | ||
|
||
public AlignItems? AlignSelf => _alignSelf.Value; | ||
|
||
public int Order | ||
{ | ||
get => _order; | ||
set => this.RaiseAndSetIfChanged(ref _order, value); | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
samples/Avalonia.Labs.Catalog/ViewModels/FlexViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using System.Windows.Input; | ||
|
||
using Avalonia.Labs.Catalog.Views; | ||
using Avalonia.Labs.Panels; | ||
|
||
using ReactiveUI; | ||
|
||
namespace Avalonia.Labs.Catalog.ViewModels | ||
{ | ||
public sealed class FlexViewModel : ViewModelBase | ||
{ | ||
private readonly ObservableCollection<FlexItemViewModel> _numbers; | ||
|
||
private FlexDirection _direction = FlexDirection.Row; | ||
private JustifyContent _justifyContent = JustifyContent.FlexStart; | ||
private AlignItems _alignItems = AlignItems.FlexStart; | ||
private AlignContent _alignContent = AlignContent.FlexStart; | ||
private FlexWrap _wrap = FlexWrap.Wrap; | ||
|
||
private int _columnSpacing = 8; | ||
private int _rowSpacing = 32; | ||
|
||
private int _currentNumber = 41; | ||
|
||
private FlexItemViewModel? _selectedItem; | ||
|
||
static FlexViewModel() | ||
{ | ||
ViewLocator.Register(typeof(FlexViewModel), () => new FlexView()); | ||
} | ||
|
||
public FlexViewModel() | ||
{ | ||
Title = "Flex View"; | ||
|
||
_numbers = new ObservableCollection<FlexItemViewModel>(Enumerable.Range(1, 40).Select(x => new FlexItemViewModel(x))); | ||
|
||
Numbers = new ReadOnlyObservableCollection<FlexItemViewModel>(_numbers); | ||
|
||
AddItemCommand = ReactiveCommand.Create(AddItem); | ||
RemoveItemCommand = ReactiveCommand.Create(RemoveItem, this.WhenAnyValue(vm => vm.SelectedItem).Select(x => x != null)); | ||
} | ||
|
||
public IEnumerable DirectionValues { get; } = Enum.GetValues(typeof(FlexDirection)); | ||
|
||
public IEnumerable JustifyContentValues { get; } = Enum.GetValues(typeof(JustifyContent)); | ||
|
||
public IEnumerable AlignItemsValues { get; } = Enum.GetValues(typeof(AlignItems)); | ||
|
||
public IEnumerable AlignContentValues { get; } = Enum.GetValues(typeof(AlignContent)); | ||
|
||
public IEnumerable WrapValues { get; } = Enum.GetValues(typeof(FlexWrap)); | ||
|
||
public IEnumerable AlignSelfValues { get; } = Enum.GetValues(typeof(AlignItems)).Cast<AlignItems>().Prepend(FlexItemViewModel.AlignSelfAuto); | ||
|
||
public FlexDirection Direction | ||
{ | ||
get => _direction; | ||
set => this.RaiseAndSetIfChanged(ref _direction, value); | ||
} | ||
|
||
public JustifyContent JustifyContent | ||
{ | ||
get => _justifyContent; | ||
set => this.RaiseAndSetIfChanged(ref _justifyContent, value); | ||
} | ||
|
||
public AlignItems AlignItems | ||
{ | ||
get => _alignItems; | ||
set => this.RaiseAndSetIfChanged(ref _alignItems, value); | ||
} | ||
|
||
public AlignContent AlignContent | ||
{ | ||
get => _alignContent; | ||
set => this.RaiseAndSetIfChanged(ref _alignContent, value); | ||
} | ||
|
||
public FlexWrap Wrap | ||
{ | ||
get => _wrap; | ||
set => this.RaiseAndSetIfChanged(ref _wrap, value); | ||
} | ||
|
||
public int ColumnSpacing | ||
{ | ||
get => _columnSpacing; | ||
set => this.RaiseAndSetIfChanged(ref _columnSpacing, value); | ||
} | ||
|
||
public int RowSpacing | ||
{ | ||
get => _rowSpacing; | ||
set => this.RaiseAndSetIfChanged(ref _rowSpacing, value); | ||
} | ||
|
||
public ReadOnlyObservableCollection<FlexItemViewModel> Numbers { get; } | ||
|
||
public FlexItemViewModel? SelectedItem | ||
{ | ||
get => _selectedItem; | ||
set => this.RaiseAndSetIfChanged(ref _selectedItem, value); | ||
} | ||
|
||
public ICommand AddItemCommand { get; } | ||
|
||
public ICommand RemoveItemCommand { get; } | ||
|
||
private void AddItem() => _numbers.Add(new FlexItemViewModel(_currentNumber++)); | ||
|
||
private void RemoveItem() | ||
{ | ||
if (SelectedItem is null) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
_numbers.Remove(SelectedItem); | ||
|
||
SelectedItem.IsSelected = false; | ||
SelectedItem = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.