roketto-launch/Windows/Forms/TilePanel.cs

248 lines
6.8 KiB
C#
Raw Normal View History

2024-08-06 20:27:08 +00:00

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.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)
{
foreach (var item in fileList ?? new string[0])
{
var model = GetTileModel(item);
if (model == null)
{
continue;
}
this.ModelInfo.Items.Add(model);
}
InvalidateGroupMenu(this.ModelInfo);
}
else
{
if ((fileList?.Length ?? 0) > 0)
{
LoadInfo(fileList[0]);
}
}
}
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);
}
}
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);
}
public void LoadInfo(string filename)
{
var model = GetTileModel(filename);
if (model == null)
{
return;
}
LoadInfo(model);
}
private 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);
}
private 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);
};
}
}
private TileModel GetTileModel(string filename)
{
if (string.IsNullOrWhiteSpace(filename))
{
return null;
}
TileModel model = new TileModel()
{
ProcessFilename = filename,
Title = Path.GetFileName(filename)
};
// exe
if (Path.GetExtension(filename).Equals(".exe", StringComparison.CurrentCultureIgnoreCase))
{
if (File.Exists(filename))
{
try
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(filename);
if (fvi != null)
{
model.Title = fvi.ProductName;
}
}
catch
{
// do nothing
}
}
if (string.IsNullOrWhiteSpace(model.Title))
{
model.Title = Path.GetFileNameWithoutExtension(filename);
}
}
return model;
}
}
}