Skip to content

Commit

Permalink
Expose the window in a static variable
Browse files Browse the repository at this point in the history
The reason is that it can't be accessed during initialization otherwise.
  • Loading branch information
Mattias1 committed Sep 3, 2023
1 parent 3733bf0 commit 7b42049
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions AvaloniaExtensions/CanvasComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public void SetupThemeColours() {
}
}

internal T SetInitialControlToFocus<T>(T control) where T : Control {
InitialControlToFocus = control;
return control;
}

public static T RegisterOnResizeAction<T>(T control, Action resizeAction) where T : Control {
var canvasComponent = FindCanvasComponent(control);
canvasComponent.RegisterOnResizeAction(resizeAction);
Expand Down
5 changes: 5 additions & 0 deletions AvaloniaExtensions/CanvasControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public static T Ref<T>(this T control) where T : Control {
return control;
}

// --- Misc ---
public static T WithInitialFocus<T>(this T control) where T : Control {
return CanvasComponentBase.FindCanvasComponent(control).SetInitialControlToFocus(control);
}

// --- Basic position and size ---
public static double GetX(this Control control) => Canvas.GetLeft(control);
public static double GetY(this Control control) => Canvas.GetTop(control);
Expand Down
5 changes: 5 additions & 0 deletions AvaloniaExtensions/ExtendedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
namespace AvaloniaExtensions;

public sealed class ExtendedWindow : Window {
// Unfortunately CanvasComponentBase.Parent is null during initialization, so we have to find another way
private static ExtendedWindow? _windowObject;
public static ExtendedWindow Get => _windowObject ?? throw new InvalidOperationException("No window created yet.");

private readonly Dictionary<Type, ViewBase> _components;
private readonly Dictionary<Type, Func<ViewBase>> _lazyComponents;
private readonly Dictionary<Type, SettingsFile> _settingsFiles;

private ExtendedWindow() {
_windowObject = this;
_components = new Dictionary<Type, ViewBase>();
_lazyComponents = new Dictionary<Type, Func<ViewBase>>();
_settingsFiles = new Dictionary<Type, SettingsFile>();
Expand Down
4 changes: 1 addition & 3 deletions ExampleApp/SettingsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ protected override void InitializeControls() {
AddTextBlockHeader("Some settings").TopLeftInPanel();
_cbExampleToggle = AddCheckBox("Something to toggle").Below();
InsertLabelAbove("A label above, to show it off");
_tbExampleString = AddTextBox().Below(_cbExampleToggle);
_tbExampleString = AddTextBox().Below(_cbExampleToggle).WithInitialFocus();
InsertLabelLeftOf("Checkbox:", _cbExampleToggle, LABEL_WIDTH);
InsertLabelLeftOf("Text:", _tbExampleString, LABEL_WIDTH);

AddButton("Reset defaults", OnResetSettingsClick).BottomLeftInPanel();
AddButton("Cancel", OnCancelClick).BottomRightInPanel();
AddButton("Ok", OnSaveClick).LeftOf();

InitialControlToFocus = _tbExampleString;
}

protected override void OnInitialized() {
Expand Down

0 comments on commit 7b42049

Please sign in to comment.