This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/OptionForm.cs

165 lines
4.5 KiB
C#

using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Form = System.Windows.Forms.Form;
using Resources = bzit.bomg.Properties.Resources;
namespace bzit.bomg
{
public partial class OptionForm : Form
{
protected BackgroundWorker threadWorker = null;
protected BookmarkItemModel itemModel = null;
protected MainForm parentForm = null;
public OptionForm(MainForm mainForm) : base()
{
InitializeComponent();
parentForm = mainForm;
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
if (threadWorker == null)
{
threadWorker = new BackgroundWorker();
threadWorker.WorkerReportsProgress = threadWorker.WorkerSupportsCancellation = true;
threadWorker.DoWork += threadWorker1_DoWork;
threadWorker.RunWorkerCompleted += threadWorker1_RunWorkerCompleted;
}
}
protected void threadWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (threadWorker.CancellationPending)
{
return;
}
List<TreeNode> nodeList = parentForm.treeView1.GetBookmarkNodeList();
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new MethodInvoker(() => {
progressBar1.Maximum = nodeList.Count;
progressBar1.Value = 0;
}));
}
else
{
progressBar1.Maximum = nodeList.Count;
progressBar1.Value = 0;
}
for (int i=0; i<nodeList.Count; i++)
{
if (threadWorker.CancellationPending)
{
return;
}
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new MethodInvoker(() => {
progressBar1.Value = (i + 1);
}));
}
else
{
progressBar1.Value = (i + 1);
}
BookmarkItemViewModel viewModel = (BookmarkItemViewModel)nodeList[i].Tag;
BookmarkItemModel model = viewModel.ToModel();
bool rv = model.UpdateFavicon();
if (rv)
{
if (parentForm.treeView1.InvokeRequired)
{
parentForm.treeView1.Invoke(new MethodInvoker(() => {
parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel());
}));
}
else
{
parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel());
}
}
}
}
protected void threadWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.IsBusy = false;
setLabelText("&Update");
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
protected bool IsBusy
{
get => threadWorker.IsBusy;
set
{
pictureBox1.Image = (value) ? Resources.aniZomq2x32 : null;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
setLabelText("&Cancelling...");
threadWorker.CancelAsync();
}
else
{
this.IsBusy = true;
setLabelText("&Updating...");
threadWorker.RunWorkerAsync();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
protected void setLabelText(string text)
{
if (button1.InvokeRequired)
{
button1.Invoke(new MethodInvoker(() => {
button1.LabelText = text;
}));
}
else
{
button1.LabelText = text;
}
}
}
}