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/TileLayoutPanel.cs

454 lines
12 KiB
C#
Raw Normal View History

2020-04-11 17:43:20 +00:00
using AppLauncher.Models;
using System;
using System.Collections.Generic;
2020-05-03 14:53:15 +00:00
using System.Diagnostics;
2020-04-11 17:43:20 +00:00
using System.Drawing;
2020-05-03 14:53:15 +00:00
using System.IO;
2020-04-11 17:43:20 +00:00
using System.Linq;
2020-05-03 14:53:15 +00:00
using System.Windows.Forms;
2020-04-11 17:43:20 +00:00
namespace AppLauncher.Windows.Forms
{
public class TileLayoutPanel : 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 int collapseHeight = 0;
protected int expandedHeight = 0;
protected List<Item> items = new List<Item>();
public TileLayoutPanel() : base()
{
2020-05-03 14:53:15 +00:00
InitializeComponent();
this.AllowDrop = true;
2020-04-11 17:43:20 +00:00
this.BackColor = System.Drawing.Color.Transparent;
}
2020-05-03 14:53:15 +00:00
private void InitializeComponent()
{
this.SuspendLayout();
//
// TileLayoutPanel
//
this.Name = "TileLayoutPanel";
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel_DragDrop);
this.DragOver += new System.Windows.Forms.DragEventHandler(this.panel_DragOver);
this.ResumeLayout(false);
}
2020-04-11 17:43:20 +00:00
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
OnResize(e);
}
public Point GridSize
{
get
{
int w = (int)Math.Floor(decimal.Divide(this.Width, this.TileSize));
int h = (int)Math.Floor(decimal.Divide(this.Height, this.TileSize));
return new Point(w, h);
}
}
2020-05-03 10:41:54 +00:00
public TileContainer TileContainer
{
get
{
if (this.Parent == null)
{
return null;
}
if (this.Parent.GetType() != typeof(TileContainer))
{
return null;
}
return (TileContainer)this.Parent;
}
}
2020-04-11 17:43:20 +00:00
public int TileSize => (tileSize + margin);
public int CollapseHeight => collapseHeight;
public int ExpandedHeight => expandedHeight;
2020-05-03 14:53:15 +00:00
public List<TileModel> Tiles
{
get
{
List<TileModel> rs = new List<TileModel>();
foreach (Item item in items)
{
2020-05-03 21:58:49 +00:00
TileModel model = item.Tile.ModelInfo;
model.Position = item.Coord;
rs.Add(model);
2020-05-03 14:53:15 +00:00
}
return rs;
}
}
2020-05-02 16:17:10 +00:00
public void AddTile(TileModel tile)
{
2020-05-03 10:41:54 +00:00
Point gridSize = this.GridSize;
2020-05-02 16:17:10 +00:00
2020-05-03 10:41:54 +00:00
if (items.Count >= (gridSize.X * gridSize.Y))
{
this.SetGridSize(gridSize.X, (gridSize.Y + 1));
}
2020-05-02 16:17:10 +00:00
2020-05-03 10:41:54 +00:00
Point? newCoord = findLastFreeCoord();
if (newCoord == null)
{
return;
}
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
tile.Position = newCoord.Value;
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
TilePanel panel = new TilePanel();
panel.LoadInfo(tile);
panel.Location = convertCoordToLocation(tile.Position);
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
items.Add(new Item()
{
Tile = panel,
Coord = tile.Position
});
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
this.Controls.Add(panel);
}
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
public void Clear()
{
this.Controls.Clear();
}
2020-04-11 17:43:20 +00:00
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, this.TileSize));
if (x < 0) x = 0;
if (y < 0) y = 0;
return new Point((x * this.TileSize), (y * this.TileSize));
}
public void LoadTiles(List<TileModel> tiles)
{
if (tiles == null)
{
return;
}
if (tiles.Count() <= 0)
{
return;
}
foreach (TileModel item in tiles)
{
// resolve final grid position
2020-05-02 16:17:10 +00:00
Point? confirmedPosition = resolveCoord(item.Position);
2020-04-11 17:43:20 +00:00
if (confirmedPosition == null)
{
continue;
}
// place control
TilePanel panel = new TilePanel();
panel.LoadInfo(item);
2020-05-02 16:17:10 +00:00
panel.Location = convertCoordToLocation(confirmedPosition.Value);
2020-04-11 17:43:20 +00:00
items.Add(new Item()
{
Tile = panel,
Coord = confirmedPosition.Value
});
this.Controls.Add(panel);
}
}
public void MoveTile(TilePanel panel, int posX, int posY)
{
2020-04-12 00:12:21 +00:00
Item item = items.Where(x => x.Tile.Equals(panel)).FirstOrDefault();
if (item == null)
{
return;
}
2020-04-11 17:43:20 +00:00
2020-05-02 16:17:10 +00:00
Point newPosition = convertLocationToCoord(posX, posY);
2020-04-12 00:12:21 +00:00
if (!isTileInBounds(newPosition))
{
return;
}
2020-04-11 17:43:20 +00:00
2020-04-12 00:12:21 +00:00
if (hasTile(newPosition))
{
Item swapItem = items.Where(x => x.Coord.Equals(newPosition)).FirstOrDefault();
if (swapItem != null)
{
swapItem.Coord = item.Coord;
2020-05-02 16:17:10 +00:00
swapItem.Tile.Location = convertCoordToLocation(item.Coord);
2020-04-12 00:12:21 +00:00
}
2020-04-11 17:43:20 +00:00
2020-04-12 00:12:21 +00:00
item.Coord = newPosition;
2020-05-02 16:17:10 +00:00
panel.Location = convertCoordToLocation(newPosition);
2020-04-12 00:12:21 +00:00
}
else
{
item.Coord = newPosition;
2020-05-02 16:17:10 +00:00
panel.Location = convertCoordToLocation(newPosition);
}
}
public void Remove(TilePanel panel)
{
Item m = items.Where(x => x.Tile.Equals(panel)).FirstOrDefault();
if (m != null)
{
items.Remove(m);
2020-04-12 00:12:21 +00:00
}
2020-05-02 16:17:10 +00:00
this.Controls.Remove(panel);
}
public void SetGridSize(int width, int height)
{
expandedHeight = (this.TileSize * height);
this.Size = new Size((this.TileSize * width), expandedHeight);
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
protected Point convertCoordToLocation(Point position) => new Point((position.X * this.TileSize), (position.Y * this.TileSize));
2020-04-11 17:43:20 +00:00
2020-05-02 16:17:10 +00:00
protected Point convertLocationToCoord(int posX, int posY)
2020-04-12 00:12:21 +00:00
{
int x = (int)Math.Round(decimal.Divide(posX, this.TileSize));
int y = (int)Math.Round(decimal.Divide(posY, this.TileSize));
if (x < 0) x = 0;
if (y < 0) y = 0;
return new Point(x, y);
}
2020-05-03 10:41:54 +00:00
protected Point? findLastFreeCoord()
{
Point gridSize = this.GridSize;
// none available
if (items.Count >= (gridSize.X * gridSize.Y))
{
return null;
}
2020-05-03 21:58:49 +00:00
if (items.Count <= 0)
{
return findFirstFreeCoord();
}
2020-05-03 10:41:54 +00:00
// 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 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;
}
2020-05-02 16:17:10 +00:00
protected Point? resolveCoord(Point coord)
2020-04-11 17:43:20 +00:00
{
2020-05-02 16:17:10 +00:00
if (!isTileInBounds(coord))
2020-04-11 17:43:20 +00:00
{
return null;
}
2020-05-02 16:17:10 +00:00
if (!hasTile(coord))
2020-04-11 17:43:20 +00:00
{
2020-05-02 16:17:10 +00:00
return coord;
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
return resolveNextCoord(coord);
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
protected Point? resolveNextCoord(Point coord)
2020-04-11 17:43:20 +00:00
{
Point gridSize = this.GridSize;
2020-05-02 16:17:10 +00:00
Point newCoord = coord;
2020-04-11 17:43:20 +00:00
while (true)
{
2020-05-02 16:17:10 +00:00
newCoord.X++;
2020-04-11 17:43:20 +00:00
2020-05-02 16:17:10 +00:00
if (newCoord.X >= gridSize.X)
2020-04-11 17:43:20 +00:00
{
2020-05-02 16:17:10 +00:00
newCoord.Y++;
newCoord.X = 0;
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
if (!isTileInBounds(newCoord))
2020-04-11 17:43:20 +00:00
{
return null;
}
2020-05-02 16:17:10 +00:00
if (hasTile(newCoord))
2020-04-11 17:43:20 +00:00
{
continue;
}
2020-05-02 16:17:10 +00:00
return newCoord;
2020-04-11 17:43:20 +00:00
}
}
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;
2020-04-12 00:12:21 +00:00
if (position.X >= gridSize.X)
2020-04-11 17:43:20 +00:00
{
return false;
}
2020-04-12 00:12:21 +00:00
if (position.Y >= gridSize.Y)
2020-04-11 17:43:20 +00:00
{
return false;
}
return true;
}
2020-05-03 14:53:15 +00:00
private void panel_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void panel_DragDrop(object sender, 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);
}
2020-04-11 17:43:20 +00:00
}
}