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

305 lines
7.9 KiB
C#

using AppLauncher.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppLauncher.Windows.Forms
{
public partial class TileLayoutContainer : AUserControl
{
protected int collapseIncrement = 6;
protected int expandIncrement = 8;
protected TileGroupModel groupInfo = null;
protected bool isAnimating = false;
public TileLayoutContainer(TileGroupModel model) : base()
{
InitializeComponent();
this.ContextMenuStrip = contextMenuStrip1;
label1.TileGroupPanel = this;
this.LoadModel(model);
panel1.Resize += panel1_Resize;
}
protected override async void OnLoad(EventArgs e)
{
base.OnLoad(e);
label1.Width = panel1.Width;
this.Margin = new Padding(0, 0, 0, 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);
}
private async void panel1_Resize(object sender, EventArgs e)
{
await this.InvalidateContainer();
}
public int CollapseHeight => label1.Height + panel1.CollapseHeight;
public int ExpandedHeight => label1.Height + panel1.ExpandedHeight + this.Padding.Top + this.Padding.Bottom;
public bool IsAnimating => isAnimating;
public TileGroupModel Model
{
get
{
TileGroupModel rs = new TileGroupModel()
{
Title = label1.TitleText,
GridSize = new Size(panel1.GridSize.X, panel1.GridSize.Y),
IsExpanded = label1.Checked,
IsExclusive = groupInfo.IsExclusive,
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 async Task Collapse()
{
await Task.Run(() =>
{
if (isAnimating) return;
isAnimating = true;
while (this.Height > this.CollapseHeight)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Height -= collapseIncrement;
}));
}
else
{
this.Height -= collapseIncrement;
}
Thread.Sleep(10);
}
isAnimating = false;
});
}
public async Task CollapseNow()
{
await Task.Run(() =>
{
if (isAnimating) return;
isAnimating = true;
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Height = this.CollapseHeight;
}));
}
else
{
this.Height = this.CollapseHeight;
}
isAnimating = false;
});
}
public async Task Expand()
{
await Task.Run(() =>
{
if (isAnimating) return;
isAnimating = true;
while (this.Height < this.ExpandedHeight)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Height += expandIncrement;
this.Invalidate();
}));
}
else
{
this.Height += expandIncrement;
this.Invalidate();
}
Thread.Sleep(10);
}
isAnimating = false;
});
}
public async Task InvalidateContainer(bool animate = true)
{
if (this.IsAnimating)
{
return;
}
if (label1.Checked)
{
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 TileLayoutContainer(new TileGroupModel()
{
Title = "New Group",
GridSize = new Size(8, 1)
}));
}
public void AddRow()
{
panel1.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;
label1.TitleText = groupInfo.Title;
panel1.SetGridSize(groupInfo.GridSize.Width, groupInfo.GridSize.Height);
this.Width = panel1.Width;
label1.Checked = groupInfo.IsExpanded;
if (loadTiles)
{
panel1.LoadTiles(model.Items);
}
}
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);
}
private void addTileMenuItem_Click(object sender, EventArgs e) => AddTileForm.ShowDialog(panel1);
private void addListTileMenuItem_Click(object sender, EventArgs e) => AddListTileForm.ShowDialog(panel1);
}
}