bookmark-manager-r4/UpdateIconsForm.cs
2024-07-17 01:56:17 +01:00

342 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookmarkManager.Services;
using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using RyzStudio.Windows.ThemedForms.PickerBox;
namespace FizzyLauncher
{
public class UpdateIconsForm : TDialog
{
private System.Windows.Forms.Label label1;
private ThButton button1;
private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator1;
private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator2;
private Label label2;
private PictureBox pictureBox2;
private ThYesNoPickerBox pickerBox1;
private ThButton button2;
private RyzStudio.Windows.ThemedForms.ThProgressBar progressBar1;
private Label label3;
private ThProgressBar thProgressBar1;
private System.ComponentModel.IContainer components;
private readonly WebProvider _webProvider;
private BookmarkTreeView _treeView;
private bool _isBusy = false;
private bool _requestCancel = false;
public UpdateIconsForm(BookmarkTreeView treeView)
{
InitializeComponent();
this.OkButton = button1;
_webProvider = new WebProvider();
_treeView = treeView;
}
private void InitializeComponent()
{
label1 = new Label();
button1 = new ThButton();
tHorizontalSeparator1 = new THorizontalSeparator();
tHorizontalSeparator2 = new THorizontalSeparator();
label2 = new Label();
pictureBox2 = new PictureBox();
pickerBox1 = new ThYesNoPickerBox();
button2 = new ThButton();
progressBar1 = new ThProgressBar();
((System.ComponentModel.ISupportInitialize)pictureBox2).BeginInit();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.BackColor = System.Drawing.Color.Transparent;
label1.ForeColor = System.Drawing.SystemColors.ControlText;
label1.Location = new System.Drawing.Point(10, 21);
label1.Margin = new Padding(0);
label1.Name = "label1";
label1.Padding = new Padding(0, 8, 0, 0);
label1.Size = new System.Drawing.Size(156, 23);
label1.TabIndex = 153;
label1.Text = "Update Missing Icons (Only)";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// button1
//
button1.AcceptButton = null;
button1.ActiveImage = null;
button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
button1.BackColor = System.Drawing.Color.Transparent;
button1.EnableMenuOnClick = false;
button1.EnableReactiveVisual = true;
button1.HoverImage = null;
button1.IdleImage = null;
button1.LabelText = "&Close";
button1.Location = new System.Drawing.Point(301, 469);
button1.Margin = new Padding(10, 10, 10, 0);
button1.Name = "button1";
button1.Padding = new Padding(4, 4, 3, 3);
button1.Size = new System.Drawing.Size(128, 32);
button1.TabIndex = 2;
button1.TabStop = false;
//
// tHorizontalSeparator1
//
tHorizontalSeparator1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
tHorizontalSeparator1.AutoScrollMargin = new System.Drawing.Size(0, 0);
tHorizontalSeparator1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
tHorizontalSeparator1.BackColor = System.Drawing.Color.Transparent;
tHorizontalSeparator1.Location = new System.Drawing.Point(10, 437);
tHorizontalSeparator1.Margin = new Padding(0, 10, 0, 0);
tHorizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2);
tHorizontalSeparator1.MinimumSize = new System.Drawing.Size(0, 22);
tHorizontalSeparator1.Name = "tHorizontalSeparator1";
tHorizontalSeparator1.Size = new System.Drawing.Size(424, 22);
tHorizontalSeparator1.TabIndex = 188;
tHorizontalSeparator1.TabStop = false;
//
// tHorizontalSeparator2
//
tHorizontalSeparator2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
tHorizontalSeparator2.AutoScrollMargin = new System.Drawing.Size(0, 0);
tHorizontalSeparator2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
tHorizontalSeparator2.BackColor = System.Drawing.Color.Transparent;
tHorizontalSeparator2.Location = new System.Drawing.Point(10, 65);
tHorizontalSeparator2.Margin = new Padding(0, 10, 0, 0);
tHorizontalSeparator2.MaximumSize = new System.Drawing.Size(4920, 2);
tHorizontalSeparator2.MinimumSize = new System.Drawing.Size(0, 22);
tHorizontalSeparator2.Name = "tHorizontalSeparator2";
tHorizontalSeparator2.Size = new System.Drawing.Size(424, 22);
tHorizontalSeparator2.TabIndex = 190;
tHorizontalSeparator2.TabStop = false;
//
// label2
//
label2.AutoSize = true;
label2.BackColor = System.Drawing.Color.Transparent;
label2.ForeColor = System.Drawing.SystemColors.ControlText;
label2.Location = new System.Drawing.Point(10, 98);
label2.Margin = new Padding(0);
label2.Name = "label2";
label2.Padding = new Padding(0, 8, 0, 0);
label2.Size = new System.Drawing.Size(289, 23);
label2.TabIndex = 195;
label2.Text = "Attempt to retrieve supported icons from bookmarks.";
label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// pictureBox2
//
pictureBox2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
pictureBox2.BackColor = System.Drawing.Color.Transparent;
pictureBox2.ErrorImage = null;
pictureBox2.InitialImage = null;
pictureBox2.Location = new System.Drawing.Point(261, 171);
pictureBox2.Name = "pictureBox2";
pictureBox2.Size = new System.Drawing.Size(32, 32);
pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox2.TabIndex = 202;
pictureBox2.TabStop = false;
//
// pickerBox1
//
pickerBox1.AcceptButton = null;
pickerBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
pickerBox1.BackColor = System.Drawing.Color.Transparent;
pickerBox1.EnableReactiveVisual = true;
pickerBox1.Font = new System.Drawing.Font("Segoe UI", 9F);
pickerBox1.Location = new System.Drawing.Point(350, 21);
pickerBox1.Margin = new Padding(10, 10, 10, 0);
pickerBox1.Name = "pickerBox1";
pickerBox1.Padding = new Padding(4, 4, 3, 3);
pickerBox1.SelectedIndex = 1;
pickerBox1.Size = new System.Drawing.Size(84, 35);
pickerBox1.TabIndex = 0;
pickerBox1.TabStop = false;
pickerBox1.Value = true;
//
// button2
//
button2.AcceptButton = null;
button2.ActiveImage = null;
button2.Anchor = AnchorStyles.Top | AnchorStyles.Right;
button2.BackColor = System.Drawing.Color.Transparent;
button2.EnableMenuOnClick = false;
button2.EnableReactiveVisual = true;
button2.HoverImage = null;
button2.IdleImage = null;
button2.LabelText = "&Run";
button2.Location = new System.Drawing.Point(306, 171);
button2.Margin = new Padding(10, 10, 10, 0);
button2.Name = "button2";
button2.Padding = new Padding(4, 4, 3, 3);
button2.Size = new System.Drawing.Size(128, 32);
button2.TabIndex = 1;
button2.TabStop = false;
button2.MouseClick += button2_MouseClick;
//
// progressBar1
//
progressBar1.AcceptButton = null;
progressBar1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
progressBar1.BackColor = System.Drawing.Color.Transparent;
progressBar1.BarColour = System.Drawing.Color.FromArgb(86, 81, 157);
progressBar1.BarTextColour = System.Drawing.Color.Black;
progressBar1.EnableReactiveVisual = false;
progressBar1.Font = new System.Drawing.Font("Segoe UI", 9F);
progressBar1.Location = new System.Drawing.Point(10, 141);
progressBar1.Margin = new Padding(10, 10, 10, 0);
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Name = "progressBar1";
progressBar1.Padding = new Padding(4, 4, 3, 3);
progressBar1.ProgressText = "50/100";
progressBar1.ShowProgressText = true;
progressBar1.Size = new System.Drawing.Size(424, 20);
progressBar1.TabIndex = 206;
progressBar1.TabStop = false;
progressBar1.Value = 50;
//
// UpdateIconsForm
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(444, 521);
Controls.Add(progressBar1);
Controls.Add(button2);
Controls.Add(pickerBox1);
Controls.Add(pictureBox2);
Controls.Add(label2);
Controls.Add(tHorizontalSeparator2);
Controls.Add(tHorizontalSeparator1);
Controls.Add(button1);
Controls.Add(label1);
KeyPreview = true;
MinimumSize = new System.Drawing.Size(460, 560);
Name = "UpdateIconsForm";
Text = "Update Icons";
((System.ComponentModel.ISupportInitialize)pictureBox2).EndInit();
ResumeLayout(false);
PerformLayout();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var model = _treeView.ToNodeList<BookmarkModel>();
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = model.Count;
pickerBox1.Focus();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
protected bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
UIControl.SetEnable(pickerBox1, !this.IsBusy);
UIControl.Invoke(button2, (x) => {
button2.LabelText = (this.IsBusy ? "&Stop" : "&Run");
});
UIControl.SetValue(pictureBox2, (this.IsBusy ? RyzStudio.Windows.ThemedForms.Resource2.loading_block : null));
UIControl.SetEnable(button1, !this.IsBusy);
}
}
private async void button2_MouseClick(object sender, MouseEventArgs e)
{
await Task.Run(async () =>
{
if (this.IsBusy)
{
_requestCancel = true;
return;
}
this.IsBusy = true;
var model = _treeView.ToNodeList<BookmarkModel>();
if (pickerBox1.Value)
{
model = model.Where(x => x.Value.Icon == null)?.ToList() ?? new List<KeyValuePair<TreeNode, BookmarkModel>>();
}
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = model.Count;
foreach (var item in model)
{
progressBar1.Value++;
if (string.IsNullOrWhiteSpace(item.Value.Address))
{
continue;
}
if (_requestCancel)
{
break;
}
var document = await _webProvider.RetrieveHtmlDocument(item.Value.Address);
if (document == null)
{
continue;
}
if (_requestCancel)
{
break;
}
var image = await _webProvider.RetrieveImage(document);
if (image == null)
{
continue;
}
var newModel = item.Value;
newModel.Icon = image;
_treeView.UpdateNode(item.Key, newModel);
}
_requestCancel = false;
this.IsBusy = false;
});
}
}
}