Skip to content

Commit

Permalink
reupload project
Browse files Browse the repository at this point in the history
  • Loading branch information
help-14 committed Jan 10, 2022
1 parent 377d7e0 commit 342ba09
Show file tree
Hide file tree
Showing 29 changed files with 1,113 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Koichi Katano

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions Pitago.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31612.314
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pitago", "Pitago\Pitago.csproj", "{EB7724BF-466C-4E87-84E2-4EDF620D703D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CDC448A1-0CA8-4B1D-A399-0B3931F8939E}"
ProjectSection(SolutionItems) = preProject
build.ps1 = build.ps1
build.sh = build.sh
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EB7724BF-466C-4E87-84E2-4EDF620D703D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB7724BF-466C-4E87-84E2-4EDF620D703D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB7724BF-466C-4E87-84E2-4EDF620D703D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB7724BF-466C-4E87-84E2-4EDF620D703D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {34BB8134-33C8-4B05-863C-A4AF839F1AB3}
EndGlobalSection
EndGlobal
36 changes: 36 additions & 0 deletions Pitago/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Application
xmlns="https://github.com/avaloniaui"
xmlns:sty="using:FluentAvalonia.Styling"
xmlns:ui="using:FluentAvalonia.UI.Controls"
xmlns:uip="using:FluentAvalonia.UI.Controls.Primitives"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:AvaloniaEdit.CodeCompletion;assembly=AvaloniaEdit"
x:Class="Pitago.App">
<Application.Styles>
<FluentTheme Mode="Dark"/>
<StyleInclude Source="avares://AvaloniaEdit/AvaloniaEdit.xaml" />

<!--<Color x:Key="AcentColor" />
<SolidColorBrush x:Key="AcentBrush" Color="{StaticResources AcentColor}" />-->

<!--Code completion-->
<Style Selector="cc|CompletionList">
<Setter Property="Template">
<ControlTemplate>
<cc:CompletionListBox Name="PART_ListBox" Background="Gray" BorderThickness="1" BorderBrush="LightGray" >
<cc:CompletionListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Height="18">
<Image Source="{Binding Image}"
Width="15"
Height="15" />
<TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Text="{Binding Content}" FontSize="15" FontFamily="Consolas" Foreground="#eeeeee"/>
</StackPanel>
</DataTemplate>
</cc:CompletionListBox.ItemTemplate>
</cc:CompletionListBox>
</ControlTemplate>
</Setter>
</Style>
</Application.Styles>
</Application>
24 changes: 24 additions & 0 deletions Pitago/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

namespace Pitago
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new Pitago.Views.MainWindow();
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file added Pitago/Assets/Fonts/Segoe_Fluent_Icons.ttf
Binary file not shown.
123 changes: 123 additions & 0 deletions Pitago/Calculation/CalculationProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using AngouriMath.Extensions;
using System;
using System.Text.RegularExpressions;

