using AppLauncher.Models; using RyzStudio.Windows.Forms; 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 TTilePanel : TUserControl { protected bool isDragging = false; protected Point startPosition = new Point(); protected ContextMenuStrip groupContextMenu = null; protected TileModel modelInfo = new TileModel(); public TTilePanel() : base() { InitializeComponent(); this.BackColor = Color.FromArgb(250, 250, 250); this.ContextMenuStrip = contextMenuStrip1; label1.ForeColor = Color.FromArgb(99, 105, 119); label1.Font = new Font(this.Font.FontFamily, 8.25F); 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; } 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))); } [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 TTilePanelLayout PanelContainer { get { if (this.Parent == null) { return null; } if (this.Parent.GetType() != typeof(TTilePanelLayout)) { return null; } return (TTilePanelLayout)this.Parent; } } public void LoadInfo(TileModel model) { this.modelInfo = model; this.Image = null; this.Title = model.Title; if (this.modelInfo.IsGroup) { this.Image = Properties.Resources.folder_32; invalidateGroupMenu(this.modelInfo); } else { this.Image = getIcon(model); } toolTip1.SetToolTip(this, this.Title); toolTip1.SetToolTip(pictureBox1, this.Title); toolTip1.SetToolTip(label1, this.Title); } private void panel_MouseDown(object sender, MouseEventArgs e) { TTilePanelLayout container = this.PanelContainer; if (container == null) { return; } this.BringToFront(); if (((e.Button == MouseButtons.Left) && (Control.ModifierKeys == Keys.Control)) || (e.Button == MouseButtons.Right)) { 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) { TTilePanelLayout 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 (Control.ModifierKeys == Keys.Control) return; if (this.ModelInfo == null) return; if (e.Button == MouseButtons.Left) { if (this.ModelInfo.IsGroup) { if (groupContextMenu != null) { groupContextMenu.Show(this, e.Location); } } else { panel_MouseDoubleClick(sender, e); } } } private void panel_MouseDoubleClick(object sender, MouseEventArgs e) { if (Control.ModifierKeys == Keys.Control) return; if (e.Button == MouseButtons.Left) { execute(this.ModelInfo); } } 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); } protected void execute(TileModel model) { if (model == null) { return; } if (model.IsGroup) { return; } if (string.IsNullOrWhiteSpace(model.ProcessFilename)) { return; } if (!File.Exists(model.ProcessFilename)) { return; } ProcessStartInfo p = new ProcessStartInfo(); 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"; MainForm parentForm = findMainForm(); if (parentForm != null) { if (parentForm.CurrentSession != null) { if (parentForm.CurrentSession.HideOnClick) { parentForm.Visible = false; } } } try { Process.Start(p); } catch (Exception exc) { MessageBox.Show(exc.Message); } } protected Image getIcon(TileModel model) { if (!File.Exists(model.ProcessFilename)) { return null; } try { return Icon.ExtractAssociatedIcon(model.ProcessFilename)?.ToBitmap(); } catch { // do nothing } return null; } protected void invalidateGroupMenu(TileModel model) { if (groupContextMenu == null) groupContextMenu = new ContextMenuStrip(); groupContextMenu.Items.Clear(); if (model.Items == null) { return; } foreach (TileModel item in model.Items) { ToolStripItem toolItem = groupContextMenu.Items.Add(item.Title); toolItem.Image = getIcon(item); toolItem.Tag = item; toolItem.Click += toolItem_Click; } } 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); } } } 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); } } }