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

261 lines
7.2 KiB
C#
Raw Normal View History

2022-12-26 23:37:53 +00:00
using System;
2021-07-22 23:45:30 +00:00
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
2024-07-06 15:30:37 +00:00
using RokettoLaunch.Models;
using RyzStudio.Windows.Forms;
2021-07-22 23:45:30 +00:00
2024-07-06 15:30:37 +00:00
namespace RokettoLaunch.Windows.Forms
2021-07-22 23:45:30 +00:00
{
public partial class TilePanel : RyzStudio.Windows.TileForms.Tile
2021-07-22 23:45:30 +00:00
{
protected TileModel modelInfo = new TileModel();
public TilePanel()
2021-07-22 23:45:30 +00:00
{
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;
2021-07-22 23:45:30 +00:00
this.EnableMovable = true;
2021-07-22 23:45:30 +00:00
}
[Browsable(false)]
public TileModel ModelInfo => modelInfo;
2021-07-22 23:45:30 +00:00
public RyzStudio.Windows.TileForms.TileContainer TileContainer { get => UIControl.GetParentsUntil<RyzStudio.Windows.TileForms.TileContainer>(this); }
2021-07-28 14:11:35 +00:00
protected MainForm MainForm { get => UIControl.GetParentsUntil<MainForm>(this); }
2021-07-22 23:45:30 +00:00
protected override void OnDragOver(DragEventArgs e)
2021-07-22 23:45:30 +00:00
{
base.OnDragDrop(e);
2021-07-22 23:45:30 +00:00
e.Effect = (e.Data.GetDataPresent(DataFormats.FileDrop)) ? DragDropEffects.Link : DragDropEffects.None;
2021-07-22 23:45:30 +00:00
}
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);
2021-07-22 23:45:30 +00:00
}
}
protected override void OnMouseClick(MouseEventArgs e)
2021-07-22 23:45:30 +00:00
{
if (Control.ModifierKeys == Keys.Control)
2021-07-22 23:45:30 +00:00
{
return;
}
if (this.ModelInfo == null)
2021-07-22 23:45:30 +00:00
{
return;
2021-07-22 23:45:30 +00:00
}
if (e.Button == MouseButtons.Left)
{
if (this.ModelInfo.IsGroup)
{
this.LeftContextMenuStrip?.Show(this, e.Location);
2021-07-22 23:45:30 +00:00
}
else
{
OnMouseDoubleClick(e);
}
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Control.ModifierKeys == Keys.Control)
{
return;
}
2021-07-22 23:45:30 +00:00
if (e.Button == MouseButtons.Left)
{
Execute(this.ModelInfo);
2021-07-22 23:45:30 +00:00
}
}
#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)
2021-07-22 23:45:30 +00:00
{
var form = new EditTileFolderForm(this.ModelInfo);
if (form.ShowDialog() == DialogResult.OK)
2021-07-22 23:45:30 +00:00
{
this.LoadInfo(form.Result);
};
2021-07-22 23:45:30 +00:00
}
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);
2021-07-22 23:45:30 +00:00
}
#endregion
2021-07-22 23:45:30 +00:00
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;
2021-07-22 23:45:30 +00:00
this.Title = model.Title;
if (this.modelInfo.IsGroup)
{
this.LargeIcon = AppResource.folder_32;
2021-07-22 23:45:30 +00:00
InvalidateGroupMenu(this.modelInfo);
2021-07-22 23:45:30 +00:00
}
else
{
this.LargeIcon = RyzStudio.IO.File.GetIcon(model.CleanProcessFilename);
2021-07-22 23:45:30 +00:00
}
toolTip1.SetToolTip(this, this.Title);
}
protected void Execute(TileModel model)
2021-07-22 23:45:30 +00:00
{
if (model == null)
{
return;
}
if (model.IsGroup)
{
return;
}
if (this.MainForm != null)
2021-07-22 23:45:30 +00:00
{
if (this.MainForm.CurrentSession?.HideOnExecute ?? true)
2021-07-22 23:45:30 +00:00
{
this.MainForm.Visible = false;
2021-07-22 23:45:30 +00:00
}
}
RyzStudio.Diagnostics.Process.Execute(model.CleanProcessFilename, model.CleanProcessWorkingDirectory, model.CleanProcessArgument, model.ProcessWindowStyle, model.ProcessAsAdmin);
2021-07-22 23:45:30 +00:00
}
protected void InvalidateGroupMenu(TileModel model)
2021-07-22 23:45:30 +00:00
{
var iconSize = ((this.MainForm?.CurrentSession?.ShowBigIcons ?? true) ? 24 : 16);
2021-07-22 23:45:30 +00:00
if (this.LeftContextMenuStrip == null)
2021-07-22 23:45:30 +00:00
{
this.LeftContextMenuStrip = new ContextMenuStrip();
2021-07-22 23:45:30 +00:00
}
this.LeftContextMenuStrip.ImageScalingSize = new Size(iconSize, iconSize);
this.LeftContextMenuStrip.Items.Clear();
2021-07-22 23:45:30 +00:00
foreach (TileModel item in model?.Items ?? new System.Collections.Generic.List<TileModel>())
2021-07-22 23:45:30 +00:00
{
ToolStripItem toolItem = this.LeftContextMenuStrip.Items.Add(item.Title);
toolItem.Image = RyzStudio.IO.File.GetIcon(item.CleanProcessFilename);
2021-07-22 23:45:30 +00:00
toolItem.Tag = item;
toolItem.Click += (object sender, EventArgs e) =>
2021-07-22 23:45:30 +00:00
{
var result = UIControl.GetTag<TileModel>((Control)sender);
2021-07-22 23:45:30 +00:00
Execute(result);
};
2021-07-22 23:45:30 +00:00
}
}
}
}