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/Tile/TTilePanel.cs

404 lines
11 KiB
C#
Raw Normal View History

2020-04-11 17:43:20 +00:00
using AppLauncher.Models;
2020-05-20 01:03:12 +00:00
using RyzStudio.Windows.Forms;
2020-04-11 17:43:20 +00:00
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace AppLauncher.Windows.Forms
{
2020-10-21 00:54:00 +00:00
public partial class TTilePanel : TUserControl
2020-04-11 17:43:20 +00:00
{
protected bool isDragging = false;
protected Point startPosition = new Point();
2020-05-17 01:17:30 +00:00
protected ContextMenuStrip groupContextMenu = null;
2020-05-02 16:17:10 +00:00
protected TileModel modelInfo = new TileModel();
2020-04-11 17:43:20 +00:00
2020-05-16 12:25:59 +00:00
public TTilePanel() : base()
2020-04-11 17:43:20 +00:00
{
InitializeComponent();
2020-05-16 12:25:59 +00:00
this.BackColor = Color.FromArgb(250, 250, 250);
2020-04-27 12:17:13 +00:00
this.ContextMenuStrip = contextMenuStrip1;
this.DoubleBuffered = true;
this.AllowDrop = true;
2020-04-27 12:17:13 +00:00
2020-05-16 12:25:59 +00:00
label1.ForeColor = Color.FromArgb(99, 105, 119);
label1.Font = new Font(this.Font.FontFamily, 8.25F);
2020-04-11 17:43:20 +00:00
this.MouseDown += panel_MouseDown;
this.MouseUp += panel_MouseUp;
this.MouseMove += panel_MouseMove;
pictureBox1.MouseDown += panel_MouseDown;
pictureBox1.MouseUp += panel_MouseUp;
pictureBox1.MouseMove += panel_MouseMove;
label1.MouseDown += panel_MouseDown;
label1.MouseUp += panel_MouseUp;
label1.MouseMove += panel_MouseMove;
}
2020-05-16 12:25:59 +00:00
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawRectangle(new Pen(Color.FromArgb(204, 206, 218), 1), new Rectangle(this.DisplayRectangle.X, this.DisplayRectangle.Y, (this.DisplayRectangle.Width - 1), (this.DisplayRectangle.Height - 1)));
}
2020-04-11 17:43:20 +00:00
[Category("Appearance"), Browsable(true)]
public string Title { get => label1.Text; protected set => label1.Text = value; }
[Category("Appearance"), Browsable(true)]
public Image Image { get => pictureBox1.BackgroundImage; protected set => pictureBox1.BackgroundImage = value; }
[Browsable(false)]
2020-05-02 16:17:10 +00:00
public TileModel ModelInfo => modelInfo;
2020-04-11 17:43:20 +00:00
2020-05-16 12:25:59 +00:00
public TTilePanelLayout PanelContainer
2020-04-11 17:43:20 +00:00
{
get
{
if (this.Parent == null)
{
return null;
}
2020-05-16 12:25:59 +00:00
if (this.Parent.GetType() != typeof(TTilePanelLayout))
2020-04-11 17:43:20 +00:00
{
return null;
}
2020-05-16 12:25:59 +00:00
return (TTilePanelLayout)this.Parent;
2020-04-11 17:43:20 +00:00
}
}
protected override void OnDragDrop(DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
if (this.ModelInfo.IsGroup)
{
this.DropFileList(fileList);
this.LoadInfo(this.ModelInfo);
}
else
{
if (this.PanelContainer != null)
{
this.PanelContainer.DropFileList(fileList);
}
}
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragDrop(e);
e.Effect = (e.Data.GetDataPresent(DataFormats.FileDrop)) ? DragDropEffects.Link : DragDropEffects.None;
}
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);
}
2020-04-11 17:43:20 +00:00
public void LoadInfo(TileModel model)
{
2020-05-02 16:17:10 +00:00
this.modelInfo = model;
2020-04-11 17:43:20 +00:00
2020-05-17 01:17:30 +00:00
this.Image = null;
2020-04-11 17:43:20 +00:00
this.Title = model.Title;
2020-05-02 16:17:10 +00:00
2020-05-10 10:03:55 +00:00
if (this.modelInfo.IsGroup)
2020-05-02 16:17:10 +00:00
{
2020-05-16 12:25:59 +00:00
this.Image = Properties.Resources.folder_32;
2020-05-17 01:17:30 +00:00
invalidateGroupMenu(this.modelInfo);
2020-05-10 10:03:55 +00:00
}
else
{
2020-05-17 01:17:30 +00:00
this.Image = getIcon(model);
2020-05-02 16:17:10 +00:00
}
2020-04-11 17:43:20 +00:00
2020-05-03 14:53:15 +00:00
toolTip1.SetToolTip(this, this.Title);
toolTip1.SetToolTip(pictureBox1, this.Title);
toolTip1.SetToolTip(label1, this.Title);
2020-04-11 17:43:20 +00:00
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
2020-05-16 12:25:59 +00:00
TTilePanelLayout container = this.PanelContainer;
2020-04-11 17:43:20 +00:00
if (container == null)
{
return;
}
this.BringToFront();
2020-05-17 01:17:30 +00:00
if (((e.Button == MouseButtons.Left) && (Control.ModifierKeys == Keys.Control)) || (e.Button == MouseButtons.Right))
{
isDragging = true;
startPosition = e.Location;
}
2020-04-11 17:43:20 +00:00
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
2020-05-16 12:25:59 +00:00
TTilePanelLayout layoutPanel = this.PanelContainer;
2020-04-11 17:43:20 +00:00
if (layoutPanel == null)
{
return;
}
int x = (this.Location.X + (e.Location.X - startPosition.X));
int y = (this.Location.Y + (e.Location.Y - startPosition.Y));
layoutPanel.MoveTile(this, x, y);
}
}
2020-05-10 10:03:55 +00:00
private void panel_MouseClick(object sender, MouseEventArgs e)
{
2020-05-17 01:17:30 +00:00
if (Control.ModifierKeys == Keys.Control) return;
if (this.ModelInfo == null) return;
if (e.Button == MouseButtons.Left)
2020-05-10 10:03:55 +00:00
{
2020-05-17 01:17:30 +00:00
if (this.ModelInfo.IsGroup)
{
if (groupContextMenu != null)
{
groupContextMenu.Show(this, e.Location);
}
}
else
{
panel_MouseDoubleClick(sender, e);
}
2020-05-10 10:03:55 +00:00
}
2020-05-17 01:17:30 +00:00
}
2020-05-10 10:03:55 +00:00
2020-05-17 01:17:30 +00:00
private void panel_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (Control.ModifierKeys == Keys.Control) return;
if (e.Button == MouseButtons.Left)
2020-05-10 10:03:55 +00:00
{
2020-05-17 01:17:30 +00:00
execute(this.ModelInfo);
2020-05-10 10:03:55 +00:00
}
2020-05-17 01:17:30 +00:00
}
2020-05-10 10:03:55 +00:00
2020-05-17 01:17:30 +00:00
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
2020-05-10 10:03:55 +00:00
if (this.ModelInfo.IsGroup)
{
2020-05-17 01:17:30 +00:00
EditListTileForm.ShowDialog(this);
2020-05-10 10:03:55 +00:00
}
else
{
2020-05-17 01:17:30 +00:00
EditTileForm.ShowDialog(this);
2020-05-10 10:03:55 +00:00
}
}
2020-05-17 01:17:30 +00:00
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
2020-04-11 17:43:20 +00:00
{
2020-05-17 01:17:30 +00:00
if (this.PanelContainer == null)
2020-05-10 10:03:55 +00:00
{
return;
}
2020-05-17 01:17:30 +00:00
this.PanelContainer.Remove(this);
}
protected void execute(TileModel model)
{
if (model == null)
2020-04-11 17:43:20 +00:00
{
return;
}
2020-05-17 01:17:30 +00:00
if (model.IsGroup)
2020-05-10 10:03:55 +00:00
{
return;
}
2020-05-17 01:17:30 +00:00
if (string.IsNullOrWhiteSpace(model.ProcessFilename))
2020-04-11 17:43:20 +00:00
{
return;
}
2020-05-17 01:17:30 +00:00
if (!File.Exists(model.ProcessFilename))
2020-04-11 17:43:20 +00:00
{
return;
}
ProcessStartInfo p = new ProcessStartInfo();
2020-05-17 01:17:30 +00:00
p.FileName = model.ProcessFilename;
p.WindowStyle = model.ProcessWindowStyle;
if (!string.IsNullOrWhiteSpace(model.ProcessArgument)) p.Arguments = model.ProcessArgument;
if (!string.IsNullOrWhiteSpace(model.ProcessWorkingDirectory)) p.WorkingDirectory = model.ProcessWorkingDirectory;
if (model.ProcessAsAdmin) p.Verb = "runas";
2020-04-11 17:43:20 +00:00
2020-05-20 01:03:12 +00:00
MainForm parentForm = findMainForm();
if (parentForm != null)
{
if (parentForm.CurrentSession != null)
{
if (parentForm.CurrentSession.HideOnClick)
{
parentForm.Visible = false;
}
}
}
2020-04-11 17:43:20 +00:00
try
{
Process.Start(p);
}
2020-05-03 14:53:15 +00:00
catch (Exception exc)
2020-04-11 17:43:20 +00:00
{
2020-05-03 14:53:15 +00:00
MessageBox.Show(exc.Message);
2020-04-11 17:43:20 +00:00
}
}
2020-05-17 01:17:30 +00:00
protected Image getIcon(TileModel model)
2020-04-27 12:17:13 +00:00
{
2020-05-17 01:17:30 +00:00
if (!File.Exists(model.ProcessFilename))
2020-05-10 10:03:55 +00:00
{
2020-05-17 01:17:30 +00:00
return null;
2020-05-10 10:03:55 +00:00
}
2020-05-17 01:17:30 +00:00
try
2020-05-10 10:03:55 +00:00
{
2020-05-17 01:17:30 +00:00
return Icon.ExtractAssociatedIcon(model.ProcessFilename)?.ToBitmap();
}
catch
{
// do nothing
2020-05-10 10:03:55 +00:00
}
2020-05-17 01:17:30 +00:00
return null;
2020-04-27 12:17:13 +00:00
}
2020-05-17 01:17:30 +00:00
protected void invalidateGroupMenu(TileModel model)
2020-04-27 12:17:13 +00:00
{
2020-05-17 01:17:30 +00:00
if (groupContextMenu == null) groupContextMenu = new ContextMenuStrip();
groupContextMenu.Items.Clear();
if (model.Items == null)
2020-05-02 16:17:10 +00:00
{
return;
}
2020-04-27 12:17:13 +00:00
2020-05-17 01:17:30 +00:00
foreach (TileModel item in model.Items)
{
ToolStripItem toolItem = groupContextMenu.Items.Add(item.Title);
toolItem.Image = getIcon(item);
toolItem.Tag = item;
toolItem.Click += toolItem_Click;
}
}
2020-05-20 01:03:12 +00:00
protected MainForm findMainForm()
{
Control item = this;
while (true)
{
item = item.Parent;
if (item == null)
{
return null;
}
if (item.GetType() == typeof(MainForm))
{
return (item as MainForm);
}
}
}
2020-05-17 01:17:30 +00:00
private void toolItem_Click(object sender, EventArgs e)
{
if (sender.GetType() != typeof(ToolStripMenuItem))
{
return;
}
ToolStripMenuItem item = (sender as ToolStripMenuItem);
if (item.Tag == null)
{
return;
}
if (item.Tag.GetType() != typeof(TileModel))
{
return;
}
TileModel model = (item.Tag as TileModel);
execute(model);
2020-04-27 12:17:13 +00:00
}
2020-04-11 17:43:20 +00:00
}
}