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

241 lines
6.7 KiB
C#
Raw Normal View History


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.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)
{
foreach (var item in fileList ?? new string[0])
{
var model = GetTileModel(item);
if (model == null)
{
continue;
}
this.ModelInfo.Items.Add(model);
}
2021-07-22 23:45:30 +00:00
InvalidateGroupMenu(this.ModelInfo);
2021-07-22 23:45:30 +00:00
}
else
{
if ((fileList?.Length ?? 0) > 0)
{
var model = GetTileModel(fileList[0]);
if (model != null)
{
LoadInfo(model);
}
}
}
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
}
}
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);
}
private 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
}
private 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
{
2024-07-06 17:46:05 +00:00
var menuItem = (System.Windows.Forms.ToolStripMenuItem)sender;
if (menuItem == null)
{
return;
}
var model = (menuItem.Tag as TileModel);
if (model == null)
{
return;
}
2021-07-22 23:45:30 +00:00
2024-07-06 17:46:05 +00:00
Execute(model);
};
2021-07-22 23:45:30 +00:00
}
}
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;
}
2021-07-22 23:45:30 +00:00
}
}