using AppLauncher.Models; using RyzStudio.Windows.Forms; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace AppLauncher.Windows.Forms { public partial class TilePanelLayout : AUserControl { public class Item { public TilePanel Tile { get; set; } public Point Coord { get; set; } = new Point(0, 0); } protected readonly int tileSize = 70; protected readonly int margin = 3; protected readonly int labelHeight = 20; protected readonly int collapseIncrement = 6; protected readonly int expandIncrement = 8; protected TileGroupModel groupInfo = null; protected List items = new List(); protected int collapseHeight = 0; protected int expandedHeight = 0; protected bool isAnimating = false; protected bool isChecked = true; public TilePanelLayout(TileGroupModel model) : base() { InitializeComponent(); this.AllowDrop = true; this.BackColor = Color.Transparent; this.LoadModel(model); //panel1.Resize += panel1_Resize; } protected override void OnDragDrop(DragEventArgs e) { string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[]; 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 } } } this.AddTile(model); } protected override void OnDragOver(DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Margin = new Padding(0); this.Padding = new Padding(0, 0, 0, 10); //this.MaximumSize = new Size(panel1.Width, ExpandedHeight); //this.MinimumSize = new Size(panel1.Width, label1.Height); //this.Size = this.MaximumSize; //this.Size = new Size(panel1.Width, this.ExpandedHeight); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.DrawImageUnscaled((isChecked ? Properties.Resources.toggle_right_ea_16 : Properties.Resources.toggle_left_ea_16), 2, 2); TextRenderer.DrawText(g, groupInfo?.Title, new Font(this.Font.FontFamily, 8.25F), new Point(25, 4), Color.FromArgb(99, 105, 119)); } protected override async void OnResize(EventArgs e) { base.OnResize(e); await this.InvalidateContainer(); } protected override async void OnMouseClick(MouseEventArgs e) { base.OnMouseClick(e); bool isLabel = ((e.Location.X >= 0) && (e.Location.X <= this.Width) && (e.Location.Y >= 0) && (e.Location.Y <= 20)); if (e.Button == MouseButtons.Left) { if (isLabel) { isChecked = !isChecked; this.Invalidate(); await this.InvalidateContainer(); } else { } } else if (e.Button == MouseButtons.Right) { if (isLabel) { contextMenuStrip2.Show(this, e.Location); } else { contextMenuStrip1.Show(this, e.Location); } } } protected override void OnMouseDoubleClick(MouseEventArgs e) => base.OnMouseClick(e); //private async void panel1_Resize(object sender, EventArgs e) => await this.InvalidateContainer(); public Point GridSize { get { int w = (int)Math.Floor(decimal.Divide(this.Width, this.TileSize)); int h = (int)Math.Floor(decimal.Divide(this.Height - labelHeight, this.TileSize)); return new Point(w, h); } } public int CollapseHeight => labelHeight + collapseHeight; public int ExpandedHeight => expandedHeight + this.Padding.Bottom; //public int CollapseHeight => labelHeight + collapseHeight panel1.CollapseHeight; //public int ExpandedHeight => labelHeight + panel1.ExpandedHeight + this.Padding.Top + this.Padding.Bottom; public TileGroupModel Model { get { TileGroupModel rs = new TileGroupModel() { Title = groupInfo.Title, //GridSize = new Size(panel1.GridSize.X, panel1.GridSize.Y), GridSize = new Size(this.GridSize.X, this.GridSize.Y), IsExpanded = isChecked, IsExclusive = groupInfo.IsExclusive, Items = this.Tiles //Items = panel1.Tiles }; return rs; } } public FlowLayoutPanel FlowLayoutPanel { get { if (this.Parent == null) { return null; } if (this.Parent.GetType() != typeof(FlowLayoutPanel)) { return null; } return this.Parent as FlowLayoutPanel; } } public List Tiles { get { List rs = new List(); foreach (Item item in items) { TileModel model = item.Tile.ModelInfo; model.Position = item.Coord; rs.Add(model); } return rs; } } public int TileSize => (tileSize + margin); public void AddTile(TileModel tile) { Point gridSize = this.GridSize; if (items.Count >= (gridSize.X * gridSize.Y)) { this.SetGridSize(gridSize.X, (gridSize.Y + 1)); } Point? newCoord = findLastFreeCoord(); if (newCoord == null) { return; } tile.Position = newCoord.Value; TilePanel panel = new TilePanel(); panel.LoadInfo(tile); panel.Location = convertCoordToLocation(tile.Position); items.Add(new Item() { Tile = panel, Coord = tile.Position }); this.Controls.Add(panel); } public void Clear() { this.Controls.Clear(); } public async Task Collapse() { await Task.Run(() => { if (isAnimating) return; isAnimating = true; while (this.Height > this.CollapseHeight) { ThreadControl.SetHeight(this, (this.Height - collapseIncrement)); Thread.Sleep(10); } ThreadControl.SetHeight(this, this.CollapseHeight); isAnimating = false; }); } public async Task CollapseNow() { await Task.Run(() => { if (isAnimating) return; isAnimating = true; ThreadControl.SetHeight(this, this.CollapseHeight); isAnimating = false; }); } public async Task Expand() { await Task.Run(() => { if (isAnimating) return; isAnimating = true; while (this.Height < this.ExpandedHeight) { ThreadControl.SetHeight(this, (this.Height + expandIncrement)); this.Invalidate(); Thread.Sleep(10); } ThreadControl.SetHeight(this, this.ExpandedHeight); isAnimating = false; }); } public Point GetTilePosition(int posX, int posY) { int x = (int)Math.Round(decimal.Divide(posX, this.TileSize)); int y = (int)Math.Round(decimal.Divide((posY - labelHeight), this.TileSize)); if (x < 0) x = 0; if (y < 0) y = 0; return new Point((x * this.TileSize), ((y * this.TileSize) + labelHeight)); } public async Task InvalidateContainer(bool animate = true) { if (isAnimating) { return; } if (isChecked) { await this.Expand(); } else { if (animate) { await this.Collapse(); } else { await this.CollapseNow(); } } } public void AddGroup() { if (this.FlowLayoutPanel == null) { return; } this.FlowLayoutPanel.Controls.Add(new TilePanelLayout(new TileGroupModel() { Title = "New Group", GridSize = new Size(8, 1) })); } public void AddRow() { //panel1.SetGridSize(groupInfo.GridSize.Width, (groupInfo.GridSize.Height + 1)); this.SetGridSize(groupInfo.GridSize.Width, (groupInfo.GridSize.Height + 1)); } public void EditGroup() { //EditGroupForm editForm = new EditGroupForm(this); //editForm.ShowDialog(); } public void LoadModel(TileGroupModel model, bool loadTiles = true) { groupInfo = model; this.SetGridSize(groupInfo.GridSize.Width, groupInfo.GridSize.Height); //this.Width = panel1.Width; isChecked = groupInfo.IsExpanded; if (loadTiles) { this.LoadTiles(model.Items); } this.Invalidate(); } public void LoadTiles(List tiles) { if (tiles == null) { return; } if (tiles.Count() <= 0) { return; } foreach (TileModel item in tiles) { // resolve final grid position Point? confirmedPosition = resolveCoord(item.Position); if (confirmedPosition == null) { continue; } // place control TilePanel panel = new TilePanel(); panel.LoadInfo(item); panel.Location = convertCoordToLocation(confirmedPosition.Value); items.Add(new Item() { Tile = panel, Coord = confirmedPosition.Value }); this.Controls.Add(panel); } } public void MoveTile(TilePanel panel, int posX, int posY) { Item item = items.Where(x => x.Tile.Equals(panel)).FirstOrDefault(); if (item == null) { return; } Point newPosition = convertLocationToCoord(posX, posY); if (!isTileInBounds(newPosition)) { return; } if (hasTile(newPosition)) { Item swapItem = items.Where(x => x.Coord.Equals(newPosition)).FirstOrDefault(); if (swapItem != null) { swapItem.Coord = item.Coord; swapItem.Tile.Location = convertCoordToLocation(item.Coord); } item.Coord = newPosition; panel.Location = convertCoordToLocation(newPosition); } else { item.Coord = newPosition; panel.Location = convertCoordToLocation(newPosition); } } public void MoveTop() { if (this.FlowLayoutPanel == null) { return; } this.FlowLayoutPanel.Controls.SetChildIndex(this, 0); } public void MoveUp() { if (this.FlowLayoutPanel == null) { return; } int pos = this.FlowLayoutPanel.Controls.GetChildIndex(this); if (pos <= 0) { return; } this.FlowLayoutPanel.Controls.SetChildIndex(this, (pos - 1)); } public void MoveDown() { if (this.FlowLayoutPanel == null) { return; } int pos = this.FlowLayoutPanel.Controls.GetChildIndex(this); if (pos >= (this.FlowLayoutPanel.Controls.Count - 1)) { return; } this.FlowLayoutPanel.Controls.SetChildIndex(this, (pos + 1)); } public void MoveBottom() { if (this.FlowLayoutPanel == null) { return; } this.FlowLayoutPanel.Controls.SetChildIndex(this, (this.FlowLayoutPanel.Controls.Count - 1)); } public void Remove() { if (this.FlowLayoutPanel == null) { return; } this.FlowLayoutPanel.Controls.Remove(this); } public void Remove(TilePanel panel) { Item m = items.Where(x => x.Tile.Equals(panel)).FirstOrDefault(); if (m != null) { items.Remove(m); } this.Controls.Remove(panel); } public void SetGridSize(int width, int height) { expandedHeight = (this.TileSize * height) + labelHeight; this.Size = new Size((this.TileSize * width), expandedHeight); } protected Point convertCoordToLocation(Point position) => new Point((position.X * this.TileSize), ((position.Y * this.TileSize) + labelHeight)); protected Point convertLocationToCoord(int posX, int posY) { int x = (int)Math.Round(decimal.Divide(posX, this.TileSize)); int y = (int)Math.Round(decimal.Divide((posY - labelHeight), this.TileSize)); if (x < 0) x = 0; if (y < 0) y = 0; return new Point(x, y); } protected Point? findFirstFreeCoord() { Point gridSize = this.GridSize; for (int y = 0; y < gridSize.Y; y++) { for (int x = 0; x < gridSize.X; x++) { if (hasTile(new Point(x, y))) { continue; } return new Point(x, y); } } return null; } protected Point? findLastFreeCoord() { Point gridSize = this.GridSize; // none available if (items.Count >= (gridSize.X * gridSize.Y)) { return null; } if (items.Count <= 0) { return findFirstFreeCoord(); } // only one available if (items.Count >= ((gridSize.X * gridSize.Y) - 1)) { return findFirstFreeCoord(); } Point? rv = null; for (int y = (gridSize.Y - 1); y >= 0; y--) { for (int x = (gridSize.X - 1); x >= 0; x--) { if (hasTile(new Point(x, y))) { if (rv.HasValue) { return rv; } } else { rv = new Point(x, y); } } } return null; } protected bool hasTile(Point position) { if (items == null) { return false; } if (items.Count <= 0) { return false; } return (items.Count(x => x.Coord.Equals(position)) > 0); } protected bool isTileInBounds(Point position) { Point gridSize = this.GridSize; if (position.X >= gridSize.X) { return false; } if (position.Y >= gridSize.Y) { return false; } return true; } protected Point? resolveCoord(Point coord) { if (!isTileInBounds(coord)) { return null; } if (!hasTile(coord)) { return coord; } return resolveNextCoord(coord); } protected Point? resolveNextCoord(Point coord) { Point gridSize = this.GridSize; Point newCoord = coord; while (true) { newCoord.X++; if (newCoord.X >= gridSize.X) { newCoord.Y++; newCoord.X = 0; } if (!isTileInBounds(newCoord)) { return null; } if (hasTile(newCoord)) { continue; } return newCoord; } } private void addTileMenuItem_Click(object sender, EventArgs e) => AddTileForm.ShowDialog(this); private void addListTileMenuItem_Click(object sender, EventArgs e) => AddListTileForm.ShowDialog(this); private void addGroupMenuItem_Click(object sender, EventArgs e) => this.AddGroup(); private void addRowMenuItem_Click(object sender, EventArgs e) => this.AddRow(); private void editGroupMenuItem_Click(object sender, EventArgs e) => this.EditGroup(); private void moveTopMenuItem_Click(object sender, EventArgs e) => this.MoveTop(); private void moveUpMenuItem_Click(object sender, EventArgs e) => this.MoveUp(); private void moveDownMenuItem_Click(object sender, EventArgs e) => this.MoveDown(); private void moveBottomMenuItem_Click(object sender, EventArgs e) => this.MoveBottom(); private void removeGroupMenuItem3_Click(object sender, EventArgs e) => this.Remove(); } }