Update checker is mostly done. Negative zoom levels are no longer permitted either.

This commit is contained in:
That-One-Nerd 2024-04-21 09:04:35 -04:00
parent aba32f8b58
commit 789a3b448e
4 changed files with 163 additions and 152 deletions

View File

@ -51,16 +51,22 @@ namespace Graphing.Forms
MenuMisc = new ToolStripMenuItem();
MenuMiscCaches = new ToolStripMenuItem();
MiscMenuPreload = new ToolStripMenuItem();
UpdaterPopup = new Panel();
UpdaterPopupDownloadButton = new Button();
UpdaterPopupCloseButton = new Button();
UpdaterPopupMessage = new Label();
GraphMenu.SuspendLayout();
UpdaterPopup.SuspendLayout();
SuspendLayout();
//
// ResetViewportButton
//
ResetViewportButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ResetViewportButton.Font = new Font("Segoe UI Emoji", 12F, FontStyle.Regular, GraphicsUnit.Point, 0);
ResetViewportButton.Location = new Point(1373, 43);
ResetViewportButton.Location = new Point(739, 20);
ResetViewportButton.Margin = new Padding(2, 1, 2, 1);
ResetViewportButton.Name = "ResetViewportButton";
ResetViewportButton.Size = new Size(64, 64);
ResetViewportButton.Size = new Size(34, 30);
ResetViewportButton.TabIndex = 0;
ResetViewportButton.Text = "🏠";
ResetViewportButton.UseVisualStyleBackColor = true;
@ -72,7 +78,8 @@ namespace Graphing.Forms
GraphMenu.Items.AddRange(new ToolStripItem[] { MenuViewport, MenuElements, MenuOperations, MenuConvert, MenuMisc });
GraphMenu.Location = new Point(0, 0);
GraphMenu.Name = "GraphMenu";
GraphMenu.Size = new Size(1449, 40);
GraphMenu.Padding = new Padding(3, 1, 0, 1);
GraphMenu.Size = new Size(780, 24);
GraphMenu.TabIndex = 1;
GraphMenu.Text = "menuStrip1";
//
@ -80,34 +87,34 @@ namespace Graphing.Forms
//
MenuViewport.DropDownItems.AddRange(new ToolStripItem[] { ButtonViewportSetZoom, ButtonViewportSetCenter, ButtonViewportReset, ButtonViewportResetWindow });
MenuViewport.Name = "MenuViewport";
MenuViewport.Size = new Size(129, 36);
MenuViewport.Size = new Size(66, 22);
MenuViewport.Text = "Viewport";
//
// ButtonViewportSetZoom
//
ButtonViewportSetZoom.Name = "ButtonViewportSetZoom";
ButtonViewportSetZoom.Size = new Size(350, 44);
ButtonViewportSetZoom.Size = new Size(174, 22);
ButtonViewportSetZoom.Text = "Set Zoom";
ButtonViewportSetZoom.Click += ButtonViewportSetZoom_Click;
//
// ButtonViewportSetCenter
//
ButtonViewportSetCenter.Name = "ButtonViewportSetCenter";
ButtonViewportSetCenter.Size = new Size(350, 44);
ButtonViewportSetCenter.Size = new Size(174, 22);
ButtonViewportSetCenter.Text = "Set Center Position";
ButtonViewportSetCenter.Click += ButtonViewportSetCenter_Click;
//
// ButtonViewportReset
//
ButtonViewportReset.Name = "ButtonViewportReset";
ButtonViewportReset.Size = new Size(350, 44);
ButtonViewportReset.Size = new Size(174, 22);
ButtonViewportReset.Text = "Reset Viewport";
ButtonViewportReset.Click += ButtonViewportReset_Click;
//
// ButtonViewportResetWindow
//
ButtonViewportResetWindow.Name = "ButtonViewportResetWindow";
ButtonViewportResetWindow.Size = new Size(350, 44);
ButtonViewportResetWindow.Size = new Size(174, 22);
ButtonViewportResetWindow.Text = "Reset Window Size";
ButtonViewportResetWindow.Click += ButtonViewportResetWindow_Click;
//
@ -115,98 +122,148 @@ namespace Graphing.Forms
//
MenuElements.DropDownItems.AddRange(new ToolStripItem[] { MenuElementsColors, MenuElementsRemove });
MenuElements.Name = "MenuElements";
MenuElements.Size = new Size(131, 36);
MenuElements.Size = new Size(67, 22);
MenuElements.Text = "Elements";
//
// MenuElementsColors
//
MenuElementsColors.Name = "MenuElementsColors";
MenuElementsColors.Size = new Size(233, 44);
MenuElementsColors.Size = new Size(117, 22);
MenuElementsColors.Text = "Colors";
//
// MenuElementsRemove
//
MenuElementsRemove.Name = "MenuElementsRemove";
MenuElementsRemove.Size = new Size(233, 44);
MenuElementsRemove.Size = new Size(117, 22);
MenuElementsRemove.Text = "Remove";
//
// MenuOperations
//
MenuOperations.DropDownItems.AddRange(new ToolStripItem[] { MenuOperationsDerivative, MenuOperationsIntegral, MenuOperationsTranslate });
MenuOperations.Name = "MenuOperations";
MenuOperations.Size = new Size(151, 36);
MenuOperations.Size = new Size(77, 22);
MenuOperations.Text = "Operations";
//
// MenuOperationsDerivative
//
MenuOperationsDerivative.Name = "MenuOperationsDerivative";
MenuOperationsDerivative.Size = new Size(360, 44);
MenuOperationsDerivative.Size = new Size(179, 22);
MenuOperationsDerivative.Text = "Compute Derivative";
//
// MenuOperationsIntegral
//
MenuOperationsIntegral.Name = "MenuOperationsIntegral";
MenuOperationsIntegral.Size = new Size(360, 44);
MenuOperationsIntegral.Size = new Size(179, 22);
MenuOperationsIntegral.Text = "Compute Integral";
//
// MenuOperationsTranslate
//
MenuOperationsTranslate.Name = "MenuOperationsTranslate";
MenuOperationsTranslate.Size = new Size(360, 44);
MenuOperationsTranslate.Size = new Size(179, 22);
MenuOperationsTranslate.Text = "Translate";
//
// MenuConvert
//
MenuConvert.DropDownItems.AddRange(new ToolStripItem[] { MenuConvertEquation, MenuConvertSlopeField });
MenuConvert.Name = "MenuConvert";
MenuConvert.Size = new Size(118, 36);
MenuConvert.Size = new Size(61, 22);
MenuConvert.Text = "Convert";
//
// MenuConvertEquation
//
MenuConvertEquation.Name = "MenuConvertEquation";
MenuConvertEquation.Size = new Size(297, 44);
MenuConvertEquation.Size = new Size(146, 22);
MenuConvertEquation.Text = "To Equation";
//
// MenuConvertSlopeField
//
MenuConvertSlopeField.Name = "MenuConvertSlopeField";
MenuConvertSlopeField.Size = new Size(297, 44);
MenuConvertSlopeField.Size = new Size(146, 22);
MenuConvertSlopeField.Text = "To Slope Field";
//
// MenuMisc
//
MenuMisc.DropDownItems.AddRange(new ToolStripItem[] { MenuMiscCaches, MiscMenuPreload });
MenuMisc.Name = "MenuMisc";
MenuMisc.Size = new Size(83, 36);
MenuMisc.Size = new Size(44, 22);
MenuMisc.Text = "Misc";
//
// MenuMiscCaches
//
MenuMiscCaches.Name = "MenuMiscCaches";
MenuMiscCaches.Size = new Size(299, 44);
MenuMiscCaches.Size = new Size(150, 22);
MenuMiscCaches.Text = "View Caches";
MenuMiscCaches.Click += MenuMiscCaches_Click;
//
// MiscMenuPreload
//
MiscMenuPreload.Name = "MiscMenuPreload";
MiscMenuPreload.Size = new Size(299, 44);
MiscMenuPreload.Size = new Size(150, 22);
MiscMenuPreload.Text = "Preload Cache";
MiscMenuPreload.Click += MiscMenuPreload_Click;
//
// UpdaterPopup
//
UpdaterPopup.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
UpdaterPopup.BackColor = SystemColors.HighlightText;
UpdaterPopup.BorderStyle = BorderStyle.FixedSingle;
UpdaterPopup.Controls.Add(UpdaterPopupDownloadButton);
UpdaterPopup.Controls.Add(UpdaterPopupCloseButton);
UpdaterPopup.Controls.Add(UpdaterPopupMessage);
UpdaterPopup.Location = new Point(520, 371);
UpdaterPopup.Name = "UpdaterPopup";
UpdaterPopup.Size = new Size(261, 55);
UpdaterPopup.TabIndex = 2;
UpdaterPopup.Visible = false;
//
// UpdaterPopupDownloadButton
//
UpdaterPopupDownloadButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
UpdaterPopupDownloadButton.Location = new Point(181, 27);
UpdaterPopupDownloadButton.Name = "UpdaterPopupDownloadButton";
UpdaterPopupDownloadButton.Size = new Size(75, 23);
UpdaterPopupDownloadButton.TabIndex = 2;
UpdaterPopupDownloadButton.Text = "Visit";
UpdaterPopupDownloadButton.UseVisualStyleBackColor = true;
//
// UpdaterPopupCloseButton
//
UpdaterPopupCloseButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
UpdaterPopupCloseButton.Location = new Point(234, 1);
UpdaterPopupCloseButton.Margin = new Padding(1);
UpdaterPopupCloseButton.Name = "UpdaterPopupCloseButton";
UpdaterPopupCloseButton.Size = new Size(24, 24);
UpdaterPopupCloseButton.TabIndex = 1;
UpdaterPopupCloseButton.Text = "X";
UpdaterPopupCloseButton.UseVisualStyleBackColor = true;
UpdaterPopupCloseButton.Click += UpdaterPopupCloseButton_Click;
//
// UpdaterPopupMessage
//
UpdaterPopupMessage.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
UpdaterPopupMessage.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point, 0);
UpdaterPopupMessage.Location = new Point(3, 3);
UpdaterPopupMessage.Margin = new Padding(3);
UpdaterPopupMessage.Name = "UpdaterPopupMessage";
UpdaterPopupMessage.Size = new Size(228, 47);
UpdaterPopupMessage.TabIndex = 0;
UpdaterPopupMessage.Text = "A <type> update is available!\r\nA.B.C → E.F.G";
//
// GraphForm
//
AutoScaleDimensions = new SizeF(13F, 32F);
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1449, 907);
ClientSize = new Size(780, 425);
Controls.Add(UpdaterPopup);
Controls.Add(ResetViewportButton);
Controls.Add(GraphMenu);
MainMenuStrip = GraphMenu;
Margin = new Padding(2, 1, 2, 1);
Name = "GraphForm";
Text = "GraphFormBase";
GraphMenu.ResumeLayout(false);
GraphMenu.PerformLayout();
UpdaterPopup.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
@ -233,5 +290,9 @@ namespace Graphing.Forms
private ToolStripMenuItem MenuElementsRemove;
private ToolStripMenuItem MenuOperationsTranslate;
private ToolStripMenuItem MenuConvertSlopeField;
private Panel UpdaterPopup;
private Label UpdaterPopupMessage;
private Button UpdaterPopupCloseButton;
private Button UpdaterPopupDownloadButton;
}
}

