Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Migrated to Avalonia v11
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Jul 19, 2023
1 parent 0c66691 commit 46cf10e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 71 deletions.
6 changes: 4 additions & 2 deletions TAS.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
namespace TAS.Avalonia;

public partial class App : Application {

public CelesteService CelesteService = new CelesteService();
public DialogService DialogService = new DialogService();

public override void Initialize() {
AvaloniaLocator.CurrentMutable.Bind<IDialogService>().ToSingleton<DialogService>();
AvaloniaLocator.CurrentMutable.Bind<ICelesteService>().ToSingleton<CelesteService>();
AvaloniaXamlLoader.Load(this);
}

Expand Down
71 changes: 32 additions & 39 deletions TAS.Avalonia/Editing/TASCaretNavigationCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Media.TextFormatting;
Expand All @@ -26,85 +27,77 @@ public static TextAreaInputHandler Create(TextArea textArea) {
return areaInputHandler;
}

private static void AddBinding(
RoutedCommand command,
EventHandler<ExecutedRoutedEventArgs> handler) {
private static void AddBinding(RoutedCommand command, EventHandler<ExecutedRoutedEventArgs> handler) {
CommandBindings.Add(new RoutedCommandBinding(command, handler));
}

private static void AddBinding(
RoutedCommand command,
KeyModifiers modifiers,
Key key,
EventHandler<ExecutedRoutedEventArgs> handler) {
private static void AddBinding(RoutedCommand command, KeyModifiers modifiers, Key key, EventHandler<ExecutedRoutedEventArgs> handler) {
AddBinding(command, new KeyGesture(key, modifiers), handler);
}

private static void AddBinding(
RoutedCommand command,
KeyGesture gesture,
EventHandler<ExecutedRoutedEventArgs> handler) {
private static void AddBinding(RoutedCommand command, KeyGesture gesture, EventHandler<ExecutedRoutedEventArgs> handler) {
AddBinding(command, handler);
KeyBindings.Add(TASInputHandler.CreateKeyBinding(command, gesture));
}

static TASCaretNavigationCommandHandler() {
PlatformHotkeyConfiguration service = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
var keymap = Application.Current.PlatformSettings.HotkeyConfiguration;

AddBinding(EditingCommands.MoveLeftByCharacter, KeyModifiers.None, Key.Left, OnMoveCaret(CaretMovementType.CharLeft));
AddBinding(EditingCommands.SelectLeftByCharacter, service.SelectionModifiers, Key.Left, OnMoveCaretExtendSelection(CaretMovementType.CharLeft));
AddBinding(RectangleSelection.BoxSelectLeftByCharacter, KeyModifiers.Alt | service.SelectionModifiers, Key.Left, OnMoveCaretBoxSelection(CaretMovementType.CharLeft));
AddBinding(EditingCommands.SelectLeftByCharacter, keymap.SelectionModifiers, Key.Left, OnMoveCaretExtendSelection(CaretMovementType.CharLeft));
AddBinding(RectangleSelection.BoxSelectLeftByCharacter, KeyModifiers.Alt | keymap.SelectionModifiers, Key.Left, OnMoveCaretBoxSelection(CaretMovementType.CharLeft));
AddBinding(EditingCommands.MoveRightByCharacter, KeyModifiers.None, Key.Right, OnMoveCaret(CaretMovementType.CharRight));
AddBinding(EditingCommands.SelectRightByCharacter, service.SelectionModifiers, Key.Right, OnMoveCaretExtendSelection(CaretMovementType.CharRight));
AddBinding(RectangleSelection.BoxSelectRightByCharacter, KeyModifiers.Alt | service.SelectionModifiers, Key.Right, OnMoveCaretBoxSelection(CaretMovementType.CharRight));
AddBinding(EditingCommands.MoveLeftByWord, service.WholeWordTextActionModifiers, Key.Left, OnMoveCaret(CaretMovementType.WordLeft));
AddBinding(EditingCommands.SelectLeftByWord, service.WholeWordTextActionModifiers | service.SelectionModifiers, Key.Left, OnMoveCaretExtendSelection(CaretMovementType.WordLeft));
AddBinding(RectangleSelection.BoxSelectLeftByWord, service.WholeWordTextActionModifiers | KeyModifiers.Alt | service.SelectionModifiers, Key.Left, OnMoveCaretBoxSelection(CaretMovementType.WordLeft));
AddBinding(EditingCommands.MoveRightByWord, service.WholeWordTextActionModifiers, Key.Right, OnMoveCaret(CaretMovementType.WordRight));
AddBinding(EditingCommands.SelectRightByWord, service.WholeWordTextActionModifiers | service.SelectionModifiers, Key.Right, OnMoveCaretExtendSelection(CaretMovementType.WordRight));
AddBinding(RectangleSelection.BoxSelectRightByWord, service.WholeWordTextActionModifiers | KeyModifiers.Alt | service.SelectionModifiers, Key.Right, OnMoveCaretBoxSelection(CaretMovementType.WordRight));
AddBinding(EditingCommands.SelectRightByCharacter, keymap.SelectionModifiers, Key.Right, OnMoveCaretExtendSelection(CaretMovementType.CharRight));
AddBinding(RectangleSelection.BoxSelectRightByCharacter, KeyModifiers.Alt | keymap.SelectionModifiers, Key.Right, OnMoveCaretBoxSelection(CaretMovementType.CharRight));
AddBinding(EditingCommands.MoveLeftByWord, keymap.WholeWordTextActionModifiers, Key.Left, OnMoveCaret(CaretMovementType.WordLeft));
AddBinding(EditingCommands.SelectLeftByWord, keymap.WholeWordTextActionModifiers | keymap.SelectionModifiers, Key.Left, OnMoveCaretExtendSelection(CaretMovementType.WordLeft));
AddBinding(RectangleSelection.BoxSelectLeftByWord, keymap.WholeWordTextActionModifiers | KeyModifiers.Alt | keymap.SelectionModifiers, Key.Left, OnMoveCaretBoxSelection(CaretMovementType.WordLeft));
AddBinding(EditingCommands.MoveRightByWord, keymap.WholeWordTextActionModifiers, Key.Right, OnMoveCaret(CaretMovementType.WordRight));
AddBinding(EditingCommands.SelectRightByWord, keymap.WholeWordTextActionModifiers | keymap.SelectionModifiers, Key.Right, OnMoveCaretExtendSelection(CaretMovementType.WordRight));
AddBinding(RectangleSelection.BoxSelectRightByWord, keymap.WholeWordTextActionModifiers | KeyModifiers.Alt | keymap.SelectionModifiers, Key.Right, OnMoveCaretBoxSelection(CaretMovementType.WordRight));
AddBinding(EditingCommands.MoveUpByLine, KeyModifiers.None, Key.Up, OnMoveCaret(CaretMovementType.LineUp));
AddBinding(EditingCommands.SelectUpByLine, service.SelectionModifiers, Key.Up, OnMoveCaretExtendSelection(CaretMovementType.LineUp));
AddBinding(RectangleSelection.BoxSelectUpByLine, KeyModifiers.Alt | service.SelectionModifiers, Key.Up, OnMoveCaretBoxSelection(CaretMovementType.LineUp));
AddBinding(EditingCommands.SelectUpByLine, keymap.SelectionModifiers, Key.Up, OnMoveCaretExtendSelection(CaretMovementType.LineUp));
AddBinding(RectangleSelection.BoxSelectUpByLine, KeyModifiers.Alt | keymap.SelectionModifiers, Key.Up, OnMoveCaretBoxSelection(CaretMovementType.LineUp));
AddBinding(EditingCommands.MoveDownByLine, KeyModifiers.None, Key.Down, OnMoveCaret(CaretMovementType.LineDown));
AddBinding(EditingCommands.SelectDownByLine, service.SelectionModifiers, Key.Down, OnMoveCaretExtendSelection(CaretMovementType.LineDown));
AddBinding(RectangleSelection.BoxSelectDownByLine, KeyModifiers.Alt | service.SelectionModifiers, Key.Down, OnMoveCaretBoxSelection(CaretMovementType.LineDown));
AddBinding(EditingCommands.SelectDownByLine, keymap.SelectionModifiers, Key.Down, OnMoveCaretExtendSelection(CaretMovementType.LineDown));
AddBinding(RectangleSelection.BoxSelectDownByLine, KeyModifiers.Alt | keymap.SelectionModifiers, Key.Down, OnMoveCaretBoxSelection(CaretMovementType.LineDown));
AddBinding(EditingCommands.MoveDownByPage, KeyModifiers.None, Key.PageDown, OnMoveCaret(CaretMovementType.PageDown));
AddBinding(EditingCommands.SelectDownByPage, service.SelectionModifiers, Key.PageDown, OnMoveCaretExtendSelection(CaretMovementType.PageDown));
AddBinding(EditingCommands.SelectDownByPage, keymap.SelectionModifiers, Key.PageDown, OnMoveCaretExtendSelection(CaretMovementType.PageDown));
AddBinding(EditingCommands.MoveUpByPage, KeyModifiers.None, Key.PageUp, OnMoveCaret(CaretMovementType.PageUp));
AddBinding(EditingCommands.SelectUpByPage, service.SelectionModifiers, Key.PageUp, OnMoveCaretExtendSelection(CaretMovementType.PageUp));
AddBinding(RectangleSelection.BoxSelectToLineStart, KeyModifiers.Alt | service.SelectionModifiers, Key.Home, OnMoveCaretBoxSelection(CaretMovementType.LineStart));
AddBinding(RectangleSelection.BoxSelectToLineEnd, KeyModifiers.Alt | service.SelectionModifiers, Key.End, OnMoveCaretBoxSelection(CaretMovementType.LineEnd));
AddBinding(EditingCommands.SelectUpByPage, keymap.SelectionModifiers, Key.PageUp, OnMoveCaretExtendSelection(CaretMovementType.PageUp));
AddBinding(RectangleSelection.BoxSelectToLineStart, KeyModifiers.Alt | keymap.SelectionModifiers, Key.Home, OnMoveCaretBoxSelection(CaretMovementType.LineStart));
AddBinding(RectangleSelection.BoxSelectToLineEnd, KeyModifiers.Alt | keymap.SelectionModifiers, Key.End, OnMoveCaretBoxSelection(CaretMovementType.LineEnd));
AddBinding(ApplicationCommands.SelectAll, OnSelectAll);

foreach (KeyGesture gesture in service.MoveCursorToTheStartOfLine) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheStartOfLine) {
AddBinding(EditingCommands.MoveToLineStart, gesture, OnMoveCaret(CaretMovementType.LineStart));
}

foreach (KeyGesture gesture in service.MoveCursorToTheStartOfLineWithSelection) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheStartOfLineWithSelection) {
AddBinding(EditingCommands.SelectToLineStart, gesture, OnMoveCaretExtendSelection(CaretMovementType.LineStart));
}

foreach (KeyGesture gesture in service.MoveCursorToTheEndOfLine) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheEndOfLine) {
AddBinding(EditingCommands.MoveToLineEnd, gesture, OnMoveCaret(CaretMovementType.LineEnd));
}

foreach (KeyGesture gesture in service.MoveCursorToTheEndOfLineWithSelection) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheEndOfLineWithSelection) {
AddBinding(EditingCommands.SelectToLineEnd, gesture, OnMoveCaretExtendSelection(CaretMovementType.LineEnd));
}

foreach (KeyGesture gesture in service.MoveCursorToTheStartOfDocument) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheStartOfDocument) {
AddBinding(EditingCommands.MoveToDocumentStart, gesture, OnMoveCaret(CaretMovementType.DocumentStart));
}

