Skip to content

Commit

Permalink
add PRELOAD_OS run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed May 18, 2024
1 parent 02f8b39 commit 246506b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 52 deletions.
56 changes: 28 additions & 28 deletions Source/ImageGlass/FrmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ private void Toolbar_ItemClicked(object? sender, ToolStripItemClickedEventArgs e
}



#region Image Loading functions

/// <summary>
Expand Down Expand Up @@ -2400,5 +2399,4 @@ private void MnuExit_Click(object sender, EventArgs e)
#endregion // Main Menu component



}
4 changes: 2 additions & 2 deletions Source/ImageGlass/FrmMain.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>23, 17</value>
<value>22, 14</value>
</metadata>
<metadata name="MnuMain.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>196, 18</value>
Expand Down Expand Up @@ -222,7 +222,7 @@
<value>637, 21</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>69</value>
<value>131</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
39 changes: 33 additions & 6 deletions Source/ImageGlass/FrmMain/FrmMain.Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,22 @@ private void FrmMain_Load(object sender, EventArgs e)
}


// start slideshow
if (Config.EnableSlideshow)
// hide app window
if (Program.IsPreloadWithOs)
{
IG_ToggleSlideshow();
RunAsPreloadOsMode();
}
else
{
// start slideshow
if (Config.EnableSlideshow)
{
IG_ToggleSlideshow();
}

// load first image
LoadImagesFromCmdArgs(Environment.GetCommandLineArgs());
// load first image
LoadImagesFromCmdArgs(Environment.GetCommandLineArgs());
}


// load other low priority data after 500ms
Expand All @@ -286,6 +294,21 @@ private void FrmMain_Load(object sender, EventArgs e)
}


private void RunAsPreloadOsMode()
{
WindowState = FormWindowState.Minimized;
ShowInTaskbar = false;
Visible = false;


Task.Run(async () =>
{
await Task.Delay(3000);
IG_Exit();
});
}


private void LoadLowPriorityFormData()
{
// load menu
Expand Down Expand Up @@ -322,7 +345,11 @@ private void LoadLowPriorityFormData()

private void FrmMainConfig_FormClosing(object? sender, FormClosingEventArgs e)
{
_ = SaveConfigsOnClosing();
// do not save configs when preloading with OS
if (!Program.IsPreloadWithOs)
{
_ = SaveConfigsOnClosing();
}

ToolbarContext.Dispose();
}
Expand Down
3 changes: 1 addition & 2 deletions Source/ImageGlass/FrmMain/FrmMain.IGMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ You should have received a copy of the GNU General Public License
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using WicNet;

namespace ImageGlass;
Expand Down Expand Up @@ -838,7 +837,7 @@ public void IG_SetBackgroundColor(string? hexColor = null)
{
color = BHelper.ColorFromHex(hexColor);
}


if (!color.IsEmpty)
{
Expand Down
26 changes: 14 additions & 12 deletions Source/ImageGlass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ internal static class Program
public static string APP_SINGLE_INSTANCE_ID => "{f2a83de1-b9ac-4461-81d0-cc4547b0b27b}";


/// <summary>
/// Gets value to indicates this ImageGlass instance is to preload to OS.
/// In PRELOAD_OS mode,
/// ImageGlass UI is totally hiden and its process auto-closes after 3s, user settings are not saved.
/// </summary>
public static bool IsPreloadWithOs => Environment.GetCommandLineArgs().Contains(IgCommands.PRELOAD_OS);


/// <summary>
/// The main entry point for the application.
/// </summary>
Expand Down Expand Up @@ -98,7 +106,7 @@ public static bool CheckAndRunQuickSetup()
{
var requiredQuickSetup = false;

if (Config.QuickSetupVersion < Const.QUICK_SETUP_VERSION)
if (Config.QuickSetupVersion < Const.QUICK_SETUP_VERSION && !IsPreloadWithOs)
{
FrmMain.IG_OpenQuickSetupDialog();

Expand Down Expand Up @@ -203,20 +211,13 @@ private static void Instance_ArgumentsReceived(object? sender, ArgsReceivedEvent
// Attempt to run a 2nd instance of IG when multi-instance turned off.
// The primary instance will crash if no file provided
// (e.g. by double-clicking on .EXE in explorer).
var realCount = 0;
foreach (var arg in e.Arguments)
{
if (arg != null)
{
realCount++;
}
}
var argsCount = e.Arguments.Count(i => i != null);

var realArgs = new string[realCount];
Array.Copy(e.Arguments, realArgs, realCount);
var args = new string[argsCount];
Array.Copy(e.Arguments, args, argsCount);

// Execute our delegate on the forms thread!
Local.FrmMain.Invoke(ActivateWindow, (object)realArgs);
Local.FrmMain.Invoke(ActivateWindow, (object)args);
}


Expand All @@ -227,6 +228,7 @@ private static void ActivateWindow(string[] args)
{
if (Local.FrmMain == null) return;


// load image file from arg
Local.FrmMain.LoadImagesFromCmdArgs(args);

Expand Down

0 comments on commit 246506b

Please sign in to comment.