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

191 lines
5.2 KiB
C#
Raw Normal View History

2020-04-11 17:43:20 +00:00
using AppLauncher.Models;
using AppLauncher.Windows.Forms;
using Newtonsoft.Json;
2020-03-28 02:54:08 +00:00
using System;
2020-03-27 23:16:34 +00:00
using System.Collections.Generic;
2020-04-11 17:43:20 +00:00
using System.IO;
2020-03-27 23:16:34 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppLauncher
{
2020-03-28 02:54:08 +00:00
public partial class MainForm : AForm
2020-03-27 23:16:34 +00:00
{
2020-04-12 00:12:21 +00:00
protected int collapsedWidth = 40;
protected int expandedWidth = 800;
2020-05-03 14:53:15 +00:00
protected bool isBusy = false;
2020-03-27 23:16:34 +00:00
2020-03-28 02:54:08 +00:00
public MainForm() : base()
2020-03-27 23:16:34 +00:00
{
InitializeComponent();
}
2020-05-02 16:17:10 +00:00
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
string jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig");
if (File.Exists(jsonfigFilename))
{
loadSession(jsonfigFilename);
}
2020-05-03 10:41:54 +00:00
this.Width = flowLayoutPanel1.Width + (flowLayoutPanel1.Left * 2);
2020-03-30 10:48:24 +00:00
}
2020-04-12 00:12:21 +00:00
public async Task ToggleSize()
{
if (this.Width > collapsedWidth)
{
flowLayoutPanel1.Visible = false;
titlePanel1.LabelVisible = false;
await collapseWindow(collapsedWidth, 6);
}
else
{
await expandWindow(expandedWidth, 8);
flowLayoutPanel1.Visible = true;
titlePanel1.LabelVisible = true;
}
}
2020-05-03 14:53:15 +00:00
/// <summary>
/// Save As
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (isBusy)
{
return;
}
if (flowLayoutPanel1.Controls.Count <= 0)
{
return;
}
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
isBusy = true;
List<TileGroupModel> rs = new List<TileGroupModel>();
for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
{
if (flowLayoutPanel1.Controls[i].GetType() != typeof(TileContainer))
{
continue;
}
TileContainer container = flowLayoutPanel1.Controls[i] as TileContainer;
rs.Add(container.Model);
}
try
{
File.WriteAllText(saveFileDialog1.FileName, JsonConvert.SerializeObject(rs));
MessageBox.Show("Session saved!", "Save session", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Save session");
}
isBusy = false;
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
toolStripMenuItem1.Checked = this.TopMost;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
2020-04-12 00:12:21 +00:00
protected async Task collapseWindow(int width, int increment = 6)
2020-04-05 00:32:49 +00:00
{
await Task.Run(() =>
{
while (this.Width > width)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Width -= increment;
}));
}
else
{
this.Width -= increment;
}
Application.DoEvents();
}
});
}
2020-03-27 23:16:34 +00:00
2020-04-05 00:32:49 +00:00
protected async Task expandWindow(int width, int increment = 8)
{
await Task.Run(() =>
{
while (this.Width < width)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.Width += increment;
2020-04-12 00:12:21 +00:00
//this.Invalidate();
2020-04-05 00:32:49 +00:00
}));
}
else
{
this.Width += increment;
2020-04-12 00:12:21 +00:00
//this.Invalidate();
2020-04-05 00:32:49 +00:00
}
2020-03-27 23:16:34 +00:00
2020-04-05 00:32:49 +00:00
Application.DoEvents();
}
});
}
2020-03-27 23:16:34 +00:00
2020-05-03 10:43:09 +00:00
protected void loadSession(string filename)
2020-04-11 17:43:20 +00:00
{
string sourceCode = File.ReadAllText(filename);
if (string.IsNullOrWhiteSpace(sourceCode))
{
return;
}
List<TileGroupModel> rs = JsonConvert.DeserializeObject<List<TileGroupModel>>(sourceCode);
if (rs == null)
{
return;
}
2020-05-03 10:41:54 +00:00
int maxWidth = 0;
2020-04-11 17:43:20 +00:00
flowLayoutPanel1.Controls.Clear();
2020-03-27 23:16:34 +00:00
2020-04-11 17:43:20 +00:00
foreach (TileGroupModel item in rs)
{
TileContainer panel = new TileContainer(item);
2020-05-03 10:41:54 +00:00
maxWidth = Math.Max(maxWidth, panel.Width);
2020-05-02 16:17:10 +00:00
2020-05-03 10:41:54 +00:00
flowLayoutPanel1.Controls.Add(panel);
2020-04-11 17:43:20 +00:00
}
2020-05-03 10:41:54 +00:00
flowLayoutPanel1.Width = maxWidth;
2020-04-11 17:43:20 +00:00
}
2020-03-27 23:16:34 +00:00
}
}