Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 97 additions & 127 deletions Knossos.NET/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,126 +21,92 @@ namespace Knossos.NET
public partial class App : Application
{
TrayIcon? trayIcon = null;
bool minimizeToTray = false;
bool trayMode = false;
private MainWindowViewModel? mainVM = null;

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public void DisableMinimizeToTrayRuntime()
{
minimizeToTray = false;
}

public void EnableMinimizeToTrayRuntime()
{
if (!minimizeToTray && ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (desktop.MainWindow != null)
{
desktop.MainWindow.PropertyChanged += (v, __) =>
{
if (minimizeToTray && v is MainWindow view && view.WindowState == WindowState.Minimized)
{
desktop.MainWindow.Hide();
desktop.MainWindow.WindowState = WindowState.Normal;
StartTrayIcon();
trayIcon!.IsVisible = true;
}
};
desktop.MainWindow.Closing += (_, __) =>
{
if (trayIcon != null)
{
trayIcon.IsVisible = false;
trayIcon = null;
}
};
minimizeToTray = true;
}
}
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
minimizeToTray = Environment.GetCommandLineArgs().FirstOrDefault(x => x.ToLower() == "-traymode") != null;
if (minimizeToTray)
trayMode = Environment.GetCommandLineArgs().FirstOrDefault(x => x.ToLower() == "-traymode") != null;
if (trayMode)
{
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Knossos.StartUp(false, false);
StartTrayIcon();
}
else
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};
CreateMainWindow(desktop);
}
}

base.OnFrameworkInitializationCompleted();
}


private async void StartTrayIcon()
private void CreateMainWindow(IClassicDesktopStyleApplicationLifetime desktop)
{
if (trayIcon == null)
if(mainVM == null)
{
mainVM = new MainWindowViewModel();
}

desktop.MainWindow = new MainWindow
{
DataContext = mainVM
};

desktop.MainWindow.Closing += (_, __) =>
{
trayIcon = new TrayIcon
if (Knossos.globalSettings.closeToTray || trayMode)
{
IsVisible = true,
ToolTipText = "Knossos.NET v" + Knossos.AppVersion,
Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://Knossos.NET/Assets/knossos-icon.ico")))),
Menu = new NativeMenu() { new NativeMenuItem("Loading...") }
};
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
StartTrayIcon();
}
};

desktop.MainWindow.Show();
}

while (!Knossos.initIsComplete) { await Task.Delay(10); }
}

trayIcon.Menu = new NativeMenu();
private async void StartTrayIcon()
{
trayIcon?.Dispose();
trayIcon = new TrayIcon
{
IsVisible = true,
ToolTipText = "Knossos.NET v" + Knossos.AppVersion,
Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://Knossos.NET/Assets/knossos-icon.ico")))),
Menu = new NativeMenu() { new NativeMenuItem("Loading...") }
};

while (!Knossos.initIsComplete) { await Task.Delay(10); }

trayIcon.Menu?.Items.Clear();

/*****************************OPEN***********************************/

var open = new NativeMenuItem("Open");
open.Click += (s, _) => {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (desktop.MainWindow == null)
CreateMainWindow(desktop);
desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
if (CustomLauncher.IsCustomMode && CustomLauncher.MenuTaskButtonAtTheEnd)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};
desktop.MainWindow.PropertyChanged += (v, __) =>
{
if (v is MainWindow view && view.WindowState == WindowState.Minimized)
{
desktop.MainWindow.Hide();
desktop.MainWindow.WindowState = WindowState.Normal;
StartTrayIcon();
trayIcon.IsVisible = true;
}
};
desktop.MainWindow.Closing += (_, __) =>
{
if (trayIcon != null)
{
trayIcon.IsVisible = false;
trayIcon = null;
}
};
desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
MainWindow.instance?.FixMarginButtomTasks();
}
desktop.MainWindow?.Show();
trayIcon.IsVisible = false;
trayIcon?.Dispose();
GC.Collect();
}
};
trayIcon.Menu.Add(open);
trayIcon.Menu.Add(new NativeMenuItemSeparator());
trayIcon.Menu?.Add(open);
trayIcon.Menu?.Add(new NativeMenuItemSeparator());

