Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
- Fixed the 'jsRuntime null' error when starting the program. This was an issue with trying to show a message when changing a setting, when the window wasn't even created yet, as well as other minor issues.
- Removed low memory minimize -- The optimizations (destroying and recreating the browser when closing/opening seems to be done automatically... or has changed somewhat to use less ram anyways. My addition to it seems unnesecary)
  • Loading branch information
TCNOco committed Jun 26, 2021
1 parent b220071 commit 329d3e4
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 63 deletions.
15 changes: 13 additions & 2 deletions TcNo-Acc-Switcher-Client/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,23 @@ protected override async void OnStartup(StartupEventArgs e)
/// </summary>
private static void IsRunningAlready()
{

AppSettings.Instance.LoadFromFile();
try
{
// Check if program is running, if not: return.
if (Mutex.WaitOne(TimeSpan.Zero, true)) return;

// Otherwise: It has probably just closed. Wait a few and try again
Thread.Sleep(2000); // 2 seconds before just making sure -- Might be an admin restart
// The program is running at this point.
// If set to minimize to tray, try open it.
if (AppSettings.Instance.TrayMinimizeNotExit)
{
if (Globals.BringToFront())
Environment.Exit(1056); // 1056 An instance of the service is already running.
}

// Otherwise: It has probably just closed. Wait a few and try again
Thread.Sleep(2000); // 2 seconds before just making sure -- Might be an admin restart

if (Mutex.WaitOne(TimeSpan.Zero, true)) return;
// Try to show from tray, as user may not know it's hidden there.
Expand Down
31 changes: 1 addition & 30 deletions TcNo-Acc-Switcher-Client/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public MainWindow()
if (Directory.Exists(Globals.OriginalWwwroot)) GeneralFuncs.RecursiveDelete(new DirectoryInfo(Globals.OriginalWwwroot), false);
Directory.Move(Path.Join(Globals.AppDataFolder, "wwwroot"), Globals.OriginalWwwroot);
}


AppSettings.LoadFromFile();

FindOpenPort();
_address = "--urls=http://localhost:" + AppSettings.ServerPort + "/";

Expand Down Expand Up @@ -230,8 +228,6 @@ private void WindowStateChange(object sender, EventArgs e)
{
Globals.DebugWriteLine(@"[Func:(Client)MainWindow.xaml.cs.WindowStateChange]");

if (AppSettings.TrayMinimizeLessMem) new Thread(CheckVisibility).Start();

var state = WindowState switch
{
WindowState.Maximized => "add",
Expand All @@ -241,31 +237,6 @@ private void WindowStateChange(object sender, EventArgs e)
MView2.ExecuteScriptAsync("document.body.classList." + state + "('maximised')");
}

private void CheckVisibility()
{
Thread.Sleep(100);
// While this could handle WindowState == WindowState.Minimised/Normal etc, it's only going to work off the hidden part.
// As currently this is only going to create/dispose the WebView for better performance when minimising to tray.
Dispatcher.Invoke(() =>
{
// Check if hidden or not:
var windowLong = Globals.GetWindow(new WindowInteropHelper(this).Handle);
if (windowLong == (windowLong & ~Globals.WsExAppWindow)) // Hidden
{
MainBackground.Children.Remove(MView2);
MView2.Dispose();
GC.WaitForPendingFinalizers();
}
else // Not hidden
{
MView2 = new WebView2();
MView2.Initialized += MView2_OnInitialised;
MainBackground.Children.Add(MView2);
MView2.BeginInit();
}
});
}

/// <summary>
/// Saves window size when closing.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion TcNo-Acc-Switcher-Globals/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Globals
#pragma warning disable CA2211 // Non-constant fields should not be visible - This is necessary due to it being a launch parameter.
public static bool VerboseMode;
#pragma warning restore CA2211 // Non-constant fields should not be visible
public static readonly string Version = "2021-06-23_01";
public static readonly string Version = "2021-06-26_00";
public static readonly string[] PlatformList = {"Steam", "Origin", "Ubisoft", "BattleNet", "Epic", "Riot"};

#region LOGGER
Expand Down
10 changes: 5 additions & 5 deletions TcNo-Acc-Switcher-Server/Data/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public string CurrentStatus
#region JS_INTEROP
public static bool InvokeVoidAsync(string func)
{
return InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func));
return ActiveIJsRuntime is not null && InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func));
}

public static bool InvokeVoidAsync(string func, string arg)
{
return InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg));
return ActiveIJsRuntime is not null && InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg));
}

public static bool InvokeVoidAsync(string func, object arg)
{
return InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg));
}
return ActiveIJsRuntime is not null && InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg));
}

public static bool InvokeVoidAsync(string func, string arg, string arg2)
{
return InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg, arg2));
return ActiveIJsRuntime is not null && InvokeVoidAsync(async () => await ActiveIJsRuntime.InvokeVoidAsync(func, arg, arg2));
}

private static bool InvokeVoidAsync(Action func)
Expand Down
26 changes: 9 additions & 17 deletions TcNo-Acc-Switcher-Server/Data/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,7 @@ public static AppSettings Instance
private bool _trayMinimizeNotExit;

