diff --git a/PartPreviewWindow/PrinterTabPage.cs b/PartPreviewWindow/PrinterTabPage.cs index 13fd756d2..a6bdb1619 100644 --- a/PartPreviewWindow/PrinterTabPage.cs +++ b/PartPreviewWindow/PrinterTabPage.cs @@ -28,6 +28,7 @@ either expressed or implied, of the FreeBSD Project. */ using System; +using System.Linq; using MatterHackers.Agg; using MatterHackers.Agg.UI; using MatterHackers.GCodeVisualizer; @@ -66,6 +67,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private Vector3 viewerVolume; private Vector2 bedCenter; + private SystemWindow parentSystemWindow; + public PrinterTabPage(PrinterSettings activeSettings, PrintItemWrapper printItem) { printer = ApplicationController.Instance.Printer; @@ -473,9 +476,33 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { ApplicationController.Instance.ActiveView3DWidget = modelViewer; LoadActivePrintItem(); + + // Find and hook the parent system window KeyDown event + if (this.Parents().FirstOrDefault() is SystemWindow systemWindow) + { + systemWindow.KeyDown += Parent_KeyDown; + parentSystemWindow = systemWindow; + } + base.OnLoad(args); } + private void Parent_KeyDown(object sender, KeyEventArgs keyEvent) + { + if (gcodeViewer.Visible) + { + switch (keyEvent.KeyCode) + { + case Keys.Up: + printer.BedPlate.ActiveLayerIndex += 1; + break; + case Keys.Down: + printer.BedPlate.ActiveLayerIndex -= 1; + break; + } + } + } + public override void OnDraw(Graphics2D graphics2D) { bool printerIsRunningPrint = PrinterConnection.Instance.PrinterIsPaused || PrinterConnection.Instance.PrinterIsPrinting; @@ -502,6 +529,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow modelViewer.meshViewerWidget.TrackballTumbleWidget.DrawGlContent -= TrackballTumbleWidget_DrawGlContent; } + // Find and unhook the parent system window KeyDown event + if (parentSystemWindow != null) + { + parentSystemWindow.KeyDown -= Parent_KeyDown; + } + printer.BedPlate.ActiveLayerChanged -= ActiveLayer_Changed; printer.BedPlate.LoadedGCodeChanged -= BedPlate_LoadedGCodeChanged; diff --git a/PartPreviewWindow/ViewGcodeBasic.cs b/PartPreviewWindow/ViewGcodeBasic.cs index 9f038f653..a047031bc 100644 --- a/PartPreviewWindow/ViewGcodeBasic.cs +++ b/PartPreviewWindow/ViewGcodeBasic.cs @@ -68,8 +68,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private PrinterConfig printer; private ViewControls3D viewControls3D; - private SystemWindow parentSystemWindow; - public ViewGcodeBasic(Vector3 viewerVolume, Vector2 bedCenter, BedShape bedShape, ViewControls3D viewControls3D) { printer = ApplicationController.Instance.Printer; @@ -97,34 +95,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => printer.BedPlate.GCodeRenderer?.Clear3DGCode(), ref unregisterEvents); } - public override void OnLoad(EventArgs args) - { - // Find and hook the parent system window KeyDown event - if (this.Parents().FirstOrDefault() is SystemWindow systemWindow) - { - systemWindow.KeyDown += Parent_KeyDown; - parentSystemWindow = systemWindow; - } - - base.OnLoad(args); - } - - private void Parent_KeyDown(object sender, KeyEventArgs keyEvent) - { - if (this.Visible) - { - switch(keyEvent.KeyCode) - { - case Keys.Up: - printer.BedPlate.ActiveLayerIndex += 1; - break; - case Keys.Down: - printer.BedPlate.ActiveLayerIndex -= 1; - break; - } - } - } - private GCodeFile loadedGCode => printer.BedPlate.LoadedGCode; internal void CreateAndAddChildren() @@ -248,13 +218,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow public override void OnClosed(ClosedEventArgs e) { unregisterEvents?.Invoke(this, null); - - // Find and unhook the parent system window KeyDown event - if (parentSystemWindow != null) - { - parentSystemWindow.KeyDown -= Parent_KeyDown; - } - base.OnClosed(e); } }