mattercontrol/MatterControlLib/SetupWizard/AndroidConnectDevicePage.cs

209 lines
6.9 KiB
C#
Raw Normal View History

/*
Copyright (c) 2016, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrinterCommunication;
namespace MatterHackers.MatterControl
{
public class AndroidConnectDevicePage : DialogPage
2018-04-10 07:35:08 -07:00
{
private TextWidget generalError;
2018-07-11 15:08:07 -07:00
private GuiWidget connectButton;
private GuiWidget skipButton;
private GuiWidget nextButton;
private GuiWidget retryButton;
private GuiWidget troubleshootButton;
private TextWidget skipMessage;
private FlowLayoutWidget retryButtonContainer;
private FlowLayoutWidget connectButtonContainer;
2018-11-16 08:44:56 -08:00
private PrinterConfig printer;
public AndroidConnectDevicePage(PrinterConfig printer)
{
this.printer = printer;
2018-07-12 09:22:28 -07:00
var printerNameLabel = new TextWidget("Connect Your Device".Localize() + ":", 0, 0, labelFontSize)
{
TextColor = theme.TextColor,
Margin = new BorderDouble(bottom: 10)
};
contentRow.AddChild(printerNameLabel);
contentRow.AddChild(new TextWidget("Instructions".Localize() + ":", 0, 0, 12,textColor:theme.TextColor));
contentRow.AddChild(new TextWidget("1. Power on your 3D Printer.".Localize(), 0, 0, 12,textColor:theme.TextColor));
contentRow.AddChild(new TextWidget("2. Attach your 3D Printer via USB.".Localize(), 0, 0, 12,textColor:theme.TextColor));
contentRow.AddChild(new TextWidget("3. Press 'Connect'.".Localize(), 0, 0, 12,textColor:theme.TextColor));
//Add inputs to main container
connectButtonContainer = new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
Margin = new BorderDouble(0, 6)
};
2018-07-11 15:08:07 -07:00
connectButton = theme.CreateLightDialogButton("Connect".Localize());
connectButton.Margin = new BorderDouble(0,0,10,0);
connectButton.Click += ConnectButton_Click;
2018-07-11 15:08:07 -07:00
skipButton = theme.CreateLightDialogButton("Skip".Localize());
skipButton.Click += NextButton_Click;
connectButtonContainer.AddChild(connectButton);
connectButtonContainer.AddChild(skipButton);
connectButtonContainer.AddChild(new HorizontalSpacer());
contentRow.AddChild(connectButtonContainer);
skipMessage = new TextWidget("(Press 'Skip' to setup connection later)".Localize(), 0, 0, 10, textColor: theme.TextColor);
contentRow.AddChild(skipMessage);
generalError = new TextWidget("", 0, 0, errorFontSize)
{
2018-10-15 18:25:53 -07:00
TextColor = theme.PrimaryAccentColor,
HAnchor = HAnchor.Stretch,
Visible = false,
Margin = new BorderDouble(top: 20),
};
contentRow.AddChild(generalError);
//Construct buttons
2018-07-11 15:08:07 -07:00
retryButton = theme.CreateLightDialogButton("Retry".Localize());
retryButton.Click += ConnectButton_Click;
retryButton.Margin = new BorderDouble(0,0,10,0);
//Construct buttons
2018-07-11 15:08:07 -07:00
troubleshootButton = theme.CreateLightDialogButton("Troubleshoot".Localize());
troubleshootButton.Click += (s, e) => UiThread.RunOnIdle(() =>
{
2018-07-12 09:22:28 -07:00
DialogWindow.ChangeToPage(
2018-11-16 08:44:56 -08:00
new SetupWizardTroubleshooting(printer));
});
retryButtonContainer = new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
Margin = new BorderDouble(0, 6),
Visible = false
};
2018-04-10 07:35:08 -07:00
retryButtonContainer.AddChild(retryButton);
retryButtonContainer.AddChild(troubleshootButton);
retryButtonContainer.AddChild(new HorizontalSpacer());
2018-04-10 07:35:08 -07:00
contentRow.AddChild(retryButtonContainer);
//Construct buttons
2018-07-11 15:08:07 -07:00
nextButton = theme.CreateDialogButton("Continue".Localize());
nextButton.Click += NextButton_Click;
nextButton.Visible = false;
GuiWidget hSpacer = new GuiWidget();
hSpacer.HAnchor = HAnchor.Stretch;
this.AddPageAction(nextButton);
// Register listeners
printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
updateControls(true);
}
2017-09-17 21:08:16 -07:00
void ConnectButton_Click(object sender, EventArgs mouseEvent)
{
2018-11-16 08:44:56 -08:00
printer.Connection.Connect();
}
void NextButton_Click(object sender, EventArgs mouseEvent)
{
this.generalError.Text = "Please wait...";
this.generalError.Visible = true;
nextButton.Visible = false;
2018-06-19 15:02:25 -07:00
UiThread.RunOnIdle(this.DialogWindow.Close);
}
2018-11-16 08:44:56 -08:00
private void Connection_CommunicationStateChanged(object sender, EventArgs args)
{
UiThread.RunOnIdle(() => updateControls(false));
}
private void updateControls(bool firstLoad)
{
connectButton.Visible = false;
skipMessage.Visible = false;
generalError.Visible = false;
nextButton.Visible = false;
connectButtonContainer.Visible = false;
retryButtonContainer.Visible = false;
if (printer.Connection.IsConnected)
{
generalError.Text = "{0}!".FormatWith ("Connection succeeded".Localize ());
generalError.Visible = true;
nextButton.Visible = true;
}
else if (firstLoad || printer.Connection.CommunicationState == CommunicationStates.Disconnected)
{
generalError.Text = "";
connectButton.Visible = true;
connectButtonContainer.Visible = true;
}
else if (printer.Connection.CommunicationState == CommunicationStates.AttemptingToConnect)
{
generalError.Text = "{0}...".FormatWith("Attempting to connect".Localize());
generalError.Visible = true;
}
else
{
generalError.Text = "Uh-oh! Could not connect to printer.".Localize();
generalError.Visible = true;
nextButton.Visible = false;
retryButtonContainer.Visible = true;
}
this.Invalidate();
}
public override void OnClosed(EventArgs e)
{
2018-11-16 08:44:56 -08:00
// Unregister listeners
printer.Connection.CommunicationStateChanged -= Connection_CommunicationStateChanged;
base.OnClosed(e);
}
}
}