try
{
Expand All @@ -151,6 +117,11 @@ private async void StartTrayIcon()
var filters = ModTags.GetListAllFilters();
foreach (var filter in filters)
{
if( string.Compare(filter, ModTags.Filters.Dependency.ToString(),true) == 0 ||
string.Compare(filter, ModTags.Filters.Utility.ToString(), true) == 0 )
{
continue; //skip dependency and utility mods
}
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
var displayName = myTI.ToTitleCase(filter.Replace("_", " "));
var filterItem = new NativeMenuItem(displayName) { Menu = new NativeMenu() };
Expand All @@ -165,23 +136,7 @@ private async void StartTrayIcon()
var m = mods.Where(x => x.id == mod.id)?.MaxBy(x => new SemanticVersion(x.version));
if (m != null)
{
var modItem = new NativeMenuItem(m.ToString()) { Menu = new NativeMenu() };
/*************************************MOD SUB BUTTONS*************************************/
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Release));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Debug));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Fred2));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Fred2Debug));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.QtFred));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.QtFredDebug));
var settings = new NativeMenuItem("Settings");
settings.Click += (s, e) => {
var dialog = new ModSettingsView();
dialog.DataContext = new ModSettingsViewModel(m);
dialog.Show();
};
modItem.Menu.Add(settings);
/*****************************************************************************************/
filterItem.Menu.Add(modItem);
filterItem.Menu.Add(CreateModMenuItem(m));
addedIds.Add(mod.id);
}
}
Expand All @@ -190,7 +145,7 @@ private async void StartTrayIcon()
}
}
if(play.Menu.Any())
trayIcon.Menu.Add(play);
trayIcon.Menu?.Add(play);
/*****************************DEVELOP***********************************/
var dev = new NativeMenuItem("Develop") { Menu = new NativeMenu(), Icon = new Bitmap(AssetLoader.Open(new Uri("avares://Knossos.NET/Assets/general/menu_develop.png"))) };
var devMods = mods.Where(x => x.devMode);
Expand All @@ -204,30 +159,14 @@ private async void StartTrayIcon()
var m = mods.Where(x => x.id == devMod.id)?.MaxBy(x => new SemanticVersion(x.version));
if(m != null)
{
var modItem = new NativeMenuItem(m.ToString()) { Menu = new NativeMenu() };
/*************************************MOD SUB BUTTONS*************************************/
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Release));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Debug));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Fred2));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.Fred2Debug));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.QtFred));
modItem.Menu.Add(CreateLaunchFSOButton(m, FsoExecType.QtFredDebug));
var settings = new NativeMenuItem("Settings");
settings.Click += (s, e) => {
var dialog = new ModSettingsView();
dialog.DataContext = new ModSettingsViewModel(m);
dialog.Show();
};
modItem.Menu.Add(settings);
/*****************************************************************************************/
dev.Menu.Add(modItem);
dev.Menu.Add(CreateModMenuItem(m));
addedIds.Add(devMod.id);
}
}
}
}
if(dev.Menu.Any())
trayIcon.Menu.Add(dev);
trayIcon.Menu?.Add(dev);
/*****************************TOOLS*************************************/
var toolsItem = new NativeMenuItem("Tools") { Menu = new NativeMenu(), Icon = new Bitmap(AssetLoader.Open(new Uri("avares://Knossos.NET/Assets/general/custom-config-icon.png"))) };
var tools = Knossos.GetTools();
Expand All @@ -244,7 +183,7 @@ private async void StartTrayIcon()
}
}
if (toolsItem.Menu.Any())
trayIcon.Menu.Add(toolsItem);
trayIcon.Menu?.Add(toolsItem);
/*****************************DEBUG*************************************/
var debug = new NativeMenuItem("Debug") { Menu = new NativeMenu(), Icon = new Bitmap(AssetLoader.Open(new Uri("avares://Knossos.NET/Assets/general/menu_debug.png"))) };
var openFs2Log = new NativeMenuItem("Open fs2_open.log");
Expand All @@ -257,34 +196,65 @@ private async void StartTrayIcon()
OpenLog();
};
debug.Menu.Add(openLog);
trayIcon.Menu.Add(debug);
trayIcon.Menu?.Add(debug);
}
catch (Exception ex)
{
Log.Add(Log.LogSeverity.Error, "App.StartTrayIconApp()", ex);
}