View File

@ -1,8 +1,7 @@
using Graphing.Abstract;
using Graphing.Helpers;
using Graphing.Parts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
@ -18,11 +17,14 @@ public partial class GraphForm : Form
{
public static readonly Color BackgroundColor = Color.White;
public static readonly Color MainAxisColor = Color.Black;
public static readonly Color SemiAxisColor = Color.FromArgb(unchecked((int)0xFF_999999));
public static readonly Color QuarterAxisColor = Color.FromArgb(unchecked((int)0xFF_E0E0E0));
public static readonly Color SemiAxisColor = Color.FromArgb(unchecked((int)0xFF_999999)); // Grayish
public static readonly Color QuarterAxisColor = Color.FromArgb(unchecked((int)0xFF_E0E0E0)); // Lighter grayish
public static readonly Color UnitsTextColor = Color.Black;
public static readonly Color ZoomBoxColor = Color.Black;
public static readonly Color MajorUpdateColor = Color.FromArgb(unchecked((int)0xFF_F74434)); // Red
public static readonly Color MinorUpdateColor = Color.FromArgb(unchecked((int)0xFF_FCA103)); // Orange
public Float2 ScreenCenter { get; set; }
public Float2 Dpi { get; private set; }
@ -630,6 +632,10 @@ public partial class GraphForm : Form
foreach (Graphable able in Graphables) able.Preload(xRange, yRange, step);
Invalidate(false);
}
private void UpdaterPopupCloseButton_Click(object? sender, EventArgs e)
{
UpdaterPopup.Dispose();
}
private void ElementsOperationsTranslate_Click(Graphable ableRaw, ITranslatable ableTrans)
{
@ -646,7 +652,7 @@ public partial class GraphForm : Form
shifter.Show();
}
private static async void RunUpdateChecker()
private async void RunUpdateChecker()
{
try
{
@ -669,56 +675,39 @@ public partial class GraphForm : Form
if (newVersion > curVersion)
{
// Updates are required.
DialogResult button = MessageBox.Show(
$"A new update is available!\n{curVersion} -> {newVersion}\nWould you like to download the update?",
"Graphing Calculator Update", MessageBoxButtons.YesNo);
string type;
if (button == DialogResult.No) return;
string? project = UpdaterHelper.GetProjectPath();
if (project is null)
if (newVersion.Major > curVersion.Major || // x.0.0
newVersion.Minor > curVersion.Minor) // 0.x.0
{
MessageBox.Show("Cannot find project root. You'll likely have to update manually.",
"Error running automatic updates.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
type = "major";
UpdaterPopupMessage.ForeColor = MajorUpdateColor;
}
else // 0.0.x
{
type = "minor";
UpdaterPopupMessage.ForeColor = MinorUpdateColor;
}
if (await UpdaterHelper.UsingNugetPackage(project))
UpdaterPopupMessage.Text = $"A {type} update is available!\n{curVersion} → {newVersion}";
UpdaterPopup.Visible = true;
string url = latest["html_url"]!.GetValue<string>();
Console.WriteLine($"An update is available! {curVersion} -> {newVersion}\n{url}");
UpdaterPopupDownloadButton.Click += (o, e) =>
{
Console.WriteLine($"Attempting to update via the NuGet Package Manager.");
bool status = await UpdaterHelper.UpdateProjectByNuget(latest["tag_name"]!.GetValue<string>(), project);
if (!status)
ProcessStartInfo website = new()
{
MessageBox.Show("Failed to update with the NuGet Package Manager. " +
"You'll likely have to update manually.",
"Error running automatic updates.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
FileName = url,
UseShellExecute = true
};
Process.Start(website);
};
}
else
{
Console.WriteLine($"Attempting to update via a GitHub asset download.");
bool status = await UpdaterHelper.UpdateProjectByGitHub(latest["tag_name"]!.GetValue<string>(), project);
if (status)
{
MessageBox.Show("Update ready. Restart the project to complete the update.");
}
else
{
MessageBox.Show("Failed to update with an automatic download. " +
"You'll likely have to update manually.",
"Error running automatic updates.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
}
}
else
{
Console.WriteLine($"Up-to-date.");
Console.WriteLine("Up-to-date.");
UpdaterPopup.Dispose();
}
}
catch (Exception ex)

View File

@ -98,6 +98,17 @@ public partial class SetZoomForm : Form
{
Float2 min = refForm.MinVisibleGraph, max = refForm.MaxVisibleGraph;
if (minX > max.x)
{
MaxBoxX.Text = MinBoxX.Text;
MaxBoxX_Finish(sender, e);
minX = max.x;
// Redefine bounds.
min = refForm.MinVisibleGraph;
max = refForm.MaxVisibleGraph;
}
double newCenterX = (minX + max.x) / 2,
zoomFactorX = (max.x - minX) / (max.x - min.x);
@ -113,6 +124,17 @@ public partial class SetZoomForm : Form
{
Float2 min = refForm.MinVisibleGraph, max = refForm.MaxVisibleGraph;
if (maxX < min.x)
{
MinBoxX.Text = MaxBoxX.Text;
MinBoxX_Finish(sender, e);
maxX = min.x;
// Redefine bounds.
min = refForm.MinVisibleGraph;
max = refForm.MaxVisibleGraph;
}
double newCenterX = (min.x + maxX) / 2,
zoomFactorX = (maxX - min.x) / (max.x - min.x);
@ -128,6 +150,17 @@ public partial class SetZoomForm : Form
{
Float2 min = refForm.MinVisibleGraph, max = refForm.MaxVisibleGraph;
if (minY > max.y)
{
MaxBoxY.Text = MinBoxY.Text;
MaxBoxY_Finish(sender, e);
minY = max.y;
// Redefine bounds.
min = refForm.MinVisibleGraph;
max = refForm.MaxVisibleGraph;
}
double newCenterY = -(minY + max.y) / 2, // Keeping it positive flips it for some reason ???
zoomFactorY = (max.y - minY) / (max.y - min.y);
@ -143,6 +176,17 @@ public partial class SetZoomForm : Form
{
Float2 min = refForm.MinVisibleGraph, max = refForm.MaxVisibleGraph;
if (maxY < min.y)
{
MinBoxY.Text = MaxBoxY.Text;
MinBoxY_Finish(sender, e);
maxY = min.y;
// Redefine bounds.
min = refForm.MinVisibleGraph;
max = refForm.MaxVisibleGraph;
}
double newCenterY = -(min.y + maxY) / 2, // Keeping it positive flips it for some reason ???
zoomFactorY = (maxY - min.y) / (max.y - min.y);

View File

@ -1,83 +0,0 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Graphing.Helpers;
internal static class UpdaterHelper
{
public static async Task<bool> UsingNugetPackage(string? project = null)
{
project ??= GetProjectPath();
Process packageListProc = new();
packageListProc.StartInfo.FileName = "dotnet";
packageListProc.StartInfo.Arguments = "list package";
packageListProc.StartInfo.WorkingDirectory = Path.GetDirectoryName(project);
packageListProc.StartInfo.UseShellExecute = false;
packageListProc.StartInfo.RedirectStandardOutput = true;
StringBuilder contentBuilder = new();
packageListProc.OutputDataReceived += (o, e) => contentBuilder.AppendLine(e.Data);
packageListProc.Start();
packageListProc.BeginOutputReadLine();
await packageListProc.WaitForExitAsync();
string content = contentBuilder.ToString();
return content.Contains($"ThatOneNerd.Graphing");
}
public static async Task<bool> UpdateProjectByNuget(string version, string? project = null)
{
project ??= GetProjectPath();
Process updateProc = new();
updateProc.StartInfo.FileName = "dotnet";
updateProc.StartInfo.Arguments = $"add package ThatOneNerd.Graphing --version {version}";
updateProc.StartInfo.WorkingDirectory = Path.GetDirectoryName(project);
updateProc.StartInfo.UseShellExecute = false;
updateProc.StartInfo.RedirectStandardError = true;
StringBuilder errorBuilder = new();
updateProc.ErrorDataReceived += (o, e) => errorBuilder.AppendLine(e.Data);
updateProc.Start();
updateProc.BeginErrorReadLine();
await updateProc.WaitForExitAsync();
// Could be shrunk but it makes it less clear. If there's any data written to
// the error stream, it did not succeed.
if (errorBuilder.Length > 0) return false; // Error.
else return true; // Success.
}
public static async Task<bool> UpdateProjectByGitHub(string version, string? project = null)
{
// TODO
return false;
}
public static string? GetProjectPath()
{
Assembly? entryAsm = Assembly.GetEntryAssembly();
if (entryAsm is null) return null;
string? directory = Path.GetDirectoryName(entryAsm.Location);
while (directory is not null)
{
string[] files = Directory.GetFiles(directory);
string? project = files.FirstOrDefault(x => x.EndsWith(".csproj") ||
x.EndsWith(".sln"));
if (project is not null) return project;
directory = Path.GetDirectoryName(directory);
}
return null;
}
}