Enable pause/resume on task, remove pause/resume/cancel from bar

- Issue MatterHackers/MCCentral#2407
Extend Tasks.Execute to support custom pause/stop implementations
This commit is contained in:
John Lewin 2017-12-12 17:53:32 -08:00
parent a45b9dab19
commit bd79414c34
8 changed files with 77 additions and 84 deletions

View file

@ -38,26 +38,23 @@ using MatterHackers.MatterControl.SlicerConfiguration;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
public class PrintPauseResumeButton : FlowLayoutWidget
public class PrintButton : FlowLayoutWidget
{
private GuiWidget finishSetupButton;
private GuiWidget pausePrintButton;
private PrinterConfig printer;
private GuiWidget resumePrintButton;
private GuiWidget startPrintButton;
private EventHandler unregisterEvents;
private PrinterConfig printer;
public PrintPauseResumeButton(PrinterActionsBar printerActionsBar, PrinterTabPage printerTabPage, PrinterConfig printer, ThemeConfig theme)
public PrintButton(PrinterTabPage printerTabPage, PrinterConfig printer, ThemeConfig theme)
{
var defaultMargin = theme.ButtonSpacing;
this.printer = printer;
// add the finish setup button
finishSetupButton = theme.ButtonFactory.Generate("Setup...".Localize(), AggContext.StaticData.LoadIcon("icon_play_32x32.png", 14, 14, IconColor.Theme));
finishSetupButton.Name = "Finish Setup Button";
finishSetupButton.ToolTipText = "Run setup configuration for printer.".Localize();
finishSetupButton.Margin = defaultMargin;
finishSetupButton.Margin = theme.ButtonSpacing;
finishSetupButton.Click += (s, e) =>
{
UiThread.RunOnIdle(async () =>
@ -75,54 +72,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
this.AddChild(finishSetupButton);
// add the start print button
startPrintButton = theme.ButtonFactory.Generate("Print".Localize(), AggContext.StaticData.LoadIcon("icon_play_32x32.png", 14, 14, IconColor.Theme));
startPrintButton.Name = "Start Print Button";
startPrintButton.ToolTipText = "Begin printing the selected item.".Localize();
startPrintButton.Margin = defaultMargin;
startPrintButton.Click += (s, e) =>
{
UiThread.RunOnIdle(async () =>
{
// Save any pending changes before starting print operation
await ApplicationController.Instance.Tasks.Execute(printerTabPage.view3DWidget.SaveChanges);
var context = printer.Bed.EditContext;
await ApplicationController.Instance.PrintPart(
context.PartFilePath,
context.GCodeFilePath,
context.SourceItem.Name,
printer,
null,
CancellationToken.None);
});
};
startPrintButton = new PrintPopupMenu(printer, theme, printerTabPage);
startPrintButton.Margin = theme.ButtonSpacing;
this.AddChild(startPrintButton);
// add the pause / resume button
pausePrintButton = theme.ButtonFactory.Generate("Pause".Localize(), AggContext.StaticData.LoadIcon("icon_pause_32x32.png", 14, 14, IconColor.Theme));
pausePrintButton.ToolTipText = "Pause the current print".Localize();
pausePrintButton.Margin = defaultMargin;
pausePrintButton.Click += (s, e) =>
{
UiThread.RunOnIdle(printer.Connection.RequestPause);
pausePrintButton.Enabled = false;
};
this.AddChild(pausePrintButton);
resumePrintButton = theme.ButtonFactory.Generate("Resume".Localize(), AggContext.StaticData.LoadIcon("icon_play_32x32.png", 14, 14, IconColor.Theme));
resumePrintButton.ToolTipText = "Resume the current print".Localize();
resumePrintButton.Margin = defaultMargin;
resumePrintButton.Name = "Resume Button";
resumePrintButton.Click += (s, e) =>
{
if (printer.Connection.PrinterIsPaused)
{
printer.Connection.Resume();
}
pausePrintButton.Enabled = true;
};
this.AddChild(resumePrintButton);
printer.Connection.CommunicationStateChanged.RegisterEvent((s, e) =>
{
UiThread.RunOnIdle(SetButtonStates);
@ -157,11 +110,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
case CommunicationStates.PrintingFromSd:
case CommunicationStates.Printing:
SetChildVisible(pausePrintButton, true);
break;
case CommunicationStates.Paused:
SetChildVisible(resumePrintButton, true);
break;
case CommunicationStates.FinishedPrint:

View file

@ -27,14 +27,13 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Threading;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.SlicerConfiguration;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
@ -43,7 +42,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
private TextImageButtonFactory buttonFactory = ApplicationController.Instance.Theme.ButtonFactory;
private PrinterConfig printer;
private PrinterTabPage printerTabPage;
private bool activelySlicing = false;
public PrintPopupMenu(PrinterConfig printer, ThemeConfig theme, PrinterTabPage printerTabPage)
{
@ -112,6 +110,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
CancellationToken.None);
});
};
button.EnabledChanged += (s, e) => Console.WriteLine();
column.AddChild(button);
return column;
@ -123,17 +122,15 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
Name = "Start Print Button",
BackgroundColor = theme.ButtonFactory.Options.NormalFillColor,
HoverColor = theme.ButtonFactory.Options.HoverFillColor,
Margin = theme.ButtonSpacing,
});
}
private class IgnoredFlowLayout : FlowLayoutWidget, IIgnoredPopupChild
{
public IgnoredFlowLayout()
: base (FlowDirection.TopToBottom)
: base(FlowDirection.TopToBottom)
{
}
}
}
}

View file

@ -82,12 +82,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
}
this.AddChild(new PrinterConnectButton(printer, theme));
this.AddChild(new PrintPauseResumeButton(this, printerTabPage, printer, theme));
var printButton = new PrintPopupMenu(printer, theme, printerTabPage);
this.AddChild(printButton);
this.AddChild(new CancelButton(printer, theme));
this.AddChild(new PrintButton(printerTabPage, printer, theme));
var sliceButton = new SliceButton(printer, theme)
{