namespace Pitago.Calculation
{
public class CalculationProcessor
{
private const string ArrowSign = "=>";
private const string EqualSign = "=";
private const char NewLineSign = '\n';
private const char ReturnSign = '\r';

public string Process(string math, bool formatResult = true)
{
try
{
string result = string.Empty;

// Check if the text is empty or not
if (string.IsNullOrEmpty(math))
return string.Empty;

// Get the letter from math text
var varriables = Regex.Matches(math, @"[a-zA-Z]");

if (varriables.Count == 0) // No varriable found then calculate the string like numerical math
{
if (math.Contains('='))
result = math.Simplify().Stringize();
else
result = math.EvalNumerical().Stringize();
}
else // Varriable found, validate the varriable and calculate, if calculation failed then try simplify the math
{
//TODO save the varriable
result = math.Simplify().Stringize();
}

if (string.IsNullOrEmpty(result)) // Calculation failed, return nothing
return string.Empty;

if (formatResult)
return FormatResult(math, result);
else
return result;
}
catch
{
return String.Empty;
}
}

public string ProcessDocument(string document, bool calculate = true)
{
document = document
.Replace($"{ReturnSign}{NewLineSign}", NewLineSign.ToString())
.Replace(ReturnSign.ToString(), "");

var lines = document.Split(NewLineSign);
for (var i = 0; i < lines.Length; i++)
{
lines[i] = GetOriginalMath(lines[i]);
if (calculate) lines[i] = String.Concat(lines[i], Process(lines[i]));
}
return string.Join(NewLineSign, lines);
}

private string FormatResult(string math, string result)
{
return string.Concat(
math.EndsWith(' ') ? "" : " ",
math.Contains(EqualSign) ? ArrowSign : EqualSign,
" ",
result
);
}

public bool IsLineCalculated(string line)
{
return line.Contains(ArrowSign) ||
line.Contains(EqualSign) && line.EvalBoolean() == AngouriMath.Entity.Boolean.True;
}

private string GetOriginalMath(string text)
{
if (!IsLineCalculated(text))
return text.Trim();

if (text.Contains(ArrowSign))
return text.Substring(0, text.IndexOf(ArrowSign)).Trim();
else if (text.Contains(EqualSign))
return text.Substring(0, text.IndexOf(EqualSign)).Trim();
else
return text.Trim();
}

public string CalculateLine(string line, bool recalculate = true)
{
if (IsLineCalculated(line))
{
if(recalculate)
return Process(GetOriginalMath(line));
return line;
}
else
return Process(line);
}

// Remove calculation result from all lines
public string RemoveCalculationResult(string document)
{
return ProcessDocument(document, false);
}

// Re-do calculation on all lines
public string ReCalculationResult(string document)
{
return ProcessDocument(document, true);
}
}

}
21 changes: 21 additions & 0 deletions Pitago/Constants/Info.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Pitago.Constants
{
public static class Info
{

/// <summary>
/// Link to the newest version txt on Github
/// </summary>
public const string NewestVersionUrl =
#if BETA
"https://raw.githubusercontent.com/help-14/pitago/main/version/beta.txt";
#else
"https://raw.githubusercontent.com/help-14/pitago/main/version/stable.txt";
#endif

/// <summary>
/// Link to download the newest version
/// </summary>
public const string DownloadUrl = "https://github.com/help-14/pitago/releases";
}
}
65 changes: 65 additions & 0 deletions Pitago/Controls/Notepad/Calculation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Avalonia;
using Avalonia.Media;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using System;
using System.Linq;

namespace Pitago.Controls
{
public partial class Notepad
{
private string CalculateLineResult(DocumentLine line)
{
var text = _textEditor.Document.GetText(line.Offset, line.Length);
if (string.IsNullOrEmpty(text?.Trim())) return String.Empty;
return _calProcessor.Process(text);
}

private void CalculateLine(DocumentLine line)
{
var result = CalculateLineResult(line);
if (string.IsNullOrEmpty(result)) return;
_textEditor.Document.Insert(line.EndOffset, result);
}

private void CalculateLines(DocumentLine[] lines)
{
foreach(var line in lines)
{
CalculateLine(line);
}
}

private void PreviewCalculation(DocumentLine line)
{
ClearPreviewResult();
//UpdatePreviewResultPosition();

var resultText = CalculateLineResult(line);
var rect = BackgroundGeometryBuilder.GetRectsForSegment(_textEditor.TextArea.TextView, line).FirstOrDefault();
if (rect == null) return;

var label = new Avalonia.Controls.TextBlock()
{
Text = resultText,
Foreground = new SolidColorBrush(Colors.Gray),
FontFamily = _textEditor.FontFamily,
FontSize = _textEditor.FontSize,
Margin = new Thickness(_textEditor.Padding.Left + rect.X + rect.Width, rect.Y, 0, 0)
};
_resultBox.Children.Add(label);
}

public void CalculateDocument()
{
_textEditor.Text = _calProcessor.ProcessDocument(_textEditor.Text);
}

private void ClearPreviewResult()
{
_resultBox.Children.Clear();
}
}
}
37 changes: 37 additions & 0 deletions Pitago/Controls/Notepad/CompletionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Avalonia.Media.Imaging;
using AvaloniaEdit.CodeCompletion;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using System;

namespace Pitago.Controls
{
public partial class Notepad
{
internal class CompletionData : ICompletionData
{
public CompletionData(string text)
{
Text = text;
}

public IBitmap Image => null;

public string Text { get; }

// Use this property if you want to show a fancy UIElement in the list.
public object Content => Text;

public object Description => "Description for " + Text;

public double Priority { get; } = 0;

IBitmap ICompletionData.Image => throw new NotImplementedException();

public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
{
textArea.Document.Replace(completionSegment, Text);
}
}
}
}
Loading

0 comments on commit 342ba09

Please sign in to comment.