using Entries;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Core;
using Windows.Management.Deployment;
namespace ShortcutUtil
{
public partial class MainForm : Form
{
private EntryList entryList;
private Font defaultFont;
private Font nameFont;
private Brush fontBrush;
private bool shown = false;
private Size listSizeOffset;
private Point buttonOffset;
public MainForm(EntryList entryList)
{
InitializeComponent();
Text = entryList.FilePath == null ? "New shortcut file" : entryList.FilePath;
defaultFont = defaultShortcutLabel.Font;
nameFont = new Font(defaultFont, FontStyle.Bold);
fontBrush = new SolidBrush(defaultShortcutLabel.ForeColor);
this.entryList = entryList;
entryListBox.Items.AddRange(this.entryList.Entries.ToArray());
if (this.entryList.Entries.Count > 0)
{
entryListBox.SelectedIndex = 0;
}
}
private Dictionary<Tuple<string, int, Size>, Icon> iconsByFileIndex = new Dictionary<Tuple<string, int, Size>, Icon>();
private Icon GetIcon(string file, int index, Size preferred)
{
Tuple<string, int, Size> key = new Tuple<string, int, Size>(file, index, preferred);
if (!iconsByFileIndex.ContainsKey(key))
{
Icon icon = null;
try
{
icon = new Icon(file, preferred);
if (icon != null)
{
iconsByFileIndex.Add(key, icon);
return icon;
}
} catch (Exception) { }
try
{
icon = IconExtractor.Extract(file, index, true);
if (icon != null)
{
iconsByFileIndex.Add(key, icon);
return icon;
}
}
catch (Exception) { }
MessageBox.Show("Couldn't load icon " + index + " from " + file);
return null;
}
return iconsByFileIndex[key];
}
private static readonly Size IconSize = new Size(64, 64);
private static readonly Size ItemPadding = new Size(6, 6);
private void entryListBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index >= 0 && e.Index < entryList.Entries.Count)
{
Rectangle iconBounds = new Rectangle(Point.Add(e.Bounds.Location, ItemPadding), IconSize);
Size textSize = new Size(0, nameFont.Height + 2);
Entry entry = entryList.Entries[e.Index];
e.Graphics.DrawIcon(GetIcon(entry.Icon, entry.IconIndex, IconSize), iconBounds);
PointF textLocation = PointF.Add(e.Bounds.Location, new Size(IconSize.Width + ItemPadding.Width * 2, ItemPadding.Width));
e.Graphics.DrawString(e.Index + ": " + entry.Name, nameFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
e.Graphics.DrawString(entry.Target, defaultFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
e.Graphics.DrawString(entry.Description, defaultFont, fontBrush, textLocation);
textLocation = PointF.Add(textLocation, textSize);
}
}
private void entryListBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = Size.Add(IconSize, new Size(0, ItemPadding.Height * 2)).Height;
}
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (shown)
{
entryListBox.Size = Size.Subtract(Size, listSizeOffset);
createButton.Location = Point.Add(buttonOffset, entryListBox.Size);
}
}
private void MainForm_Shown(object sender, EventArgs e)
{
listSizeOffset = Size.Subtract(Size, entryListBox.Size);
buttonOffset = Point.Subtract(createButton.Location, entryListBox.Size);
shown = true;
}
private SearchForm search = new SearchForm();
private async void appButton_Click(object sender, EventArgs e)
{
appButton.Enabled = false;
PackageManager pm = new PackageManager();
List<Tuple<string, string>> options = new List<Tuple<string, string>>();
foreach (Package p in pm.FindPackagesForUser(string.Empty))
{
var apps = await p.GetAppListEntriesAsync().AsTask();
foreach (AppListEntry app in apps)
{
options.Add(new Tuple<string, string>(app.AppUserModelId, app.DisplayInfo.DisplayName + ": " + app.AppUserModelId));
}
}
string selectedAppUserModelId = search.GetSelection("Select UWP App", "Available Apps:", options);
if (selectedAppUserModelId != null)
{
appUserModelIdBox.Text = selectedAppUserModelId;
}
appButton.Enabled = true;
}
}
}