Files
cms_thermo_active/Client/View/MainForm.cs
T

805 lines
26 KiB
C#

using Chromium;
using Chromium.Event;
using Chromium.Remote.Event;
using Chromium.WebBrowser;
using Client.Config;
using Client.Utils;
using Active_Client.Browser_Tools;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static Client.Utils.Constants;
using System.Diagnostics;
namespace Active_Client.View
{
public partial class MainForm : Form
{
//Struct of PageError
private struct PageError
{
public bool Error;
public string ErrorInfo;
}
Random rnd = new Random();
//Internal Variables
private PageError LoadingError;
private NcForm NcFrm;
private ProdForm ProdFrm;
private LoadingForm LdFrm;
private static IntPtr MainHandle;
private static IntPtr NcHandle;
private static IntPtr ProdHandle;
private int X = 0, Y = 0;
private string BrokenUrl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region WINDOW_START_&_BEHAVIOUR_METHOD
//Instance Method
public MainForm()
{
LdFrm = new LoadingForm();
LdFrm.Owner = this;
//Set the Loading window
ShowLoading("Setup Main View...");
//Initialize the Windows
InitializeComponent();
//Setup the Icon
if (Config.ClientConfig.IsSCM)
this.Icon = Properties.Resources.MAESTRO_ACTIVE_ICON;
//Start Client Setting
InitializeClientSettings();
//Initialize the Browser-Components
InitializeBrowser();
//Set the client property
Text = Application.ProductName + " " + Application.ProductVersion;
Opacity = 0.0;
//Initialize the Nc-Form
InitializeNcForm();
//Load the page
Browser.LoadUrl(Config.ConnectionConfig.StartingUrl);
//Set the title of the window in "Loading"
SetWindowTitle("Loading...");
ShowLoading("Loading Main View...");
NcWindow.mainFrm = this;
}
#if DEBUG
private void ChooseBestScreen()
{
var candidate = Screen.PrimaryScreen;
// If primary screen is not full HD find another screen
if(candidate.WorkingArea.Width != 1920)
{
foreach (var s in Screen.AllScreens)
{
if (s.WorkingArea.Width == 1920) candidate = s;
}
if (candidate != Screen.PrimaryScreen)
{
WindowState = FormWindowState.Normal;
X = Left = candidate.WorkingArea.Left;
Y = Top = candidate.WorkingArea.Top;
Width = candidate.WorkingArea.Width;
Height = candidate.WorkingArea.Height;
WindowState = FormWindowState.Maximized;
}
}
}
#endif
//On windows-Load Method
private void MainForm_Load(object sender, EventArgs e)
{
DesktopLocation = new Point(X, Y);
#if DEBUG
ChooseBestScreen();
#endif
}
//On Resize Form
private void MainForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
NcWindow.ShowTaskBar();
NcWindow.MinimizeNcWindow();
NcWindow.MinimizeProdWindow();
}
else
{
NcWindow.ShowNcWindow();
NcWindow.ShowProdWindow();
if (NcWindow.State == NcState.SHOW)
{
ShowNCWindow();
}
else if(NcWindow.State == NcState.SHOWPROD)
{
ShowProdWindow();
}
else
HideAUXWindow();
}
//Refresh Browser Paint
Browser.Refresh();
Refresh();
}
//Before closing the Window -> Remove the Browser (Increase the speed of closing window)
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
//Show Taskbar
NcWindow.ShowTaskBar();
//Hide Window
Hide();
//Close Virtual Keyboard Runtime
if (Config.ClientConfig.ShowVirtualKeyboard && Environment.OSVersion.Version.Major < 10)
NcWindow.closeVirtualKeyboard();
//Close the NC HMI && Stop Following Nc
if (Config.VendorHmiConfig.Enabled)
NcWindow.CloseNcWindow(false);
//Close Server
if (Config.ClientConfig.Autorun)
{
Process[] proc = Process.GetProcessesByName("Active");
foreach(Process p in proc)
{
p.Kill();
p.WaitForExit();
}
}
if (Config.ProdSoftwareConfig.Enabled)
NcWindow.CloseProdWindow(false);
//Close Chromium Runtime
try
{
CfxRuntime.Shutdown();
}
catch (Exception)
{
}
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region SETUP_METHODS
//Initialize CMS-Client Settings
private void InitializeClientSettings()
{
//Set the Transparency Key
if (Config.ClientConfig.RenderingMethod == Constants.Rendering.CPU && Environment.OSVersion.Version.Major < 10)
TransparencyKey = Config.ClientConfig.TranspColor;
//Calculate Window Position
CalcWindowPosition();
}
//Initialize CMS-Client Settings
private void InitializeNcForm()
{
//Create the Nc-Form
if (Config.VendorHmiConfig.Enabled)
{
NcFrm = new NcForm(Config.ClientConfig.TranspColor, X, Y);
NcHandle = NcFrm.Handle;
NcFrm.Show();
}
if (Config.ProdSoftwareConfig.Enabled)
{
ProdFrm = new ProdForm(Config.ClientConfig.TranspColor, X, Y);
ProdHandle = ProdFrm.Handle;
ProdFrm.Show();
}
//Steup the Step variables
MainHandle = Handle;
}
//Initialize the Browser-Component
private void InitializeBrowser()
{
//Add Custom object to browser
Browser.GlobalObject.Add(Constants.BROWSER_JS_OBJ_NAME, new BrowserJSObject(this));
//Add all Handlers
Browser.DisplayHandler.OnTitleChange += BrowserTitleChanged;
Browser.LoadHandler.OnLoadingStateChange += BrowserLoadsChanged;
Browser.LoadHandler.OnLoadError += BrowserLoadsError;
Browser.KeyboardHandler.OnKeyEvent += BrowserKeyPress;
Browser.ContextMenuHandler.OnBeforeContextMenu += BrowserContextMenu;
Browser.DisplayHandler.OnConsoleMessage += BrowserConsoleMessage;
Browser.DownloadHandler.OnBeforeDownload += BeforeDownload;
//Filter only < Win_10 Platform
if (Config.ClientConfig.ShowVirtualKeyboard && Environment.OSVersion.Version.Major < 10)
ChromiumWebBrowser.RemoteProcessCreated += (e) => { e.RenderProcessHandler.OnFocusedNodeChanged += BrowserNodeChanged; };
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region BROWSER_EVENT_HANDLERS_METHODS
private void BeforeDownload(object sender, CfxOnBeforeDownloadEventArgs e)
{
e.Callback.Continue("",true);
}
//On browser Load-Error. Event Handler called by Browser
private void BrowserLoadsError(object sender, CfxOnLoadErrorEventArgs e)
{
//Set the Load-Error global variable to TRUE
LoadingError.Error = true;
LoadingError.ErrorInfo = e.ErrorCode.GetHashCode() + " - " + e.ErrorText;
if (e.FailedUrl != Constants.errorPageUrl)
BrokenUrl = e.FailedUrl;
//write the error in the Title Bar
SetWindowTitle(LoadingError.ErrorInfo);
}
//On browser Load-Changed. Event Handler called by Browser
private void BrowserLoadsChanged(object sender, CfxOnLoadingStateChangeEventArgs e)
{
//if the State is NOT "loading"
if (!e.Browser.IsLoading)
{
//Hide Loading Window
HideLoadingWindow();
//Show the Main Window, when the page is loaded
ShowWindow();
//If is an Error Show the Error page
if (LoadingError.Error && !LoadingError.ErrorInfo.Contains("ERR_ABORTED"))
{
Browser.LoadString(Config.ConnectionConfig.ErrorPage, Constants.errorPageUrl);
}
//Set focus on the page
BrowserFocus();
}
//if the State is "loading"
else
{
//Hide NC Window
NcWindow.SetState(0);
HideAUXWindow();
//Set the title of the window in "Loading"
SetWindowTitle("Loading...");
//Set the Load-Error global variable to FALSE
LoadingError.Error = false;
}
}
//On browser Title-Changed. Event Handler called by Browser
private void BrowserTitleChanged(object sender, CfxOnTitleChangeEventArgs e)
{
if (!LoadingError.Error)
SetWindowTitle(e.Title);
}
// on Key-Press Browser
private void BrowserKeyPress(object sender, CfxOnKeyEventEventArgs e)
{
}
//Show Dev Tools Window
private void ShowDevTools()
{
//Setup Dev Window
CfxWindowInfo windowInfo = new CfxWindowInfo
{
Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE),
ParentWindow = MainHandle,
WindowName = Application.ProductName + " | CEF Dev Tools",
X = 1120,
Y = 0,
Width = 800,
Height = 1080
};
//Show Dev Window
Browser.BrowserHost.ShowDevTools(windowInfo, new CfxClient(), new CfxBrowserSettings(), null);
}
//Show Chrome GPU Window
private void ShowChromeGPU()
{
int w = 1600, h = 800;
//Setup Window
CfxWindowInfo windowInfo = new CfxWindowInfo();
windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE);
windowInfo.ParentWindow = MainHandle;
windowInfo.WindowName = Application.ProductName + " | CEF Graphic Tools";
windowInfo.X = (Screen.PrimaryScreen.Bounds.Width / 2) - (w / 2);
windowInfo.Y = (Screen.PrimaryScreen.Bounds.Height / 2) - (h / 2);
windowInfo.Width = w;
windowInfo.Height = h;
//Show Window
CfxBrowserHost.CreateBrowser(windowInfo, new CfxClient(), "chrome://gpu/", ChromiumWebBrowser.DefaultBrowserSettings, null);
}
//Show Chrome Version Window
private void ShowChromeVersion()
{
int w = 1600, h = 800;
//Setup Window
CfxWindowInfo windowInfo = new CfxWindowInfo();
windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_POPUP | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE);
windowInfo.ParentWindow = MainHandle;
windowInfo.WindowName = Application.ProductName + " | CEF Chromium-Version Tools";
windowInfo.X = (Screen.PrimaryScreen.Bounds.Width / 2) - (w / 2);
windowInfo.Y = (Screen.PrimaryScreen.Bounds.Height / 2) - (h / 2);
windowInfo.Width = w;
windowInfo.Height = h;
//Show Window
CfxBrowserHost.CreateBrowser(windowInfo, new CfxClient(), "chrome://version/", ChromiumWebBrowser.DefaultBrowserSettings, null);
}
//Override default Browser Context Menu
private void BrowserContextMenu(object sender, CfxOnBeforeContextMenuEventArgs e)
{
e.Model.Clear();
}
//Override Javascript Console Message
private void BrowserConsoleMessage(object sender, CfxOnConsoleMessageEventArgs e)
{
e.SetReturnValue(true);
}
//Node Changed Handler
private void BrowserNodeChanged(object sender, CfrOnFocusedNodeChangedEventArgs ev)
{
if (ev.Node != null)
{
string NodeName = ev.Node.ElementTagName.ToLower();
//Filter if this node is an INPUT Node
if (NodeName != null && NodeName.Equals("input"))
{
string dom = ev.Node.GetElementAttribute("type").ToLower();
//Filter if the node Type is TEXT or PASSWORD
if (dom != null && (dom.Equals("text") || dom.Equals("password")))
NcWindow.openVirtualKeyboard(ev.Node.ElementBounds, ev.Browser.Identifier > 1, false);
}
//Filter if this node is TEXTAREA Node
else if (NodeName != null && NodeName.Equals("textarea"))
NcWindow.openVirtualKeyboard(ev.Node.ElementBounds, ev.Browser.Identifier > 1, false);
else
NcWindow.hideVirtualKeyboard();
}
else
NcWindow.hideVirtualKeyboard();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region INVOKES_&_OTHER_METHODS
//Reload Broken Url
public void reloadBrokenPage()
{
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
if (!string.IsNullOrWhiteSpace(BrokenUrl))
Browser.LoadUrl(BrokenUrl);
});
else
{
if (!string.IsNullOrWhiteSpace(BrokenUrl))
Browser.LoadUrl(BrokenUrl);
}
}
}
//Show MainWindow Method
private void ShowWindow()
{
if(Config.ClientConfig.DeveloperMode && Width < 1920 && Height < 1080)
{
double ratio = (((double)Width) / 1920.0) * 100.0;
//Funzione sperimentale presa da Forum CEF
double delta = 5.46149645 * Math.Log(ratio) - 25.12;
Browser.BrowserHost.ZoomLevel = delta;
}
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
WindowState = FormWindowState.Maximized;
Opacity = 1.0;
});
else
{
WindowState = FormWindowState.Maximized;
Opacity = 1.0;
}
}
if (!NcWindow.isFollowing)
{
//Start following NC Window
if (Config.VendorHmiConfig.Enabled || Config.ProdSoftwareConfig.Enabled)
NcWindow.StartNcFollowing(MainHandle, NcHandle, ProdHandle, NcFrm.Width, NcFrm.Height);
else
NcWindow.StartStepFollowing(MainHandle);
}
}
//Show Hide window
private void HideLoadingWindow()
{
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
LdFrm.Hide();
});
else
{
LdFrm.Hide();
}
}
}
//Show Message window
private void ShowLoading(string Message)
{
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
//Set Message Status
LdFrm.Show(Message);
LdFrm.Focus();
});
else
{
//Set Message Status
LdFrm.Show(Message);
}
}
}
//Show MainWindow Method
private void BrowserFocus()
{
//Invoke method if is needed or call the method in STD mode
if (Browser.InvokeRequired)
Browser.Invoke((MethodInvoker)delegate () { Browser.Focus(); });
else
Browser.Focus();
}
//Set Window Title
private void SetWindowTitle(string Title)
{
//Setup product label
if (Environment.Is64BitProcess)
Title = Application.ProductName + " x64 | " + Title;
else
Title = Application.ProductName + " x86 | " + Title;
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate () { Text = Title; });
else
Text = Title;
}
}
//Show NC Method
public void ShowNCWindow()
{
if (!Config.VendorHmiConfig.Enabled)
return;
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
if (NcFrm.Owner == null)
{
Owner = null;
NcFrm.Owner = this;
if (ProdFrm != null)
ProdFrm.Owner = null;
}
NcFrm.TopMost = true;
NcFrm.TopMost = false;
});
else
{
if (NcFrm.Owner == null)
{
Owner = null;
NcFrm.Owner = this;
if (ProdFrm != null)
ProdFrm.Owner = null;
}
NcFrm.TopMost = true;
NcFrm.TopMost = false;
}
}
}
//Show NC Method
public void ShowProdWindow()
{
if (!Config.ProdSoftwareConfig.Enabled)
return;
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
if (ProdFrm.Owner == null)
{
Owner = null;
ProdFrm.Owner = this;
if (NcFrm != null)
NcFrm.Owner = null;
}
ProdFrm.TopMost = true;
ProdFrm.TopMost = false;
});
else
{
if (ProdFrm.Owner == null)
{
Owner = null;
ProdFrm.Owner = this;
if (NcFrm != null)
NcFrm.Owner = null;
}
ProdFrm.TopMost = true;
ProdFrm.TopMost = false;
}
}
}
//Hide NC Method
public void HideAUXWindow()
{
if ((!Config.VendorHmiConfig.Enabled && !Config.ProdSoftwareConfig.Enabled) || (NcFrm==null && ProdFrm == null))
return;
//Invoke method if is needed or call the method in STD mode
if (!IsDisposed)
{
if (InvokeRequired)
Invoke((MethodInvoker)delegate ()
{
if (NcFrm != null && NcFrm.Owner != null)
NcFrm.Owner = null;
if (ProdFrm != null && ProdFrm.Owner != null)
ProdFrm.Owner = null;
TopMost = true;
TopMost = false;
});
else
{
if (NcFrm != null && NcFrm.Owner != null)
NcFrm.Owner = null;
if (ProdFrm != null && ProdFrm.Owner != null)
ProdFrm.Owner = null;
TopMost = true;
TopMost = false;
}
}
}
//Calculate Window Position
private void CalcWindowPosition()
{
//Position of the Window
Screen ps = Screen.AllScreens.FirstOrDefault(S => S.Primary == true);
if (Screen.AllScreens.Length > 1 && Config.ClientConfig.RunningOnSecondaryScreen)
{
Screen ss = Screen.AllScreens.FirstOrDefault(S => S.Primary == false);
X = ps.Bounds.Width;
if (ss.Bounds.Top != 0)
Y = ss.Bounds.Top;
//Set the Secondary screen Size
if (Config.ClientConfig.DeveloperMode)
{
if (ss.Bounds.Width < 1920 && ss.Bounds.Height < 1080)
{
this.Width = ss.Bounds.Width;
this.Height = ss.Bounds.Height;
}
}
else
{
if (ss.Bounds.Width != 1920 && ss.Bounds.Height != 1080)
{
HideLoadingWindow();
if(Config.VendorHmiConfig.Enabled)
NcWindow.CloseNcWindow(false);
if (Config.ProdSoftwareConfig.Enabled)
NcWindow.CloseProdWindow(false);
Program.ShowAlarmAndClose("Screen resolution " + ss.Bounds.Width + "x" + ss.Bounds.Height + " is not supported");
}
}
}
else
{
if (Config.ClientConfig.DeveloperMode)
{
//Set the Prymary screen Size
if (ps.Bounds.Width < 1920 && ps.Bounds.Height < 1080)
{
this.Width = ps.Bounds.Width;
this.Height = ps.Bounds.Height;
}
}
else
{
if (ps.Bounds.Width != 1920 && ps.Bounds.Height != 1080)
{
HideLoadingWindow();
if (Config.VendorHmiConfig.Enabled)
NcWindow.CloseNcWindow(false);
if (Config.ProdSoftwareConfig.Enabled)
NcWindow.CloseProdWindow(false);
Program.ShowAlarmAndClose("Screen resolution " + ps.Bounds.Width + "x" + ps.Bounds.Height + " is not supported");
}
}
}
}
//Windows Keys Handler
public void keyPressedHandler(bool altPressed, bool ctrlPressed, bool shiftPressed, int key)
{
//Work only if is a Developer mode
if (Config.ClientConfig.DeveloperMode && altPressed)
{
if (key == (int)Keys.F1)
Browser.GoBack();
if (key == (int)Keys.F2)
Browser.GoForward();
if (key == (int)Keys.F5)
{
if (Browser.Url.AbsoluteUri != Constants.errorPageUrl)
Browser.Browser.ReloadIgnoreCache();
else
Browser.LoadUrl(BrokenUrl);
}
if (key == (int)Keys.F10)
ShowChromeVersion();
if (key == (int)Keys.F11)
ShowChromeGPU();
if (key == (int)Keys.F12)
ShowDevTools();
}
}
#endregion
}
}