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/TilePanel.cs
2020-05-10 11:03:55 +01:00

227 lines
6.1 KiB
C#

using AppLauncher.Models;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace AppLauncher.Windows.Forms
{
public partial class TilePanel : AUserControl
{
protected bool isDragging = false;
protected Point startPosition = new Point();
protected TileModel modelInfo = new TileModel();
public TilePanel() : base()
{
InitializeComponent();
this.ContextMenuStrip = contextMenuStrip1;
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;
}
[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)]
public TileModel ModelInfo => modelInfo;
public TileLayoutPanel PanelContainer
{
get
{
if (this.Parent == null)
{
return null;
}
if (this.Parent.GetType() != typeof(TileLayoutPanel))
{
return null;
}
return (TileLayoutPanel)this.Parent;
}
}
public void LoadInfo(TileModel model)
{
this.modelInfo = model;
this.Title = model.Title;
if (this.modelInfo.IsGroup)
{
this.Image = Properties.Resources.folder_ea_32;
}
else
{
this.Image = model.Icon;
if (this.Image == null)
{
if (File.Exists(model.ProcessFilename))
{
try
{
this.Image = Icon.ExtractAssociatedIcon(model.ProcessFilename)?.ToBitmap();
}
catch
{
}
}
}
}
toolTip1.SetToolTip(this, this.Title);
toolTip1.SetToolTip(pictureBox1, this.Title);
toolTip1.SetToolTip(label1, this.Title);
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
TileLayoutPanel container = this.PanelContainer;
if (container == null)
{
return;
}
if (e.Button != MouseButtons.Right)
{
return;
}
this.BringToFront();
isDragging = true;
startPosition = e.Location;
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
TileLayoutPanel layoutPanel = this.PanelContainer;
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);
}
}
private void panel_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
if (this.ModelInfo == null)
{
return;
}
if (this.ModelInfo.IsGroup)
{
}
else
{
panel_MouseDoubleClick(sender, e);
}
}
private void panel_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
if (this.ModelInfo == null)
{
return;
}
if (this.ModelInfo.IsGroup)
{
return;
}
if (string.IsNullOrWhiteSpace(this.ModelInfo.ProcessFilename))
{
return;
}
if (!File.Exists(this.ModelInfo.ProcessFilename))
{
return;
}
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = this.ModelInfo.ProcessFilename;
p.WindowStyle = this.ModelInfo.ProcessWindowStyle;
if (!string.IsNullOrWhiteSpace(this.ModelInfo.ProcessArgument)) p.Arguments = this.ModelInfo.ProcessArgument;
if (!string.IsNullOrWhiteSpace(this.ModelInfo.ProcessWorkingDirectory)) p.WorkingDirectory = this.ModelInfo.ProcessWorkingDirectory;
if (this.ModelInfo.ProcessAsAdmin) p.Verb = "runas";
try
{
Process.Start(p);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ModelInfo.IsGroup)
{
EditListTileForm.ShowDialog(this);
}
else
{
EditTileForm.ShowDialog(this);
}
}
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.PanelContainer == null)
{
return;
}
this.PanelContainer.Remove(this);
}
}
}