diff --git a/MainForm.cs b/MainForm.cs index 1649abb..982c774 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text.Json; using System.Threading.Tasks; @@ -39,6 +40,7 @@ namespace FizzyLauncher { InitializeComponent(); + tileContainer1.OnColumnSizeChanged += tileContainer1_OnSizeChanged; notifyIcon1.Text = Application.ProductName; this.AutoScaleMode = AutoScaleMode.None; @@ -231,15 +233,6 @@ namespace FizzyLauncher protected void newSession() { tileContainer1.Clear(true); - //flowLayoutPanel1.Controls.Clear(); - //flowLayoutPanel1.Controls.Add(new TilePanelLayout(new TileGroupModel() - //{ - // Title = "New Group", - // IsExpanded = true, - // GridSize = new Size(6, 1) - //})); - - InvalidateWidth(6); } protected async Task loadFile(string filename) @@ -301,23 +294,7 @@ namespace FizzyLauncher } // load tiles - int maxTileWidth = 0; - tileContainer1.Clear(); - - if (this.CurrentSession.Groups != null) - { - foreach (TileGroupModel item in this.CurrentSession.Groups) - { - maxTileWidth = Math.Max(maxTileWidth, item.GridSize.Width); - - TilePanelLayout panel = new TilePanelLayout(item); - - tileContainer1.Add(panel); - } - } - - // resize - InvalidateSize(maxTileWidth); + tileContainer1.Load(this.CurrentSession.Groups); // reposition if (!this.CurrentSession.StartPosition.IsEmpty) ThreadControl.SetLocation(this, this.CurrentSession.StartPosition); @@ -327,6 +304,7 @@ namespace FizzyLauncher ThreadControl.SetVisible(this, true); ThreadControl.SetChecked(enableAnimationsToolStripMenuItem, this.CurrentSession.EnableAnimation); ThreadControl.SetChecked(showBigIconsToolStripMenuItem, this.CurrentSession.EnableBigIconInFolder); + ThreadControl.SetClientHeight(this, this.CurrentSession.DefaultHeight); ThreadControl.SetFocus(this); @@ -364,14 +342,7 @@ namespace FizzyLauncher this.CurrentSession.DefaultHeight = this.Height; this.CurrentSession.AlwaysOnTop = this.TopMost; this.CurrentSession.StartPosition = this.Location; - - // save - this.CurrentSession.Groups = new List(); - - foreach (TilePanelLayout item in tileContainer1.Groups) - { - this.CurrentSession.Groups.Add(item.Model); - } + this.CurrentSession.Groups = tileContainer1.GroupModels?.ToList() ?? new List(); var options = new JsonSerializerOptions(); options.Converters.Add(new JsonPointConverter()); @@ -419,28 +390,6 @@ namespace FizzyLauncher return false; } - - protected void InvalidateSize(int columnWidth) - { - int newWidth = TilePanelLayout.CalcWidth(columnWidth); - newWidth += SystemInformation.VerticalScrollBarWidth; - newWidth += tileContainer1.CalcWidth; - newWidth += this.Padding.Horizontal; - - ThreadControl.ClientSize(this, newWidth, this.CurrentSession.DefaultHeight); - } - - protected void InvalidateWidth(int columnWidth) - { - int newWidth = TilePanelLayout.CalcWidth(columnWidth); - newWidth += SystemInformation.VerticalScrollBarWidth; - newWidth += tileContainer1.CalcWidth; - newWidth += this.Padding.Horizontal; - - ThreadControl.SetClientWidth(this, newWidth); - } - - #region main menu private void newToolStripMenuItem_Click(object sender, EventArgs e) @@ -669,9 +618,18 @@ namespace FizzyLauncher this.Close(); } +#endregion + private void tileContainer1_OnSizeChanged(object sender, EventArgs e) + { + int newWidth = TilePanelLayout.CalcWidth(tileContainer1.TileWidthCount); + newWidth += SystemInformation.VerticalScrollBarWidth; + newWidth += tileContainer1.CalcWidth; + newWidth += this.Padding.Horizontal; + + ThreadControl.SetClientWidth(this, newWidth); + } - #endregion } } \ No newline at end of file diff --git a/RyzStudio/Windows/Forms/ThreadControl.cs b/RyzStudio/Windows/Forms/ThreadControl.cs index 49220fb..f183a18 100644 --- a/RyzStudio/Windows/Forms/ThreadControl.cs +++ b/RyzStudio/Windows/Forms/ThreadControl.cs @@ -399,9 +399,9 @@ namespace RyzStudio.Windows.Forms } } - public static void ClientSize(Control control, int width, int height) => ClientSize(control, new Size(width, height)); + public static void SetClientSize(Control control, int width, int height) => SetClientSize(control, new Size(width, height)); - public static void ClientSize(Control control, Size value) + public static void SetClientSize(Control control, Size value) { if (control.InvokeRequired) { @@ -415,6 +415,20 @@ namespace RyzStudio.Windows.Forms } } + public static void SetClientHeight(Control control, int value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.ClientSize = new Size(control.ClientSize.Width, value); + })); + } + else + { + control.ClientSize = new Size(control.ClientSize.Width, value); + } + } + public static void SetSize(Control control, int width, int height) => SetSize(control, new Size(width, height)); public static void SetSize(Control control, Size value) diff --git a/Windows/Forms/TileContainer.cs b/Windows/Forms/TileContainer.cs index f3e0090..30a78df 100644 --- a/Windows/Forms/TileContainer.cs +++ b/Windows/Forms/TileContainer.cs @@ -2,6 +2,7 @@ using RyzStudio.Windows.Forms; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; @@ -12,6 +13,8 @@ namespace FizzyLauncher.Windows.Forms { public class TileContainer : Panel { + protected const int DEFAULT_COLUMN = 6; + protected FlowLayoutPanel flowLayoutPanel1 = null; @@ -38,6 +41,10 @@ namespace FizzyLauncher.Windows.Forms } + [Browsable(false)] + public event EventHandler OnColumnSizeChanged; + + public int CalcWidth { get => flowLayoutPanel1.Padding.Horizontal + flowLayoutPanel1.Margin.Horizontal + this.Left + this.Padding.Horizontal + this.Margin.Horizontal; @@ -65,25 +72,76 @@ namespace FizzyLauncher.Windows.Forms } } + public IEnumerable GroupModels + { + get + { + foreach (TilePanelLayout item in this.Groups) + { + yield return item.Model; + } + } + } + + public int TileWidthCount { get; private set; } = DEFAULT_COLUMN; + public void Add(TilePanelLayout tilePanelLayout) { ThreadControl.Add(flowLayoutPanel1, tilePanelLayout); } + public void Add() + { + ThreadControl.Add(flowLayoutPanel1, new TilePanelLayout(new TileGroupModel() + { + Title = "New Group", + IsExpanded = true, + GridSize = new Size(6, 1) + })); + + this.TileWidthCount = Math.Max(this.TileWidthCount, 6); + } + public void Clear(bool addDefault = false) { ThreadControl.Clear(flowLayoutPanel1); + this.TileWidthCount = DEFAULT_COLUMN; + if (addDefault) { - flowLayoutPanel1.Controls.Add(new TilePanelLayout(new TileGroupModel() - { - Title = "New Group", - IsExpanded = true, - GridSize = new Size(6, 1) - })); + this.Add(); } + + ColumnSizeChanged(); + } + + public void Load(List groupList) + { + this.Clear(); + + if (groupList == null) + { + return; + } + + foreach (TileGroupModel item in groupList) + { + this.TileWidthCount = Math.Max(this.TileWidthCount, item.GridSize.Width); + + TilePanelLayout panel = new TilePanelLayout(item); + + this.Add(panel); + } + + ColumnSizeChanged(); + } + + + protected void ColumnSizeChanged() + { + this.OnColumnSizeChanged?.Invoke(this, null); }