[JsonProperty("TrayMinimizeNotExit", Order = 6)]
public bool TrayMinimizeNotExit
{
get => _instance._trayMinimizeNotExit;
set
{
if (value)
{
_ = GeneralInvocableFuncs.ShowToast("info", "On clicking the Exit button: I'll be on the Windows Tray! (Right of Start Bar)", duration: 15000, renderTo: "toastarea");
_ = GeneralInvocableFuncs.ShowToast("info", "Hint: Ctrl+Click the 'X' to close me completely, or via the Tray > 'Exit'", duration: 15000, renderTo: "toastarea");
}
_instance._trayMinimizeNotExit = value;

}
}

private bool _trayMinimizeLessMem;
[JsonProperty("TrayMinimizeLessMem", Order = 7)] public bool TrayMinimizeLessMem { get => _instance._trayMinimizeLessMem; set => _instance._trayMinimizeLessMem = value; }
public bool TrayMinimizeNotExit { get => _instance._trayMinimizeNotExit; set => _instance._trayMinimizeNotExit = value; }


private bool _desktopShortcut;
Expand Down Expand Up @@ -611,6 +595,14 @@ public void DesktopShortcut_Toggle()
s.ToggleShortcut(!DesktopShortcut);
}

public void TrayMinimizeNotExit_Toggle()
{
Globals.DebugWriteLine(@"[Func:Data\Settings\Steam.DesktopShortcut_Toggle]");
if (!TrayMinimizeNotExit) return;
_ = GeneralInvocableFuncs.ShowToast("info", "On clicking the Exit button: I'll be on the Windows Tray! (Right of Start Bar)", duration: 15000, renderTo: "toastarea");
_ = GeneralInvocableFuncs.ShowToast("info", "Hint: Ctrl+Click the 'X' to close me completely, or via the Tray > 'Exit'", duration: 15000, renderTo: "toastarea");
}

/// <summary>
/// Check for existence of protocol key in registry (tcno:\\)
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions TcNo-Acc-Switcher-Server/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@
// This is run before everything is painted on, so that it can get the correct values for the CSS Block.
protected override void OnParametersSet()
{
if (!GeneralFuncs.WindowSettingsValid())
{
// Do something here if necessary.
// For now this is just being used to load settings.
}
}

protected override void OnAfterRender(bool firstRender)
{
AppData.Instance.WindowTitle = "TcNo Account Switcher";
if (!GeneralFuncs.WindowSettingsValid())
{
// Do something here if necessary.
// For now this is just being used to load settings.
}
if (firstRender)
{
GeneralFuncs.HandleQueries();
Expand Down
1 change: 0 additions & 1 deletion TcNo-Acc-Switcher-Server/Resources/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Settings_ClearAccounts: Clear Accounts
Settings_RestoreForgotten: Restore forgotten account
Settings_StreamerMode: Enable streamer mode
Settings_ExitToTray: Exit minimises to tray
Settings_TrayLowMem: Use less RAM while in tray
Settings_DesktopShortcut: Desktop shortcut
Settings_StartMenu_ProgramTray: Program & Tray
Settings_StartMenu_Platform: Platform
Expand Down
3 changes: 1 addition & 2 deletions TcNo-Acc-Switcher-Server/Shared/SharedSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
<div class="form-check"><input class="form-check-input" type="checkbox" id="StreamerMode" @bind="_appSett.StreamerModeEnabled"><label class="form-check-label" for="StreamerMode"></label></div><label for="StreamerMode">@_locale["Settings_StreamerMode"]<br></label>
</div>
<div class="rowSetting">
<div class="form-check"><input class="form-check-input" type="checkbox" id="TrayMinimizeNotExit" @bind="_appSett.TrayMinimizeNotExit"><label class="form-check-label" for="TrayMinimizeNotExit"></label></div><label for="TrayMinimizeNotExit">@_locale["Settings_ExitToTray"]<br></label>
<div class="form-check"><input class="form-check-input" type="checkbox" id="TrayMinimizeLessMem" @bind="_appSett.TrayMinimizeLessMem"><label class="form-check-label" for="TrayMinimizeLessMem"></label></div><label for="TrayMinimizeLessMem">@_locale["Settings_TrayLowMem"]<br></label>
<div class="form-check"><input class="form-check-input" type="checkbox" id="TrayMinimizeNotExit" @bind="_appSett.TrayMinimizeNotExit" @onclick="() => _appSett.TrayMinimizeNotExit_Toggle()"><label class="form-check-label" for="TrayMinimizeNotExit"></label></div><label for="TrayMinimizeNotExit">@_locale["Settings_ExitToTray"]<br></label>
</div>
<div class="rowSetting">
<div class="form-check"><input class="form-check-input" type="checkbox" id="AppSett_DesktopShortcut" @bind="_appSett.DesktopShortcut" @onclick="() => _appSett.DesktopShortcut_Toggle()"><label class="form-check-label" for="AppSett_DesktopShortcut"></label></div><label for="AppSett_DesktopShortcut">@_locale["Settings_DesktopShortcut"]<br></label>
Expand Down

0 comments on commit 329d3e4

Please sign in to comment.