foreach (KeyGesture gesture in service.MoveCursorToTheStartOfDocumentWithSelection) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheStartOfDocumentWithSelection) {
AddBinding(EditingCommands.SelectToDocumentStart, gesture, OnMoveCaretExtendSelection(CaretMovementType.DocumentStart));
}

foreach (KeyGesture gesture in service.MoveCursorToTheEndOfDocument) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheEndOfDocument) {
AddBinding(EditingCommands.MoveToDocumentEnd, gesture, OnMoveCaret(CaretMovementType.DocumentEnd));
}

foreach (KeyGesture gesture in service.MoveCursorToTheEndOfDocumentWithSelection) {
foreach (KeyGesture gesture in keymap.MoveCursorToTheEndOfDocumentWithSelection) {
AddBinding(EditingCommands.SelectToDocumentEnd, gesture, OnMoveCaretExtendSelection(CaretMovementType.DocumentEnd));
}
}
Expand Down
17 changes: 8 additions & 9 deletions TAS.Avalonia/Services/CelesteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

namespace TAS.Avalonia.Services;

[UsedImplicitly]
public class CelesteService : BaseService, ICelesteService {
public class CelesteService {
private Dictionary<HotkeyID, List<Keys>> _bindings;
private StudioInfo _state;

Expand Down Expand Up @@ -46,19 +45,19 @@ public void Play() {
private const int DefaultFastForwardSpeed = 10;
private const float DefaultSlowForwardSpeed = 0.1f;

private int GetDecimals(string settingName) {
private int GetDecimals(string settingName) {
string decimals = DefaultDecimals.ToString();
if (Server.GetDataFromGame(GameDataType.SettingValue, settingName) is { } settingValue) {
decimals = settingValue;
}

bool success = int.TryParse(decimals, out int result);
bool success = int.TryParse(decimals, out int result);
return success ? result : DefaultDecimals;
}

public int GetPositionDecimals() => GetDecimals("PositionDecimals");
public void SetPositionDecimals(int value) => Server.ToggleGameSetting("PositionDecimals", value);

public int GetSpeedDecimals() => GetDecimals("SpeedDecimals");
public void SetSpeedDecimals(int value) => Server.ToggleGameSetting("SpeedDecimals", value);

Expand All @@ -71,24 +70,24 @@ private int GetDecimals(string settingName) {
public int GetSubpixelIndicatorDecimals() => GetDecimals("SubpixelIndicatorDecimals");
public void SetSubpixelIndicatorDecimals(int value) => Server.ToggleGameSetting("SubpixelIndicatorDecimals", value);

public int GetFastForwardSpeed() {
public int GetFastForwardSpeed() {
string speed = DefaultFastForwardSpeed.ToString();
if (Server.GetDataFromGame(GameDataType.SettingValue, "FastForwardSpeed") is { } settingValue) {
speed = settingValue;
}

bool success = int.TryParse(speed, out int result);
bool success = int.TryParse(speed, out int result);
return success ? result : DefaultFastForwardSpeed;
}
public void SetFastForwardSpeed(int value) => Server.ToggleGameSetting("FastForwardSpeed", value);

public float GetSlowForwardSpeed() {
public float GetSlowForwardSpeed() {
string speed = DefaultSlowForwardSpeed.ToString(CultureInfo.InvariantCulture);
if (Server.GetDataFromGame(GameDataType.SettingValue, "SlowForwardSpeed") is { } settingValue) {
speed = settingValue;
}

bool success = float.TryParse(speed, NumberStyles.None, CultureInfo.InvariantCulture, out float result);
bool success = float.TryParse(speed, NumberStyles.None, CultureInfo.InvariantCulture, out float result);
return success ? result : DefaultSlowForwardSpeed;
}
public void SetSlowForwardSpeed(float value) => Server.ToggleGameSetting("SlowForwardSpeed", value);
Expand Down
18 changes: 10 additions & 8 deletions TAS.Avalonia/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace TAS.Avalonia.Services;

public class DialogService : BaseService, IDialogService {
public MainWindow MainWindow => (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow as MainWindow;
public class DialogService {
private static MainWindow _mainWindow => (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow as MainWindow;

public async Task<bool> ShowConfirmDialogAsync(string message, string title = null) {
var dialog = new SimpleDialog {
Title = title ?? string.Empty, Description = message
Title = title ?? string.Empty,
Description = message
};
try {
var result = await dialog.ShowAsync().ConfigureAwait(true);
Expand All @@ -29,14 +30,15 @@ public async Task<bool> ShowConfirmDialogAsync(string message, string title = nu

public async Task ShowDialogAsync(string message, string title = null) {
var dialog = new Dialog {
Title = title ?? string.Empty, Description = message
Title = title ?? string.Empty,
Description = message
};
dialog.Buttons.AddButton(DefaultButtons.OkButton);
await dialog.ShowAsync().ConfigureAwait(true);
}

public async Task<string[]> ShowOpenFileDialogAsync(string title, params FilePickerFileType[] fileTypes) {
var files = await MainWindow.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions {
var files = await _mainWindow.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions {
Title = title,
AllowMultiple = false,
FileTypeFilter = fileTypes,
Expand All @@ -46,7 +48,7 @@ public async Task<string[]> ShowOpenFileDialogAsync(string title, params FilePic
}

public async Task<string> ShowSaveFileDialogAsync(string title, string defaultExtension, params FilePickerFileType[] fileTypes) {
var file = await MainWindow.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions {
var file = await _mainWindow.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions {
Title = title,
ShowOverwritePrompt = true,
DefaultExtension = defaultExtension,
Expand All @@ -58,12 +60,12 @@ public async Task<string> ShowSaveFileDialogAsync(string title, string defaultEx
public async Task<int> ShowIntInputDialogAsync(int currentValue, int minValue, int maxValue, string title = null) {
var dialog = new InputDialogWindow();
dialog.DataContext = new InputDialogWindowViewModel(currentValue, minValue, maxValue);
return (int)await dialog.ShowDialog<object>(MainWindow);
return (int) await dialog.ShowDialog<object>(_mainWindow);
}

public async Task<float> ShowFloatInputDialogAsync(float currentValue, float minValue, float maxValue, string title = null) {
var dialog = new InputDialogWindow();
dialog.DataContext = new InputDialogWindowViewModel(currentValue, minValue, maxValue);
return (float)await dialog.ShowDialog<object>(MainWindow);
return (float) await dialog.ShowDialog<object>(_mainWindow);
}
}
16 changes: 8 additions & 8 deletions TAS.Avalonia/TAS.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0-preview8" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview8" />
<PackageReference Include="Avalonia.Xaml.Interactions" Version="11.0.0-preview8" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0-preview8" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview8" />
<PackageReference Include="Avalonia" Version="11.0.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0" />
<PackageReference Include="Avalonia.Xaml.Interactions" Version="11.0.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview8" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0" />

<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.0-preview6" />
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.0-preview6" />
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.0" />
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.0" />
<PackageReference Include="AvaloniaEdit.TextMate.Grammars" Version="0.10.12" />

<PackageReference Include="ReactiveUI" Version="18.3.1" />
Expand Down
8 changes: 4 additions & 4 deletions TAS.Avalonia/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public TextViewPosition CaretPosition {

public bool MenuVisible => true; //!RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

private readonly ICelesteService _celesteService;
private readonly IDialogService _dialogService;
private readonly CelesteService _celesteService;
private readonly DialogService _dialogService;

private MenuModel[] MainMenu { get; }
private MenuModel[] EditorContextMenu { get; }
Expand All @@ -79,8 +79,8 @@ public TextViewPosition CaretPosition {
};

public MainWindowViewModel() {
_celesteService = AvaloniaLocator.Current.GetService<ICelesteService>()!;
_dialogService = AvaloniaLocator.Current.GetService<IDialogService>()!;
_celesteService = (Application.Current as App).CelesteService;
_dialogService = (Application.Current as App).DialogService;

// File
NewFileCommand = ReactiveCommand.Create(NewFile);
Expand Down
Loading

0 comments on commit 46cf10e

Please sign in to comment.