Skip to content

Commit

Permalink
Avalonia v11 - Main window overrides and setting focus
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianSuess committed Jul 21, 2023
1 parent c6c65fd commit baed0c2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 19 deletions.
46 changes: 46 additions & 0 deletions Avalonia-v11-Update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Avalonia v11 Upgrade

[Avalonia v11 Upgrade](https://docs.avaloniaui.net/docs/next/stay-up-to-date/upgrade-from-0.10)

## TO DO

* AvaloniaLocator has been removed
* https://docs.avaloniaui.net/docs/next/stay-up-to-date/upgrade-from-0.10#locator
* https://github.com/AvaloniaUI/Avalonia/pull/11557
* `protected override ... CreateItemContainerGenerator()`
* This is not overridable. [Marked as obsolete](https://github.com/AvaloniaUI/Avalonia/blob/904b8ae287ac97968a75af64f1814b55f95b40b0/src/Avalonia.Controls/ItemsControl.cs#L635)
* `private protected virtual ItemContainerGenerator CreateItemContainerGenerator()`
* `IItemContainerGenerator` -> `ItemContainerGenerator`
* `ItemContainerEventArgs` has been **deprecated**
* [PR #9677](https://github.com/AvaloniaUI/Avalonia/pull/9677)
* [Commit 1101f28 - Refactored ItemContainerGenerator](https://github.com/AvaloniaUI/Avalonia/commit/1101f28dd7a6460764873b6cc2e9ce4da293eb61)
* `override void OnContainerMaterialized()` has been replaced by `internal override void PrepareContainerForItemOverride(...)`
* `MainWindow.HandleWindowStateChanged` has been **deprecated** in v11
* NOTE: Undocumented
* https://github.com/AvaloniaUI/Avalonia/discussions/11593
* Use `protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)`

## Stretch TO-DO

* Rename `.xaml` to `.axaml`

## Updated

* `IBitmap` -> `Bitmap`
* `FocusManager`
* `WbFb : IFramebufferPlatformSurface` now implements `CreateFramebufferRenderTarget()` - https://github.com/AvaloniaUI/Avalonia/pull/11914

### Focus Manager

`OpenListDialog.xaml.cs`

```cs
// Avalonia v0.10
//// TemplateApplied += (sender, e) => Application.Current.FocusManager.Focus(listView);
TemplateApplied += (sender, e) => {
// Avalonia v11
var focusManager = TopLevel.GetTopLevel(listView).FocusManager;
focusManager.GetFocusedElement().Focus();
};
```
6 changes: 0 additions & 6 deletions ILSpy.Core/Controls/DialogWindow.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Styling;
Expand Down
65 changes: 53 additions & 12 deletions ILSpy.Core/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using Avalonia.Markup.Xaml.Styling;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Threading;
using AvaloniaEdit;
Expand Down Expand Up @@ -330,7 +331,7 @@ private void ApplyTheme()
void SetWindowBounds(Rect bounds)
{
ClientSize = bounds.Size;
Position = PixelPoint.FromPoint(bounds.Position, PlatformImpl.DesktopScaling);
Position = PixelPoint.FromPoint(bounds.Position, DesktopScaling);
}

#region Toolbar extensibility
Expand Down Expand Up @@ -483,7 +484,8 @@ static void OnShow(AvaloniaPropertyChangedEventArgs args)
bool boundsOK = false;
foreach (var screen in instance.Screens.All)
{
var intersection = boundsRect.Intersect(screen.WorkingArea.ToRect(instance.PlatformImpl.DesktopScaling));
// Avalonia v0.10 var intersection = boundsRect.Intersect(screen.WorkingArea.ToRect(instance.PlatformImpl.DesktopScaling));
var intersection = boundsRect.Intersect(screen.WorkingArea.ToRect(instance.DesktopScaling));
if (intersection.Width > 10 && intersection.Height > 10)
boundsOK = true;
}
Expand Down Expand Up @@ -680,7 +682,14 @@ private bool CanResolveTypeInPEFile(PEFile module, ITypeReference typeRef, out E

void MainWindow_Loaded(object sender, EventArgs e)
{
Application.Current.FocusManager.Focus(this);
// Avalonia v11
//// this.FocusManager.GetFocusedElement().Focus();

var focusManager = TopLevel.GetTopLevel(this).FocusManager;
focusManager.GetFocusedElement().Focus();

// Avalonia v0.10
//// Application.Current.FocusManager.Focus(this);

InitToolbar();

Expand Down Expand Up @@ -1339,29 +1348,61 @@ void NavigateHistory(bool forward)

#endregion

protected override void HandleWindowStateChanged(WindowState state)
// Avalonia v11
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.HandleWindowStateChanged(state);
// store window state in settings only if it's not minimized
if (this.WindowState != WindowState.Minimized)
sessionSettings.WindowState = this.WindowState;
base.OnPropertyChanged(change);

if (change.Property == WindowStateProperty)
{
// store window state in settings only if it's not minimized
if (this.WindowState != WindowState.Minimized)
sessionSettings.WindowState = this.WindowState;
}
}

protected override bool HandleClosing()
// Avalonia v0.10
////protected override void HandleWindowStateChanged(WindowState state)
////{
//// base.HandleWindowStateChanged(state);
//// // store window state in settings only if it's not minimized
//// if (this.WindowState != WindowState.Minimized)
//// sessionSettings.WindowState = this.WindowState;
////}

protected override void OnClosing(WindowClosingEventArgs e)
{
sessionSettings.ActiveAssemblyList = assemblyList.ListName;
sessionSettings.ActiveTreeViewPath = GetPathForNode(treeView.SelectedItem as SharpTreeNode);
sessionSettings.ActiveAutoLoadedAssembly = GetAutoLoadedAssemblyNode(treeView.SelectedItem as SharpTreeNode);
sessionSettings.WindowBounds = new Rect(Position.ToPoint(PlatformImpl.DesktopScaling), ClientSize);
//// Avalonia v0.10: sessionSettings.WindowBounds = new Rect(Position.ToPoint(PlatformImpl.DesktopScaling), ClientSize);
sessionSettings.WindowBounds = new Rect(Position.ToPoint(DesktopScaling), ClientSize);
sessionSettings.SplitterPosition = leftColumn.Width.Value / (leftColumn.Width.Value + rightColumn.Width.Value);
if (topPane.IsVisible == true)
sessionSettings.TopPaneSplitterPosition = topPaneRow.Height.Value / (topPaneRow.Height.Value + textViewRow.Height.Value);
if (bottomPane.IsVisible == true)
sessionSettings.BottomPaneSplitterPosition = bottomPaneRow.Height.Value / (bottomPaneRow.Height.Value + textViewRow.Height.Value);
sessionSettings.Save();

return base.HandleClosing();
}
base.OnClosing(e);
}

// Avalonia v0.10
////protected override bool HandleClosing()
////{
//// sessionSettings.ActiveAssemblyList = assemblyList.ListName;
//// sessionSettings.ActiveTreeViewPath = GetPathForNode(treeView.SelectedItem as SharpTreeNode);
//// sessionSettings.ActiveAutoLoadedAssembly = GetAutoLoadedAssemblyNode(treeView.SelectedItem as SharpTreeNode);
//// sessionSettings.WindowBounds = new Rect(Position.ToPoint(PlatformImpl.DesktopScaling), ClientSize);
//// sessionSettings.SplitterPosition = leftColumn.Width.Value / (leftColumn.Width.Value + rightColumn.Width.Value);
//// if (topPane.IsVisible == true)
//// sessionSettings.TopPaneSplitterPosition = topPaneRow.Height.Value / (topPaneRow.Height.Value + textViewRow.Height.Value);
//// if (bottomPane.IsVisible == true)
//// sessionSettings.BottomPaneSplitterPosition = bottomPaneRow.Height.Value / (bottomPaneRow.Height.Value + textViewRow.Height.Value);
//// sessionSettings.Save();
////
//// return base.HandleClosing();
////}

private string GetAutoLoadedAssemblyNode(SharpTreeNode node)
{
Expand Down
2 changes: 1 addition & 1 deletion ILSpy.Core/Search/SearchPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void Show()
set { SetValue(SearchTermProperty, value ?? string.Empty); }
}

static void OnSearchTermChanged(IAvaloniaObject o, bool changed)
static void OnSearchTermChanged(AvaloniaObject o, bool changed)
{
if (changed)
{
Expand Down

0 comments on commit baed0c2

Please sign in to comment.