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 string sessionFilename = null; public MainForm() : base() { InitializeComponent(); //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)) { loadFile(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; } } /// /// New /// /// /// private void toolStripMenuItem5_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(sessionFilename)) { newSession(); } else { DialogResult dr = MessageBox.Show("Save existing session?", "New session", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { bool rv = saveFile(sessionFilename, false); if (rv) { newSession(); sessionFilename = null; } } else if (dr == DialogResult.No) { newSession(); sessionFilename = null; } else if (dr == DialogResult.Cancel) { return; } } } /// /// Open /// /// /// private void toolStripMenuItem7_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(sessionFilename)) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { loadFile(openFileDialog1.FileName); } } else { DialogResult dr = MessageBox.Show("Save existing session?", "Open session", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { bool rv = saveFile(sessionFilename, false); if (rv) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { loadFile(openFileDialog1.FileName); } } } else if (dr == DialogResult.No) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { loadFile(openFileDialog1.FileName); } } else if (dr == DialogResult.Cancel) { return; } } } /// /// Close /// /// /// private void toolStripMenuItem8_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(sessionFilename)) { flowLayoutPanel1.Controls.Clear(); } else { DialogResult dr = MessageBox.Show("Save existing session?", "Close session", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { bool rv = saveFile(sessionFilename, false); if (rv) { flowLayoutPanel1.Controls.Clear(); sessionFilename = null; } } else if (dr == DialogResult.No) { flowLayoutPanel1.Controls.Clear(); sessionFilename = null; } else if (dr == DialogResult.Cancel) { return; } } } /// /// Save /// /// /// private void toolStripMenuItem6_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(sessionFilename)) { saveAsFile(); } else { saveFile(sessionFilename, true); } } /// /// Save As /// /// /// private void toolStripMenuItem2_Click(object sender, EventArgs e) => saveAsFile(); /// /// Always On Top /// /// /// private void toolStripMenuItem1_Click(object sender, EventArgs e) => this.TopMost = !this.TopMost; /// /// Exit /// /// /// 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 loadFile(string filename) { if (isBusy) { return; } if (string.IsNullOrWhiteSpace(filename)) { return; } if (!File.Exists(filename)) { return; } string sourceCode = null; try { sessionFilename = filename; sourceCode = File.ReadAllText(sessionFilename); } catch (Exception exc) { MessageBox.Show(exc.Message, "Load session"); return; } 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); } this.Width = maxWidth + SystemInformation.VerticalScrollBarWidth + 20 + flowLayoutPanel1.Left; } protected void newSession() { int maxWidth = 0; flowLayoutPanel1.Controls.Clear(); TTilePanelLayout panel = new TTilePanelLayout(new TileGroupModel() { Title = "New Group", IsExpanded = true, GridSize = new Size(8, 1) }); maxWidth = Math.Max(maxWidth, panel.Width); flowLayoutPanel1.Controls.Add(panel); this.Width = maxWidth + SystemInformation.VerticalScrollBarWidth + 20 + flowLayoutPanel1.Left; } protected bool saveFile(string filename, bool showNotices = true) { if (isBusy) { return false; } if (string.IsNullOrWhiteSpace(filename)) { return false; } if (flowLayoutPanel1.Controls.Count <= 0) { return true; } 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(filename, JsonConvert.SerializeObject(rs)); if (showNotices) { MessageBox.Show("Session saved!", "Save session", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception exc) { MessageBox.Show(exc.Message, "Save session"); return false; } isBusy = false; return true; } protected bool saveAsFile() { if (isBusy) { return false; } if (saveFileDialog1.ShowDialog() == DialogResult.OK) { bool rv = saveFile(saveFileDialog1.FileName); if (rv) { sessionFilename = saveFileDialog1.FileName; } return rv; } return false; } private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e) { toolStripMenuItem1.Checked = this.TopMost; toolStripMenuItem6.Enabled = !string.IsNullOrWhiteSpace(sessionFilename); } } }