commit e0adb55d43078b72ed972789291c876498f75fb0 Author: Timerix22 Date: Fri Nov 24 10:47:00 2023 +0600 basic design diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1b75be --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +bin/ +obj/ +.idea/ +.vs/ +tmp/ +temp/ +packages/ +*.user diff --git a/SharpCalculator.Avalonia/App.axaml b/SharpCalculator.Avalonia/App.axaml new file mode 100644 index 0000000..69b003e --- /dev/null +++ b/SharpCalculator.Avalonia/App.axaml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/SharpCalculator.Avalonia/App.axaml.cs b/SharpCalculator.Avalonia/App.axaml.cs new file mode 100644 index 0000000..6ea5e33 --- /dev/null +++ b/SharpCalculator.Avalonia/App.axaml.cs @@ -0,0 +1,23 @@ +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; + +namespace SharpCalculator.Avalonia; + +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(); + } +} \ No newline at end of file diff --git a/SharpCalculator.Avalonia/MainWindow.axaml b/SharpCalculator.Avalonia/MainWindow.axaml new file mode 100644 index 0000000..4cd22e8 --- /dev/null +++ b/SharpCalculator.Avalonia/MainWindow.axaml @@ -0,0 +1,42 @@ + + + 4* 1* 4* + + = 0 + + * * * * + * * * * * + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SharpCalculator.Avalonia/MainWindow.axaml.cs b/SharpCalculator.Avalonia/MainWindow.axaml.cs new file mode 100644 index 0000000..5b6036a --- /dev/null +++ b/SharpCalculator.Avalonia/MainWindow.axaml.cs @@ -0,0 +1,90 @@ +using System; +using System.Globalization; +using System.Text; +using Avalonia.Controls; +using Avalonia.Interactivity; + +namespace SharpCalculator.Avalonia; + +public partial class MainWindow : Window +{ + public MainWindow() + { + InitializeComponent(); + } + + private double _rezult; + private StringBuilder currentNumberB = new(); + + private double CompleteCurrentNumber() + { + var n = double.Parse(currentNumberB.ToString(), CultureInfo.InvariantCulture); + currentNumberB.Clear(); + return n; + } + + private void UpdateOutput() + { + Output.Text = "= " + _rezult; + } + + private Func prevOperation = (_, firstN) => firstN; + + private void TryUpdateRezult(Func operation, string text) + { + if (currentNumberB.Length > 0) + { + _rezult = prevOperation(_rezult, CompleteCurrentNumber()); + prevOperation = operation; + Input.Text += " " + text + " "; + } + + UpdateOutput(); + } + + private void NumberButton_OnClick(object? sender, RoutedEventArgs e) + { + if (sender is not Button button) + throw new Exception(); + string text = button.Content?.ToString()!; + Input.Text += text; + currentNumberB.Append(text); + } + + private void OperationButton_OnClick(object? sender, RoutedEventArgs e) + { + if (sender is not Button button) + throw new Exception(); + string text = button.Content?.ToString()!; + switch (text) + { + case "+": + TryUpdateRezult((a, b) => a + b, text); + break; + case "-": + TryUpdateRezult((a, b) => a - b, text); + break; + case "*": + TryUpdateRezult((a, b) => a * b, text); + break; + case "/": + TryUpdateRezult((a, b) => a / b, text); + break; + case "^": + TryUpdateRezult(Math.Pow, text); + break; + case "=": + TryUpdateRezult((a, _) => a, ""); + break; + default: + throw new Exception("incorrect button text: " + text); + } + } + + private void ClearButton_OnClick(object? sender, RoutedEventArgs e) + { + _rezult = 0; + UpdateOutput(); + Input.Text = ""; + } +} \ No newline at end of file diff --git a/SharpCalculator.Avalonia/Program.cs b/SharpCalculator.Avalonia/Program.cs new file mode 100644 index 0000000..85f1a38 --- /dev/null +++ b/SharpCalculator.Avalonia/Program.cs @@ -0,0 +1,20 @@ +using Avalonia; +using System; + +namespace SharpCalculator.Avalonia; + +class Program +{ + // Initialization code. Don't use any Avalonia, third-party APIs or any + // SynchronizationContext-reliant code before AppMain is called: things aren't initialized + // yet and stuff might break. + [STAThread] + public static void Main(string[] args) => BuildAvaloniaApp() + .StartWithClassicDesktopLifetime(args); + + // Avalonia configuration, don't remove; also used by visual designer. + private static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .LogToTrace(); +} \ No newline at end of file diff --git a/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj b/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj new file mode 100644 index 0000000..8d15fd0 --- /dev/null +++ b/SharpCalculator.Avalonia/SharpCalculator.Avalonia.csproj @@ -0,0 +1,18 @@ + + + WinExe + net8.0 + 12 + enable + disable + true + true + + + + + + + + + diff --git a/SharpCalculator.sln b/SharpCalculator.sln new file mode 100644 index 0000000..b20003b --- /dev/null +++ b/SharpCalculator.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCalculator.Avalonia", "SharpCalculator.Avalonia\SharpCalculator.Avalonia.csproj", "{93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93E7D002-22D7-4C4F-AE6B-F1E0D938BCBA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/nuget.config b/nuget.config new file mode 100644 index 0000000..42f932e --- /dev/null +++ b/nuget.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file