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

363 lines
9.9 KiB
C#
Raw Normal View History

2020-05-03 10:41:54 +00:00
using AppLauncher.Models;
using System;
2020-05-03 14:53:15 +00:00
using System.Collections.Generic;
using System.ComponentModel;
2020-04-11 17:43:20 +00:00
using System.Drawing;
2020-05-03 10:41:54 +00:00
using System.Threading;
2020-04-11 17:43:20 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppLauncher.Windows.Forms
{
2020-05-07 22:31:03 +00:00
public partial class TileLayoutContainer : AUserControl
2020-04-11 17:43:20 +00:00
{
protected TileGroupModel groupInfo = null;
protected readonly int labelHeight = 20;
protected readonly int collapseIncrement = 6;
protected readonly int expandIncrement = 8;
2020-05-02 16:17:10 +00:00
protected bool isAnimating = false;
protected bool isChecked = true;
2020-04-11 17:43:20 +00:00
2020-05-07 22:31:03 +00:00
public TileLayoutContainer(TileGroupModel model) : base()
2020-04-11 17:43:20 +00:00
{
InitializeComponent();
2020-05-03 21:58:49 +00:00
this.LoadModel(model);
2020-04-11 17:43:20 +00:00
2020-05-03 10:41:54 +00:00
panel1.Resize += panel1_Resize;
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
protected override async void OnLoad(EventArgs e)
2020-04-11 17:43:20 +00:00
{
base.OnLoad(e);
this.Margin = new Padding(0);
2020-05-03 21:58:49 +00:00
this.Padding = new Padding(0, 0, 0, 10);
2020-05-03 10:41:54 +00:00
//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)
2020-05-03 10:41:54 +00:00
{
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));
2020-04-11 17:43:20 +00:00
}
private async void panel1_Resize(object sender, EventArgs e) => await this.InvalidateContainer();
2020-04-11 17:43:20 +00:00
public int CollapseHeight => labelHeight + panel1.CollapseHeight;
2020-05-02 16:17:10 +00:00
public int ExpandedHeight => labelHeight + panel1.ExpandedHeight + this.Padding.Top + this.Padding.Bottom;
2020-04-11 17:43:20 +00:00
2020-05-03 14:53:15 +00:00
public TileGroupModel Model
{
get
{
TileGroupModel rs = new TileGroupModel()
{
Title = groupInfo.Title,
2020-05-03 14:53:15 +00:00
GridSize = new Size(panel1.GridSize.X, panel1.GridSize.Y),
IsExpanded = isChecked,
2020-05-03 14:53:15 +00:00
IsExclusive = groupInfo.IsExclusive,
Items = panel1.Tiles
};
return rs;
}
}
2020-05-03 18:36:06 +00:00
public FlowLayoutPanel FlowLayoutPanel
{
get
{
if (this.Parent == null)
{
return null;
}
if (this.Parent.GetType() != typeof(FlowLayoutPanel))
{
return null;
}
return this.Parent as FlowLayoutPanel;
}
}
2020-04-11 17:43:20 +00:00
public async Task Collapse()
{
await Task.Run(() =>
{
2020-05-02 16:17:10 +00:00
if (isAnimating) return;
isAnimating = true;
2020-05-03 10:41:54 +00:00
while (this.Height > this.CollapseHeight)
2020-04-11 17:43:20 +00:00
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Height -= collapseIncrement;
}));
}
else
{
this.Height -= collapseIncrement;
}
Thread.Sleep(10);
}
2020-05-02 16:17:10 +00:00
isAnimating = false;
2020-04-11 17:43:20 +00:00
});
}
public async Task CollapseNow()
{
await Task.Run(() =>
{
2020-05-02 16:17:10 +00:00
if (isAnimating) return;
isAnimating = true;
2020-04-11 17:43:20 +00:00
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
2020-05-03 10:41:54 +00:00
this.Height = this.CollapseHeight;
2020-04-11 17:43:20 +00:00
}));
}
else
{
2020-05-03 10:41:54 +00:00
this.Height = this.CollapseHeight;
2020-04-11 17:43:20 +00:00
}
2020-05-02 16:17:10 +00:00
isAnimating = false;
2020-04-11 17:43:20 +00:00
});
}
public async Task Expand()
{
await Task.Run(() =>
{
2020-05-02 16:17:10 +00:00
if (isAnimating) return;
isAnimating = true;
2020-05-03 10:41:54 +00:00
while (this.Height < this.ExpandedHeight)
2020-04-11 17:43:20 +00:00
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Height += expandIncrement;
this.Invalidate();
}));
}
else
{
this.Height += expandIncrement;
this.Invalidate();
}
Thread.Sleep(10);
}
2020-05-02 16:17:10 +00:00
isAnimating = false;
2020-04-11 17:43:20 +00:00
});
}
2020-05-02 16:17:10 +00:00
public async Task InvalidateContainer(bool animate = true)
{
if (isAnimating)
2020-05-02 16:17:10 +00:00
{
return;
}
if (isChecked)
2020-05-02 16:17:10 +00:00
{
await this.Expand();
}
else
{
if (animate)
{
await this.Collapse();
}
else
{
await this.CollapseNow();
}
}
}
2020-05-03 18:36:06 +00:00
public void AddGroup()
{
if (this.FlowLayoutPanel == null)
{
return;
}
2020-05-07 22:31:03 +00:00
this.FlowLayoutPanel.Controls.Add(new TileLayoutContainer(new TileGroupModel()
2020-05-03 18:36:06 +00:00
{
Title = "New Group",
GridSize = new Size(8, 1)
}));
}
2020-05-07 22:31:03 +00:00
public void AddRow()
{
panel1.SetGridSize(groupInfo.GridSize.Width, (groupInfo.GridSize.Height + 1));
}
2020-05-03 18:36:06 +00:00
public void EditGroup()
{
2020-05-03 21:58:49 +00:00
EditGroupForm editForm = new EditGroupForm(this);
editForm.ShowDialog();
}
public void LoadModel(TileGroupModel model, bool loadTiles = true)
{
groupInfo = model;
2020-05-03 18:36:06 +00:00
2020-05-03 21:58:49 +00:00
panel1.SetGridSize(groupInfo.GridSize.Width, groupInfo.GridSize.Height);
this.Width = panel1.Width;
2020-05-07 22:31:03 +00:00
isChecked = groupInfo.IsExpanded;
2020-05-03 21:58:49 +00:00
if (loadTiles)
{
panel1.LoadTiles(model.Items);
}
this.Invalidate();
2020-05-03 18:36:06 +00:00
}
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);
}
2020-05-10 10:03:55 +00:00
private void addTileMenuItem_Click(object sender, EventArgs e) => AddTileForm.ShowDialog(panel1);
private void addListTileMenuItem_Click(object sender, EventArgs e) => AddListTileForm.ShowDialog(panel1);
2020-05-02 16:17:10 +00:00
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 void toolStripMenuItem2_Click(object sender, EventArgs e) => this.AddGroup();
private void toolStripMenuItem5_Click(object sender, EventArgs e) => this.AddRow();
private void toolStripMenuItem1_Click(object sender, EventArgs e) => this.EditGroup();
private void topToolStripMenuItem_Click(object sender, EventArgs e) => this.MoveTop();
private void upToolStripMenuItem_Click(object sender, EventArgs e) => this.MoveUp();
private void downToolStripMenuItem_Click(object sender, EventArgs e) => this.MoveDown();
private void bottomToolStripMenuItem_Click(object sender, EventArgs e) => this.MoveBottom();
private void toolStripMenuItem3_Click(object sender, EventArgs e) => this.Remove();
2020-04-11 17:43:20 +00:00
}
}