This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
linear-app-launcher/Windows/Forms/TilePanel.cs
2024-07-06 18:46:05 +01:00

271 lines
7.5 KiB
C#

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using RokettoLaunch.Models;
using RyzStudio.Windows.Forms;
namespace RokettoLaunch.Windows.Forms
{
public partial class TilePanel : RyzStudio.Windows.TileForms.Tile
{
protected TileModel modelInfo = new TileModel();
public TilePanel()
{
InitializeComponent();
this.AllowDrop = true;
this.ContextMenuStrip = contextMenuStrip1;
this.Font = new Font(this.Font.FontFamily, 8.25F);
this.Size = new Size(70, 70);
this.AutoScaleMode = AutoScaleMode.None;
this.EnableMovable = true;
}
[Browsable(false)]
public TileModel ModelInfo => modelInfo;
public RyzStudio.Windows.TileForms.TileContainer TileContainer { get => UIControl.GetParentsUntil<RyzStudio.Windows.TileForms.TileContainer>(this); }
protected MainForm MainForm { get => UIControl.GetParentsUntil<MainForm>(this); }
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragDrop(e);
e.Effect = (e.Data.GetDataPresent(DataFormats.FileDrop)) ? DragDropEffects.Link : DragDropEffects.None;
}
protected override void OnDragDrop(DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
if (this.ModelInfo.IsGroup)
{
this.DropFileList(fileList);
InvalidateGroupMenu(this.ModelInfo);
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
if (Control.ModifierKeys == Keys.Control)
{
return;
}
if (this.ModelInfo == null)
{
return;
}
if (e.Button == MouseButtons.Left)
{
if (this.ModelInfo.IsGroup)
{
this.LeftContextMenuStrip?.Show(this, e.Location);
}
else
{
OnMouseDoubleClick(e);
}
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Control.ModifierKeys == Keys.Control)
{
return;
}
if (e.Button == MouseButtons.Left)
{
Execute(this.ModelInfo);
}
}
#region Context Menu
/// <summary>
/// Edit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ModelInfo.IsGroup)
{
var form = new EditTileFolderForm(this.ModelInfo);
if (form.ShowDialog() == DialogResult.OK)
{
this.LoadInfo(form.Result);
};
}
else
{
var form = new EditTileForm(this.ModelInfo);
if (form.ShowDialog() == DialogResult.OK)
{
this.LoadInfo(form.Result);
};
}
}
/// <summary>
/// Remove
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.TileContainer?.Controls?.Remove(this);
}
#endregion
public void DropFileList(string[] fileList)
{
if (fileList == null)
{
return;
}
if (fileList.Length <= 0)
{
return;
}
if (string.IsNullOrWhiteSpace(fileList[0]))
{
return;
}
TileModel model = new TileModel()
{
ProcessFilename = fileList[0],
Title = Path.GetFileName(fileList[0])
};
// exe
if (Path.GetExtension(fileList[0]).Equals(".exe", StringComparison.CurrentCultureIgnoreCase))
{
if (File.Exists(fileList[0]))
{
try
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileList[0]);
if (fvi != null)
{
model.Title = fvi.ProductName;
}
}
catch
{
// do nothing
}
}
if (string.IsNullOrWhiteSpace(model.Title))
{
model.Title = Path.GetFileNameWithoutExtension(fileList[0]);
}
}
this.ModelInfo.Items.Add(model);
}
public void LoadInfo(TileModel model)
{
this.modelInfo = model;
this.LargeIcon = null;
this.Title = model.Title;
if (this.modelInfo.IsGroup)
{
this.LargeIcon = AppResource.folder_32;
InvalidateGroupMenu(this.modelInfo);
}
else
{
this.LargeIcon = RyzStudio.IO.File.GetIcon(model.CleanProcessFilename);
}
toolTip1.SetToolTip(this, this.Title);
}
protected void Execute(TileModel model)
{
if (model == null)
{
return;
}
if (model.IsGroup)
{
return;
}
if (this.MainForm != null)
{
if (this.MainForm.CurrentSession?.HideOnExecute ?? true)
{
this.MainForm.Visible = false;
}
}
RyzStudio.Diagnostics.Process.Execute(model.CleanProcessFilename, model.CleanProcessWorkingDirectory, model.CleanProcessArgument, model.ProcessWindowStyle, model.ProcessAsAdmin);
}
protected void InvalidateGroupMenu(TileModel model)
{
var iconSize = ((this.MainForm?.CurrentSession?.ShowBigIcons ?? true) ? 24 : 16);
if (this.LeftContextMenuStrip == null)
{
this.LeftContextMenuStrip = new ContextMenuStrip();
}
this.LeftContextMenuStrip.ImageScalingSize = new Size(iconSize, iconSize);
this.LeftContextMenuStrip.Items.Clear();
foreach (TileModel item in model?.Items ?? new System.Collections.Generic.List<TileModel>())
{
ToolStripItem toolItem = this.LeftContextMenuStrip.Items.Add(item.Title);
toolItem.Image = RyzStudio.IO.File.GetIcon(item.CleanProcessFilename);
toolItem.Tag = item;
toolItem.Click += (object sender, EventArgs e) =>
{
var menuItem = (System.Windows.Forms.ToolStripMenuItem)sender;
if (menuItem == null)
{
return;
}
var model = (menuItem.Tag as TileModel);
if (model == null)
{
return;
}
Execute(model);
};
}
}
}
}