made buttons square and added hotkeys

This commit is contained in:
2026-01-11 03:15:23 +05:00
parent 595c02231c
commit a08838bb8b
7 changed files with 104 additions and 78 deletions

View File

@@ -1,3 +1,4 @@
using Avalonia.Input;
using FusionCalculator;
namespace SharpCalculator.Avalonia;
@@ -9,6 +10,31 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
// https://github.com/AvaloniaUI/Avalonia/discussions/12179
KeyDownEvent.AddClassHandler<TopLevel>(GlobalOnKeyDown, handledEventsToo: true);
}
private void GlobalOnKeyDown(object? sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
CalculateResult();
break;
case Key.Delete:
Clear();
break;
}
}
private void CalculateResult()
{
if(Input.Text == null || Output.Text == null || Output.Text.StartsWith("Error"))
return;
string expression = Input.Text;
string result = Output.Text;
AppendToHistory($"{expression}={result}");
Input.Text = result;
}
private void AppendToHistory(string line) //function of appending expression to the history list
@@ -24,6 +50,23 @@ public partial class MainWindow : Window
.Replace(" ", " ");
}
private void Clear()
{
Input.Clear();
Output.Clear();
}
private void ResultButton_OnClick(object? sender, RoutedEventArgs e)
{
CalculateResult();
}
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
{
Clear();
}
private void MathButton_OnClick(object? sender, RoutedEventArgs e)
{
if (sender is not Button button)
@@ -48,30 +91,6 @@ public partial class MainWindow : Window
_ => buttonText
};
}
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
{
Input.Text = "";
Output.Text = "";
}
private void UndoButton_OnClick(object? sender, RoutedEventArgs e)
{
// delete last digit
// if (!string.IsNullOrEmpty(Input.Text))
// Input.Text = Input.Text.Remove(Input.Text.Length - 1);
Input.Undo();
}
private void ResultButton_OnClick(object? sender, RoutedEventArgs e)
{
if(Input.Text == null || Output.Text == null || Output.Text.StartsWith("Error"))
return;
string expression = Input.Text;
string result = Output.Text;
AppendToHistory($"{expression}={result}");
Input.Text = result;
}
private void Input_OnTextChanged(object? sender, TextChangedEventArgs e)
{