Files
cms_thermo_active/Client/View/MainForm.cs
T
2018-03-08 11:43:53 +01:00

623 lines
20 KiB
C#

using Client.Config;
using Client.Utils;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Chromium;
using Chromium.WebBrowser;
using Chromium.Event;
using Chromium.WebBrowser.Event;
using System.Diagnostics;
using CMS_Client.Browser_Tools;
using Chromium.Remote;
using Chromium.Remote.Event;
using System.IO;
namespace CMS_Client.View
{
public partial class MainForm : Form
{
//Struct of PageError
private struct PageError
{
public Boolean Error;
public String ErrorInfo;
}
Random rnd = new Random();
//Internal Variables
private PageError LoadingError;
private NcForm NcFrm;
private LoadingForm LdFrm;
private static IntPtr MainHandle;
private static IntPtr NcHandle;
private int X = 0, Y = 0;
private bool CntrlPressed;
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();
//Start Client Setting
InitializeClientSettings();
//Initialize the Browser-Components
InitializeBrowser();
//Set the client property
this.Text = Application.ProductName + " " + Application.ProductVersion;
this.Opacity = 0.0;
//Initialize the Nc-Form
InitializeNcForm();
//Setup Wait Cursor
Cursor.Current = Cursors.WaitCursor;
//Start following NC Window
if (Config.VendorHmiConfig.Enabled)
NcWindow.StartNcFollowing(MainHandle, NcHandle, NcFrm.Width, NcFrm.Height);
else
NcWindow.StartStepFollowing(MainHandle);
//Load the page
Browser.LoadUrl(Config.ConnectionConfig.StartingUrl);
//Set the title of the window in "Loading"
SetWindowTitle("Loading...");
ShowLoading("Loading Main View...");
}
//On windows-Load Method
private void MainForm_Load(object sender, EventArgs e)
{
this.DesktopLocation = new Point(X, Y);
}
//On Resize Form
private void MainForm_Resize(object sender, EventArgs e)
{
if (Config.VendorHmiConfig.Enabled)
{
if (this.WindowState == FormWindowState.Minimized)
{
NcFrm.Hide();
NcWindow.ShowTaskBar();
}
else
{
NcWindow.ShowNcWindow();
NcFrm.Show();
}
}
//Refresh Browser Paint
Browser.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 (!this.IsDisposed)
{
//Show Taskbar
NcWindow.ShowTaskBar();
//Hide Window
this.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();
//Close Chromium Runtime
CfxRuntime.Shutdown();
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region SETUP_METHODS
//Initialize CMS-Client Settings
private void InitializeClientSettings()
{
//Set the Transparency Key
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(TransparencyKey, X, Y);
NcHandle = NcFrm.Handle;
NcFrm.Show();
}
//Steup the Step variables
MainHandle = this.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;
//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
//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)
this.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);
HideNCWindow();
//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)
{
//Save the Control-Key pressed
if (e.Event.Type == CfxKeyEventType.RawKeydown && e.Event.WindowsKeyCode == (int)Keys.ControlKey)
CntrlPressed = true;
else if (e.Event.Type == CfxKeyEventType.Keyup && e.Event.WindowsKeyCode == (int)Keys.ControlKey)
CntrlPressed = false;
//If is a Key Up do an action
if (e.Event.Type == CfxKeyEventType.Keyup)
{
//Work only if is a Developer mode
if (Config.ClientConfig.DeveloperMode)
{
if (e.Event.WindowsKeyCode == (int)Keys.F1)
Browser.GoBack();
if (e.Event.WindowsKeyCode == (int)Keys.F2)
Browser.GoForward();
if (e.Event.WindowsKeyCode == (int)Keys.F5)
{
if (Browser.Url.AbsoluteUri != Constants.errorPageUrl)
Browser.Browser.ReloadIgnoreCache();
else
Browser.LoadUrl(this.BrokenUrl);
}
if (e.Event.WindowsKeyCode == (int)Keys.F10)
ShowChromeVersion();
if (e.Event.WindowsKeyCode == (int)Keys.F11)
ShowChromeGPU();
if (e.Event.WindowsKeyCode == (int)Keys.F12)
ShowDevTools();
}
//Work everytime
if (CntrlPressed && e.Event.WindowsKeyCode == (int)Keys.R)
Browser.Browser.ReloadIgnoreCache();
}
}
//Show Dev Tools Window
private void ShowDevTools(){
//Setup Dev 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 Dev Tools";
windowInfo.X = 1120;
windowInfo.Y = 0;
windowInfo.Width = 800;
windowInfo.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 (!this.IsDisposed)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate ()
{
if(!String.IsNullOrWhiteSpace(this.BrokenUrl))
Browser.LoadUrl(this.BrokenUrl);
});
else
{
if (!String.IsNullOrWhiteSpace(this.BrokenUrl))
Browser.LoadUrl(this.BrokenUrl);
}
}
}
//Show MainWindow Method
private void ShowWindow()
{
//Invoke method if is needed or call the method in STD mode
if (!this.IsDisposed)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate ()
{
this.WindowState = FormWindowState.Maximized;
this.Opacity = 1.0;
});
else
{
this.WindowState = FormWindowState.Maximized;
this.Opacity = 1.0;
}
}
}
//Show Hide window
private void HideLoadingWindow()
{
//Invoke method if is needed or call the method in STD mode
if (!this.IsDisposed)
{
if (this.InvokeRequired)
this.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 (!this.IsDisposed)
{
if (this.InvokeRequired)
this.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 (!this.IsDisposed)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate () { this.Text = Title; });
else
this.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(!this.IsDisposed)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate () {
if (this.NcFrm.Owner == null)
{
this.Owner = null;
this.NcFrm.Owner = this;
this.NcFrm.TopMost = true;
this.NcFrm.TopMost = false;
}
});
else
{
if (this.NcFrm.Owner == null)
{
this.Owner = null;
this.NcFrm.Owner = this;
this.NcFrm.TopMost = true;
this.NcFrm.TopMost = false;
}
}
}
}
//Hide NC Method
public void HideNCWindow()
{
if (!Config.VendorHmiConfig.Enabled)
return;
//Invoke method if is needed or call the method in STD mode
if (!this.IsDisposed)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate ()
{
if(this.NcFrm.Owner != null)
{
this.NcFrm.Owner = null;
this.TopMost = true;
this.TopMost = false;
}
});
else
{
if (this.NcFrm.Owner != null)
{
this.NcFrm.Owner = null;
this.TopMost = true;
this.TopMost = false;
}
}
}
}
//CAlculate Window Position
private void CalcWindowPosition()
{
//Position of the Window
if (Screen.AllScreens.Length > 1 && Config.ClientConfig.RunningOnSecondaryScreen)
{
Screen ps = Screen.AllScreens.FirstOrDefault(S => S.Primary == true);
Screen ss = Screen.AllScreens.FirstOrDefault(S => S.Primary == false);
X = ps.Bounds.Width;
if(ss.Bounds.Top != 0)
Y = ss.Bounds.Top;
}
}
#endregion
}
}