Use standard event listener pattern

This commit is contained in:
John Lewin 2018-11-16 08:44:56 -08:00
parent 469a92fab9
commit 2873a74ef7
38 changed files with 1298 additions and 1182 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, Kevin Pope, John Lewin
Copyright (c) 2018, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -46,7 +46,6 @@ namespace MatterHackers.MatterControl.ActionBar
protected ImageWidget ImageWidget;
protected EventHandler unregisterEvents;
protected PrinterConfig printer;
protected List<GuiWidget> alwaysEnabled;
@ -111,12 +110,8 @@ namespace MatterHackers.MatterControl.ActionBar
};
container.AddChild(DirectionIndicator);
void CommunicationStateChanged(object s, EventArgs e)
{
this.EnableControls();
}
printer.Connection.CommunicationStateChanged += CommunicationStateChanged;
this.Closed += (s, e) => printer.Connection.CommunicationStateChanged -= CommunicationStateChanged;
// Register listeners
printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
foreach (var child in this.Children)
{
@ -157,12 +152,19 @@ namespace MatterHackers.MatterControl.ActionBar
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
// Unregister listeners
printer.Connection.CommunicationStateChanged -= Connection_CommunicationStateChanged;
base.OnClosed(e);
}
bool? isEnabled = null;
private void Connection_CommunicationStateChanged(object s, EventArgs e)
{
this.EnableControls();
}
private void EnableControls()
{
bool status = printer.Connection.IsConnected && !printer.Connection.PrinterIsPrinting;

View file

@ -31,7 +31,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.ImageProcessing;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
@ -48,6 +47,7 @@ namespace MatterHackers.MatterControl.ActionBar
private string waitingForBedToHeatMessage = "The bed is currently heating and its target temperature cannot be changed until it reaches {0}°C.\n\nYou can set the starting bed temperature in SETTINGS -> Filament -> Temperatures.\n\n{1}".Localize();
private string waitingForBedToHeatTitle = "Waiting For Bed To Heat".Localize();
private Dictionary<string, UIField> allUiFields = new Dictionary<string, UIField>();
private RunningInterval runningInterval;
public TemperatureWidgetBed(PrinterConfig printer, ThemeConfig theme)
: base(printer, "150.3°", theme)
@ -60,12 +60,8 @@ namespace MatterHackers.MatterControl.ActionBar
this.PopupContent = this.GetPopupContent(ApplicationController.Instance.MenuTheme);
void BedTemperatureRead(object s, EventArgs e)
{
DisplayCurrentTemperature();
}
printer.Connection.BedTemperatureRead += BedTemperatureRead;
this.Closed += (s, e) => printer.Connection.BedTemperatureRead -= BedTemperatureRead;
// Register listeners
printer.Connection.BedTemperatureRead += Connection_BedTemperatureRead;
}
protected override int ActualTemperature => (int)printer.Connection.ActualBedTemperature;
@ -141,11 +137,10 @@ namespace MatterHackers.MatterControl.ActionBar
Margin = new BorderDouble(0, 5, 0, 0),
};
var runningInterval = UiThread.SetInterval(() =>
runningInterval = UiThread.SetInterval(() =>
{
graph.AddData(this.ActualTemperature);
}, 1);
this.Closed += (s, e) => UiThread.ClearInterval(runningInterval);
var settingsRow = temperatureRow.DescendantsAndSelf<SliceSettingsRow>().FirstOrDefault();
@ -200,6 +195,15 @@ namespace MatterHackers.MatterControl.ActionBar
base.OnDraw(graphics2D);
}
public override void OnClosed(EventArgs e)
{
// Unregister listeners
printer.Connection.BedTemperatureRead -= Connection_BedTemperatureRead;
UiThread.ClearInterval(runningInterval);
base.OnClosed(e);
}
protected override void SetTargetTemperature(double targetTemp)
{
double goalTemp = (int)(targetTemp + .5);
@ -215,5 +219,10 @@ namespace MatterHackers.MatterControl.ActionBar
printer.Connection.TargetBedTemperature = (int)(targetTemp + .5);
}
}
private void Connection_BedTemperatureRead(object s, EventArgs e)
{
DisplayCurrentTemperature();
}
}
}

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, Kevin Pope, John Lewin
Copyright (c) 2018, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -29,10 +29,8 @@ either expressed or implied, of the FreeBSD Project.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.ConfigurationPage;
@ -190,6 +188,7 @@ namespace MatterHackers.MatterControl.ActionBar
private string sliceSettingsNote = "Note: Slice Settings are applied before the print actually starts. Changes while printing will not effect the active print.".Localize();
private string waitingForExtruderToHeatMessage = "The extruder is currently heating and its target temperature cannot be changed until it reaches {0}°C.\n\nYou can set the starting extruder temperature in 'Slice Settings' -> 'Filament'.\n\n{1}".Localize();
private Dictionary<string, UIField> allUiFields = new Dictionary<string, UIField>();
private RunningInterval runningInterval;
public TemperatureWidgetHotend(PrinterConfig printer, int hotendIndex, ThemeConfig theme)
: base(printer, "150.3°", theme)
@ -201,12 +200,8 @@ namespace MatterHackers.MatterControl.ActionBar
this.PopupContent = this.GetPopupContent(ApplicationController.Instance.MenuTheme);
void HotendTemperatureRead(object s, EventArgs e)
{
DisplayCurrentTemperature();
}
printer.Connection.HotendTemperatureRead += HotendTemperatureRead;
this.Closed += (s, e) => printer.Connection.HotendTemperatureRead -= HotendTemperatureRead;
// Register listeners
printer.Connection.HotendTemperatureRead += Connection_HotendTemperatureRead;
}
protected override int ActualTemperature => (int)printer.Connection.GetActualHotendTemperature(this.hotendIndex);
@ -284,12 +279,12 @@ namespace MatterHackers.MatterControl.ActionBar
Width = widget.Width - 20,
Height = 35, // this works better if it is a common multiple of the Width
};
var runningInterval = UiThread.SetInterval(() =>
runningInterval = UiThread.SetInterval(() =>
{
graph.AddData(this.ActualTemperature);
}, 1);
this.Closed += (s, e) => UiThread.ClearInterval(runningInterval);
var valueField = temperatureRow.Descendants<MHNumberEdit>().FirstOrDefault();
valueField.Name = "Temperature Input";
@ -423,6 +418,15 @@ namespace MatterHackers.MatterControl.ActionBar
base.OnDraw(graphics2D);
}
public override void OnClosed(EventArgs e)
{
// Unregister listeners
printer.Connection.HotendTemperatureRead -= Connection_HotendTemperatureRead;
UiThread.ClearInterval(runningInterval);
base.OnClosed(e);
}
protected override void SetTargetTemperature(double targetTemp)
{
double goalTemp = (int)(targetTemp + .5);
@ -438,5 +442,11 @@ namespace MatterHackers.MatterControl.ActionBar
printer.Connection.SetTargetHotendTemperature(hotendIndex, (int)(targetTemp + .5));
}
}
private void Connection_HotendTemperatureRead(object s, EventArgs e)
{
DisplayCurrentTemperature();
}
}
}