/*****************************CLOSE***********************************/
trayIcon.Menu.Add(new NativeMenuItemSeparator());
trayIcon.Menu?.Add(new NativeMenuItemSeparator());
var close = new NativeMenuItem("Exit Knossos.NET");
close.Click += (s, e) => {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
trayIcon?.Dispose();
desktop.Shutdown();
}
if(trayIcon != null)
{
trayIcon.IsVisible = false;
trayIcon = null;
}
};
trayIcon.Menu.Add(close);
trayIcon.Menu?.Add(close);
GC.Collect();
}

private NativeMenuItem CreateModMenuItem(Mod mod)
{
var modItem = new NativeMenuItem(mod.ToString()) { Menu = new NativeMenu() };
/*************************************MOD SUB BUTTONS*************************************/
modItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.Release));

var advItem = new NativeMenuItem("Advanced") { Menu = new NativeMenu() };
if (mod.devMode)
{
modItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.Fred2));
}
else
{
advItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.Fred2));
}

advItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.Debug));
advItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.Fred2Debug));
advItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.QtFred));
advItem.Menu.Add(CreateLaunchFSOButton(mod, FsoExecType.QtFredDebug));

modItem.Menu.Add(advItem);

var settings = new NativeMenuItem("Settings");
settings.Click += (s, e) => {
var dialog = new ModSettingsView();
dialog.DataContext = new ModSettingsViewModel(mod);
dialog.Show();
};
modItem.Menu.Add(settings);
/*****************************************************************************************/
return modItem;
}

private NativeMenuItem CreateLaunchFSOButton(Mod mod, FsoExecType fsoExecType)
{
var item = new NativeMenuItem(fsoExecType.ToString());
var name = fsoExecType != FsoExecType.Release ? fsoExecType.ToString() : "Play";
var item = new NativeMenuItem(name);
item.Click += (s, e) => {
Knossos.PlayMod(mod, fsoExecType);
};
Expand Down
25 changes: 7 additions & 18 deletions Knossos.NET/Models/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,13 @@ public AutoUpdateFsoBuilds(bool stable = false, bool rc = false, bool nightly =
[JsonPropertyName("warn_new_settings_system")]
public bool warnNewSettingsSystem { get; set; } = true;
[JsonIgnore]
public bool _minimizeToTray { get; set; } = false;
[JsonPropertyName("minimize_to_tray")]
public bool minimizeToTray {
get { return _minimizeToTray; }
set { if (_minimizeToTray != value) {
_minimizeToTray = value;
private bool _closeToTray { get; set; } = false;
[JsonPropertyName("close_to_tray")]
public bool closeToTray {
get { return _closeToTray; }
set { if (_closeToTray != value) {
_closeToTray = value;
pendingChangesOnAppClose = true;
if (Avalonia.Application.Current is App app)
{
if (_minimizeToTray)
{
app.EnableMinimizeToTrayRuntime();
}
else
{
app.DisableMinimizeToTrayRuntime();
}
}
}
}
}
Expand Down Expand Up @@ -695,7 +684,7 @@ public void Load()
mainMenuOpen = tempSettings.mainMenuOpen;
sortType = tempSettings.sortType;
portableFsoPreferences = tempSettings.portableFsoPreferences;
minimizeToTray = tempSettings.minimizeToTray;
closeToTray = tempSettings.closeToTray;
ignoredLauncherUpdates = tempSettings.ignoredLauncherUpdates;

ReadFS2IniValues();
Expand Down
Loading