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
2020-08-23 14:42:33 +01:00

131 lines
3.6 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();
ThreadControl.SetValue(progressBar1, 0, nodeList.Count);
for (int i=0; i<nodeList.Count; i++)
{
if (threadWorker.CancellationPending)
{
return;
}
ThreadControl.SetValue(progressBar1, (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;
ThreadControl.SetText(button2, "&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)
{
ThreadControl.SetText(button2, "&Cancelling...");
threadWorker.CancelAsync();
}
else
{
this.IsBusy = true;
ThreadControl.SetText(button2, "&Updating...");
threadWorker.RunWorkerAsync();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
}
}