using AppLauncher.Models;
using AppLauncher.Windows.Forms;
using Newtonsoft.Json;
using RyzStudio.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppLauncher
{
public partial class MainForm : AForm
{
protected int collapsedWidth = 40;
protected int expandedWidth = 800;
protected bool isBusy = false;
public MainForm() : base()
{
InitializeComponent();
//this.Visible = false;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//this.Visible = false;
}
protected override void OnShown(EventArgs e)
{
this.Visible = false;
base.OnShown(e);
string jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig");
if (File.Exists(jsonfigFilename))
{
loadSession(jsonfigFilename);
}
this.Location = this.DefaultLocation;
this.Visible = true;
}
public async Task ToggleSize()
{
if (this.Width > collapsedWidth)
{
flowLayoutPanel1.Visible = false;
await collapseWindow(collapsedWidth, 6);
}
else
{
await expandWindow(expandedWidth, 8);
flowLayoutPanel1.Visible = true;
}
}
///
/// Save As
///
///
///
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 rs = new List();
for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
{
if (flowLayoutPanel1.Controls[i].GetType() != typeof(TTilePanelLayout))
{
continue;
}
TTilePanelLayout container = flowLayoutPanel1.Controls[i] as TTilePanelLayout;
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;
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
protected async Task collapseWindow(int width, int increment = 6)
{
await Task.Run(() =>
{
while (this.Width > width)
{
ThreadControl.SetWidth(this, (this.Width - increment));
Application.DoEvents();
}
ThreadControl.SetWidth(this, width);
});
}
protected async Task expandWindow(int width, int increment = 8)
{
await Task.Run(() =>
{
while (this.Width < width)
{
ThreadControl.SetWidth(this, (this.Width + increment));
Application.DoEvents();
}
ThreadControl.SetWidth(this, width);
});
}
protected void loadSession(string filename)
{
string sourceCode = File.ReadAllText(filename);
if (string.IsNullOrWhiteSpace(sourceCode))
{
return;
}
List rs = JsonConvert.DeserializeObject>(sourceCode);
if (rs == null)
{
return;
}
int maxWidth = 0;
flowLayoutPanel1.Controls.Clear();
foreach (TileGroupModel item in rs)
{
TTilePanelLayout panel = new TTilePanelLayout(item);
maxWidth = Math.Max(maxWidth, panel.Width);
flowLayoutPanel1.Controls.Add(panel);
}
//flowLayoutPanel1.Width = maxWidth + SystemInformation.VerticalScrollBarWidth + 20;
//flowLayoutPanel1.Width = maxWidth + SystemInformation.VerticalScrollBarWidth + 20;
//this.Width = flowLayoutPanel1.Width + (flowLayoutPanel1.Left * 2);
//this.Width = flowLayoutPanel1.Width + flowLayoutPanel1.Left;
this.Width = maxWidth + SystemInformation.VerticalScrollBarWidth + 20 + flowLayoutPanel1.Left;
}
private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
toolStripMenuItem1.Checked = this.TopMost;
}
}
}