Skip to content

Commit

Permalink
use special designed Point + Size structs
Browse files Browse the repository at this point in the history
  • Loading branch information
murrty committed Sep 24, 2022
1 parent a72e827 commit 865aa67
Show file tree
Hide file tree
Showing 27 changed files with 279 additions and 125 deletions.
2 changes: 1 addition & 1 deletion youtube-dl-gui/Classes/DataClasses/YoutubeDlData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static YoutubeDlData GenerateData(string URL, out string RetrievedData) {
return null;
}
public Image GetThumbnail() {
if (Config.Settings.Downloads.YtdlType switch { (int)updater.GitID.YtDlp or (int)updater.GitID.YtDlpNightly => false, _ => true }) {
if (Config.Settings.Downloads.YtdlType switch { (int)GitID.YtDlp or (int)GitID.YtDlpNightly => false, _ => true }) {
Log.Write($"Cannot download the thumbnail for \"{URL}\" because the selected youtube-dl fork is not supported.");
return null;
}
Expand Down
1 change: 0 additions & 1 deletion youtube-dl-gui/Classes/Verification.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace youtube_dl_gui;

using System.IO;
using youtube_dl_gui.updater;

internal static class Verification {
private static GitID YoutubeDlGitType = GitID.YtDlp;
Expand Down
25 changes: 1 addition & 24 deletions youtube-dl-gui/Config/Config.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
namespace youtube_dl_gui;

using System.Drawing;

internal class Config {
public static volatile Config Settings;

internal static readonly Point InvalidPoint = new(-32_000, -32_000);

public Config_Initialization Initialization;
public Config_Batch Batch;
public Config_Converts Converts;
Expand Down Expand Up @@ -110,22 +105,4 @@ public void Save(ConfigType Type) {
break;
}
}

/// <summary>
/// Checks if a point is a valid one to use.
/// </summary>
/// <param name="input">The <seealso cref="Point"/> value to validate.</param>
/// <returns>If the input is a valid point.</returns>
public static bool ValidPoint(Point input) {
return input.X != InvalidPoint.X && input.Y != InvalidPoint.Y;
}

/// <summary>
/// Checks if a size is a valid one to use.
/// </summary>
/// <param name="input">The <seealso cref="Size"/> value to validate.</param>
/// <returns>If the input is a valid size.</returns>
public static bool ValidSize(Size input) {
return input.Width > 0 && input.Height > 0;
}
}
}
18 changes: 8 additions & 10 deletions youtube-dl-gui/Config/Config_Saved.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace youtube_dl_gui;

using System.Drawing;

internal class Config_Saved {
private const string ConfigName = "Saved";

Expand Down Expand Up @@ -79,24 +77,24 @@ public void Load() {
VideoFormat = fVideoFormat = IniProvider.Read(VideoFormat, 0, ConfigName);
AudioFormat = fAudioFormat = IniProvider.Read(AudioFormat, 0, ConfigName);
AudioVBRQuality = fAudioVBRQuality = IniProvider.Read(AudioVBRQuality, 0, ConfigName);
BatchDownloaderLocation = fBatchDownloaderLocation = IniProvider.Read(BatchDownloaderLocation, Config.InvalidPoint, ConfigName);
BatchConverterLocation = fBatchConverterLocation = IniProvider.Read(BatchConverterLocation, Config.InvalidPoint, ConfigName);
BatchDownloaderLocation = fBatchDownloaderLocation = IniProvider.Read(BatchDownloaderLocation, Point.Invalid, ConfigName);
BatchConverterLocation = fBatchConverterLocation = IniProvider.Read(BatchConverterLocation, Point.Invalid, ConfigName);
MainFormSize = fMainFormSize = IniProvider.Read(MainFormSize, Size.Empty, ConfigName);
SettingsFormSize = fSettingsFormSize = IniProvider.Read(SettingsFormSize, Size.Empty, ConfigName);
FileNameSchemaHistory = fFileNameSchemaHistory = IniProvider.Read(FileNameSchemaHistory, "%(title)s-%(id)s.%(ext)s|%(uploader)s\\(%(playlist_index)s) %(title)s-%(id)s.%(ext)s", ConfigName);
DownloadCustomArguments = fDownloadCustomArguments = IniProvider.Read(DownloadCustomArguments, string.Empty, ConfigName);
CustomArgumentsIndex = fCustomArgumentsIndex = IniProvider.Read(CustomArgumentsIndex, -1, ConfigName);
MainFormLocation = fMainFormLocation = IniProvider.Read(MainFormLocation, Config.InvalidPoint, ConfigName);
MainFormLocation = fMainFormLocation = IniProvider.Read(MainFormLocation, Config.InvalidPoint, ConfigName);
ExtendedDownloaderLocation = fExtendedDownloaderLocation = IniProvider.Read(ExtendedDownloaderLocation, Config.InvalidPoint, ConfigName);
MainFormLocation = fMainFormLocation = IniProvider.Read(MainFormLocation, Point.Invalid, ConfigName);
MainFormLocation = fMainFormLocation = IniProvider.Read(MainFormLocation, Point.Invalid, ConfigName);
ExtendedDownloaderLocation = fExtendedDownloaderLocation = IniProvider.Read(ExtendedDownloaderLocation, Point.Invalid, ConfigName);
ExtendedDownloaderSize = fExtendedDownloaderSize = IniProvider.Read(ExtendedDownloaderSize, Size.Empty, ConfigName);
ArchiveDownloaderLocation = fArchiveDownloaderLocation = IniProvider.Read(ArchiveDownloaderLocation, Config.InvalidPoint, ConfigName);
LogLocation = fLogLocation = IniProvider.Read(LogLocation, Config.InvalidPoint, ConfigName);
ArchiveDownloaderLocation = fArchiveDownloaderLocation = IniProvider.Read(ArchiveDownloaderLocation, Point.Invalid, ConfigName);
LogLocation = fLogLocation = IniProvider.Read(LogLocation, Point.Invalid, ConfigName);
LogSize = fLogSize = IniProvider.Read(LogSize, Size.Empty, ConfigName);
ExtendedDownloadVideoColumns = fExtendedDownloadVideoColumns = IniProvider.Read(ExtendedDownloadVideoColumns, string.Empty, ConfigName);
ExtendedDownloadAudioColumns = fExtendedDownloadAudioColumns = IniProvider.Read(ExtendedDownloadAudioColumns, string.Empty, ConfigName);
ExtendedDownloadUnknownColumns = fExtendedDownloadUnknownColumns = IniProvider.Read(ExtendedDownloadUnknownColumns, string.Empty, ConfigName);
QuickDownloaderLocation = fQuickDownloaderLocation = IniProvider.Read(QuickDownloaderLocation, Config.InvalidPoint, ConfigName);
QuickDownloaderLocation = fQuickDownloaderLocation = IniProvider.Read(QuickDownloaderLocation, Point.Invalid, ConfigName);
}

public void Save() {
Expand Down
64 changes: 64 additions & 0 deletions youtube-dl-gui/Config/Point.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace murrty.structs;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Point {
/// <summary>
/// Represents an invalid Point value.
/// </summary>
public static readonly Point Invalid = new(-32_000, -32_000);
/// <summary>
/// The X-coordinate of the Point.
/// </summary>
public int X {
get; set;
}

/// <summary>
/// The Y-coordinate of the Point.
/// </summary>
public int Y {
get; set;
}

/// <summary>
/// Whether the <see cref="Point"/> can be considered a valid <see cref="Point"/> compared to <see cref="Invalid"/>.
/// </summary>
public bool Valid => this.X != Invalid.X && this.Y != Invalid.Y;

public Point() {
X = 0;
Y = 0;
}

public Point(int X, int Y) {
this.X = X;
this.Y = Y;
}

/// <inheritdoc />
public override bool Equals(object obj) => obj is Point point && this.X == point.X && this.Y == point.Y;

/// <inheritdoc />
public override int GetHashCode() {
int hashCode = 1861411795;
hashCode = hashCode * -1521134295 + this.X.GetHashCode();
hashCode = hashCode * -1521134295 + this.Y.GetHashCode();
return hashCode;
}

public static bool operator ==(Point a, Point b) => a.X == b.X && a.Y == b.Y;

public static bool operator !=(Point a, Point b) => a.X != b.X || a.Y != b.Y;

/// <summary>
/// Implicitly converts this Point structure to a System.Drawing.Point structure.
/// </summary>
/// <param name="p"></param>
public static implicit operator System.Drawing.Point(Point p) => new(p.X, p.Y);

/// <summary>
/// Implicity converts a System.Drawing.Point structure to this Point structure.
/// </summary>
/// <param name="p"></param>
public static implicit operator Point(System.Drawing.Point p) => new(p.X, p.Y);
}
72 changes: 72 additions & 0 deletions youtube-dl-gui/Config/Size.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace murrty.structs;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Size {
/// <summary>
/// Represents an empty Size value.
/// </summary>
public static readonly Size Empty = new(0, 0);
/// <summary>
/// The width of the Size.
/// </summary>
public int Width {
get; set;
}
/// <summary>
/// The height of the Size.
/// </summary>
public int Height {
get; set;
}
/// <summary>
/// Whether the <see cref="Size"/> can be considered a valid <see cref="Size"/> compared to <see cref="Empty"/>.
/// </summary>
public bool Valid => this.Width > Empty.Width && this.Height > Empty.Height;

public Size() {
this = Empty;
}

public Size(int Width, int Height) {
this.Width = Width;
this.Height = Height;
}

/// <inheritdoc />
public override bool Equals(object obj) => obj is Size size && this.Width == size.Width && this.Height == size.Height;

/// <inheritdoc />
public override int GetHashCode() {
int hashCode = 859600377;
hashCode = hashCode * -1521134295 + this.Width.GetHashCode();
hashCode = hashCode * -1521134295 + this.Height.GetHashCode();
return hashCode;
}

/// <summary>
/// Equals-to operator.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator ==(Size a, Size b) => a.Width == b.Width && a.Height == b.Height;
/// <summary>
/// Not equals-to operator.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator !=(Size a, Size b) => a.Width != b.Width || a.Height != b.Height;

/// <summary>
/// Implicitly converts this Point structure to a System.Drawing.Point structure.
/// </summary>
/// <param name="p"></param>
public static implicit operator System.Drawing.Size(Size s) => new(s.Width, s.Height);

/// <summary>
/// Implicity converts a System.Drawing.Point structure to this Point structure.
/// </summary>
/// <param name="p"></param>
public static implicit operator Size(System.Drawing.Size s) => new(s.Width, s.Height);
}
13 changes: 1 addition & 12 deletions youtube-dl-gui/CopyData/CopyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class CopyData {
public const int WM_UPDATEDATA = 0x1002;
public const int WM_UPDATERREADY = 0x1003;

public static nint NintAlloc<T>(T param) {
public static nint NintAlloc<StructVal>(StructVal param) {
nint retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return retval;
Expand All @@ -28,20 +28,9 @@ public static T GetParam<T>(nint LParam) {
return Marshal.PtrToStructure<T>(DataStruct.lpData);
}

/// <summary>
/// The FindWindow function retrieves a handle to the top-level
/// window whose class name and window name match the specified strings.
/// This function does not search child windows. This function does not perform a case-sensitive search.
/// </summary>
/// <param name="strClassName">the class name for the window to search for</param>
/// <param name="strWindowName">the name of the window to search for</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern nint FindWindow(string strClassName, string strWindowName);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(nint hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(nint hWnd, int Msg, nint wParam, nint lParam);
}
2 changes: 1 addition & 1 deletion youtube-dl-gui/Forms/frmArchiveDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public frmArchiveDownloader() {
LoadLanguage();

this.Load += (s, e) => {
if (Config.ValidPoint(Config.Settings.Saved.ArchiveDownloaderLocation)) {
if (Config.Settings.Saved.ArchiveDownloaderLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.ArchiveDownloaderLocation;
}
Expand Down
3 changes: 2 additions & 1 deletion youtube-dl-gui/Forms/frmBatchConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public frmBatchConverter() {
lvBatchConvertQueue.SmallImageList = Program.BatchStatusImages;

this.Load += (s, e) => {
if (Config.ValidPoint(Config.Settings.Saved.BatchConverterLocation)) {
if (Config.Settings.Saved.BatchConverterLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.BatchConverterLocation;
}
};
Expand Down
3 changes: 2 additions & 1 deletion youtube-dl-gui/Forms/frmBatchDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private void frmBatchDownloader_Load(object sender, EventArgs e) {
cbBatchFormat.SelectedIndex = Config.Settings.Batch.SelectedAudioFormat;
}

if (Config.ValidPoint(Config.Settings.Saved.BatchConverterLocation)) {
if (Config.Settings.Saved.BatchConverterLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.BatchConverterLocation;
}
chkBatchDownloadClipboardScanVerifyLinks.Checked = Config.Settings.Batch.ClipboardScannerVerifyLinks;
Expand Down
3 changes: 1 addition & 2 deletions youtube-dl-gui/Forms/frmDownloadLanguage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Drawing;
using System.Windows.Forms;

using youtube_dl_gui.updater;
using murrty.updater;

namespace youtube_dl_gui {
public partial class frmDownloadLanguage : Form {
Expand Down
2 changes: 1 addition & 1 deletion youtube-dl-gui/Forms/frmDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void frmExtendedMassDownloader_Load(object sender, EventArgs e) {
}
}

if (Config.ValidPoint(Config.Settings.Saved.QuickDownloaderLocation)) {
if (Config.Settings.Saved.QuickDownloaderLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.QuickDownloaderLocation;
}
Expand Down
4 changes: 2 additions & 2 deletions youtube-dl-gui/Forms/frmExtendedDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public frmExtendedDownloader(string URL, bool Archived) {
lvUnknownFormats.SmallImageList = Program.ExtendedDownloaderSelectedImages;

this.Load += (s, e) => {
if (Config.ValidPoint(Config.Settings.Saved.ExtendedDownloaderLocation)) {
if (Config.Settings.Saved.ExtendedDownloaderLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.ExtendedDownloaderLocation;
}
if (Config.ValidSize(Config.Settings.Saved.ExtendedDownloaderSize))
if (Config.Settings.Saved.ExtendedDownloaderSize.Valid)
this.Size = Config.Settings.Saved.ExtendedDownloaderSize;
if (!Config.Settings.Saved.ExtendedDownloadVideoColumns.IsNullEmptyWhitespace())
lvVideoFormats.SetColumnWidths(Config.Settings.Saved.ExtendedDownloadVideoColumns);
Expand Down
4 changes: 2 additions & 2 deletions youtube-dl-gui/Forms/frmGenericDownloadProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public frmGenericDownloadProgress(string URL, string Output) {
}
};
}
public frmGenericDownloadProgress(string URL, string Output, System.Drawing.Point Location) : this(URL, Output) {
public frmGenericDownloadProgress(string URL, string Output, Point Location) : this(URL, Output) {
this.Load += (s, e) => {
if (Config.ValidPoint(Location)) {
if (Location.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Location;
}
Expand Down
6 changes: 4 additions & 2 deletions youtube-dl-gui/Forms/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ private void frmMain_Load(object sender, EventArgs e) {
YtdlUpdateCheckThread.Start();
}

if (Config.ValidPoint(Config.Settings.Saved.MainFormLocation))
if (Config.Settings.Saved.MainFormLocation.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Config.Settings.Saved.MainFormLocation;
}

this.Size = Config.ValidSize(Config.Settings.Saved.MainFormSize) ?
this.Size = Config.Settings.Saved.MainFormSize.Valid ?
Config.Settings.Saved.MainFormSize : this.MinimumSize;

LoadLanguage();
Expand Down
13 changes: 6 additions & 7 deletions youtube-dl-gui/Forms/frmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public partial class frmSettings : Form {
public frmSettings() {
InitializeComponent();

for (int i = 0; i < updater.GithubLinks.Repos.Length; i++) {
cbSettingsDownloadsUpdatingYtdlType.Items.Add($"{updater.GithubLinks.Users[i]}/{updater.GithubLinks.Repos[i]}{(i == 0 ? " (default)" : "")}");
for (int i = 0; i < GithubLinks.Repos.Length; i++) {
cbSettingsDownloadsUpdatingYtdlType.Items.Add($"{GithubLinks.Users[i]}/{GithubLinks.Repos[i]}{(i == 0 ? " (default)" : "")}");
}

LoadingForm = true;
Expand All @@ -38,11 +38,10 @@ public frmSettings() {
}

private void frmSettings_Load(object sender, EventArgs e) {
if (Config.Settings.Saved.SettingsFormSize != System.Drawing.Size.Empty) {
if (Config.Settings.Saved.SettingsFormSize.Valid) {
this.StartPosition = FormStartPosition.Manual;
this.Size = Config.Settings.Saved.SettingsFormSize;
}

LoadingForm = false;
}

Expand Down Expand Up @@ -747,9 +746,9 @@ private void llbSettingsDownloadsYtdlTypeViewRepo_LinkClicked(object sender, Lin
if (cbSettingsDownloadsUpdatingYtdlType.SelectedIndex > -1) {
Process.Start(
string.Format(
updater.GithubLinks.GithubRepoUrl,
updater.GithubLinks.Users[cbSettingsDownloadsUpdatingYtdlType.SelectedIndex],
updater.GithubLinks.Repos[cbSettingsDownloadsUpdatingYtdlType.SelectedIndex]
GithubLinks.GithubRepoUrl,
GithubLinks.Users[cbSettingsDownloadsUpdatingYtdlType.SelectedIndex],
GithubLinks.Repos[cbSettingsDownloadsUpdatingYtdlType.SelectedIndex]
)
);
}
Expand Down
Loading

0 comments on commit 865aa67

Please sign in to comment.