using Active_Client.Browser_Tools; using Chromium; using Chromium.Event; using Chromium.Remote.Event; using Chromium.WebBrowser; using Client.Config; using Client.Utils; using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using static Client.Utils.Constants; 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; private Boolean closeRight = false; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #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(); if (Config.VendorHmiConfig.Type == 3) NcWindow.MinimizeNcWindow(); if (Config.VendorHmiConfig.Enabled) NcFrm.Hide(); if (Config.ProdSoftwareConfig.Enabled) ProdFrm.Hide(); } else { if (Config.VendorHmiConfig.Enabled) NcWindow.ShowNcWindow(); if (Config.ProdSoftwareConfig.Enabled) NcWindow.ShowProdWindow(); if (Config.VendorHmiConfig.Enabled) NcFrm.Show(); if (Config.ProdSoftwareConfig.Enabled) ProdFrm.Show(); if (NcWindow.State == NcState.SHOW) { ShowNCWindow(); } else if (NcWindow.State == NcState.SHOWPROD) { ShowProdWindow(); } else HideAUXWindow(); } //Refresh Browser Paint Browser.Refresh(); Refresh(); } public void sendClose() { //Close Virtual Keyboard Runtime if (Config.ClientConfig.ShowVirtualKeyboard) NcWindow.closeVirtualKeyboard(); //Close the NC HMI && Stop Following Nc if (Config.VendorHmiConfig.Enabled) NcWindow.CloseNcWindow(false); if (Config.ProdSoftwareConfig.Enabled) NcWindow.CloseProdWindow(false); //Show Taskbar NcWindow.ShowTaskBar(); //Close Chromium Runtime closeRight = true; Close(); try { CfxRuntime.Shutdown(); } catch (Exception) { Environment.Exit(0); } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (!closeRight) e.Cancel = true; } #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) 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, 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, 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") || dom.Equals(""))) 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 int w = 0; int h = 0; if (Config.VendorHmiConfig.Enabled) { w = NcFrm.Width; h = NcFrm.Height; } else if (Config.ProdSoftwareConfig.Enabled) { w = ProdFrm.Width; h = ProdFrm.Height; } if (Config.VendorHmiConfig.Enabled || Config.ProdSoftwareConfig.Enabled) NcWindow.StartNcFollowing(MainHandle, NcHandle, ProdHandle, w, h); 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; if (Config.ProdSoftwareConfig.Enabled) NcWindow.HideProdWindow(); NcWindow.ShowNcWindow(); }); else { if (NcFrm.Owner == null) { Owner = null; NcFrm.Owner = this; if (ProdFrm != null) ProdFrm.Owner = null; } NcFrm.TopMost = true; NcFrm.TopMost = false; if (Config.ProdSoftwareConfig.Enabled) NcWindow.HideProdWindow(); NcWindow.ShowNcWindow(); } } } //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; if (Config.VendorHmiConfig.Enabled) NcWindow.HideNcWindow(); NcWindow.ShowProdWindow(); }); else { if (ProdFrm.Owner == null) { Owner = null; ProdFrm.Owner = this; if (NcFrm != null) NcFrm.Owner = null; } ProdFrm.TopMost = true; ProdFrm.TopMost = false; if (Config.VendorHmiConfig.Enabled) NcWindow.HideNcWindow(); NcWindow.ShowProdWindow(); } } } //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; if (Config.VendorHmiConfig.Enabled) NcWindow.HideNcWindow(); if (Config.ProdSoftwareConfig.Enabled) NcWindow.HideProdWindow(); }); else { if (NcFrm != null && NcFrm.Owner != null) NcFrm.Owner = null; if (ProdFrm != null && ProdFrm.Owner != null) ProdFrm.Owner = null; TopMost = true; TopMost = false; if (Config.VendorHmiConfig.Enabled) NcWindow.HideNcWindow(); if (Config.ProdSoftwareConfig.Enabled) NcWindow.HideProdWindow(); } } } //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"); } } } } private void MainForm_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode == Keys.F4) { e.Handled = true; } } private void Browser_KeyDown(object sender, KeyEventArgs e) { } //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.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 } }