initial basic implementation of (linux) desktop shortcuts

pull/35/head
Miepee 4 years ago
parent c5c124e457
commit 583e9557af

@ -578,5 +578,11 @@ namespace AM2RLauncher.Language {
return ResourceManager.GetString("MatrixToolTip", resourceCulture); return ResourceManager.GetString("MatrixToolTip", resourceCulture);
} }
} }
public static string CreateShortcut {
get {
return ResourceManager.GetString("CreateShortcut", resourceCulture);
}
}
} }
} }

@ -386,4 +386,7 @@ Bitte äußerste Vorsicht walten lassen.</value>
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>Der offizielle AM2R Matrix Space</value> <value>Der offizielle AM2R Matrix Space</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>DESKTOPVERKNÜPFUNG ERTELLEN</value>
</data>
</root> </root>

@ -386,4 +386,7 @@ Procede con cautela.</value>
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>El Espacio Oficial de Matrix para AM2R</value> <value>El Espacio Oficial de Matrix para AM2R</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>CREAR ACCESO DIRECTO EN EL ESCRITORIO</value>
</data>
</root> </root>

@ -386,4 +386,7 @@
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>公式AM2Rマトリクススペース</value> <value>公式AM2Rマトリクススペース</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>ショートカットをデスクトップに作成</value>
</data>
</root> </root>

@ -388,4 +388,7 @@ Proceed with caution.</value>
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>The Official AM2R Matrix Space</value> <value>The Official AM2R Matrix Space</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>CREATE SHORTCUT ON DESKTOP</value>
</data>
</root> </root>

@ -386,4 +386,7 @@
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>Официальное Matrix-пространство по AM2R</value> <value>Официальное Matrix-пространство по AM2R</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>СОЗДАТЬ ЯРЛЫК НА РАБОЧЕМ СТОЛЕ</value>
</data>
</root> </root>

@ -388,4 +388,7 @@
<data name="MatrixToolTip" xml:space="preserve"> <data name="MatrixToolTip" xml:space="preserve">
<value>AM2R 官方 Matrix 空间</value> <value>AM2R 官方 Matrix 空间</value>
</data> </data>
<data name="CreateShortcut" xml:space="preserve">
<value>创建桌面快捷方式</value>
</data>
</root> </root>

@ -8,6 +8,7 @@ using System.ComponentModel;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -795,6 +796,71 @@ public partial class MainForm : Form
profileNotesTextArea.Text = Text.ProfileNotes + "\n" + profileList[modSettingsProfileDropDown.SelectedIndex].ProfileNotes; profileNotesTextArea.Text = Text.ProfileNotes + "\n" + profileList[modSettingsProfileDropDown.SelectedIndex].ProfileNotes;
} }
/// <summary>
/// Creates a shortcut of the selected profile on the Desktop
/// </summary>
private void DesktopShortcutButtonClicked(object sender, EventArgs e)
{
ProfileXML profile = profileList[modSettingsProfileDropDown.SelectedIndex];
log.Info($"User wants to create a desktop shortcut for {profile.Name}.");
//TODO: warning if used on community updates
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.Create);
string shortcutFile = "";
if (OS.IsWindows)
{
//TODO: implement this
}
else if (OS.IsLinux)
{
shortcutFile = $"{desktopFolder}/{profile.Name}.desktop";
const string desktopEntryTemplate =
"[Desktop Entry]\n" +
"Type=Application\n" +
"Categories=Game\n" +
"Encoding=UTF-8\n" +
"Name=PROFILENAME\n" +
"Comment=PROFILEDESCRIPTION\n" +
"Exec=EXECUTABLE\n" +
"Icon=ICONPATH\n" +
"Terminal=false";
string desktopEntryText = desktopEntryTemplate;
// Replace values
desktopEntryText = desktopEntryText.Replace("PROFILENAME", $"{profile.Name}");
desktopEntryText = desktopEntryText.Replace("PROFILEDESCRIPTION", $"{profile.ProfileNotes}");
desktopEntryText = desktopEntryText.Replace("ICONPATH", $"{Core.PatchDataPath}/data/files_to_copy/icon.png");
string gameName;
#if !NOAPPIMAGE
gameName = "runner";
#else
gameName = "AM2R.AppImage";
#endif
if (OS.IsThisRunningFromFlatpak)
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"flatpak run \"--command={Core.ProfilesPath}/{profile.Name}/{gameName}\" io.github.am2r_community_developers.AM2RLauncher");
else
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"{Core.ProfilesPath}/{profile.Name}/{gameName}");
File.WriteAllText(shortcutFile, desktopEntryText);
}
else if (OS.IsMac)
{
throw new NotImplementedException("Creating Desktop Shortcuts on Mac has currently not been implemented!");
}
else
{
log.Error($"{OS.Name} has no way of creating shortcuts");
return;
}
CrossPlatformOperations.OpenFolderAndSelectFile(shortcutFile);
}
/// <summary> /// <summary>
/// This opens the game files directory for the current profile. /// This opens the game files directory for the current profile.
/// </summary> /// </summary>

