1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-03 23:06:09 +00:00
6502bench/MakeDist/CopyProgress.cs

135 lines
4.7 KiB
C#
Raw Normal View History

2018-09-28 17:05:11 +00:00
/*
* Copyright 2018 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using CommonWinForms;
namespace MakeDist {
public partial class CopyProgress : Form {
/// <summary>
/// Progress message, with colorful text. This is generated by the worker thread and
/// passed to the UI thread.
/// </summary>
public class ProgressMessage {
public string Text { get; private set; }
public Color Color { get; private set; }
public bool HasColor { get { return Color.A != 0; } }
public ProgressMessage(string msg) : this(msg, Color.FromArgb(0, 0, 0, 0)) { }
public ProgressMessage(string msg, Color color) {
Text = msg;
Color = color;
}
}
private bool mClosedWhileRunning;
// Copy parameters.
private FileCopier.BuildType mBuildType;
private bool mCopyTestFiles;
public CopyProgress(FileCopier.BuildType buildType, bool copyTestFiles) {
InitializeComponent();
this.Size = new Size(1200, 600);
mBuildType = buildType;
mCopyTestFiles = copyTestFiles;
}
private void CopyProgress_Load(object sender, EventArgs e) {
backgroundWorker1.RunWorkerAsync();
}
private void CopyProgress_FormClosing(object sender, FormClosingEventArgs e) {
if (backgroundWorker1.IsBusy) {
backgroundWorker1.CancelAsync();
DialogResult = DialogResult.Cancel;
// First close attempt is converted to a cancel.
if (!mClosedWhileRunning) {
e.Cancel = true;
mClosedWhileRunning = true;
}
} else {
DialogResult = DialogResult.OK;
}
}
private void cancelButton_Click(object sender, EventArgs e) {
if (backgroundWorker1.IsBusy) {
backgroundWorker1.CancelAsync();
} else {
Close();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
BackgroundWorker worker = sender as BackgroundWorker;
FileCopier copier = new FileCopier(mBuildType, mCopyTestFiles);
e.Result = copier.CopyAllFiles(worker);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
if (e.UserState is ProgressMessage) {
ProgressMessage msg = e.UserState as ProgressMessage;
if (msg.HasColor) {
progressRichTextBox.AppendText(msg.Text, msg.Color);
} else {
// plain foreground text color
progressRichTextBox.AppendText(msg.Text);
}
progressRichTextBox.SelectionStart = progressRichTextBox.Text.Length;
progressRichTextBox.ScrollToCaret();
} else {
if (!string.IsNullOrEmpty((string)e.UserState)) {
Debug.WriteLine("Weird progress: " + e.UserState);
}
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e) {
if (e.Cancelled) {
Debug.WriteLine("Test halted -- user cancellation");
} else if (e.Error != null) {
// test harness shouldn't be throwing errors like this
Debug.WriteLine("Test failed: " + e.Error.ToString());
progressRichTextBox.AppendText("\r\n");
progressRichTextBox.AppendText(e.Error.ToString());
progressRichTextBox.SelectionStart = progressRichTextBox.Text.Length;
progressRichTextBox.ScrollToCaret();
} else {
bool ok = (bool)e.Result;
Debug.WriteLine("Tests complete, success=" + ok);
}
if (mClosedWhileRunning) {
Close();
}
cancelButton.Text = "Close";
}
}
}