diff --git a/samples/VirtualizationDemo/App.axaml b/samples/VirtualizationDemo/App.axaml new file mode 100644 index 0000000000..f5f06ffb6a --- /dev/null +++ b/samples/VirtualizationDemo/App.axaml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/samples/VirtualizationDemo/App.axaml.cs b/samples/VirtualizationDemo/App.axaml.cs new file mode 100644 index 0000000000..5ac5c9a92b --- /dev/null +++ b/samples/VirtualizationDemo/App.axaml.cs @@ -0,0 +1,20 @@ +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; + +namespace VirtualizationDemo; + +public partial class App : Application +{ + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + desktop.MainWindow = new MainWindow(); + base.OnFrameworkInitializationCompleted(); + } +} diff --git a/samples/VirtualizationDemo/App.xaml b/samples/VirtualizationDemo/App.xaml deleted file mode 100644 index eb5f0e4dca..0000000000 --- a/samples/VirtualizationDemo/App.xaml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/samples/VirtualizationDemo/App.xaml.cs b/samples/VirtualizationDemo/App.xaml.cs deleted file mode 100644 index 81b80c1f40..0000000000 --- a/samples/VirtualizationDemo/App.xaml.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Avalonia; -using Avalonia.Controls.ApplicationLifetimes; -using Avalonia.Markup.Xaml; - -namespace VirtualizationDemo -{ - public class App : Application - { - public override void Initialize() - { - AvaloniaXamlLoader.Load(this); - } - - public override void OnFrameworkInitializationCompleted() - { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - desktop.MainWindow = new MainWindow(); - base.OnFrameworkInitializationCompleted(); - } - } -} diff --git a/samples/VirtualizationDemo/Assets/chat.json b/samples/VirtualizationDemo/Assets/chat.json new file mode 100644 index 0000000000..cc628b534a --- /dev/null +++ b/samples/VirtualizationDemo/Assets/chat.json @@ -0,0 +1,190 @@ +{ + "chat": [ + { + "sender": "Alice", + "message": "Hey Bob! How was your weekend?", + "timestamp": "2023-04-01T10:00:00" + }, + { + "sender": "Bob", + "message": "It was great, thanks for asking. I went on a camping trip with some friends. How about you?", + "timestamp": "2023-04-01T10:01:00" + }, + { + "sender": "Alice", + "message": "My weekend was pretty chill. I just stayed home and caught up on some TV shows.", + "timestamp": "2023-04-01T10:03:00" + }, + { + "sender": "Bob", + "message": "That sounds relaxing. What shows did you watch?", + "timestamp": "2023-04-01T10:05:00" + }, + { + "sender": "Alice", + "message": "I watched the new season of 'Stranger Things' and started watching 'Ozark'. Have you seen them?", + "timestamp": "2023-04-01T10:07:00" + }, + { + "sender": "Bob", + "message": "Yeah, I've seen both of those. They're really good! What do you think of them so far?", + "timestamp": "2023-04-01T10:10:00" + }, + { + "sender": "Alice", + "message": "I'm really enjoying 'Stranger Things', but 'Ozark' is a bit darker than I expected. I'm only a few episodes in though, so we'll see how it goes.", + "timestamp": "2023-04-01T10:12:00" + }, + { + "sender": "Bob", + "message": "Yeah, 'Ozark' can be intense at times, but it's really well done. Keep watching, it gets even better.", + "timestamp": "2023-04-01T10:15:00" + }, + { + "sender": "Alice", + "message": "Thanks for the recommendation, I'll definitely keep watching. So, how's work been for you lately?", + "timestamp": "2023-04-01T10:20:00" + }, + { + "sender": "Bob", + "message": "It's been pretty busy, but I'm managing. How about you?", + "timestamp": "2023-04-01T10:22:00" + }, + { + "sender": "Alice", + "message": "Same here, things have been pretty hectic. But it keeps us on our toes, right?", + "timestamp": "2023-04-01T10:25:00" + }, + { + "sender": "Bob", + "message": "Absolutely. Hey, have you heard about the new project we're starting next week?", + "timestamp": "2023-04-01T10:30:00" + }, + { + "sender": "Alice", + "message": "No, I haven't. What's it about?", + "timestamp": "2023-04-01T10:32:00" + }, + { + "sender": "Bob", + "message": "It's a big project for a new client, and it's going to require a lot of extra hours from all of us. But the pay is going to be great,so it's definitely worth the extra effort. I'll fill you in on the details later, but for now, let's just enjoy our coffee break, shall we?", + "timestamp": "2023-04-01T10:35:00" + }, + { + "sender": "Alice", + "message": "Sounds good to me. I could use a break right about now.", + "timestamp": "2023-04-01T10:40:00" + }, + { + "sender": "Bob", + "message": "Me too. So, have you tried the new caf� down the street yet?", + "timestamp": "2023-04-01T10:45:00" + }, + { + "sender": "Alice", + "message": "No, I haven't. Is it any good?", + "timestamp": "2023-04-01T10:47:00" + }, + { + "sender": "Bob", + "message": "It's really good! They have the best croissants I've ever tasted.", + "timestamp": "2023-04-01T10:50:00" + }, + { + "sender": "Alice", + "message": "Hmm, I'll have to try it out sometime. Do they have any vegan options?", + "timestamp": "2023-04-01T10:52:00" + }, + { + "sender": "Bob", + "message": "I'm not sure, but I think they do. You should ask them the next time you go there.", + "timestamp": "2023-04-01T10:55:00" + }, + { + "sender": "Alice", + "message": "Thanks for the suggestion. I'm always looking for good vegan options around here.", + "timestamp": "2023-04-01T11:00:00" + }, + { + "sender": "Bob", + "message": "No problem. So, have you made any plans for the weekend yet?", + "timestamp": "2023-04-01T11:05:00" + }, + { + "sender": "Alice", + "message": "Not yet. I was thinking of maybe going for a hike or something. What about you?", + "timestamp": "2023-04-01T11:07:00" + }, + { + "sender": "Bob", + "message": "I haven't made any plans either. Maybe we could do something together?", + "timestamp": "2023-04-01T11:10:00" + }, + { + "sender": "Alice", + "message": "That sounds like a great idea! Let's plan on it.", + "timestamp": "2023-04-01T11:12:00" + }, + { + "sender": "Bob", + "message": "Awesome. I'll check out some hiking trails and let you know which ones look good.", + "timestamp": "2023-04-01T11:15:00" + }, + { + "sender": "Alice", + "message": "Sounds good. I can't wait!", + "timestamp": "2023-04-01T11:20:00" + }, + { + "sender": "John", + "message": "Hey Lisa, how was your day?", + "timestamp": "2023-04-01T18:00:00" + }, + { + "sender": "Lisa", + "message": "It was good, thanks for asking. How about you?", + "timestamp": "2023-04-01T18:05:00" + }, + { + "sender": "John", + "message": "Eh, it was alright. Work was pretty busy, but nothing too crazy.", + "timestamp": "2023-04-01T18:10:00" + }, + { + "sender": "Lisa", + "message": "Yeah, I know what you mean. My boss has been on my case lately about meeting our deadlines.", + "timestamp": "2023-04-01T18:15:00" + }, + { + "sender": "John", + "message": "That sucks. Are you feeling stressed out?", + "timestamp": "2023-04-01T18:20:00" + }, + { + "sender": "Lisa", + "message": "A little bit, yeah. But I'm trying to stay positive and focus on getting my work done.", + "timestamp": "2023-04-01T18:25:00" + }, + { + "sender": "John", + "message": "That's a good attitude to have. Have you tried doing some meditation or other relaxation techniques?", + "timestamp": "2023-04-01T18:30:00" + }, + { + "sender": "Lisa", + "message": "I haven't, but I've been thinking about it. Do you have any suggestions?", + "timestamp": "2023-04-01T18:35:00" + }, + { + "sender": "John", + "message": "Sure, I could send you some links to guided meditations that I've found helpful. And there are also some great apps out there that can help you with relaxation.", + "timestamp": "2023-04-01T18:40:00" + }, + { + "sender": "Lisa", + "message": "That would be awesome, thanks so much!", + "timestamp": "2023-04-01T18:45:00" + } + ] +} + diff --git a/samples/VirtualizationDemo/MainWindow.axaml b/samples/VirtualizationDemo/MainWindow.axaml new file mode 100644 index 0000000000..04e75450bf --- /dev/null +++ b/samples/VirtualizationDemo/MainWindow.axaml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + diff --git a/samples/VirtualizationDemo/MainWindow.axaml.cs b/samples/VirtualizationDemo/MainWindow.axaml.cs new file mode 100644 index 0000000000..533dc00aa1 --- /dev/null +++ b/samples/VirtualizationDemo/MainWindow.axaml.cs @@ -0,0 +1,15 @@ +using Avalonia; +using Avalonia.Controls; +using VirtualizationDemo.ViewModels; + +namespace VirtualizationDemo; + +public partial class MainWindow : Window +{ + public MainWindow() + { + InitializeComponent(); + this.AttachDevTools(); + DataContext = new MainWindowViewModel(); + } +} diff --git a/samples/VirtualizationDemo/MainWindow.xaml b/samples/VirtualizationDemo/MainWindow.xaml deleted file mode 100644 index 3aee63c246..0000000000 --- a/samples/VirtualizationDemo/MainWindow.xaml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - Horiz. ScrollBar - - Vert. ScrollBar - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/VirtualizationDemo/MainWindow.xaml.cs b/samples/VirtualizationDemo/MainWindow.xaml.cs deleted file mode 100644 index cea200dcec..0000000000 --- a/samples/VirtualizationDemo/MainWindow.xaml.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Avalonia; -using Avalonia.Controls; -using Avalonia.Markup.Xaml; -using VirtualizationDemo.ViewModels; - -namespace VirtualizationDemo -{ - public class MainWindow : Window - { - public MainWindow() - { - this.InitializeComponent(); - this.AttachDevTools(); - DataContext = new MainWindowViewModel(); - } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } - } -} diff --git a/samples/VirtualizationDemo/Models/Chat.cs b/samples/VirtualizationDemo/Models/Chat.cs new file mode 100644 index 0000000000..813e8650f5 --- /dev/null +++ b/samples/VirtualizationDemo/Models/Chat.cs @@ -0,0 +1,23 @@ +using System; +using System.IO; +using System.Text.Json; + +namespace VirtualizationDemo.Models; + +public class ChatFile +{ + public ChatMessage[]? Chat { get; set; } + + public static ChatFile Load(string path) + { + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + using var s = File.OpenRead(path); + return JsonSerializer.Deserialize(s, options)!; + } +} + +public record ChatMessage(string Sender, string Message, DateTimeOffset Timestamp); diff --git a/samples/VirtualizationDemo/Program.cs b/samples/VirtualizationDemo/Program.cs index febda46450..87212b6daa 100644 --- a/samples/VirtualizationDemo/Program.cs +++ b/samples/VirtualizationDemo/Program.cs @@ -1,15 +1,14 @@ using Avalonia; -namespace VirtualizationDemo +namespace VirtualizationDemo; + +class Program { - class Program - { - public static AppBuilder BuildAvaloniaApp() - => AppBuilder.Configure() - .UsePlatformDetect() - .LogToTrace(); + public static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .LogToTrace(); - public static int Main(string[] args) - => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); - } + public static int Main(string[] args) + => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); } diff --git a/samples/VirtualizationDemo/ViewModels/ChatPageViewModel.cs b/samples/VirtualizationDemo/ViewModels/ChatPageViewModel.cs new file mode 100644 index 0000000000..c0abe62bd5 --- /dev/null +++ b/samples/VirtualizationDemo/ViewModels/ChatPageViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.ObjectModel; +using System.IO; +using VirtualizationDemo.Models; + +namespace VirtualizationDemo.ViewModels; + +public class ChatPageViewModel +{ + public ChatPageViewModel() + { + var chat = ChatFile.Load(Path.Combine("Assets", "chat.json")); + Messages = new(chat.Chat ?? Array.Empty()); + } + + public ObservableCollection Messages { get; } +} diff --git a/samples/VirtualizationDemo/ViewModels/ExpanderItemViewModel.cs b/samples/VirtualizationDemo/ViewModels/ExpanderItemViewModel.cs new file mode 100644 index 0000000000..a17fc2d303 --- /dev/null +++ b/samples/VirtualizationDemo/ViewModels/ExpanderItemViewModel.cs @@ -0,0 +1,21 @@ +using MiniMvvm; + +namespace VirtualizationDemo.ViewModels; + +public class ExpanderItemViewModel : ViewModelBase +{ + private string? _header; + private bool _isExpanded; + + public string? Header + { + get => _header; + set => RaiseAndSetIfChanged(ref _header, value); + } + + public bool IsExpanded + { + get => _isExpanded; + set => RaiseAndSetIfChanged(ref _isExpanded, value); + } +} diff --git a/samples/VirtualizationDemo/ViewModels/ExpanderPageViewModel.cs b/samples/VirtualizationDemo/ViewModels/ExpanderPageViewModel.cs new file mode 100644 index 0000000000..f2807a803b --- /dev/null +++ b/samples/VirtualizationDemo/ViewModels/ExpanderPageViewModel.cs @@ -0,0 +1,17 @@ +using System.Collections.ObjectModel; +using System.Linq; + +namespace VirtualizationDemo.ViewModels; + +internal class ExpanderPageViewModel +{ + public ExpanderPageViewModel() + { + Items = new(Enumerable.Range(0, 100).Select(x => new ExpanderItemViewModel + { + Header = $"Item {x}", + })); + } + + public ObservableCollection Items { get; set; } +} diff --git a/samples/VirtualizationDemo/ViewModels/ItemViewModel.cs b/samples/VirtualizationDemo/ViewModels/ItemViewModel.cs deleted file mode 100644 index 9ba505ffe5..0000000000 --- a/samples/VirtualizationDemo/ViewModels/ItemViewModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using MiniMvvm; - -namespace VirtualizationDemo.ViewModels -{ - internal class ItemViewModel : ViewModelBase - { - private string _prefix; - private int _index; - private double _height = double.NaN; - - public ItemViewModel(int index, string prefix = "Item") - { - _prefix = prefix; - _index = index; - } - - public string Header => $"{_prefix} {_index}"; - - public double Height - { - get => _height; - set => this.RaiseAndSetIfChanged(ref _height, value); - } - } -} diff --git a/samples/VirtualizationDemo/ViewModels/MainWindowViewModel.cs b/samples/VirtualizationDemo/ViewModels/MainWindowViewModel.cs index 96dbbc1a83..478e40187e 100644 --- a/samples/VirtualizationDemo/ViewModels/MainWindowViewModel.cs +++ b/samples/VirtualizationDemo/ViewModels/MainWindowViewModel.cs @@ -1,160 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reactive; -using Avalonia.Collections; -using Avalonia.Controls; -using Avalonia.Controls.Primitives; -using Avalonia.Layout; -using Avalonia.Controls.Selection; -using MiniMvvm; +using MiniMvvm; -namespace VirtualizationDemo.ViewModels -{ - internal class MainWindowViewModel : ViewModelBase - { - private int _itemCount = 200; - private string _newItemString = "New Item"; - private int _newItemIndex; - private AvaloniaList _items; - private string _prefix = "Item"; - private ScrollBarVisibility _horizontalScrollBarVisibility = ScrollBarVisibility.Auto; - private ScrollBarVisibility _verticalScrollBarVisibility = ScrollBarVisibility.Auto; - private Orientation _orientation = Orientation.Vertical; - - public MainWindowViewModel() - { - this.WhenAnyValue(x => x.ItemCount).Subscribe(ResizeItems); - RecreateCommand = MiniCommand.Create(() => Recreate()); - - AddItemCommand = MiniCommand.Create(() => AddItem()); - - RemoveItemCommand = MiniCommand.Create(() => Remove()); - - SelectFirstCommand = MiniCommand.Create(() => SelectItem(0)); - - SelectLastCommand = MiniCommand.Create(() => SelectItem(Items.Count - 1)); - } - - public string NewItemString - { - get { return _newItemString; } - set { this.RaiseAndSetIfChanged(ref _newItemString, value); } - } - - public int ItemCount - { - get { return _itemCount; } - set { this.RaiseAndSetIfChanged(ref _itemCount, value); } - } - - public SelectionModel Selection { get; } = new SelectionModel(); - - public AvaloniaList Items - { - get { return _items; } - private set { this.RaiseAndSetIfChanged(ref _items, value); } - } - - public Orientation Orientation - { - get { return _orientation; } - set { this.RaiseAndSetIfChanged(ref _orientation, value); } - } - - public IEnumerable Orientations => - Enum.GetValues(typeof(Orientation)).Cast(); - - public ScrollBarVisibility HorizontalScrollBarVisibility - { - get { return _horizontalScrollBarVisibility; } - set { this.RaiseAndSetIfChanged(ref _horizontalScrollBarVisibility, value); } - } +namespace VirtualizationDemo.ViewModels; - public ScrollBarVisibility VerticalScrollBarVisibility - { - get { return _verticalScrollBarVisibility; } - set { this.RaiseAndSetIfChanged(ref _verticalScrollBarVisibility, value); } - } - - public IEnumerable ScrollBarVisibilities => - Enum.GetValues(typeof(ScrollBarVisibility)).Cast(); - - public MiniCommand AddItemCommand { get; private set; } - public MiniCommand RecreateCommand { get; private set; } - public MiniCommand RemoveItemCommand { get; private set; } - public MiniCommand SelectFirstCommand { get; private set; } - public MiniCommand SelectLastCommand { get; private set; } - - public void RandomizeSize() - { - var random = new Random(); - - foreach (var i in Items) - { - i.Height = random.Next(240) + 10; - } - } - - public void ResetSize() - { - foreach (var i in Items) - { - i.Height = double.NaN; - } - } - - private void ResizeItems(int count) - { - if (Items == null) - { - var items = Enumerable.Range(0, count) - .Select(x => new ItemViewModel(x)); - Items = new AvaloniaList(items); - } - else if (count > Items.Count) - { - var items = Enumerable.Range(Items.Count, count - Items.Count) - .Select(x => new ItemViewModel(x)); - Items.AddRange(items); - } - else if (count < Items.Count) - { - Items.RemoveRange(count, Items.Count - count); - } - } - - private void AddItem() - { - var index = Items.Count; - - if (Selection.SelectedItems.Count > 0) - { - index = Selection.SelectedIndex; - } - - Items.Insert(index, new ItemViewModel(_newItemIndex++, NewItemString)); - } - - private void Remove() - { - if (Selection.SelectedItems.Count > 0) - { - Items.RemoveAll(Selection.SelectedItems.ToList()); - } - } - - private void Recreate() - { - _prefix = _prefix == "Item" ? "Recreated" : "Item"; - var items = Enumerable.Range(0, _itemCount) - .Select(x => new ItemViewModel(x, _prefix)); - Items = new AvaloniaList(items); - } - - private void SelectItem(int index) - { - Selection.SelectedIndex = index; - } - } +internal class MainWindowViewModel : ViewModelBase +{ + public PlaygroundPageViewModel Playground { get; } = new(); + public ChatPageViewModel Chat { get; } = new(); + public ExpanderPageViewModel Expanders { get; } = new(); } diff --git a/samples/VirtualizationDemo/ViewModels/PlaygroundItemViewModel.cs b/samples/VirtualizationDemo/ViewModels/PlaygroundItemViewModel.cs new file mode 100644 index 0000000000..584ef4600b --- /dev/null +++ b/samples/VirtualizationDemo/ViewModels/PlaygroundItemViewModel.cs @@ -0,0 +1,17 @@ +using MiniMvvm; + +namespace VirtualizationDemo.ViewModels; + +public class PlaygroundItemViewModel : ViewModelBase +{ + private string? _header; + + public PlaygroundItemViewModel(int index) => Header = $"Item {index}"; + public PlaygroundItemViewModel(string? header) => Header = header; + + public string? Header + { + get => _header; + set => RaiseAndSetIfChanged(ref _header, value); + } +} diff --git a/samples/VirtualizationDemo/ViewModels/PlaygroundPageViewModel.cs b/samples/VirtualizationDemo/ViewModels/PlaygroundPageViewModel.cs new file mode 100644 index 0000000000..98ab91b0a6 --- /dev/null +++ b/samples/VirtualizationDemo/ViewModels/PlaygroundPageViewModel.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.ObjectModel; +using System.Linq; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Selection; +using MiniMvvm; + +namespace VirtualizationDemo.ViewModels; + +public class PlaygroundPageViewModel : ViewModelBase +{ + private SelectionMode _selectionMode = SelectionMode.Multiple; + private int _scrollToIndex = 500; + private string? _newItemHeader = "New Item 1"; + + public PlaygroundPageViewModel() + { + Items = new(Enumerable.Range(0, 1000).Select(x => new PlaygroundItemViewModel(x))); + Selection = new(); + } + + public ObservableCollection Items { get; } + + public bool Multiple + { + get => _selectionMode.HasAnyFlag(SelectionMode.Multiple); + set => SetSelectionMode(SelectionMode.Multiple, value); + } + + public bool Toggle + { + get => _selectionMode.HasAnyFlag(SelectionMode.Toggle); + set => SetSelectionMode(SelectionMode.Toggle, value); + } + + public bool AlwaysSelected + { + get => _selectionMode.HasAnyFlag(SelectionMode.AlwaysSelected); + set => SetSelectionMode(SelectionMode.AlwaysSelected, value); + } + + public SelectionModel Selection { get; } + + public SelectionMode SelectionMode + { + get => _selectionMode; + set => RaiseAndSetIfChanged(ref _selectionMode, value); + } + + public int ScrollToIndex + { + get => _scrollToIndex; + set => RaiseAndSetIfChanged(ref _scrollToIndex, value); + } + + public string? NewItemHeader + { + get => _newItemHeader; + set => RaiseAndSetIfChanged(ref _newItemHeader, value); + } + + public void ExecuteScrollToIndex() + { + Selection.Select(ScrollToIndex); + } + + public void RandomizeScrollToIndex() + { + var rnd = new Random(); + ScrollToIndex = rnd.Next(Items.Count); + } + + public void AddAtSelectedIndex() + { + if (Selection.SelectedIndex == -1) + return; + Items.Insert(Selection.SelectedIndex, new(NewItemHeader)); + } + + public void DeleteSelectedItem() + { + var count = Selection.Count; + for (var i = count - 1; i >= 0; i--) + Items.RemoveAt(Selection.SelectedIndexes[i]); + } + + private void SetSelectionMode(SelectionMode mode, bool value) + { + if (value) + SelectionMode |= mode; + else + SelectionMode &= ~mode; + } +} diff --git a/samples/VirtualizationDemo/Views/ChatPageView.axaml b/samples/VirtualizationDemo/Views/ChatPageView.axaml new file mode 100644 index 0000000000..fc182f15ae --- /dev/null +++ b/samples/VirtualizationDemo/Views/ChatPageView.axaml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/samples/VirtualizationDemo/Views/ChatPageView.axaml.cs b/samples/VirtualizationDemo/Views/ChatPageView.axaml.cs new file mode 100644 index 0000000000..b5c90db69c --- /dev/null +++ b/samples/VirtualizationDemo/Views/ChatPageView.axaml.cs @@ -0,0 +1,11 @@ +using Avalonia.Controls; + +namespace VirtualizationDemo.Views; + +public partial class ChatPageView : UserControl +{ + public ChatPageView() + { + InitializeComponent(); + } +} diff --git a/samples/VirtualizationDemo/Views/ExpanderPageView.axaml b/samples/VirtualizationDemo/Views/ExpanderPageView.axaml new file mode 100644 index 0000000000..972d885229 --- /dev/null +++ b/samples/VirtualizationDemo/Views/ExpanderPageView.axaml @@ -0,0 +1,18 @@ + + + + + + + + + + + diff --git a/samples/VirtualizationDemo/Views/ExpanderPageView.axaml.cs b/samples/VirtualizationDemo/Views/ExpanderPageView.axaml.cs new file mode 100644 index 0000000000..df3689cf24 --- /dev/null +++ b/samples/VirtualizationDemo/Views/ExpanderPageView.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace VirtualizationDemo.Views; + +public partial class ExpanderPageView : UserControl +{ + public ExpanderPageView() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml b/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml new file mode 100644 index 0000000000..52bc6fd27a --- /dev/null +++ b/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml @@ -0,0 +1,66 @@ + + + + + + + + Multiple + Toggle + AlwaysSelected + AutoScrollToSelectedItem + WrapSelection + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml.cs b/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml.cs new file mode 100644 index 0000000000..5282475778 --- /dev/null +++ b/samples/VirtualizationDemo/Views/PlaygroundPageView.axaml.cs @@ -0,0 +1,44 @@ +using System; +using System.Linq; +using System.Threading; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Avalonia.Threading; + +namespace VirtualizationDemo.Views; + +public partial class PlaygroundPageView : UserControl +{ + private DispatcherTimer _timer; + + public PlaygroundPageView() + { + InitializeComponent(); + + _timer = new DispatcherTimer + { + Interval = TimeSpan.FromMilliseconds(500), + }; + + _timer.Tick += TimerTick; + } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + _timer.Start(); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + _timer.Stop(); + } + + private void TimerTick(object? sender, EventArgs e) + { + var message = $"Realized {list.GetRealizedContainers().Count()} of {list.ItemsPanelRoot?.Children.Count}"; + itemCount.Text = message; + } +} diff --git a/samples/VirtualizationDemo/VirtualizationDemo.csproj b/samples/VirtualizationDemo/VirtualizationDemo.csproj index 81b30c6cbe..3ac7aab589 100644 --- a/samples/VirtualizationDemo/VirtualizationDemo.csproj +++ b/samples/VirtualizationDemo/VirtualizationDemo.csproj @@ -1,19 +1,24 @@  - Exe + WinExe net6.0 + true + + + - - + + + + + PreserveNewest + - - - - - + + diff --git a/src/Avalonia.Base/Controls/ResourceDictionary.cs b/src/Avalonia.Base/Controls/ResourceDictionary.cs index 5123803f6e..231a19baab 100644 --- a/src/Avalonia.Base/Controls/ResourceDictionary.cs +++ b/src/Avalonia.Base/Controls/ResourceDictionary.cs @@ -15,6 +15,7 @@ namespace Avalonia.Controls /// public class ResourceDictionary : IResourceDictionary { + private object? lastDeferredItemKey; private Dictionary? _inner; private IResourceHost? _owner; private AvaloniaList? _mergedDictionaries; @@ -241,12 +242,27 @@ namespace Avalonia.Controls { if (value is DeferredItem deffered) { - _inner[key] = value = deffered.Factory(null) switch + // Avoid simple reentrancy, which could commonly occur on redefining the resource. + if (lastDeferredItemKey == key) { - ITemplateResult t => t.Result, - object v => v, - _ => null, - }; + value = null; + return false; + } + + try + { + lastDeferredItemKey = key; + _inner[key] = value = deffered.Factory(null) switch + { + ITemplateResult t => t.Result, + { } v => v, + _ => null, + }; + } + finally + { + lastDeferredItemKey = null; + } } return true; } diff --git a/src/Avalonia.Base/Media/DrawingContext.cs b/src/Avalonia.Base/Media/DrawingContext.cs index f2106f2f86..18d6968168 100644 --- a/src/Avalonia.Base/Media/DrawingContext.cs +++ b/src/Avalonia.Base/Media/DrawingContext.cs @@ -132,7 +132,7 @@ namespace Avalonia.Media double radiusX = 0, double radiusY = 0, BoxShadows boxShadows = default) { - if (brush == null && !PenIsVisible(pen)) + if (brush == null && !PenIsVisible(pen) && boxShadows.Count == 0) return; if (!MathUtilities.IsZero(radiusX)) { @@ -160,7 +160,7 @@ namespace Avalonia.Media /// public void DrawRectangle(IBrush? brush, IPen? pen, RoundedRect rrect, BoxShadows boxShadows = default) { - if (brush == null && !PenIsVisible(pen)) + if (brush == null && !PenIsVisible(pen) && boxShadows.Count == 0) return; DrawRectangleCore(brush, pen, rrect, boxShadows); } diff --git a/src/Avalonia.Base/Media/FontManager.cs b/src/Avalonia.Base/Media/FontManager.cs index 2e8d8e415d..4425147098 100644 --- a/src/Avalonia.Base/Media/FontManager.cs +++ b/src/Avalonia.Base/Media/FontManager.cs @@ -30,13 +30,15 @@ namespace Avalonia.Media _fontFallbacks = options?.FontFallbacks; - DefaultFontFamilyName = options?.DefaultFamilyName ?? PlatformImpl.GetDefaultFontFamilyName(); + var defaultFontFamilyName = options?.DefaultFamilyName ?? PlatformImpl.GetDefaultFontFamilyName(); - if (string.IsNullOrEmpty(DefaultFontFamilyName)) + if (string.IsNullOrEmpty(defaultFontFamilyName)) { throw new InvalidOperationException("Default font family name can't be null or empty."); } + DefaultFontFamily = new FontFamily(defaultFontFamilyName); + AddFontCollection(new SystemFontCollection(this)); } @@ -65,9 +67,9 @@ namespace Avalonia.Media } /// - /// Gets the system's default font family's name. + /// Gets the system's default font family. /// - public string DefaultFontFamilyName + public FontFamily DefaultFontFamily { get; } @@ -93,6 +95,11 @@ namespace Avalonia.Media var fontFamily = typeface.FontFamily; + if(typeface.FontFamily.Name == FontFamily.DefaultFontFamilyName) + { + return TryGetGlyphTypeface(new Typeface(DefaultFontFamily, typeface.Style, typeface.Weight, typeface.Stretch), out glyphTypeface); + } + if (fontFamily.Key is FontFamilyKey key) { var source = key.Source; @@ -131,15 +138,21 @@ namespace Avalonia.Media } } - foreach (var familyName in fontFamily.FamilyNames) + for (var i = 0; i < fontFamily.FamilyNames.Count; i++) { + var familyName = fontFamily.FamilyNames[i]; + if (SystemFonts.TryGetGlyphTypeface(familyName, typeface.Style, typeface.Weight, typeface.Stretch, out glyphTypeface)) { - return true; + if (!fontFamily.FamilyNames.HasFallbacks || glyphTypeface.FamilyName != DefaultFontFamily.Name) + { + return true; + } } } - return TryGetGlyphTypeface(new Typeface(DefaultFontFamilyName, typeface.Style, typeface.Weight, typeface.Stretch), out glyphTypeface); + //Nothing was found so use the default + return TryGetGlyphTypeface(new Typeface(DefaultFontFamily, typeface.Style, typeface.Weight, typeface.Stretch), out glyphTypeface); } /// @@ -199,16 +212,37 @@ namespace Avalonia.Media { foreach (var fallback in _fontFallbacks) { - typeface = new Typeface(fallback.FontFamily, fontStyle, fontWeight, fontStretch); + if (fallback.UnicodeRange.IsInRange(codepoint)) + { + typeface = new Typeface(fallback.FontFamily, fontStyle, fontWeight, fontStretch); + + if (TryGetGlyphTypeface(typeface, out var glyphTypeface) && glyphTypeface.TryGetGlyph((uint)codepoint, out _)) + { + return true; + } + } + } + } - if (TryGetGlyphTypeface(typeface, out var glyphTypeface) && glyphTypeface.TryGetGlyph((uint)codepoint, out _)) + //Try to match against fallbacks first + if (fontFamily != null && fontFamily.FamilyNames.HasFallbacks) + { + for (int i = 1; i < fontFamily.FamilyNames.Count; i++) + { + var familyName = fontFamily.FamilyNames[i]; + + foreach (var fontCollection in _fontCollections.Values) { - return true; + if (fontCollection.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, familyName, culture, out typeface)) + { + return true; + }; } } } - return PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, fontFamily, culture, out typeface); + //Try to find a match with the system font manager + return PlatformImpl.TryMatchCharacter(codepoint, fontStyle, fontWeight, fontStretch, culture, out typeface); } } } diff --git a/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs b/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs index f2fb490592..4d4751db02 100644 --- a/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs +++ b/src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs @@ -8,10 +8,8 @@ using Avalonia.Platform; namespace Avalonia.Media.Fonts { - public class EmbeddedFontCollection : IFontCollection + public class EmbeddedFontCollection : FontCollectionBase { - private readonly ConcurrentDictionary> _glyphTypefaceCache = new(); - private readonly List _fontFamilies = new List(1); private readonly Uri _key; @@ -25,13 +23,13 @@ namespace Avalonia.Media.Fonts _source = source; } - public Uri Key => _key; + public override Uri Key => _key; - public FontFamily this[int index] => _fontFamilies[index]; + public override FontFamily this[int index] => _fontFamilies[index]; - public int Count => _fontFamilies.Count; + public override int Count => _fontFamilies.Count; - public void Initialize(IFontManagerImpl fontManager) + public override void Initialize(IFontManagerImpl fontManager) { var assetLoader = AvaloniaLocator.Current.GetRequiredService(); @@ -45,7 +43,7 @@ namespace Avalonia.Media.Fonts { if (!_glyphTypefaceCache.TryGetValue(glyphTypeface.FamilyName, out var glyphTypefaces)) { - glyphTypefaces = new ConcurrentDictionary(); + glyphTypefaces = new ConcurrentDictionary(); if (_glyphTypefaceCache.TryAdd(glyphTypeface.FamilyName, glyphTypefaces)) { @@ -63,27 +61,8 @@ namespace Avalonia.Media.Fonts } } - public void Dispose() - { - foreach (var fontFamily in _fontFamilies) - { - if (_glyphTypefaceCache.TryGetValue(fontFamily.Name, out var glyphTypefaces)) - { - foreach (var glyphTypeface in glyphTypefaces.Values) - { - glyphTypeface.Dispose(); - } - } - } - GC.SuppressFinalize(this); - } - - public IEnumerator GetEnumerator() => _fontFamilies.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - public bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, + public override bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) { var key = new FontCollectionKey(style, weight, stretch); @@ -116,175 +95,6 @@ namespace Avalonia.Media.Fonts return false; } - private static bool TryGetNearestMatch( - ConcurrentDictionary glyphTypefaces, - FontCollectionKey key, - [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) - { - if (glyphTypefaces.TryGetValue(key, out glyphTypeface)) - { - return true; - } - - if (key.Style != FontStyle.Normal) - { - key = key with { Style = FontStyle.Normal }; - } - - if (key.Stretch != FontStretch.Normal) - { - if (TryFindStretchFallback(glyphTypefaces, key, out glyphTypeface)) - { - return true; - } - - if (key.Weight != FontWeight.Normal) - { - if (TryFindStretchFallback(glyphTypefaces, key with { Weight = FontWeight.Normal }, out glyphTypeface)) - { - return true; - } - } - - key = key with { Stretch = FontStretch.Normal }; - } - - if (TryFindWeightFallback(glyphTypefaces, key, out glyphTypeface)) - { - return true; - } - - if (TryFindStretchFallback(glyphTypefaces, key, out glyphTypeface)) - { - return true; - } - - //Take the first glyph typeface we can find. - foreach (var typeface in glyphTypefaces.Values) - { - glyphTypeface = typeface; - - return true; - } - - return false; - } - - private static bool TryFindStretchFallback( - ConcurrentDictionary glyphTypefaces, - FontCollectionKey key, - [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) - { - glyphTypeface = null; - - var stretch = (int)key.Stretch; - - if (stretch < 5) - { - for (var i = 0; stretch + i < 9; i++) - { - if (glyphTypefaces.TryGetValue(key with { Stretch = (FontStretch)(stretch + i) }, out glyphTypeface)) - { - return true; - } - } - } - else - { - for (var i = 0; stretch - i > 1; i++) - { - if (glyphTypefaces.TryGetValue(key with { Stretch = (FontStretch)(stretch - i) }, out glyphTypeface)) - { - return true; - } - } - } - - return false; - } - - private static bool TryFindWeightFallback( - ConcurrentDictionary glyphTypefaces, - FontCollectionKey key, - [NotNullWhen(true)] out IGlyphTypeface? typeface) - { - typeface = null; - var weight = (int)key.Weight; - - //If the target weight given is between 400 and 500 inclusive - if (weight >= 400 && weight <= 500) - { - //Look for available weights between the target and 500, in ascending order. - for (var i = 0; weight + i <= 500; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out typeface)) - { - return true; - } - } - - //If no match is found, look for available weights less than the target, in descending order. - for (var i = 0; weight - i >= 100; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out typeface)) - { - return true; - } - } - - //If no match is found, look for available weights greater than 500, in ascending order. - for (var i = 0; weight + i <= 900; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out typeface)) - { - return true; - } - } - } - - //If a weight less than 400 is given, look for available weights less than the target, in descending order. - if (weight < 400) - { - for (var i = 0; weight - i >= 100; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out typeface)) - { - return true; - } - } - - //If no match is found, look for available weights less than the target, in descending order. - for (var i = 0; weight + i <= 900; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out typeface)) - { - return true; - } - } - } - - //If a weight greater than 500 is given, look for available weights greater than the target, in ascending order. - if (weight > 500) - { - for (var i = 0; weight + i <= 900; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out typeface)) - { - return true; - } - } - - //If no match is found, look for available weights less than the target, in descending order. - for (var i = 0; weight - i >= 100; i += 50) - { - if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out typeface)) - { - return true; - } - } - } - - return false; - } + public override IEnumerator GetEnumerator() => _fontFamilies.GetEnumerator(); } } diff --git a/src/Avalonia.Base/Media/Fonts/FontCollectionBase.cs b/src/Avalonia.Base/Media/Fonts/FontCollectionBase.cs new file mode 100644 index 0000000000..713b3dafcd --- /dev/null +++ b/src/Avalonia.Base/Media/Fonts/FontCollectionBase.cs @@ -0,0 +1,259 @@ +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using Avalonia.Platform; + +namespace Avalonia.Media.Fonts +{ + public abstract class FontCollectionBase : IFontCollection + { + protected readonly ConcurrentDictionary> _glyphTypefaceCache = new(); + + public abstract Uri Key { get; } + + public abstract int Count { get; } + + public abstract FontFamily this[int index] { get; } + + public abstract bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, FontStretch stretch, + [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface); + + public bool TryMatchCharacter(int codepoint, FontStyle style, FontWeight weight, FontStretch stretch, + string? familyName, CultureInfo? culture, out Typeface match) + { + match = default; + + if (string.IsNullOrEmpty(familyName)) + { + foreach (var typefaces in _glyphTypefaceCache.Values) + { + if (TryGetNearestMatch(typefaces, new FontCollectionKey { Style = style, Weight = weight, Stretch = stretch }, out var glyphTypeface)) + { + if (glyphTypeface.TryGetGlyph((uint)codepoint, out _)) + { + match = new Typeface(glyphTypeface.FamilyName, style, weight, stretch); + + return true; + } + } + } + } + else + { + if (TryGetGlyphTypeface(familyName, style, weight, stretch, out var glyphTypeface)) + { + if (glyphTypeface.TryGetGlyph((uint)codepoint, out _)) + { + match = new Typeface(familyName, style, weight, stretch); + + return true; + } + } + } + + return false; + } + + public abstract void Initialize(IFontManagerImpl fontManager); + + public abstract IEnumerator GetEnumerator(); + + void IDisposable.Dispose() + { + foreach (var glyphTypefaces in _glyphTypefaceCache.Values) + { + foreach (var pair in glyphTypefaces) + { + pair.Value?.Dispose(); + } + } + + GC.SuppressFinalize(this); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + internal static bool TryGetNearestMatch( + ConcurrentDictionary glyphTypefaces, + FontCollectionKey key, + [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) + { + if (glyphTypefaces.TryGetValue(key, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + + if (key.Style != FontStyle.Normal) + { + key = key with { Style = FontStyle.Normal }; + } + + if (key.Stretch != FontStretch.Normal) + { + if (TryFindStretchFallback(glyphTypefaces, key, out glyphTypeface)) + { + return true; + } + + if (key.Weight != FontWeight.Normal) + { + if (TryFindStretchFallback(glyphTypefaces, key with { Weight = FontWeight.Normal }, out glyphTypeface)) + { + return true; + } + } + + key = key with { Stretch = FontStretch.Normal }; + } + + if (TryFindWeightFallback(glyphTypefaces, key, out glyphTypeface)) + { + return true; + } + + if (TryFindStretchFallback(glyphTypefaces, key, out glyphTypeface)) + { + return true; + } + + //Take the first glyph typeface we can find. + foreach (var typeface in glyphTypefaces.Values) + { + if(typeface != null) + { + glyphTypeface = typeface; + + return true; + } + } + + return false; + } + + internal static bool TryFindStretchFallback( + ConcurrentDictionary glyphTypefaces, + FontCollectionKey key, + [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) + { + glyphTypeface = null; + + var stretch = (int)key.Stretch; + + if (stretch < 5) + { + for (var i = 0; stretch + i < 9; i++) + { + if (glyphTypefaces.TryGetValue(key with { Stretch = (FontStretch)(stretch + i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + } + else + { + for (var i = 0; stretch - i > 1; i++) + { + if (glyphTypefaces.TryGetValue(key with { Stretch = (FontStretch)(stretch - i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + } + + return false; + } + + internal static bool TryFindWeightFallback( + ConcurrentDictionary glyphTypefaces, + FontCollectionKey key, + [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) + { + glyphTypeface = null; + var weight = (int)key.Weight; + + //If the target weight given is between 400 and 500 inclusive + if (weight >= 400 && weight <= 500) + { + //Look for available weights between the target and 500, in ascending order. + for (var i = 0; weight + i <= 500; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + + //If no match is found, look for available weights less than the target, in descending order. + for (var i = 0; weight - i >= 100; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + + //If no match is found, look for available weights greater than 500, in ascending order. + for (var i = 0; weight + i <= 900; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + } + + //If a weight less than 400 is given, look for available weights less than the target, in descending order. + if (weight < 400) + { + for (var i = 0; weight - i >= 100; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + + //If no match is found, look for available weights less than the target, in descending order. + for (var i = 0; weight + i <= 900; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + } + + //If a weight greater than 500 is given, look for available weights greater than the target, in ascending order. + if (weight > 500) + { + for (var i = 0; weight + i <= 900; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight + i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + + //If no match is found, look for available weights less than the target, in descending order. + for (var i = 0; weight - i >= 100; i += 50) + { + if (glyphTypefaces.TryGetValue(key with { Weight = (FontWeight)(weight - i) }, out glyphTypeface) && glyphTypeface != null) + { + return true; + } + } + } + + return false; + } + } +} diff --git a/src/Avalonia.Base/Media/Fonts/IFontCollection.cs b/src/Avalonia.Base/Media/Fonts/IFontCollection.cs index 814230bcf3..1a30f168f1 100644 --- a/src/Avalonia.Base/Media/Fonts/IFontCollection.cs +++ b/src/Avalonia.Base/Media/Fonts/IFontCollection.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using Avalonia.Platform; namespace Avalonia.Media.Fonts @@ -29,5 +30,21 @@ namespace Avalonia.Media.Fonts /// Returns true if a glyph typface can be found; otherwise, false bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface); + + /// + /// Tries to match a specified character to a that supports specified font properties. + /// + /// The codepoint to match against. + /// The font style. + /// The font weight. + /// The font stretch. + /// The family name. This is optional and used for fallback lookup. + /// The culture. + /// The matching . + /// + /// True, if the could match the character to specified parameters, False otherwise. + /// + bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, + FontStretch fontStretch, string? familyName, CultureInfo? culture, out Typeface typeface); } } diff --git a/src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs b/src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs index fd332c6ebe..2f2948cb3e 100644 --- a/src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs +++ b/src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -7,10 +6,8 @@ using Avalonia.Platform; namespace Avalonia.Media.Fonts { - internal class SystemFontCollection : IFontCollection + internal class SystemFontCollection : FontCollectionBase { - private readonly ConcurrentDictionary> _glyphTypefaceCache = new(); - private readonly FontManager _fontManager; private readonly string[] _familyNames; @@ -20,9 +17,9 @@ namespace Avalonia.Media.Fonts _familyNames = fontManager.PlatformImpl.GetInstalledFontFamilyNames(); } - public Uri Key => FontManager.SystemFontsKey; + public override Uri Key => FontManager.SystemFontsKey; - public FontFamily this[int index] + public override FontFamily this[int index] { get { @@ -32,76 +29,41 @@ namespace Avalonia.Media.Fonts } } - public int Count => _familyNames.Length; + public override int Count => _familyNames.Length; - public bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, + public override bool TryGetGlyphTypeface(string familyName, FontStyle style, FontWeight weight, FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) { - if (familyName == FontFamily.DefaultFontFamilyName) - { - familyName = _fontManager.DefaultFontFamilyName; - } + glyphTypeface = null; var key = new FontCollectionKey(style, weight, stretch); - if (_glyphTypefaceCache.TryGetValue(familyName, out var glyphTypefaces)) - { - if (glyphTypefaces.TryGetValue(key, out glyphTypeface)) - { - return true; - } - else - { - if (_fontManager.PlatformImpl.TryCreateGlyphTypeface(familyName, style, weight, stretch, out glyphTypeface) && - glyphTypefaces.TryAdd(key, glyphTypeface)) - { - return true; - } - } - } + var glyphTypefaces = _glyphTypefaceCache.GetOrAdd(familyName, (key) => new ConcurrentDictionary()); - if (_fontManager.PlatformImpl.TryCreateGlyphTypeface(familyName, style, weight, stretch, out glyphTypeface)) + if (!glyphTypefaces.TryGetValue(key, out glyphTypeface)) { - glyphTypefaces = new ConcurrentDictionary(); + _fontManager.PlatformImpl.TryCreateGlyphTypeface(familyName, style, weight, stretch, out glyphTypeface); - if (glyphTypefaces.TryAdd(key, glyphTypeface) && _glyphTypefaceCache.TryAdd(familyName, glyphTypefaces)) + if (!glyphTypefaces.TryAdd(key, glyphTypeface)) { - return true; + return false; } } - return false; + return glyphTypeface != null; } - public void Initialize(IFontManagerImpl fontManager) + public override void Initialize(IFontManagerImpl fontManager) { //We initialize the system font collection during construction. } - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public IEnumerator GetEnumerator() + public override IEnumerator GetEnumerator() { foreach (var familyName in _familyNames) { yield return new FontFamily(familyName); } } - - void IDisposable.Dispose() - { - foreach (var glyphTypefaces in _glyphTypefaceCache.Values) - { - foreach (var pair in glyphTypefaces) - { - pair.Value.Dispose(); - } - } - - GC.SuppressFinalize(this); - } } } diff --git a/src/Avalonia.Base/Platform/IFontManagerImpl.cs b/src/Avalonia.Base/Platform/IFontManagerImpl.cs index 116f7cd6e2..222e7196bb 100644 --- a/src/Avalonia.Base/Platform/IFontManagerImpl.cs +++ b/src/Avalonia.Base/Platform/IFontManagerImpl.cs @@ -27,15 +27,13 @@ namespace Avalonia.Platform /// The font style. /// The font weight. /// The font stretch. - /// The font family. This is optional and used for fallback lookup. /// The culture. /// The matching typeface. /// /// True, if the could match the character to specified parameters, False otherwise. /// bool TryMatchCharacter(int codepoint, FontStyle fontStyle, - FontWeight fontWeight, FontStretch fontStretch, - FontFamily? fontFamily, CultureInfo? culture, out Typeface typeface); + FontWeight fontWeight, FontStretch fontStretch, CultureInfo? culture, out Typeface typeface); /// /// Tries to get a glyph typeface for specified parameters. diff --git a/src/Avalonia.Controls/ISelectable.cs b/src/Avalonia.Controls/ISelectable.cs index 144adaa2f5..d0da7c4932 100644 --- a/src/Avalonia.Controls/ISelectable.cs +++ b/src/Avalonia.Controls/ISelectable.cs @@ -1,16 +1,9 @@ -using Avalonia.Controls.Primitives; - namespace Avalonia.Controls { /// - /// Interface for objects that are selectable. + /// An interface that is implemented by objects that expose their selection state via a + /// boolean property. /// - /// - /// Controls such as use this interface to indicate the - /// selected control in a list. If changing the control's property - /// should update the selection in a or equivalent, then - /// the control should raise the . - /// public interface ISelectable { /// @@ -18,4 +11,4 @@ namespace Avalonia.Controls /// bool IsSelected { get; set; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Controls/ItemsControl.cs b/src/Avalonia.Controls/ItemsControl.cs index 54ffba462f..60b0f8b193 100644 --- a/src/Avalonia.Controls/ItemsControl.cs +++ b/src/Avalonia.Controls/ItemsControl.cs @@ -94,7 +94,6 @@ namespace Avalonia.Controls private ItemContainerGenerator? _itemContainerGenerator; private EventHandler? _childIndexChanged; private IDataTemplate? _displayMemberItemTemplate; - private ScrollViewer? _scrollViewer; private ItemsPresenter? _itemsPresenter; /// @@ -425,6 +424,21 @@ namespace Avalonia.Controls } } + /// + /// Called when a container has been fully prepared to display an item. + /// + /// The container control. + /// The item being displayed. + /// The index of the item being displayed. + /// + /// This method will be called when a container has been fully prepared and added to the + /// logical and visual trees, but may be called before a layout pass has completed. It is + /// called immediately before the event is raised. + /// + protected internal virtual void ContainerForItemPreparedOverride(Control container, object? item, int index) + { + } + /// /// Called when the index for a container changes due to an insertion or removal in the /// items collection. @@ -442,6 +456,34 @@ namespace Avalonia.Controls /// The container element. protected internal virtual void ClearContainerForItemOverride(Control container) { + if (container is HeaderedContentControl hcc) + { + if (hcc.Content is Control) + hcc.Content = null; + if (hcc.Header is Control) + hcc.Header = null; + } + else if (container is ContentControl cc) + { + if (cc.Content is Control) + cc.Content = null; + } + else if (container is ContentPresenter p) + { + if (p.Content is Control) + p.Content = null; + } + else if (container is HeaderedItemsControl hic) + { + if (hic.Header is Control) + hic.Header = null; + } + else if (container is HeaderedSelectingItemsControl hsic) + { + if (hsic.Header is Control) + hsic.Header = null; + } + // Feels like we should be clearing the HeaderedItemsControl.Items binding here, but looking at // the WPF source it seems that this isn't done there. } @@ -457,7 +499,6 @@ namespace Avalonia.Controls protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); - _scrollViewer = e.NameScope.Find("PART_ScrollViewer"); _itemsPresenter = e.NameScope.Find("PART_ItemsPresenter"); } @@ -628,8 +669,8 @@ namespace Avalonia.Controls internal void ItemContainerPrepared(Control container, object? item, int index) { + ContainerForItemPreparedOverride(container, item, index); _childIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(container, index)); - _scrollViewer?.RegisterAnchorCandidate(container); ContainerPrepared?.Invoke(this, new(container, index)); } @@ -642,7 +683,6 @@ namespace Avalonia.Controls internal void ClearItemContainer(Control container) { - _scrollViewer?.UnregisterAnchorCandidate(container); ClearContainerForItemOverride(container); ContainerClearing?.Invoke(this, new(container)); } diff --git a/src/Avalonia.Controls/ListBoxItem.cs b/src/Avalonia.Controls/ListBoxItem.cs index 66a46cab4a..0a6873cd59 100644 --- a/src/Avalonia.Controls/ListBoxItem.cs +++ b/src/Avalonia.Controls/ListBoxItem.cs @@ -1,6 +1,7 @@ using Avalonia.Automation.Peers; using Avalonia.Controls.Metadata; using Avalonia.Controls.Mixins; +using Avalonia.Controls.Primitives; namespace Avalonia.Controls { @@ -14,7 +15,7 @@ namespace Avalonia.Controls /// Defines the property. /// public static readonly StyledProperty IsSelectedProperty = - AvaloniaProperty.Register(nameof(IsSelected)); + SelectingItemsControl.IsSelectedProperty.AddOwner(); /// /// Initializes static members of the class. diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs index 06d84e715d..a0dbf33a1d 100644 --- a/src/Avalonia.Controls/MenuItem.cs +++ b/src/Avalonia.Controls/MenuItem.cs @@ -57,7 +57,7 @@ namespace Avalonia.Controls /// Defines the property. /// public static readonly StyledProperty IsSelectedProperty = - ListBoxItem.IsSelectedProperty.AddOwner(); + SelectingItemsControl.IsSelectedProperty.AddOwner(); /// /// Defines the property. diff --git a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs index 0efa8d38f8..1704965fd7 100644 --- a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs @@ -85,7 +85,7 @@ namespace Avalonia.Controls.Presenters private Size _viewport; private Dictionary? _activeLogicalGestureScrolls; private Dictionary? _scrollGestureSnapPoints; - private List? _anchorCandidates; + private HashSet? _anchorCandidates; private Control? _anchorElement; private Rect _anchorElementBounds; private bool _isAnchorElementDirty; @@ -347,7 +347,7 @@ namespace Avalonia.Controls.Presenters "An anchor control must be a visual descendent of the ScrollContentPresenter."); } - _anchorCandidates ??= new List(); + _anchorCandidates ??= new(); _anchorCandidates.Add(element); _isAnchorElementDirty = true; } diff --git a/src/Avalonia.Controls/Primitives/PopupRoot.cs b/src/Avalonia.Controls/Primitives/PopupRoot.cs index 952ba92e9b..a2034a2dbb 100644 --- a/src/Avalonia.Controls/Primitives/PopupRoot.cs +++ b/src/Avalonia.Controls/Primitives/PopupRoot.cs @@ -90,7 +90,6 @@ namespace Avalonia.Controls.Primitives public void Dispose() { PlatformImpl?.Dispose(); - HandleClosed(); } private void UpdatePosition() diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 9c060f2258..663a315732 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -104,6 +104,14 @@ namespace Avalonia.Controls.Primitives AvaloniaProperty.Register( nameof(SelectionMode)); + /// + /// Defines the IsSelected attached property. + /// + public static readonly StyledProperty IsSelectedProperty = + AvaloniaProperty.RegisterAttached( + "IsSelected", + defaultBindingMode: BindingMode.TwoWay); + /// /// Defines the property. /// @@ -111,9 +119,8 @@ namespace Avalonia.Controls.Primitives AvaloniaProperty.Register(nameof(IsTextSearchEnabled), false); /// - /// Event that should be raised by items that implement to - /// notify the parent that their selection state - /// has changed. + /// Event that should be raised by containers when their selection state changes to notify + /// the parent that their selection state has changed. /// public static readonly RoutedEvent IsSelectedChangedEvent = RoutedEvent.Register( @@ -302,20 +309,9 @@ namespace Avalonia.Controls.Primitives { get { - if (_updateState?.Selection.HasValue == true) - { - return _updateState.Selection.Value; - } - else - { - if (_selection is null) - { - _selection = CreateDefaultSelectionModel(); - InitializeSelectionModel(_selection); - } - - return _selection; - } + return _updateState?.Selection.HasValue == true ? + _updateState.Selection.Value : + GetOrCreateSelectionModel(); } set { @@ -420,6 +416,21 @@ namespace Avalonia.Controls.Primitives /// The item. public void ScrollIntoView(object item) => ScrollIntoView(ItemsView.IndexOf(item)); + /// + /// Gets the value of the on the specified control. + /// + /// The control. + /// The value of the attached property. + public static bool GetIsSelected(Control control) => control.GetValue(IsSelectedProperty); + + /// + /// Gets the value of the on the specified control. + /// + /// The control. + /// The value of the property. + /// The value of the attached property. + public static void SetIsSelected(Control control, bool value) => control.SetValue(IsSelectedProperty, value); + /// /// Tries to get the container that was the source of an event. /// @@ -473,20 +484,36 @@ namespace Avalonia.Controls.Primitives } } - /// - protected internal override void PrepareContainerForItemOverride(Control element, object? item, int index) + protected internal override void PrepareContainerForItemOverride(Control container, object? item, int index) + { + // Ensure that the selection model is created at this point so that accessing it in + // ContainerForItemPreparedOverride doesn't cause it to be initialized (which can + // make containers become deselected when they're synced with the empty selection + // mode). + GetOrCreateSelectionModel(); + + base.PrepareContainerForItemOverride(container, item, index); + } + + protected internal override void ContainerForItemPreparedOverride(Control container, object? item, int index) { - base.PrepareContainerForItemOverride(element, item, index); + base.ContainerForItemPreparedOverride(container, item, index); - if ((element as ISelectable)?.IsSelected == true) + // Once the container has been full prepared and added to the tree, any bindings from + // styles or item container themes are guaranteed to be applied. + if (!container.IsSet(IsSelectedProperty)) { - Selection.Select(index); - MarkContainerSelected(element, true); + // The IsSelected property is not set on the container: update the container + // selection based on the current selection as understood by this control. + MarkContainerSelected(container, Selection.IsSelected(index)); } else { - var selected = Selection.IsSelected(index); - MarkContainerSelected(element, selected); + // The IsSelected property is set on the container: there is a style or item + // container theme which has bound the IsSelected property. Update our selection + // based on the selection state of the container. + var containerIsSelected = GetIsSelected(container); + UpdateSelection(index, containerIsSelected, toggleModifier: true); } } @@ -508,8 +535,7 @@ namespace Avalonia.Controls.Primitives KeyboardNavigation.SetTabOnceActiveElement(panel, null); } - if (element is ISelectable) - MarkContainerSelected(element, false); + element.ClearValue(IsSelectedProperty); } /// @@ -874,6 +900,17 @@ namespace Avalonia.Controls.Primitives return false; } + private ISelectionModel GetOrCreateSelectionModel() + { + if (_selection is null) + { + _selection = CreateDefaultSelectionModel(); + InitializeSelectionModel(_selection); + } + + return _selection; + } + private void OnItemsViewSourceChanged(object? sender, EventArgs e) { if (_selection is not null && _updateState is null) @@ -1098,11 +1135,14 @@ namespace Avalonia.Controls.Primitives { if (!_ignoreContainerSelectionChanged && e.Source is Control control && - e.Source is ISelectable selectable && control.Parent == this && - IndexFromContainer(control) != -1) + IndexFromContainer(control) is var index && + index >= 0) { - UpdateSelection(control, selectable.IsSelected); + if (GetIsSelected(control)) + Selection.Select(index); + else + Selection.Deselect(index); } if (e.Source != this) @@ -1112,31 +1152,18 @@ namespace Avalonia.Controls.Primitives } /// - /// Sets a container's 'selected' class or . + /// Sets the on the specified container. /// /// The container. /// Whether the control is selected /// The previous selection state. - private bool MarkContainerSelected(Control container, bool selected) + private void MarkContainerSelected(Control container, bool selected) { + _ignoreContainerSelectionChanged = true; + try { - bool result; - - _ignoreContainerSelectionChanged = true; - - if (container is ISelectable selectable) - { - result = selectable.IsSelected; - selectable.IsSelected = selected; - } - else - { - result = container.Classes.Contains(":selected"); - ((IPseudoClasses)container.Classes).Set(":selected", selected); - } - - return result; + container.SetCurrentValue(IsSelectedProperty, selected); } finally { diff --git a/src/Avalonia.Controls/Primitives/Thumb.cs b/src/Avalonia.Controls/Primitives/Thumb.cs index cb3195cf52..c205830bc2 100644 --- a/src/Avalonia.Controls/Primitives/Thumb.cs +++ b/src/Avalonia.Controls/Primitives/Thumb.cs @@ -2,6 +2,7 @@ using System; using Avalonia.Controls.Metadata; using Avalonia.Input; using Avalonia.Interactivity; +using Avalonia.VisualTree; namespace Avalonia.Controls.Primitives { @@ -80,20 +81,22 @@ namespace Avalonia.Controls.Primitives { if (_lastPoint.HasValue) { + var point = e.GetPosition(this.GetVisualParent()); var ev = new VectorEventArgs { RoutedEvent = DragDeltaEvent, - Vector = e.GetPosition(this) - _lastPoint.Value, + Vector = point - _lastPoint.Value, }; RaiseEvent(ev); + _lastPoint = point; } } protected override void OnPointerPressed(PointerPressedEventArgs e) { e.Handled = true; - _lastPoint = e.GetPosition(this); + _lastPoint = e.GetPosition(this.GetVisualParent()); var ev = new VectorEventArgs { @@ -116,7 +119,7 @@ namespace Avalonia.Controls.Primitives var ev = new VectorEventArgs { RoutedEvent = DragCompletedEvent, - Vector = (Vector)e.GetPosition(this), + Vector = (Vector)e.GetPosition(this.GetVisualParent()), }; RaiseEvent(ev); diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index ed9075c155..b0dff5be79 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -246,11 +246,11 @@ namespace Avalonia.Controls break; case Key.Home: - Value = Minimum; + SetCurrentValue(ValueProperty, Minimum); break; case Key.End: - Value = Maximum; + SetCurrentValue(ValueProperty, Maximum); break; default: @@ -313,7 +313,7 @@ namespace Avalonia.Controls // Update if we've found a better value if (Math.Abs(next - value) > Tolerance) { - Value = next; + SetCurrentValue(ValueProperty, next); } } @@ -366,7 +366,7 @@ namespace Avalonia.Controls var range = Maximum - Minimum; var finalValue = calcVal * range + Minimum; - Value = IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue; + SetCurrentValue(ValueProperty, IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue); } /// diff --git a/src/Avalonia.Controls/TabItem.cs b/src/Avalonia.Controls/TabItem.cs index 76680c0420..46265fb5bc 100644 --- a/src/Avalonia.Controls/TabItem.cs +++ b/src/Avalonia.Controls/TabItem.cs @@ -22,7 +22,7 @@ namespace Avalonia.Controls /// Defines the property. /// public static readonly StyledProperty IsSelectedProperty = - ListBoxItem.IsSelectedProperty.AddOwner(); + SelectingItemsControl.IsSelectedProperty.AddOwner(); /// /// Initializes static members of the class. diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 2cd7a6f5ef..07b1e9b51f 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics; using Avalonia.Reactive; using Avalonia.Controls.Metadata; using Avalonia.Controls.Platform; @@ -527,6 +528,14 @@ namespace Avalonia.Controls /// protected virtual void HandleClosed() { + Renderer.SceneInvalidated -= SceneInvalidated; + // We need to wait for the renderer to complete any in-flight operations + Renderer.Dispose(); + + Debug.Assert(PlatformImpl != null); + // The PlatformImpl is completely invalid at this point + PlatformImpl = null; + if (_globalStyles is object) { _globalStyles.GlobalStylesAdded -= ((IStyleHost)this).StylesAdded; @@ -536,10 +545,7 @@ namespace Avalonia.Controls { _applicationThemeHost.ActualThemeVariantChanged -= GlobalActualThemeVariantChanged; } - - Renderer.SceneInvalidated -= SceneInvalidated; - Renderer.Dispose(); - + _layoutDiagnosticBridge?.Dispose(); _layoutDiagnosticBridge = null; @@ -547,8 +553,6 @@ namespace Avalonia.Controls _pointerOverPreProcessorSubscription?.Dispose(); _backGestureSubscription?.Dispose(); - PlatformImpl = null; - var logicalArgs = new LogicalTreeAttachmentEventArgs(this, this, null); ((ILogical)this).NotifyDetachedFromLogicalTree(logicalArgs); diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 2cf8d941ca..e3a9a05951 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -10,6 +10,7 @@ using Avalonia.Controls.Generators; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Input.Platform; +using Avalonia.Interactivity; using Avalonia.Layout; using Avalonia.Threading; using Avalonia.VisualTree; @@ -60,7 +61,8 @@ namespace Avalonia.Controls /// static TreeView() { - // HACK: Needed or SelectedItem property will not be found in Release build. + SelectingItemsControl.IsSelectedChangedEvent.AddClassHandler((x, e) => + x.ContainerSelectionChanged(e)); } /// @@ -430,9 +432,8 @@ namespace Avalonia.Controls private void MarkItemSelected(object item, bool selected) { - var container = TreeContainerFromItem(item)!; - - MarkContainerSelected(container, selected); + if (TreeContainerFromItem(item) is Control container) + MarkContainerSelected(container, selected); } private void SelectedItemsAdded(IList items) @@ -487,16 +488,24 @@ namespace Avalonia.Controls protected internal override Control CreateContainerForItemOverride() => new TreeViewItem(); protected internal override bool IsItemItsOwnContainerOverride(Control item) => item is TreeViewItem; - protected internal override void PrepareContainerForItemOverride(Control container, object? item, int index) + protected internal override void ContainerForItemPreparedOverride(Control container, object? item, int index) { - base.PrepareContainerForItemOverride(container, item, index); + base.ContainerForItemPreparedOverride(container, item, index); - if (item == SelectedItem) + // Once the container has been full prepared and added to the tree, any bindings from + // styles or item container themes are guaranteed to be applied. + if (container.IsSet(SelectingItemsControl.IsSelectedProperty)) { - MarkContainerSelected(container, true); - if (AutoScrollToSelectedItem) - Dispatcher.UIThread.Post(container.BringIntoView); + // The IsSelected property is set on the container: there is a style or item + // container theme which has bound the IsSelected property. Update our selection + // based on the selection state of the container. + var containerIsSelected = SelectingItemsControl.GetIsSelected(container); + UpdateSelectionFromContainer(container, select: containerIsSelected, toggleModifier: true); } + + // The IsSelected property is not set on the container: update the container + // selection based on the current selection as understood by this control. + MarkContainerSelected(container, SelectedItems.Contains(item)); } /// @@ -663,7 +672,11 @@ namespace Avalonia.Controls var multi = mode.HasAllFlags(SelectionMode.Multiple); var range = multi && rangeModifier && selectedContainer != null; - if (rightButton) + if (!select) + { + SelectedItems.Remove(item); + } + else if (rightButton) { if (!SelectedItems.Contains(item)) { @@ -863,27 +876,44 @@ namespace Avalonia.Controls } /// - /// Sets a container's 'selected' class or . + /// Called when a container raises the + /// . /// - /// The container. - /// Whether the control is selected - private void MarkContainerSelected(Control? container, bool selected) + /// The event. + private void ContainerSelectionChanged(RoutedEventArgs e) { - if (container == null) + if (e.Source is TreeViewItem container && + container.TreeViewOwner == this && + TreeItemFromContainer(container) is object item) { - return; - } + var containerIsSelected = SelectingItemsControl.GetIsSelected(container); + var ourIsSelected = SelectedItems.Contains(item); - if (container is ISelectable selectable) - { - selectable.IsSelected = selected; + if (containerIsSelected != ourIsSelected) + { + if (containerIsSelected) + SelectedItems.Add(item); + else + SelectedItems.Remove(item); + } } - else + + if (e.Source != this) { - ((IPseudoClasses)container.Classes).Set(":selected", selected); + e.Handled = true; } } + /// + /// Sets a container's 'selected' class or . + /// + /// The container. + /// Whether the control is selected + private void MarkContainerSelected(Control container, bool selected) + { + container.SetCurrentValue(SelectingItemsControl.IsSelectedProperty, selected); + } + /// /// Makes a list of objects equal another (though doesn't preserve order). /// diff --git a/src/Avalonia.Controls/TreeViewItem.cs b/src/Avalonia.Controls/TreeViewItem.cs index 5dbfe49533..806d7e320b 100644 --- a/src/Avalonia.Controls/TreeViewItem.cs +++ b/src/Avalonia.Controls/TreeViewItem.cs @@ -31,7 +31,7 @@ namespace Avalonia.Controls /// Defines the property. /// public static readonly StyledProperty IsSelectedProperty = - ListBoxItem.IsSelectedProperty.AddOwner(); + SelectingItemsControl.IsSelectedProperty.AddOwner(); /// /// Defines the property. @@ -105,6 +105,11 @@ namespace Avalonia.Controls EnsureTreeView().PrepareContainerForItemOverride(container, item, index); } + protected internal override void ContainerForItemPreparedOverride(Control container, object? item, int index) + { + EnsureTreeView().ContainerForItemPreparedOverride(container, item, index); + } + /// protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) { diff --git a/src/Avalonia.Controls/Utils/RealizedStackElements.cs b/src/Avalonia.Controls/Utils/RealizedStackElements.cs new file mode 100644 index 0000000000..8dbfb2c957 --- /dev/null +++ b/src/Avalonia.Controls/Utils/RealizedStackElements.cs @@ -0,0 +1,499 @@ +using System; +using System.Collections.Generic; +using Avalonia.Utilities; + +namespace Avalonia.Controls.Utils +{ + /// + /// Stores the realized element state for a virtualizing panel that arranges its children + /// in a stack layout, such as . + /// + internal class RealizedStackElements + { + private int _firstIndex; + private List? _elements; + private List? _sizes; + private double _startU; + private bool _startUUnstable; + + /// + /// Gets the number of realized elements. + /// + public int Count => _elements?.Count ?? 0; + + /// + /// Gets the index of the first realized element, or -1 if no elements are realized. + /// + public int FirstIndex => _elements?.Count > 0 ? _firstIndex : -1; + + /// + /// Gets the index of the last realized element, or -1 if no elements are realized. + /// + public int LastIndex => _elements?.Count > 0 ? _firstIndex + _elements.Count - 1 : -1; + + /// + /// Gets the elements. + /// + public IReadOnlyList Elements => _elements ??= new List(); + + /// + /// Gets the sizes of the elements on the primary axis. + /// + public IReadOnlyList SizeU => _sizes ??= new List(); + + /// + /// Gets the position of the first element on the primary axis. + /// + public double StartU => _startU; + + /// + /// Adds a newly realized element to the collection. + /// + /// The index of the element. + /// The element. + /// The position of the elemnt on the primary axis. + /// The size of the element on the primary axis. + public void Add(int index, Control element, double u, double sizeU) + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + _elements ??= new List(); + _sizes ??= new List(); + + if (Count == 0) + { + _elements.Add(element); + _sizes.Add(sizeU); + _startU = u; + _firstIndex = index; + } + else if (index == LastIndex + 1) + { + _elements.Add(element); + _sizes.Add(sizeU); + } + else if (index == FirstIndex - 1) + { + --_firstIndex; + _elements.Insert(0, element); + _sizes.Insert(0, sizeU); + _startU = u; + } + else + { + throw new NotSupportedException("Can only add items to the beginning or end of realized elements."); + } + } + + /// + /// Gets the element at the specified index, if realized. + /// + /// The index in the source collection of the element to get. + /// The element if realized; otherwise null. + public Control? GetElement(int index) + { + var i = index - FirstIndex; + if (i >= 0 && i < _elements?.Count) + return _elements[i]; + return null; + } + + /// + /// Gets or estimates the index and start U position of the anchor element for the + /// specified viewport. + /// + /// The U position of the start of the viewport. + /// The U position of the end of the viewport. + /// The number of items in the list. + /// The current estimated element size. + /// + /// A tuple containing: + /// - The index of the anchor element, or -1 if an anchor could not be determined + /// - The U position of the start of the anchor element, if determined + /// + /// + /// This method tries to find an existing element in the specified viewport from which + /// element realization can start. Failing that it estimates the first element in the + /// viewport. + /// + public (int index, double position) GetOrEstimateAnchorElementForViewport( + double viewportStartU, + double viewportEndU, + int itemCount, + ref double estimatedElementSizeU) + { + // We have no elements, nothing to do here. + if (itemCount <= 0) + return (-1, 0); + + // If we're at 0 then display the first item. + if (MathUtilities.IsZero(viewportStartU)) + return (0, 0); + + if (_sizes is not null && !_startUUnstable) + { + var u = _startU; + + for (var i = 0; i < _sizes.Count; ++i) + { + var size = _sizes[i]; + + if (double.IsNaN(size)) + break; + + var endU = u + size; + + if (endU > viewportStartU && u < viewportEndU) + return (FirstIndex + i, u); + + u = endU; + } + } + + // We don't have any realized elements in the requested viewport, or can't rely on + // StartU being valid. Estimate the index using only the estimated size. First, + // estimate the element size, using defaultElementSizeU if we don't have any realized + // elements. + var estimatedSize = EstimateElementSizeU() switch + { + -1 => estimatedElementSizeU, + double v => v, + }; + + // Store the estimated size for the next layout pass. + estimatedElementSizeU = estimatedSize; + + // Estimate the element at the start of the viewport. + var index = Math.Min((int)(viewportStartU / estimatedSize), itemCount - 1); + return (index, index * estimatedSize); + } + + /// + /// Gets the position of the element with the requested index on the primary axis, if realized. + /// + /// + /// The position of the element, or NaN if the element is not realized. + /// + public double GetElementU(int index) + { + if (index < FirstIndex || _sizes is null) + return double.NaN; + + var endIndex = index - FirstIndex; + + if (endIndex >= _sizes.Count) + return double.NaN; + + var u = StartU; + + for (var i = 0; i < endIndex; ++i) + u += _sizes[i]; + + return u; + } + + public double GetOrEstimateElementU(int index, ref double estimatedElementSizeU) + { + // Return the position of the existing element if realized. + var u = GetElementU(index); + + if (!double.IsNaN(u)) + return u; + + // Estimate the element size, using defaultElementSizeU if we don't have any realized + // elements. + var estimatedSize = EstimateElementSizeU() switch + { + -1 => estimatedElementSizeU, + double v => v, + }; + + // Store the estimated size for the next layout pass. + estimatedElementSizeU = estimatedSize; + + // TODO: Use _startU to work this out. + return index * estimatedSize; + } + + /// + /// Estimates the average U size of all elements in the source collection based on the + /// realized elements. + /// + /// + /// The estimated U size of an element, or -1 if not enough information is present to make + /// an estimate. + /// + public double EstimateElementSizeU() + { + var total = 0.0; + var divisor = 0.0; + + // Average the size of the realized elements. + if (_sizes is not null) + { + foreach (var size in _sizes) + { + if (double.IsNaN(size)) + continue; + total += size; + ++divisor; + } + } + + // We don't have any elements on which to base our estimate. + if (divisor == 0 || total == 0) + return -1; + + return total / divisor; + } + + /// + /// Gets the index of the specified element. + /// + /// The element. + /// The index or -1 if the element is not present in the collection. + public int GetIndex(Control element) + { + return _elements?.IndexOf(element) is int index && index >= 0 ? index + FirstIndex : -1; + } + + /// + /// Updates the elements in response to items being inserted into the source collection. + /// + /// The index in the source collection of the insert. + /// The number of items inserted. + /// A method used to update the element indexes. + public void ItemsInserted(int index, int count, Action updateElementIndex) + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + if (_elements is null || _elements.Count == 0) + return; + + // Get the index within the realized _elements collection. + var first = FirstIndex; + var realizedIndex = index - first; + + if (realizedIndex < Count) + { + // The insertion point affects the realized elements. Update the index of the + // elements after the insertion point. + var elementCount = _elements.Count; + var start = Math.Max(realizedIndex, 0); + var newIndex = realizedIndex + count; + + for (var i = start; i < elementCount; ++i) + { + if (_elements[i] is Control element) + updateElementIndex(element, newIndex - count, newIndex); + ++newIndex; + } + + if (realizedIndex < 0) + { + // The insertion point was before the first element, update the first index. + _firstIndex += count; + } + else + { + // The insertion point was within the realized elements, insert an empty space + // in _elements and _sizes. + _elements!.InsertMany(realizedIndex, null, count); + _sizes!.InsertMany(realizedIndex, double.NaN, count); + } + } + } + + /// + /// Updates the elements in response to items being removed from the source collection. + /// + /// The index in the source collection of the remove. + /// The number of items removed. + /// A method used to update the element indexes. + /// A method used to recycle elements. + public void ItemsRemoved( + int index, + int count, + Action updateElementIndex, + Action recycleElement) + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + if (_elements is null || _elements.Count == 0) + return; + + // Get the removal start and end index within the realized _elements collection. + var first = FirstIndex; + var last = LastIndex; + var startIndex = index - first; + var endIndex = (index + count) - first; + + if (endIndex < 0) + { + // The removed range was before the realized elements. Update the first index and + // the indexes of the realized elements. + _firstIndex -= count; + _startUUnstable = true; + + var newIndex = _firstIndex; + for (var i = 0; i < _elements.Count; ++i) + { + if (_elements[i] is Control element) + updateElementIndex(element, newIndex - count, newIndex); + ++newIndex; + } + } + else if (startIndex < _elements.Count) + { + // Recycle and remove the affected elements. + var start = Math.Max(startIndex, 0); + var end = Math.Min(endIndex, _elements.Count); + + for (var i = start; i < end; ++i) + { + if (_elements[i] is Control element) + recycleElement(element); + } + + _elements.RemoveRange(start, end - start); + _sizes!.RemoveRange(start, end - start); + + // If the remove started before and ended within our realized elements, then our new + // first index will be the index where the remove started. Mark StartU as unstable + // because we can't rely on it now to estimate element heights. + if (startIndex <= 0 && end < last) + { + _firstIndex = first = index; + _startUUnstable = true; + } + + // Update the indexes of the elements after the removed range. + end = _elements.Count; + var newIndex = first + start; + for (var i = start; i < end; ++i) + { + if (_elements[i] is Control element) + updateElementIndex(element, newIndex + count, newIndex); + ++newIndex; + } + } + } + + /// + /// Recycles all elements in response to the source collection being reset. + /// + /// A method used to recycle elements. + public void ItemsReset(Action recycleElement) + { + if (_elements is null || _elements.Count == 0) + return; + + foreach (var e in _elements) + { + if (e is not null) + recycleElement(e); + } + + _startU = _firstIndex = 0; + _elements?.Clear(); + _sizes?.Clear(); + + } + + /// + /// Recycles elements before a specific index. + /// + /// The index in the source collection of new first element. + /// A method used to recycle elements. + public void RecycleElementsBefore(int index, Action recycleElement) + { + if (index <= FirstIndex || _elements is null || _elements.Count == 0) + return; + + if (index > LastIndex) + { + RecycleAllElements(recycleElement); + } + else + { + var endIndex = index - FirstIndex; + + for (var i = 0; i < endIndex; ++i) + { + if (_elements[i] is Control e) + recycleElement(e, i + FirstIndex); + } + + _elements.RemoveRange(0, endIndex); + _sizes!.RemoveRange(0, endIndex); + _firstIndex = index; + } + } + + /// + /// Recycles elements after a specific index. + /// + /// The index in the source collection of new last element. + /// A method used to recycle elements. + public void RecycleElementsAfter(int index, Action recycleElement) + { + if (index >= LastIndex || _elements is null || _elements.Count == 0) + return; + + if (index < FirstIndex) + { + RecycleAllElements(recycleElement); + } + else + { + var startIndex = (index + 1) - FirstIndex; + var count = _elements.Count; + + for (var i = startIndex; i < count; ++i) + { + if (_elements[i] is Control e) + recycleElement(e, i + FirstIndex); + } + + _elements.RemoveRange(startIndex, _elements.Count - startIndex); + _sizes!.RemoveRange(startIndex, _sizes.Count - startIndex); + } + } + + /// + /// Recycles all realized elements. + /// + /// A method used to recycle elements. + public void RecycleAllElements(Action recycleElement) + { + if (_elements is null || _elements.Count == 0) + return; + + var i = FirstIndex; + + foreach (var e in _elements) + { + if (e is not null) + recycleElement(e, i); + ++i; + } + + _startU = _firstIndex = 0; + _elements?.Clear(); + _sizes?.Clear(); + } + + /// + /// Resets the element list and prepares it for reuse. + /// + public void ResetForReuse() + { + _startU = _firstIndex = 0; + _startUUnstable = false; + _elements?.Clear(); + _sizes?.Clear(); + } + } + +} diff --git a/src/Avalonia.Controls/VirtualizingStackPanel.cs b/src/Avalonia.Controls/VirtualizingStackPanel.cs index e86a0de657..77268c7831 100644 --- a/src/Avalonia.Controls/VirtualizingStackPanel.cs +++ b/src/Avalonia.Controls/VirtualizingStackPanel.cs @@ -60,13 +60,14 @@ namespace Avalonia.Controls private readonly Action _recycleElement; private readonly Action _recycleElementOnItemRemoved; private readonly Action _updateElementIndex; - private int _anchorIndex = -1; - private Control? _anchorElement; + private int _scrollToIndex = -1; + private Control? _scrollToElement; private bool _isInLayout; private bool _isWaitingForViewportUpdate; private double _lastEstimatedElementSizeU = 25; - private RealizedElementList? _measureElements; - private RealizedElementList? _realizedElements; + private RealizedStackElements? _measureElements; + private RealizedStackElements? _realizedElements; + private ScrollViewer? _scrollViewer; private Rect _viewport = s_invalidViewport; private Stack? _recyclePool; private Control? _unrealizedFocusedElement; @@ -129,50 +130,55 @@ namespace Avalonia.Controls set { SetValue(AreVerticalSnapPointsRegularProperty, value); } } + /// + /// Gets the index of the first realized element, or -1 if no elements are realized. + /// + public int FirstRealizedIndex => _realizedElements?.FirstIndex ?? -1; + + /// + /// Gets the index of the last realized element, or -1 if no elements are realized. + /// + public int LastRealizedIndex => _realizedElements?.LastIndex ?? -1; + protected override Size MeasureOverride(Size availableSize) { - if (!IsEffectivelyVisible) + var items = Items; + + if (items.Count == 0) return default; + // If we're bringing an item into view, ignore any layout passes until we receive a new + // effective viewport. + if (_isWaitingForViewportUpdate) + return DesiredSize; + _isInLayout = true; try { - var items = Items; var orientation = Orientation; _realizedElements ??= new(); _measureElements ??= new(); - // If we're bringing an item into view, ignore any layout passes until we receive a new - // effective viewport. - if (_isWaitingForViewportUpdate) - { - var sizeV = orientation == Orientation.Horizontal ? DesiredSize.Height : DesiredSize.Width; - return CalculateDesiredSize(orientation, items, sizeV); - } - // We handle horizontal and vertical layouts here so X and Y are abstracted to: // - Horizontal layouts: U = horizontal, V = vertical // - Vertical layouts: U = vertical, V = horizontal var viewport = CalculateMeasureViewport(items); - // Recycle elements outside of the expected range. - _realizedElements.RecycleElementsBefore(viewport.firstIndex, _recycleElement); - _realizedElements.RecycleElementsAfter(viewport.lastIndex, _recycleElement); + // If the viewport is disjunct then we can recycle everything. + if (viewport.viewportIsDisjunct) + _realizedElements.RecycleAllElements(_recycleElement); // Do the measure, creating/recycling elements as necessary to fill the viewport. Don't // write to _realizedElements yet, only _measureElements. - GenerateElements(availableSize, ref viewport); - - // Now we know what definitely fits, recycle anything left over. - _realizedElements.RecycleElementsAfter(_measureElements.LastIndex, _recycleElement); + RealizeElements(items, availableSize, ref viewport); - // And swap the measureElements and realizedElements collection. + // Now swap the measureElements and realizedElements collection. (_measureElements, _realizedElements) = (_realizedElements, _measureElements); _measureElements.ResetForReuse(); - return CalculateDesiredSize(orientation, items, viewport.measuredV); + return CalculateDesiredSize(orientation, items.Count, viewport); } finally { @@ -203,6 +209,7 @@ namespace Avalonia.Controls new Rect(u, 0, sizeU, finalSize.Height) : new Rect(0, u, finalSize.Width, sizeU); e.Arrange(rect); + _scrollViewer?.RegisterAnchorCandidate(e); u += orientation == Orientation.Horizontal ? rect.Width : rect.Height; } } @@ -217,6 +224,18 @@ namespace Avalonia.Controls } } + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + _scrollViewer = this.FindAncestorOfType(); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + _scrollViewer = null; + } + protected override void OnItemsChanged(IReadOnlyList items, NotifyCollectionChangedEventArgs e) { InvalidateMeasure(); @@ -314,7 +333,7 @@ namespace Avalonia.Controls { var items = Items; - if (_isInLayout || index < 0 || index >= items.Count) + if (_isInLayout || index < 0 || index >= items.Count || _realizedElements is null) return null; if (GetRealizedElement(index) is Control element) @@ -326,16 +345,16 @@ namespace Avalonia.Controls { // Create and measure the element to be brought into view. Store it in a field so that // it can be re-used in the layout pass. - _anchorElement = GetOrCreateElement(items, index); - _anchorElement.Measure(Size.Infinity); - _anchorIndex = index; + _scrollToElement = GetOrCreateElement(items, index); + _scrollToElement.Measure(Size.Infinity); + _scrollToIndex = index; // Get the expected position of the elment and put it in place. - var anchorU = GetOrEstimateElementPosition(index); + var anchorU = _realizedElements.GetOrEstimateElementU(index, ref _lastEstimatedElementSizeU); var rect = Orientation == Orientation.Horizontal ? - new Rect(anchorU, 0, _anchorElement.DesiredSize.Width, _anchorElement.DesiredSize.Height) : - new Rect(0, anchorU, _anchorElement.DesiredSize.Width, _anchorElement.DesiredSize.Height); - _anchorElement.Arrange(rect); + new Rect(anchorU, 0, _scrollToElement.DesiredSize.Width, _scrollToElement.DesiredSize.Height) : + new Rect(0, anchorU, _scrollToElement.DesiredSize.Width, _scrollToElement.DesiredSize.Height); + _scrollToElement.Arrange(rect); // If the item being brought into view was added since the last layout pass then // our bounds won't be updated, so any containing scroll viewers will not have an @@ -349,7 +368,7 @@ namespace Avalonia.Controls } // Try to bring the item into view. - _anchorElement.BringIntoView(); + _scrollToElement.BringIntoView(); // If the viewport does not contain the item to scroll to, set _isWaitingForViewportUpdate: // this should cause the following chain of events: @@ -368,9 +387,9 @@ namespace Avalonia.Controls root.LayoutManager.ExecuteLayoutPass(); } - var result = _anchorElement; - _anchorElement = null; - _anchorIndex = -1; + var result = _scrollToElement; + _scrollToElement = null; + _scrollToIndex = -1; return result; } @@ -394,46 +413,40 @@ namespace Avalonia.Controls var viewportStart = Orientation == Orientation.Horizontal ? viewport.X : viewport.Y; var viewportEnd = Orientation == Orientation.Horizontal ? viewport.Right : viewport.Bottom; - var (firstIndex, firstIndexU) = _realizedElements.GetIndexAt(viewportStart); - var (lastIndex, _) = _realizedElements.GetIndexAt(viewportEnd); - var estimatedElementSize = -1.0; + // Get or estimate the anchor element from which to start realization. var itemCount = items?.Count ?? 0; - var maxIndex = Math.Max(itemCount - 1, 0); - - if (firstIndex == -1) - { - estimatedElementSize = EstimateElementSizeU(); - firstIndex = Math.Min((int)(viewportStart / estimatedElementSize), maxIndex); - firstIndexU = firstIndex * estimatedElementSize; - } + var (anchorIndex, anchorU) = _realizedElements.GetOrEstimateAnchorElementForViewport( + viewportStart, + viewportEnd, + itemCount, + ref _lastEstimatedElementSizeU); - if (lastIndex == -1) - { - if (estimatedElementSize == -1) - estimatedElementSize = EstimateElementSizeU(); - lastIndex = Math.Min((int)(viewportEnd / estimatedElementSize), maxIndex); - } + // Check if the anchor element is not within the currently realized elements. + var disjunct = anchorIndex < _realizedElements.FirstIndex || + anchorIndex > _realizedElements.LastIndex; return new MeasureViewport { - firstIndex = firstIndex, - lastIndex = lastIndex, + anchorIndex = anchorIndex, + anchorU = anchorU, viewportUStart = viewportStart, viewportUEnd = viewportEnd, - startU = firstIndexU, + viewportIsDisjunct = disjunct, }; } - private Size CalculateDesiredSize(Orientation orientation, IReadOnlyList items, double sizeV) + private Size CalculateDesiredSize(Orientation orientation, int itemCount, in MeasureViewport viewport) { - var sizeU = EstimateElementSizeU() * items.Count; + var sizeU = 0.0; + var sizeV = viewport.measuredV; - if (double.IsInfinity(sizeU) || double.IsNaN(sizeU)) - throw new InvalidOperationException("Invalid calculated size."); + if (viewport.lastIndex >= 0) + { + var remaining = itemCount - viewport.lastIndex - 1; + sizeU = viewport.realizedEndU + (remaining * _lastEstimatedElementSizeU); + } - return orientation == Orientation.Horizontal ? - new Size(sizeU, sizeV) : - new Size(sizeV, sizeU); + return orientation == Orientation.Horizontal ? new(sizeU, sizeV) : new(sizeV, sizeU); } private double EstimateElementSizeU() @@ -474,19 +487,25 @@ namespace Avalonia.Controls return viewport; } - private void GenerateElements(Size availableSize, ref MeasureViewport viewport) + private void RealizeElements( + IReadOnlyList items, + Size availableSize, + ref MeasureViewport viewport) { Debug.Assert(_measureElements is not null); + Debug.Assert(_realizedElements is not null); + Debug.Assert(items.Count > 0); - var items = Items; + var index = viewport.anchorIndex; var horizontal = Orientation == Orientation.Horizontal; - var index = viewport.firstIndex; - var u = viewport.startU; + var u = viewport.anchorU; - // The layout is likely invalid. Don't create any elements and instead rely on our previous - // element size estimates to calculate a new desired size and trigger a new layout pass. - if (index >= items.Count) - return; + // If the anchor element is at the beginning of, or before, the start of the viewport + // then we can recycle all elements before it. + if (u <= viewport.anchorU) + _realizedElements.RecycleElementsBefore(viewport.anchorIndex, _recycleElement); + + // Start at the anchor element and move forwards, realizing elements. do { var e = GetOrCreateElement(items, index); @@ -501,6 +520,34 @@ namespace Avalonia.Controls u += sizeU; ++index; } while (u < viewport.viewportUEnd && index < items.Count); + + // Store the last index and end U position for the desired size calculation. + viewport.lastIndex = index - 1; + viewport.realizedEndU = u; + + // We can now recycle elements after the last element. + _realizedElements.RecycleElementsAfter(viewport.lastIndex, _recycleElement); + + // Next move backwards from the anchor element, realizing elements. + index = viewport.anchorIndex - 1; + u = viewport.anchorU; + + while (u > viewport.viewportUStart && index >= 0) + { + var e = GetOrCreateElement(items, index); + e.Measure(availableSize); + + var sizeU = horizontal ? e.DesiredSize.Width : e.DesiredSize.Height; + var sizeV = horizontal ? e.DesiredSize.Height : e.DesiredSize.Width; + u -= sizeU; + + _measureElements!.Add(index, e, u, sizeU); + viewport.measuredV = Math.Max(viewport.measuredV, sizeV); + --index; + } + + // We can now recycle elements before the first element. + _realizedElements.RecycleElementsBefore(index + 1, _recycleElement); } private Control GetOrCreateElement(IReadOnlyList items, int index) @@ -515,8 +562,8 @@ namespace Avalonia.Controls private Control? GetRealizedElement(int index) { - if (_anchorIndex == index) - return _anchorElement; + if (_scrollToIndex == index) + return _scrollToElement; return _realizedElements?.GetElement(index); } @@ -554,13 +601,15 @@ namespace Avalonia.Controls var generator = ItemContainerGenerator!; var item = items[index]; - if (_unrealizedFocusedIndex == index) + if (_unrealizedFocusedIndex == index && _unrealizedFocusedElement is not null) { var element = _unrealizedFocusedElement; + _unrealizedFocusedElement.LostFocus -= OnUnrealizedFocusedElementLostFocus; _unrealizedFocusedElement = null; _unrealizedFocusedIndex = -1; return element; } + if (_recyclePool?.Count > 0) { var recycled = _recyclePool.Pop(); @@ -588,16 +637,12 @@ namespace Avalonia.Controls return container; } - private double GetOrEstimateElementPosition(int index) - { - var estimatedElementSize = EstimateElementSizeU(); - return index * estimatedElementSize; - } - private void RecycleElement(Control element, int index) { Debug.Assert(ItemContainerGenerator is not null); + _scrollViewer?.UnregisterAnchorCandidate(element); + if (element.IsSet(ItemIsOwnContainerProperty)) { element.IsVisible = false; @@ -606,6 +651,7 @@ namespace Avalonia.Controls { _unrealizedFocusedElement = element; _unrealizedFocusedIndex = index; + _unrealizedFocusedElement.LostFocus += OnUnrealizedFocusedElementLostFocus; } else { @@ -646,7 +692,7 @@ namespace Avalonia.Controls var oldViewportStart = vertical ? _viewport.Top : _viewport.Left; var oldViewportEnd = vertical ? _viewport.Bottom : _viewport.Right; - _viewport = e.EffectiveViewport; + _viewport = e.EffectiveViewport.Intersect(new(Bounds.Size)); _isWaitingForViewportUpdate = false; var newViewportStart = vertical ? _viewport.Top : _viewport.Left; @@ -692,6 +738,17 @@ namespace Avalonia.Controls Invalidate(c); } + private void OnUnrealizedFocusedElementLostFocus(object? sender, RoutedEventArgs e) + { + if (_unrealizedFocusedElement is null || sender != _unrealizedFocusedElement) + return; + + _unrealizedFocusedElement.LostFocus -= OnUnrealizedFocusedElementLostFocus; + RecycleElement(_unrealizedFocusedElement, _unrealizedFocusedIndex); + _unrealizedFocusedElement = null; + _unrealizedFocusedIndex = -1; + } + /// public IReadOnlyList GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment snapPointsAlignment) { @@ -853,455 +910,16 @@ namespace Avalonia.Controls return snapPoint; } - /// - /// Stores the realized element state for a . - /// - internal class RealizedElementList - { - private int _firstIndex; - private List? _elements; - private List? _sizes; - private double _startU; - private bool _startUUnstable; - - /// - /// Gets the number of realized elements. - /// - public int Count => _elements?.Count ?? 0; - - /// - /// Gets the index of the first realized element, or -1 if no elements are realized. - /// - public int FirstIndex => _elements?.Count > 0 ? _firstIndex : -1; - - /// - /// Gets the index of the last realized element, or -1 if no elements are realized. - /// - public int LastIndex => _elements?.Count > 0 ? _firstIndex + _elements.Count - 1 : -1; - - /// - /// Gets the elements. - /// - public IReadOnlyList Elements => _elements ??= new List(); - - /// - /// Gets the sizes of the elements on the primary axis. - /// - public IReadOnlyList SizeU => _sizes ??= new List(); - - /// - /// Gets the position of the first element on the primary axis. - /// - public double StartU => _startU; - - /// - /// Adds a newly realized element to the collection. - /// - /// The index of the element. - /// The element. - /// The position of the elemnt on the primary axis. - /// The size of the element on the primary axis. - public void Add(int index, Control element, double u, double sizeU) - { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - _elements ??= new List(); - _sizes ??= new List(); - - if (Count == 0) - { - _elements.Add(element); - _sizes.Add(sizeU); - _startU = u; - _firstIndex = index; - } - else if (index == LastIndex + 1) - { - _elements.Add(element); - _sizes.Add(sizeU); - } - else if (index == FirstIndex - 1) - { - --_firstIndex; - _elements.Insert(0, element); - _sizes.Insert(0, sizeU); - _startU = u; - } - else - { - throw new NotSupportedException("Can only add items to the beginning or end of realized elements."); - } - } - - /// - /// Gets the element at the specified index, if realized. - /// - /// The index in the source collection of the element to get. - /// The element if realized; otherwise null. - public Control? GetElement(int index) - { - var i = index - FirstIndex; - if (i >= 0 && i < _elements?.Count) - return _elements[i]; - return null; - } - - /// - /// Gets the index and start U position of the element at the specified U position. - /// - /// The U position. - /// - /// A tuple containing: - /// - The index of the item at the specified U position, or -1 if the item could not be - /// determined - /// - The U position of the start of the item, if determined - /// - public (int index, double position) GetIndexAt(double u) - { - if (_elements is null || _sizes is null || _startU > u || _startUUnstable) - return (-1, 0); - - var index = 0; - var position = _startU; - - while (index < _elements.Count) - { - var size = _sizes[index]; - if (double.IsNaN(size)) - break; - if (u >= position && u < position + size) - return (index + FirstIndex, position); - position += size; - ++index; - } - - return (-1, 0); - } - - /// - /// Gets the element at the specified position on the primary axis, if realized. - /// - /// The position. - /// - /// A tuple containing the index of the element (or -1 if not found) and the position of the element on the - /// primary axis. - /// - public (int index, double position) GetElementAt(double position) - { - if (_sizes is null || position < StartU) - return (-1, 0); - - var u = StartU; - var i = FirstIndex; - - foreach (var size in _sizes) - { - var endU = u + size; - if (position < endU) - return (i, u); - u += size; - ++i; - } - - return (-1, 0); - } - - /// - /// Estimates the average U size of all elements in the source collection based on the - /// realized elements. - /// - /// - /// The estimated U size of an element, or -1 if not enough information is present to make - /// an estimate. - /// - public double EstimateElementSizeU() - { - var total = 0.0; - var divisor = 0.0; - - // Start by averaging the size of the elements before the first realized element. - if (FirstIndex >= 0 && !_startUUnstable) - { - total += _startU; - divisor += FirstIndex; - } - - // Average the size of the realized elements. - if (_sizes is not null) - { - foreach (var size in _sizes) - { - if (double.IsNaN(size)) - continue; - total += size; - ++divisor; - } - } - - // We don't have any elements on which to base our estimate. - if (divisor == 0 || total == 0) - return -1; - - return total / divisor; - } - - /// - /// Gets the index of the specified element. - /// - /// The element. - /// The index or -1 if the element is not present in the collection. - public int GetIndex(Control element) - { - return _elements?.IndexOf(element) is int index && index >= 0 ? index + FirstIndex : -1; - } - - /// - /// Updates the elements in response to items being inserted into the source collection. - /// - /// The index in the source collection of the insert. - /// The number of items inserted. - /// A method used to update the element indexes. - public void ItemsInserted(int index, int count, Action updateElementIndex) - { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - if (_elements is null || _elements.Count == 0) - return; - - // Get the index within the realized _elements collection. - var first = FirstIndex; - var realizedIndex = index - first; - - if (realizedIndex < Count) - { - // The insertion point affects the realized elements. Update the index of the - // elements after the insertion point. - var elementCount = _elements.Count; - var start = Math.Max(realizedIndex, 0); - var newIndex = realizedIndex + count; - - for (var i = start; i < elementCount; ++i) - { - if (_elements[i] is Control element) - updateElementIndex(element, newIndex - count, newIndex); - ++newIndex; - } - - if (realizedIndex < 0) - { - // The insertion point was before the first element, update the first index. - _firstIndex += count; - } - else - { - // The insertion point was within the realized elements, insert an empty space - // in _elements and _sizes. - _elements!.InsertMany(realizedIndex, null, count); - _sizes!.InsertMany(realizedIndex, double.NaN, count); - } - } - } - - /// - /// Updates the elements in response to items being removed from the source collection. - /// - /// The index in the source collection of the remove. - /// The number of items removed. - /// A method used to update the element indexes. - /// A method used to recycle elements. - public void ItemsRemoved( - int index, - int count, - Action updateElementIndex, - Action recycleElement) - { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - if (_elements is null || _elements.Count == 0) - return; - - // Get the removal start and end index within the realized _elements collection. - var first = FirstIndex; - var last = LastIndex; - var startIndex = index - first; - var endIndex = (index + count) - first; - - if (endIndex < 0) - { - // The removed range was before the realized elements. Update the first index and - // the indexes of the realized elements. - _firstIndex -= count; - _startUUnstable = true; - - var newIndex = _firstIndex; - for (var i = 0; i < _elements.Count; ++i) - { - if (_elements[i] is Control element) - updateElementIndex(element, newIndex - count, newIndex); - ++newIndex; - } - } - else if (startIndex < _elements.Count) - { - // Recycle and remove the affected elements. - var start = Math.Max(startIndex, 0); - var end = Math.Min(endIndex, _elements.Count); - - for (var i = start; i < end; ++i) - { - if (_elements[i] is Control element) - recycleElement(element); - } - - _elements.RemoveRange(start, end - start); - _sizes!.RemoveRange(start, end - start); - - // If the remove started before and ended within our realized elements, then our new - // first index will be the index where the remove started. Mark StartU as unstable - // because we can't rely on it now to estimate element heights. - if (startIndex <= 0 && end < last) - { - _firstIndex = first = index; - _startUUnstable = true; - } - - // Update the indexes of the elements after the removed range. - end = _elements.Count; - var newIndex = first + start; - for (var i = start; i < end; ++i) - { - if (_elements[i] is Control element) - updateElementIndex(element, newIndex + count, newIndex); - ++newIndex; - } - } - } - - /// - /// Recycles all elements in response to the source collection being reset. - /// - /// A method used to recycle elements. - public void ItemsReset(Action recycleElement) - { - if (_elements is null || _elements.Count == 0) - return; - - foreach (var e in _elements) - { - if (e is not null) - recycleElement(e); - } - - _startU = _firstIndex = 0; - _elements?.Clear(); - _sizes?.Clear(); - - } - - /// - /// Recycles elements before a specific index. - /// - /// The index in the source collection of new first element. - /// A method used to recycle elements. - public void RecycleElementsBefore(int index, Action recycleElement) - { - if (index <= FirstIndex || _elements is null || _elements.Count == 0) - return; - - if (index > LastIndex) - { - RecycleAllElements(recycleElement); - } - else - { - var endIndex = index - FirstIndex; - - for (var i = 0; i < endIndex; ++i) - { - if (_elements[i] is Control e) - recycleElement(e, i + FirstIndex); - } - - _elements.RemoveRange(0, endIndex); - _sizes!.RemoveRange(0, endIndex); - _firstIndex = index; - } - } - - /// - /// Recycles elements after a specific index. - /// - /// The index in the source collection of new last element. - /// A method used to recycle elements. - public void RecycleElementsAfter(int index, Action recycleElement) - { - if (index >= LastIndex || _elements is null || _elements.Count == 0) - return; - - if (index < FirstIndex) - { - RecycleAllElements(recycleElement); - } - else - { - var startIndex = (index + 1) - FirstIndex; - var count = _elements.Count; - - for (var i = startIndex; i < count; ++i) - { - if (_elements[i] is Control e) - recycleElement(e, i + FirstIndex); - } - - _elements.RemoveRange(startIndex, _elements.Count - startIndex); - _sizes!.RemoveRange(startIndex, _sizes.Count - startIndex); - } - } - - /// - /// Recycles all realized elements. - /// - /// A method used to recycle elements. - public void RecycleAllElements(Action recycleElement) - { - if (_elements is null || _elements.Count == 0) - return; - - var i = FirstIndex; - - foreach (var e in _elements) - { - if (e is not null) - recycleElement(e, i); - ++i; - } - - _startU = _firstIndex = 0; - _elements?.Clear(); - _sizes?.Clear(); - } - - /// - /// Resets the element list and prepares it for reuse. - /// - public void ResetForReuse() - { - _startU = _firstIndex = 0; - _startUUnstable = false; - _elements?.Clear(); - _sizes?.Clear(); - } - } - private struct MeasureViewport { - public int firstIndex; - public int lastIndex; + public int anchorIndex; + public double anchorU; public double viewportUStart; public double viewportUEnd; public double measuredV; - public double startU; + public double realizedEndU; + public int lastIndex; + public bool viewportIsDisjunct; } } } diff --git a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs index 319b0da7bf..90749caf6f 100644 --- a/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs +++ b/src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs @@ -43,6 +43,8 @@ namespace Avalonia.Headless _framesPerSecond = framesPerSecond; } + public override bool RunsInBackground => false; + public void ForceTick() => _forceTick?.Invoke(); } diff --git a/src/Avalonia.Headless/HeadlessPlatformStubs.cs b/src/Avalonia.Headless/HeadlessPlatformStubs.cs index ee4cd5af98..aa400ab3e6 100644 --- a/src/Avalonia.Headless/HeadlessPlatformStubs.cs +++ b/src/Avalonia.Headless/HeadlessPlatformStubs.cs @@ -179,8 +179,7 @@ namespace Avalonia.Headless return true; } - public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, - FontFamily fontFamily, CultureInfo culture, out Typeface typeface) + public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, CultureInfo culture, out Typeface typeface) { typeface = new Typeface("Arial", fontStyle, fontWeight, fontStretch); return true; diff --git a/src/Avalonia.X11/Glx/GlxContext.cs b/src/Avalonia.X11/Glx/GlxContext.cs index b1cb330b00..035354f337 100644 --- a/src/Avalonia.X11/Glx/GlxContext.cs +++ b/src/Avalonia.X11/Glx/GlxContext.cs @@ -106,8 +106,8 @@ namespace Avalonia.X11.Glx var success = false; try { - var old = new RestoreContext(Glx, _x11.Display, _lock); - if (!Glx.MakeContextCurrent(_x11.Display, xid, xid, Handle)) + var old = new RestoreContext(Glx, _x11.DeferredDisplay, _lock); + if (!Glx.MakeContextCurrent(_x11.DeferredDisplay, xid, xid, Handle)) throw new OpenGlException("glXMakeContextCurrent failed "); success = true; @@ -124,9 +124,9 @@ namespace Avalonia.X11.Glx public void Dispose() { - Glx.DestroyContext(_x11.Display, Handle); + Glx.DestroyContext(_x11.DeferredDisplay, Handle); if (_ownsPBuffer) - Glx.DestroyPbuffer(_x11.Display, _defaultXid); + Glx.DestroyPbuffer(_x11.DeferredDisplay, _defaultXid); } public object TryGetFeature(Type featureType) diff --git a/src/Avalonia.X11/Glx/GlxDisplay.cs b/src/Avalonia.X11/Glx/GlxDisplay.cs index 7749749eaa..9bcd8f3763 100644 --- a/src/Avalonia.X11/Glx/GlxDisplay.cs +++ b/src/Avalonia.X11/Glx/GlxDisplay.cs @@ -22,7 +22,7 @@ namespace Avalonia.X11.Glx { _x11 = x11; _probeProfiles = probeProfiles.ToArray(); - _displayExtensions = Glx.GetExtensions(_x11.Display); + _displayExtensions = Glx.GetExtensions(_x11.DeferredDisplay); var baseAttribs = new[] { @@ -46,12 +46,12 @@ namespace Avalonia.X11.Glx baseAttribs, }) { - var ptr = Glx.ChooseFBConfig(_x11.Display, x11.DefaultScreen, + var ptr = Glx.ChooseFBConfig(_x11.DeferredDisplay, x11.DefaultScreen, attribs, out var count); for (var c = 0 ; c < count; c++) { - var visual = Glx.GetVisualFromFBConfig(_x11.Display, ptr[c]); + var visual = Glx.GetVisualFromFBConfig(_x11.DeferredDisplay, ptr[c]); // We prefer 32 bit visuals if (_fbconfig == IntPtr.Zero || visual->depth == 32) { @@ -71,17 +71,17 @@ namespace Avalonia.X11.Glx if (_visual == null) throw new OpenGlException("Unable to get visual info from FBConfig"); - if (Glx.GetFBConfigAttrib(_x11.Display, _fbconfig, GLX_SAMPLES, out var samples) == 0) + if (Glx.GetFBConfigAttrib(_x11.DeferredDisplay, _fbconfig, GLX_SAMPLES, out var samples) == 0) sampleCount = samples; - if (Glx.GetFBConfigAttrib(_x11.Display, _fbconfig, GLX_STENCIL_SIZE, out var stencil) == 0) + if (Glx.GetFBConfigAttrib(_x11.DeferredDisplay, _fbconfig, GLX_STENCIL_SIZE, out var stencil) == 0) stencilSize = stencil; var attributes = new[] { GLX_PBUFFER_WIDTH, 1, GLX_PBUFFER_HEIGHT, 1, 0 }; - Glx.CreatePbuffer(_x11.Display, _fbconfig, attributes); - Glx.CreatePbuffer(_x11.Display, _fbconfig, attributes); + Glx.CreatePbuffer(_x11.DeferredDisplay, _fbconfig, attributes); + Glx.CreatePbuffer(_x11.DeferredDisplay, _fbconfig, attributes); - XLib.XFlush(_x11.Display); + XLib.XFlush(_x11.DeferredDisplay); DeferredContext = CreateContext(CreatePBuffer(), null, sampleCount, stencilSize, true); @@ -108,7 +108,7 @@ namespace Avalonia.X11.Glx private IntPtr CreatePBuffer() { - return Glx.CreatePbuffer(_x11.Display, _fbconfig, new[] { GLX_PBUFFER_WIDTH, 1, GLX_PBUFFER_HEIGHT, 1, 0 }); + return Glx.CreatePbuffer(_x11.DeferredDisplay, _fbconfig, new[] { GLX_PBUFFER_WIDTH, 1, GLX_PBUFFER_HEIGHT, 1, 0 }); } public GlxContext CreateContext() => CreateContext(CreatePBuffer(), null, DeferredContext.SampleCount, @@ -139,7 +139,7 @@ namespace Avalonia.X11.Glx try { - handle = Glx.CreateContextAttribsARB(_x11.Display, _fbconfig, sharelist, true, attrs); + handle = Glx.CreateContextAttribsARB(_x11.DeferredDisplay, _fbconfig, sharelist, true, attrs); if (handle != IntPtr.Zero) { _version = profile; @@ -181,6 +181,6 @@ namespace Avalonia.X11.Glx throw new OpenGlException("Unable to create direct GLX context"); } - public void SwapBuffers(IntPtr xid) => Glx.SwapBuffers(_x11.Display, xid); + public void SwapBuffers(IntPtr xid) => Glx.SwapBuffers(_x11.DeferredDisplay, xid); } } diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index e4e5dbfcb8..fc8b6f2ed6 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -50,9 +50,8 @@ namespace Avalonia.X11 useXim = true; } - // XIM doesn't work at all otherwise - if (useXim) - setlocale(0, ""); + // We have problems with text input otherwise + setlocale(0, ""); XInitThreads(); Display = XOpenDisplay(IntPtr.Zero); diff --git a/src/Avalonia.X11/X11Window.Ime.cs b/src/Avalonia.X11/X11Window.Ime.cs index 26ead5e6b8..4ee418fd0a 100644 --- a/src/Avalonia.X11/X11Window.Ime.cs +++ b/src/Avalonia.X11/X11Window.Ime.cs @@ -89,7 +89,7 @@ namespace Avalonia.X11 } } - private void UpdateImePosition() => _imeControl?.UpdateWindowInfo(Position, RenderScaling); + private void UpdateImePosition() => _imeControl?.UpdateWindowInfo(_position ?? default, RenderScaling); private void HandleKeyEvent(ref XEvent ev) { diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 8bd84215ed..e5373ce42f 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -48,6 +48,7 @@ namespace Avalonia.X11 private readonly X11NativeControlHost _nativeControlHost; private PixelPoint? _position; private PixelSize _realSize; + private bool _cleaningUp; private IntPtr _handle; private IntPtr _xic; private IntPtr _renderHandle; @@ -124,7 +125,7 @@ namespace Avalonia.X11 if (!_popup && Screen != null) { var monitor = Screen.AllScreens.OrderBy(x => x.Scaling) - .FirstOrDefault(m => m.Bounds.Contains(Position)); + .FirstOrDefault(m => m.Bounds.Contains(_position ?? default)); if (monitor != null) { @@ -183,7 +184,7 @@ namespace Avalonia.X11 surfaces.Insert(0, new EglGlPlatformSurface(new SurfaceInfo(this, _x11.DeferredDisplay, _handle, _renderHandle))); if (glx != null) - surfaces.Insert(0, new GlxGlPlatformSurface(new SurfaceInfo(this, _x11.Display, _handle, _renderHandle))); + surfaces.Insert(0, new GlxGlPlatformSurface(new SurfaceInfo(this, _x11.DeferredDisplay, _handle, _renderHandle))); surfaces.Add(Handle); @@ -326,23 +327,16 @@ namespace Avalonia.X11 { get { - XGetWindowProperty(_x11.Display, _handle, _x11.Atoms._NET_FRAME_EXTENTS, IntPtr.Zero, - new IntPtr(4), false, (IntPtr)Atom.AnyPropertyType, out var _, - out var _, out var nitems, out var _, out var prop); + var extents = GetFrameExtents(); - if (nitems.ToInt64() != 4) + if(extents == null) { - // Window hasn't been mapped by the WM yet, so can't get the extents. return null; } - var data = (IntPtr*)prop.ToPointer(); - var extents = new Thickness(data[0].ToInt32(), data[2].ToInt32(), data[1].ToInt32(), data[3].ToInt32()); - XFree(prop); - return new Size( - (_realSize.Width + extents.Left + extents.Right) / RenderScaling, - (_realSize.Height + extents.Top + extents.Bottom) / RenderScaling); + (_realSize.Width + extents.Value.Left + extents.Value.Right) / RenderScaling, + (_realSize.Height + extents.Value.Top + extents.Value.Bottom) / RenderScaling); } } @@ -529,7 +523,7 @@ namespace Avalonia.X11 else if (ev.type == XEventName.DestroyNotify && ev.DestroyWindowEvent.window == _handle) { - Cleanup(); + Cleanup(true); } else if (ev.type == XEventName.ClientMessage) { @@ -556,6 +550,25 @@ namespace Avalonia.X11 } } + private Thickness? GetFrameExtents() + { + XGetWindowProperty(_x11.Display, _handle, _x11.Atoms._NET_FRAME_EXTENTS, IntPtr.Zero, + new IntPtr(4), false, (IntPtr)Atom.AnyPropertyType, out var _, + out var _, out var nitems, out var _, out var prop); + + if (nitems.ToInt64() != 4) + { + // Window hasn't been mapped by the WM yet, so can't get the extents. + return null; + } + + var data = (IntPtr*)prop.ToPointer(); + var extents = new Thickness(data[0].ToInt32(), data[2].ToInt32(), data[1].ToInt32(), data[3].ToInt32()); + XFree(prop); + + return extents; + } + private bool UpdateScaling(bool skipResize = false) { double newScaling; @@ -564,7 +577,7 @@ namespace Avalonia.X11 else { var monitor = _platform.X11Screens.Screens.OrderBy(x => x.Scaling) - .FirstOrDefault(m => m.Bounds.Contains(Position)); + .FirstOrDefault(m => m.Bounds.Contains(_position ?? default)); newScaling = monitor?.Scaling ?? RenderScaling; } @@ -804,7 +817,7 @@ namespace Avalonia.X11 public void Dispose() { - Cleanup(); + Cleanup(false); } public virtual object? TryGetFeature(Type featureType) @@ -837,8 +850,17 @@ namespace Avalonia.X11 return null; } - private void Cleanup() + private void Cleanup(bool fromDestroyNotification) { + // Prevent reentrancy + if(_cleaningUp) + return; + _cleaningUp = true; + + // Before doing anything else notify the TopLevel that ITopLevelImpl is no longer valid + if (_handle != IntPtr.Zero) + Closed?.Invoke(); + if (_rawEventGrouper != null) { _rawEventGrouper.Dispose(); @@ -876,10 +898,10 @@ namespace Avalonia.X11 _platform.XI2?.OnWindowDestroyed(_handle); var handle = _handle; _handle = IntPtr.Zero; - Closed?.Invoke(); _mouse.Dispose(); _touch.Dispose(); - XDestroyWindow(_x11.Display, handle); + if (!fromDestroyNotification) + XDestroyWindow(_x11.Display, handle); } if (_useRenderWindow && _renderHandle != IntPtr.Zero) @@ -916,11 +938,11 @@ namespace Avalonia.X11 public void Hide() => XUnmapWindow(_x11.Display, _handle); - public Point PointToClient(PixelPoint point) => new Point((point.X - Position.X) / RenderScaling, (point.Y - Position.Y) / RenderScaling); + public Point PointToClient(PixelPoint point) => new Point((point.X - (_position ?? default).X) / RenderScaling, (point.Y - (_position ?? default).Y) / RenderScaling); public PixelPoint PointToScreen(Point point) => new PixelPoint( - (int)(point.X * RenderScaling + Position.X), - (int)(point.Y * RenderScaling + Position.Y)); + (int)(point.X * RenderScaling + (_position ?? default).X), + (int)(point.Y * RenderScaling + (_position ?? default).Y)); public void SetSystemDecorations(SystemDecorations enabled) { @@ -984,20 +1006,36 @@ namespace Avalonia.X11 public PixelPoint Position { - get => _position ?? default; + get + { + if(_position == null) + { + return default; + } + + var extents = GetFrameExtents(); + + if(extents == null) + { + extents = default(Thickness); + } + + return new PixelPoint(_position.Value.X - (int)extents.Value.Left, _position.Value.Y - (int)extents.Value.Top); + } set { - if(!_usePositioningFlags) + if (!_usePositioningFlags) { _usePositioningFlags = true; UpdateSizeHints(null); } - + var changes = new XWindowChanges { - x = (int)value.X, + x = value.X, y = (int)value.Y }; + XConfigureWindow(_x11.Display, _handle, ChangeWindowFlags.CWX | ChangeWindowFlags.CWY, ref changes); XFlush(_x11.Display); @@ -1006,7 +1044,6 @@ namespace Avalonia.X11 _position = value; PositionChanged?.Invoke(value); } - } } diff --git a/src/Skia/Avalonia.Skia/FontManagerImpl.cs b/src/Skia/Avalonia.Skia/FontManagerImpl.cs index 29e5687423..a97a198621 100644 --- a/src/Skia/Avalonia.Skia/FontManagerImpl.cs +++ b/src/Skia/Avalonia.Skia/FontManagerImpl.cs @@ -30,8 +30,7 @@ namespace Avalonia.Skia [ThreadStatic] private static string[]? t_languageTagBuffer; public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, - FontWeight fontWeight, FontStretch fontStretch, - FontFamily? fontFamily, CultureInfo? culture, out Typeface fontKey) + FontWeight fontWeight, FontStretch fontStretch, CultureInfo? culture, out Typeface fontKey) { SKFontStyle skFontStyle; @@ -60,35 +59,13 @@ namespace Avalonia.Skia t_languageTagBuffer[0] = culture.TwoLetterISOLanguageName; t_languageTagBuffer[1] = culture.ThreeLetterISOLanguageName; - if (fontFamily is not null && fontFamily.FamilyNames.HasFallbacks) - { - var familyNames = fontFamily.FamilyNames; - - for (var i = 1; i < familyNames.Count; i++) - { - var skTypeface = - _skFontManager.MatchCharacter(familyNames[i], skFontStyle, t_languageTagBuffer, codepoint); - - if (skTypeface == null) - { - continue; - } + var skTypeface = _skFontManager.MatchCharacter(null, skFontStyle, t_languageTagBuffer, codepoint); - fontKey = new Typeface(skTypeface.FamilyName, fontStyle, fontWeight, fontStretch); - - return true; - } - } - else + if (skTypeface != null) { - var skTypeface = _skFontManager.MatchCharacter(null, skFontStyle, t_languageTagBuffer, codepoint); + fontKey = new Typeface(skTypeface.FamilyName, fontStyle, fontWeight, fontStretch); - if (skTypeface != null) - { - fontKey = new Typeface(skTypeface.FamilyName, fontStyle, fontWeight, fontStretch); - - return true; - } + return true; } fontKey = default; @@ -96,7 +73,7 @@ namespace Avalonia.Skia return false; } - public bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight, + public bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight, FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) { glyphTypeface = null; @@ -111,12 +88,6 @@ namespace Avalonia.Skia return false; } - //MatchFamily can return a font other than we requested so we have to verify we got the expected. - if (!skTypeface.FamilyName.ToLower(CultureInfo.InvariantCulture).Equals(familyName.ToLower(CultureInfo.InvariantCulture), StringComparison.Ordinal)) - { - return false; - } - var fontSimulations = FontSimulations.None; if ((int)weight >= 600 && !skTypeface.IsBold) diff --git a/src/Windows/Avalonia.Direct2D1/Media/FontManagerImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/FontManagerImpl.cs index ec2f6385da..85bf2b6c4c 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/FontManagerImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/FontManagerImpl.cs @@ -33,8 +33,7 @@ namespace Avalonia.Direct2D1.Media } public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, - FontWeight fontWeight, FontStretch fontStretch, - FontFamily fontFamily, CultureInfo culture, out Typeface typeface) + FontWeight fontWeight, FontStretch fontStretch, CultureInfo culture, out Typeface typeface) { var familyCount = Direct2D1FontCollectionCache.InstalledFontCollection.FontFamilyCount; diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index cf52f91785..0cb8b09579 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -85,6 +85,9 @@ namespace Avalonia.Win32 case WindowsMessage.WM_DESTROY: { + // The first and foremost thing to do - notify the TopLevel + Closed?.Invoke(); + if (UiaCoreTypesApi.IsNetComInteropAvailable) { UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, IntPtr.Zero, IntPtr.Zero, null); @@ -95,12 +98,23 @@ namespace Avalonia.Win32 { Imm32InputMethod.Current.ClearLanguageAndWindow(); } + + // Cleanup render targets + (_gl as IDisposable)?.Dispose(); + + if (_dropTarget != null) + { + OleContext.Current?.UnregisterDragDrop(Handle); + _dropTarget.Dispose(); + _dropTarget = null; + } + + _framebuffer.Dispose(); //Window doesn't exist anymore _hwnd = IntPtr.Zero; //Remove root reference to this class, so unmanaged delegate can be collected s_instances.Remove(this); - Closed?.Invoke(); _mouseDevice.Dispose(); _touchDevice.Dispose(); diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index e066bbe5f6..545513c732 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -150,7 +150,12 @@ namespace Avalonia.Win32 CreateWindow(); _framebuffer = new FramebufferManager(_hwnd); - UpdateInputMethod(GetKeyboardLayout(0)); + + if (this is not PopupImpl) + { + UpdateInputMethod(GetKeyboardLayout(0)); + } + if (glPlatform != null) { if (_isUsingComposition) @@ -619,15 +624,6 @@ namespace Avalonia.Win32 public void Dispose() { - (_gl as IDisposable)?.Dispose(); - - if (_dropTarget != null) - { - OleContext.Current?.UnregisterDragDrop(Handle); - _dropTarget.Dispose(); - _dropTarget = null; - } - if (_hwnd != IntPtr.Zero) { // Detect if we are being closed programmatically - this would mean that WM_CLOSE was not called @@ -640,8 +636,6 @@ namespace Avalonia.Win32 DestroyWindow(_hwnd); _hwnd = IntPtr.Zero; } - - _framebuffer.Dispose(); } public void Invalidate(Rect rect) diff --git a/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs b/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs index 89e609eb10..3ccec872d2 100644 --- a/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs +++ b/tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs @@ -43,7 +43,7 @@ namespace Avalonia.Base.UnitTests.Media { AvaloniaLocator.CurrentMutable.Bind().ToConstant(options); - Assert.Equal("MyFont", FontManager.Current.DefaultFontFamilyName); + Assert.Equal("MyFont", FontManager.Current.DefaultFontFamily.Name); } } diff --git a/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs b/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs index 41aaa7b670..1a0ea5fdab 100644 --- a/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs @@ -1,18 +1,25 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using Avalonia.Collections; using Avalonia.Controls.Presenters; +using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Data; using Avalonia.Input; +using Avalonia.Layout; using Avalonia.LogicalTree; +using Avalonia.Markup.Xaml.Templates; using Avalonia.Styling; using Avalonia.UnitTests; +using Avalonia.VisualTree; using Xunit; +#nullable enable + namespace Avalonia.Controls.UnitTests { public class ItemsControlTests @@ -20,12 +27,8 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Setting_ItemsSource_Should_Populate_Items() { - var target = new ItemsControl - { - Template = GetTemplate(), - ItemTemplate = new FuncDataTemplate((_, __) => new Canvas()), - ItemsSource = new[] { "foo", "bar" }, - }; + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "foo", "bar" }); Assert.NotSame(target.ItemsSource, target.Items); Assert.Equal(target.ItemsSource, target.Items); @@ -34,12 +37,9 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Cannot_Set_ItemsSource_With_Items_Present() { - var target = new ItemsControl - { - Template = GetTemplate(), - ItemTemplate = new FuncDataTemplate((_, __) => new Canvas()), - Items = { "foo", "bar" }, - }; + using var app = Start(); + var target = CreateTarget(); + target.Items.Add("foo"); Assert.Throws(() => target.ItemsSource = new[] { "baz" }); } @@ -47,12 +47,8 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Cannot_Modify_Items_When_ItemsSource_Set() { - var target = new ItemsControl - { - Template = GetTemplate(), - ItemTemplate = new FuncDataTemplate((_, __) => new Canvas()), - ItemsSource = Array.Empty(), - }; + using var app = Start(); + var target = CreateTarget(itemsSource: Array.Empty()); Assert.Throws(() => target.Items.Add("foo")); } @@ -60,18 +56,11 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Should_Use_ItemTemplate_To_Create_Control() { - var target = new ItemsControl - { - Template = GetTemplate(), - ItemTemplate = new FuncDataTemplate((_, __) => new Canvas()), - }; - - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - - var container = (ContentPresenter)target.Presenter.Panel.Children[0]; - container.UpdateChild(); + using var app = Start(); + var target = CreateTarget( + itemsSource: new[] { "Foo" }, + itemTemplate: new FuncDataTemplate((_, __) => new Canvas())); + var container = GetContainer(target); Assert.IsType(container.Child); } @@ -79,24 +68,18 @@ namespace Avalonia.Controls.UnitTests [Fact] public void ItemTemplate_Can_Be_Changed() { - var target = new ItemsControl - { - Template = GetTemplate(), - ItemTemplate = new FuncDataTemplate((_, __) => new Canvas()), - }; - - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - - var container = (ContentPresenter)target.Presenter.Panel.Children[0]; - container.UpdateChild(); + using var app = Start(); + var target = CreateTarget( + itemsSource: new[] { "Foo" }, + itemTemplate: new FuncDataTemplate((_, __) => new Canvas())); + var container = GetContainer(target); Assert.IsType(container.Child); target.ItemTemplate = new FuncDataTemplate((_, __) => new Border()); - container = (ContentPresenter)target.Presenter.Panel.Children[0]; - container.UpdateChild(); + Layout(target); + + container = GetContainer(target); Assert.IsType(container.Child); } @@ -104,40 +87,28 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Panel_Should_Have_TemplatedParent_Set_To_ItemsControl() { - var target = new ItemsControl(); + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "Foo" }); - target.Template = GetTemplate(); - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - - Assert.Equal(target, target.Presenter.Panel.TemplatedParent); + Assert.Equal(target, target.ItemsPanelRoot?.TemplatedParent); } [Fact] public void Panel_Should_Have_ItemsHost_Set_To_True() { - var target = new ItemsControl(); - - target.Template = GetTemplate(); - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter!.ApplyTemplate(); + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "Foo" }); - Assert.True(target.Presenter.Panel!.IsItemsHost); + Assert.True(target.ItemsPanelRoot?.IsItemsHost); } [Fact] public void Container_Should_Have_TemplatedParent_Set_To_Null() { - var target = new ItemsControl(); - - target.Template = GetTemplate(); - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "Foo" }); - var container = (ContentPresenter)target.Presenter.Panel.Children[0]; + var container = GetContainer(target); Assert.Null(container.TemplatedParent); } @@ -145,18 +116,13 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Container_Should_Have_Theme_Set_To_ItemContainerTheme() { + using var app = Start(); var theme = new ControlTheme { TargetType = typeof(ContentPresenter) }; - var target = new ItemsControl - { - ItemContainerTheme = theme, - }; - - target.Template = GetTemplate(); - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + var target = CreateTarget( + itemsSource: new[] { "Foo" }, + itemContainerTheme: theme); - var container = (ContentPresenter)target.Presenter.Panel.Children[0]; + var container = GetContainer(target); Assert.Same(container.Theme, theme); } @@ -164,38 +130,31 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Container_Should_Have_LogicalParent_Set_To_ItemsControl() { - using (UnitTestApplication.Start(TestServices.StyledWindow)) - { - var root = new Window(); - var target = new ItemsControl(); - - root.Content = target; - - var templatedParent = new Button(); - target.TemplatedParent = templatedParent; - target.Template = GetTemplate(); + using var app = UnitTestApplication.Start(TestServices.StyledWindow); + var target = new ItemsControl(); + var root = CreateRoot(target); + var templatedParent = new Button(); - target.ItemsSource = new[] { "Foo" }; + target.TemplatedParent = templatedParent; + target.Template = CreateItemsControlTemplate(); + target.ItemsSource = new[] { "Foo" }; - root.ApplyTemplate(); - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); + root.LayoutManager.ExecuteInitialLayoutPass(); - var container = (ContentPresenter)target.Presenter.Panel.Children[0]; + var container = GetContainer(target); - Assert.Equal(target, container.Parent); - } + Assert.Equal(target, container.Parent); } [Fact] public void Control_Item_Should_Be_Logical_Child_Before_ApplyTemplate() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }, performLayout: false); - target.Template = GetTemplate(); - target.Items.Add(child); - + Assert.False(target.IsMeasureValid); + Assert.Empty(target.GetVisualChildren()); Assert.Equal(child.Parent, target); Assert.Equal(child.GetLogicalParent(), target); Assert.Equal(new[] { child }, target.GetLogicalChildren()); @@ -204,17 +163,12 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Control_Item_Should_Be_Logical_Child_After_Layout() { - var target = new ItemsControl - { - Template = GetTemplate(), - }; - var root = new TestRoot(target); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }); - target.Template = GetTemplate(); - target.Items.Add(child); - root.LayoutManager.ExecuteInitialLayoutPass(); - + Assert.True(target.IsMeasureValid); + Assert.Single(target.GetVisualChildren()); Assert.Equal(target, child.Parent); Assert.Equal(target, child.GetLogicalParent()); Assert.Equal(new[] { child }, target.GetLogicalChildren()); @@ -223,34 +177,25 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Added_Container_Should_Have_LogicalParent_Set_To_ItemsControl() { - var item = new Border(); + using var app = Start(); var items = new ObservableCollection(); + var target = CreateTarget(itemsSource: items); - var target = new ItemsControl - { - Template = GetTemplate(), - ItemsSource = items, - }; - - var root = new TestRoot(true, target); - - root.Measure(new Size(100, 100)); - root.Arrange(new Rect(0, 0, 100, 100)); - + var item = new Border(); items.Add(item); Assert.Equal(target, item.Parent); } [Fact] - public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTemplate() + public void Control_Item_Can_Be_Removed_From_Logical_Children_Before_ApplyTemplate() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }, performLayout: false); - target.Template = GetTemplate(); - target.Items.Add(child); - + Assert.False(target.IsMeasureValid); + Assert.Empty(target.GetVisualChildren()); Assert.Single(target.GetLogicalChildren()); target.Items.RemoveAt(0); @@ -263,25 +208,26 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Clearing_Items_Should_Clear_Child_Controls_Parent_Before_ApplyTemplate() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }, performLayout: false); - target.Template = GetTemplate(); - target.Items.Add(child); - + Assert.False(target.IsMeasureValid); + Assert.Empty(target.GetVisualChildren()); Assert.Single(target.GetLogicalChildren()); target.Items.Clear(); Assert.Null(child.Parent); - Assert.Null(((ILogical)child).LogicalParent); + Assert.Null(child.GetLogicalParent()); } [Fact] public void Assigning_ItemsSource_Should_Not_Fire_LogicalChildren_CollectionChanged_Before_ApplyTemplate() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(itemsSource: new[] { child }, performLayout: false); var called = false; ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; @@ -292,11 +238,27 @@ namespace Avalonia.Controls.UnitTests Assert.False(called); } + [Fact] + public void Removing_ItemsSource_Items_Should_Not_Fire_LogicalChildren_CollectionChanged_Before_ApplyTemplate() + { + using var app = Start(); + var items = new AvaloniaList { "Foo", "Bar" }; + var target = CreateTarget(itemsSource: items, performLayout: false); + var called = false; + + ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; + + items.Remove("Bar"); + + Assert.False(called); + } + [Fact] public void Changing_ItemsSource_Should_Not_Fire_LogicalChildren_CollectionChanged_Before_ApplyTemplate() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(itemsSource: new[] { child }, performLayout: false); var called = false; ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; @@ -311,12 +273,10 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Clearing_Items_Should_Clear_Child_Controls_Parent() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }); - target.Template = GetTemplate(); - target.Items.Add(child); - target.ApplyTemplate(); target.Items.Clear(); Assert.Null(child.Parent); @@ -326,16 +286,14 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); - - target.Template = GetTemplate(); - target.Items.Add(child); + var target = CreateTarget(items: new[] { child }, performLayout: false); // Should appear both before and after applying template. Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren()); - target.ApplyTemplate(); + Layout(target); Assert.Equal(new ILogical[] { child }, target.GetLogicalChildren()); } @@ -343,14 +301,10 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Adding_String_Item_Should_Make_ContentPresenter_Appear_In_LogicalChildren() { - var target = new ItemsControl(); - - target.Template = GetTemplate(); - target.ItemsSource = new[] { "Foo" }; - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "Foo " }); var logical = (ILogical)target; + Assert.Equal(1, logical.LogicalChildren.Count); Assert.IsType(logical.LogicalChildren[0]); } @@ -358,16 +312,17 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged() { - var target = new ItemsControl(); - var child = new Control(); + using var app = Start(); + var target = CreateTarget(); var called = false; - target.Template = GetTemplate(); + target.Template = CreateItemsControlTemplate(); target.ApplyTemplate(); ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = e.Action == NotifyCollectionChangedAction.Add; + var child = new Control(); target.Items.Add(child); Assert.True(called); @@ -376,14 +331,11 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Clearing_Items_Should_Fire_LogicalChildren_CollectionChanged() { - var target = new ItemsControl(); + using var app = Start(); var child = new Control(); + var target = CreateTarget(items: new[] { child }); var called = false; - target.Template = GetTemplate(); - target.Items.Add(child); - target.ApplyTemplate(); - ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = e.Action == NotifyCollectionChangedAction.Remove; @@ -392,35 +344,16 @@ namespace Avalonia.Controls.UnitTests Assert.True(called); } - [Fact] - public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged() - { - var target = new ItemsControl(); - var items = new AvaloniaList { "Foo", "Bar" }; - var called = false; - - target.Template = GetTemplate(); - target.ItemsSource = items; - - ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true; - - items.Remove("Bar"); - - Assert.False(called); - } - [Fact] public void LogicalChildren_Should_Not_Change_Instance_When_Template_Changed() { - var target = new ItemsControl() - { - Template = GetTemplate(), - }; - + using var app = Start(); + var target = CreateTarget(); var before = ((ILogical)target).LogicalChildren; target.Template = null; - target.Template = GetTemplate(); + target.Template = CreateItemsControlTemplate(); + Layout(target); var after = ((ILogical)target).LogicalChildren; @@ -432,19 +365,13 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Should_Clear_Containers_When_ItemsPresenter_Changes() { - var target = new ItemsControl - { - ItemsSource = new[] { "foo", "bar" }, - Template = GetTemplate(), - }; + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { "foo", "bar" }); + var panel = Assert.IsAssignableFrom(target.Presenter?.Panel); - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - - var panel = target.Presenter.Panel; Assert.Equal(2, panel.Children.Count()); - target.Template = GetTemplate(); + target.Template = CreateItemsControlTemplate(); target.ApplyTemplate(); Assert.Empty(panel.Children); @@ -453,10 +380,8 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Empty_Class_Should_Initially_Be_Applied() { - var target = new ItemsControl() - { - Template = GetTemplate(), - }; + using var app = Start(); + var target = CreateTarget(performLayout: false); Assert.Contains(":empty", target.Classes); } @@ -464,34 +389,26 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Empty_Class_Should_Be_Cleared_When_Items_Added() { - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = new[] { 1, 2, 3 }, - }; + using var app = Start(); + var target = CreateTarget(items: new[] { 1, 2, 3 }, performLayout: false); Assert.DoesNotContain(":empty", target.Classes); } [Fact] - public void Empty_Class_Should_Be_Set_When_Items_Not_Set() + public void Empty_Class_Should_Be_Cleared_When_ItemsSource_Items_Added() { - var target = new ItemsControl() - { - Template = GetTemplate(), - }; + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { 1, 2, 3 }, performLayout: false); - Assert.Contains(":empty", target.Classes); + Assert.DoesNotContain(":empty", target.Classes); } [Fact] - public void Empty_Class_Should_Be_Set_When_Empty_Collection_Set() + public void Empty_Class_Should_Be_Set_When_ItemsSource_Collection_Cleared() { - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = new[] { 1, 2, 3 }, - }; + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { 1, 2, 3 }); target.ItemsSource = new int[0]; @@ -499,13 +416,10 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Item_Count_Should_Be_Set_When_Items_Added() + public void Item_Count_Should_Be_Set_When_ItemsSource_Set() { - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = new[] { 1, 2, 3 }, - }; + using var app = Start(); + var target = CreateTarget(itemsSource: new[] { 1, 2, 3 }); Assert.Equal(3, target.ItemCount); } @@ -513,13 +427,25 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Item_Count_Should_Be_Set_When_Items_Changed() { + using var app = Start(); var items = new ObservableCollection() { 1, 2, 3 }; + var target = CreateTarget(items: new[] { 1, 2, 3 }); - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + target.Items.Add(4); + + Assert.Equal(4, target.ItemCount); + + target.Items.Clear(); + + Assert.Equal(0, target.ItemCount); + } + + [Fact] + public void Item_Count_Should_Be_Set_When_ItemsSource_Items_Changed() + { + using var app = Start(); + var items = new ObservableCollection() { 1, 2, 3 }; + var target = CreateTarget(itemsSource: items); items.Add(4); @@ -533,13 +459,9 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Empty_Class_Should_Be_Set_When_Items_Collection_Cleared() { + using var app = Start(); var items = new ObservableCollection() { 1, 2, 3 }; - - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + var target = CreateTarget(itemsSource: items); items.Clear(); @@ -547,15 +469,11 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases() + public void Empty_Class_Should_Not_Be_Set_When_ItemsSource_Collection_Count_Increases() { + using var app = Start(); var items = new ObservableCollection() { }; - - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + var target = CreateTarget(itemsSource: items); items.Add(1); @@ -565,13 +483,9 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Single_Item_Class_Should_Be_Set_When_ItemsSource_Collection_Count_Increases_To_One() { + using var app = Start(); var items = new ObservableCollection() { }; - - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + var target = CreateTarget(itemsSource: items); items.Add(1); @@ -579,15 +493,11 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Cleared() + public void Empty_Class_Should_Not_Be_Set_When_ItemsSource_Collection_Cleared() { + using var app = Start(); var items = new ObservableCollection() { 1, 2, 3 }; - - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + var target = CreateTarget(itemsSource: items); items.Clear(); @@ -597,13 +507,9 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Single_Item_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases_Beyond_One() { + using var app = Start(); var items = new ObservableCollection() { 1 }; - - var target = new ItemsControl() - { - Template = GetTemplate(), - ItemsSource = items, - }; + var target = CreateTarget(itemsSource: items); items.Add(2); @@ -613,6 +519,7 @@ namespace Avalonia.Controls.UnitTests [Fact] public void DataContexts_Should_Be_Correctly_Set() { + using var app = Start(); var items = new object[] { "Foo", @@ -620,22 +527,13 @@ namespace Avalonia.Controls.UnitTests new TextBlock { Text = "Baz" }, new ListBoxItem { Content = "Qux" }, }; - - var target = new ItemsControl - { - Template = GetTemplate(), - DataContext = "Base", - DataTemplates = - { - new FuncDataTemplate((x, __) => new Button { Content = x }) - }, - ItemsSource = items, - }; - - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - - var dataContexts = target.Presenter.Panel.Children + var dataTemplate = new FuncDataTemplate((x, __) => new Button { Content = x }); + var target = CreateTarget( + dataContext: "Base", + itemsSource: items, + dataTemplates: new[] { dataTemplate }); + var panel = Assert.IsAssignableFrom(target.ItemsPanelRoot); + var dataContexts = panel.Children .Do(x => (x as ContentPresenter)?.UpdateChild()) .Cast() .Select(x => x.DataContext) @@ -649,198 +547,146 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Control_Item_Should_Not_Be_NameScope() { - var items = new object[] - { - new TextBlock(), - }; - - var target = new ItemsControl - { - Template = GetTemplate(), - ItemsSource = items, - }; - - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - + using var app = Start(); + var items = new object[] { new TextBlock() }; + var target = CreateTarget(itemsSource: items); var item = target.LogicalChildren[0]; + Assert.Null(NameScope.GetNameScope((TextBlock)item)); } [Fact] public void Focuses_Next_Item_On_Key_Down() { - using (UnitTestApplication.Start(TestServices.RealFocus)) + using var app = Start(); + var items = new object[] { - var items = new object[] - { - new Button(), - new Button(), - }; - - var target = new ItemsControl - { - Template = GetTemplate(), - ItemsSource = items, - }; + new Button(), + new Button(), + }; - var root = new TestRoot { Child = target }; + var target = CreateTarget(itemsSource: items); + GetContainer