Files
VRCX/Dotnet/Cef/MainForm.cs
Natsumi 938fff63d0 Electron support for Linux (#1074)
* init

* SQLite changes

* Move html folder, edit build scripts

* AppApi interface

* Build flags

* AppApi inheritance

* Finishing touches

* Merge upstream changes

* Test CI

* Fix class inits

* Rename AppApi

* Merge upstream changes

* Fix SQLiteLegacy on Linux, Add Linux interop, build tools

* Linux specific localisation strings

* Make it run

* Bring back most of Linux functionality

* Clean up

* Fix TTS voices

* Fix UI var

* Changes

* Electron minimise to tray

* Remove separate toggle for WlxOverlay

* Fixes

* Touchups

* Move csproj

* Window zoom, Desktop Notifications, VR check on Linux

* Fix desktop notifications, VR check spam

* Fix building on Linux

* Clean up

* Fix WebApi headers

* Rewrite VRCX updater

* Clean up

* Linux updater

* Add Linux to build action

* init

* SQLite changes

* Move html folder, edit build scripts

* AppApi interface

* Build flags

* AppApi inheritance

* Finishing touches

* Merge upstream changes

* Test CI

* Fix class inits

* Rename AppApi

* Merge upstream changes

* Fix SQLiteLegacy on Linux, Add Linux interop, build tools

* Linux specific localisation strings

* Make it run

* Bring back most of Linux functionality

* Clean up

* Fix TTS voices

* Changes

* Electron minimise to tray

* Remove separate toggle for WlxOverlay

* Fixes

* Touchups

* Move csproj

* Window zoom, Desktop Notifications, VR check on Linux

* Fix desktop notifications, VR check spam

* Fix building on Linux

* Clean up

* Fix WebApi headers

* Rewrite VRCX updater

* Clean up

* Linux updater

* Add Linux to build action

* Test updater

* Rebase and handle merge conflicts

* Fix Linux updater

* Fix Linux app restart

* Fix friend order

* Handle AppImageInstaller, show an install message on Linux

* Updates to the AppImage installer

* Fix Linux updater, fix set version, check for .NET, copy wine prefix

* Handle random errors

* Rotate tall prints

* try fix Linux restart bug

* Final

---------

Co-authored-by: rs189 <35667100+rs189@users.noreply.github.com>
2025-01-11 13:09:44 +13:00

247 lines
7.8 KiB
C#

// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System;
using System.Drawing;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using NLog;
namespace VRCX
{
public partial class MainForm : WinformBase
{
public static MainForm Instance;
public static NativeWindow nativeWindow;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public ChromiumWebBrowser Browser;
private readonly Timer _saveTimer;
private int LastLocationX;
private int LastLocationY;
private int LastSizeWidth;
private int LastSizeHeight;
private FormWindowState _LastWindowStateToRestore = FormWindowState.Normal;
private FormWindowState LastWindowStateToRestore
{
get => _LastWindowStateToRestore;
set
{
// Used to restore window state after minimized
if (FormWindowState.Minimized != value)
{
_LastWindowStateToRestore = value;
}
}
}
public MainForm()
{
Instance = this;
InitializeComponent();
nativeWindow = NativeWindow.FromHandle(this.Handle);
// adding a 5s delay here to avoid excessive writes to disk
_saveTimer = new Timer();
_saveTimer.Interval = 5000;
_saveTimer.Tick += SaveTimer_Tick;
try
{
var location = Assembly.GetExecutingAssembly().Location;
var icon = Icon.ExtractAssociatedIcon(location);
Icon = icon;
TrayIcon.Icon = icon;
}
catch (Exception ex)
{
logger.Error(ex);
}
Browser = new ChromiumWebBrowser("file://vrcx/index.html")
{
DragHandler = new CustomDragHandler(),
MenuHandler = new CustomMenuHandler(),
DownloadHandler = new CustomDownloadHandler(),
BrowserSettings =
{
DefaultEncoding = "UTF-8",
},
Dock = DockStyle.Fill
};
Browser.IsBrowserInitializedChanged += (A, B) =>
{
if (Program.LaunchDebug)
Browser.ShowDevTools();
};
JavascriptBindings.ApplyAppJavascriptBindings(Browser.JavascriptObjectRepository);
Browser.ConsoleMessage += (_, args) =>
{
logger.Debug(args.Message + " (" + args.Source + ":" + args.Line + ")");
};
Controls.Add(Browser);
}
private void MainForm_Load(object sender, System.EventArgs e)
{
try
{
int.TryParse(VRCXStorage.Instance.Get("VRCX_LocationX"), out LastLocationX);
int.TryParse(VRCXStorage.Instance.Get("VRCX_LocationY"), out LastLocationY);
int.TryParse(VRCXStorage.Instance.Get("VRCX_SizeWidth"), out LastSizeWidth);
int.TryParse(VRCXStorage.Instance.Get("VRCX_SizeHeight"), out LastSizeHeight);
var location = new Point(LastLocationX, LastLocationY);
var size = new Size(LastSizeWidth, LastSizeHeight);
var screen = Screen.FromPoint(location);
if (screen.Bounds.Contains(location.X, location.Y))
{
Location = location;
}
Size = new Size(1920, 1080);
if (size.Width > 0 && size.Height > 0)
{
Size = size;
}
}
catch (Exception ex)
{
logger.Error(ex);
}
try
{
var state = WindowState;
if (int.TryParse(VRCXStorage.Instance.Get("VRCX_WindowState"), out int v))
{
state = (FormWindowState)v;
}
if (state == FormWindowState.Minimized)
{
state = FormWindowState.Normal;
}
if ("true".Equals(VRCXStorage.Instance.Get("VRCX_StartAsMinimizedState")))
{
state = FormWindowState.Minimized;
}
if ("true".Equals(VRCXStorage.Instance.Get("VRCX_StartAsMinimizedState")) &&
"true".Equals(VRCXStorage.Instance.Get("VRCX_CloseToTray")))
{
BeginInvoke(Hide);
}
else
{
WindowState = state;
}
}
catch (Exception ex)
{
logger.Error(ex);
}
LastWindowStateToRestore = WindowState;
// 가끔 화면 위치가 안맞음.. 이걸로 해결 될지는 모르겠음
Browser.Invalidate();
}
private void MainForm_Resize(object sender, System.EventArgs e)
{
LastWindowStateToRestore = WindowState;
if (WindowState != FormWindowState.Normal)
{
return;
}
LastSizeWidth = Size.Width;
LastSizeHeight = Size.Height;
_saveTimer?.Start();
}
private void SaveTimer_Tick(object sender, EventArgs e)
{
SaveWindowState();
_saveTimer?.Stop();
}
private void MainForm_Move(object sender, System.EventArgs e)
{
if (WindowState != FormWindowState.Normal)
{
return;
}
LastLocationX = Location.X;
LastLocationY = Location.Y;
_saveTimer?.Start();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing &&
"true".Equals(VRCXStorage.Instance.Get("VRCX_CloseToTray")))
{
e.Cancel = true;
Hide();
}
}
private void SaveWindowState()
{
VRCXStorage.Instance.Set("VRCX_LocationX", LastLocationX.ToString());
VRCXStorage.Instance.Set("VRCX_LocationY", LastLocationY.ToString());
VRCXStorage.Instance.Set("VRCX_SizeWidth", LastSizeWidth.ToString());
VRCXStorage.Instance.Set("VRCX_SizeHeight", LastSizeHeight.ToString());
VRCXStorage.Instance.Set("VRCX_WindowState", ((int)WindowState).ToString());
VRCXStorage.Instance.Flush();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
SaveWindowState();
}
private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Focus_Window();
}
}
private void TrayMenu_Open_Click(object sender, System.EventArgs e)
{
Focus_Window();
}
public void Focus_Window()
{
Show();
if (WindowState == FormWindowState.Minimized)
{
WindowState = LastWindowStateToRestore;
}
// Focus();
Activate();
}
private void TrayMenu_DevTools_Click(object sender, System.EventArgs e)
{
Instance.Browser.ShowDevTools();
}
private void TrayMenu_Quit_Click(object sender, System.EventArgs e)
{
SaveWindowState();
Application.Exit();
}
}
}