@ -657,6 +657,18 @@ public partial class MainForm : Form
modSettingsProfileDropDown.DataStore = profileDropDown.DataStore; // It's actually more comfortable if it's outside, because of GTK shenanigans modSettingsProfileDropDown.DataStore = profileDropDown.DataStore; // It's actually more comfortable if it's outside, because of GTK shenanigans
modSettingsProfileDropDown.Bind(m => m.SelectedIndex, profileDropDown, p => p.SelectedIndex); modSettingsProfileDropDown.Bind(m => m.SelectedIndex, profileDropDown, p => p.SelectedIndex);
desktopShortcutButton = profileButton = new ColorButton
{
Text = Text.CreateShortcut,
Font = smallButtonFont,
Height = 30,
Width = 275,
TextColor = colorGreen,
BackgroundColor = colorBG,
FrameColor = colorGreen,
BackgroundColorHover = colorBGHover
};
profileButton = new ColorButton profileButton = new ColorButton
{ {
Text = Text.OpenProfileFolder, Text = Text.OpenProfileFolder,
@ -717,7 +729,7 @@ public partial class MainForm : Form
modSettingsLayout.BeginHorizontal(); modSettingsLayout.BeginHorizontal();
modSettingsLayout.AddSpace(); modSettingsLayout.AddSpace();
modSettingsLayout.AddColumn(null, addModButton, modSpacer, settingsProfileLabel, modSettingsProfileDropDown, profileButton, saveButton, updateModButton, deleteModButton, profileNotesTextArea, null); modSettingsLayout.AddColumn(null, addModButton, modSpacer, settingsProfileLabel, modSettingsProfileDropDown, desktopShortcutButton, profileButton, saveButton, updateModButton, deleteModButton, profileNotesTextArea, null);
modSettingsLayout.AddSpace(); modSettingsLayout.AddSpace();
TabPage modSettingsPage = new TabPage TabPage modSettingsPage = new TabPage
@ -769,6 +781,7 @@ public partial class MainForm : Form
mirrorDropDown.SelectedIndexChanged += MirrorDropDownSelectedIndexChanged; mirrorDropDown.SelectedIndexChanged += MirrorDropDownSelectedIndexChanged;
modSettingsLayout.LoadComplete += ProfileLayoutLoadComplete; modSettingsLayout.LoadComplete += ProfileLayoutLoadComplete;
addModButton.Click += AddModButtonClicked; addModButton.Click += AddModButtonClicked;
desktopShortcutButton.Click += DesktopShortcutButtonClicked;
profileButton.Click += ProfileDataButtonClickEvent; profileButton.Click += ProfileDataButtonClickEvent;
saveButton.Click += SaveButtonClickEvent; saveButton.Click += SaveButtonClickEvent;
modSettingsProfileDropDown.SelectedIndexChanged += ModSettingsProfileDropDownSelectedIndexChanged; modSettingsProfileDropDown.SelectedIndexChanged += ModSettingsProfileDropDownSelectedIndexChanged;
@ -825,6 +838,8 @@ public partial class MainForm : Form
private readonly ColorButton apkButton; private readonly ColorButton apkButton;
/// <summary>A <see cref="ColorButton"/> that is used to add mods.</summary> /// <summary>A <see cref="ColorButton"/> that is used to add mods.</summary>
private readonly ColorButton addModButton; private readonly ColorButton addModButton;
/// <summary>A <see cref="ColorButton"/> that will create a desktop shortcut of the current profile.</summary>
private readonly ColorButton desktopShortcutButton;
/// <summary>A <see cref="ColorButton"/> that will open the game files directory for the selected mod.</summary> /// <summary>A <see cref="ColorButton"/> that will open the game files directory for the selected mod.</summary>
private readonly ColorButton profileButton; private readonly ColorButton profileButton;
/// <summary>A <see cref="ColorButton"/> that will open the save directory for the selected mod.</summary> /// <summary>A <see cref="ColorButton"/> that will open the save directory for the selected mod.</summary>

Loading…
Cancel
Save