emeow

| about 1 month ago |
31
|
| View Raw
  1. 1
    using Musewave.Resources;
  2. 2
    using System;
  3. 3
    using System.Drawing;
  4. 4
    using System.IO;
  5. 5
    using System.Threading;
  6. 6
    using System.Windows.Forms;
  7. 7
     
  8. 8
    namespace Musewave.About {
  9. 9
    public sealed class AboutWindow : FormBase {
  10. 10
    public const string ABOUT = "about";
  11. 11
    public const string CHANGELOG = "changelog";
  12. 12
    public const string UPDATE = "update";
  13. 13
     
  14. 14
    private const int PADDING = 8;
  15. 15
    private const string CHANGELOG_FILE = "changelog.txt";
  16. 16
    private const string HOME_URL = "//musewave.flash.moe";
  17. 17
     
  18. 18
    private const string UPDATE_IDLE = "Check for updates";
  19. 19
    private const string UPDATE_CHECKING = "Checking...";
  20. 20
    private const string UPDATE_FAIL = "Check failed. Retry?";
  21. 21
    private const string UPDATE_LATEST = "On latest version!";
  22. 22
    private const string UPDATE_OLDER = "On a future version?!";
  23. 23
    private const string UPDATE_NEWER_NO_URL = "{0} is available!";
  24. 24
    private const string UPDATE_NEWER_YES_URL = "Download {0}";
  25. 25
     
  26. 26
    private readonly PictureBox Header;
  27. 27
    private readonly TabControl Tabs;
  28. 28
     
  29. 29
    private readonly AboutMainPage MainPage;
  30. 30
    private readonly AboutChangelogPage ChangelogPage;
  31. 31
     
  32. 32
    private readonly Button UpdateButton;
  33. 33
    private string UpdateButtonIdleText = UPDATE_IDLE;
  34. 34
    private Thread UpdateThread;
  35. 35
    private string UpdateUrl = null;
  36. 36
    private readonly bool AutoClickUpdateButton = false;
  37. 37
     
  38. 38
    private readonly AboutCredit[] Credits = new[] {
  39. 39
    new AboutCredit("BASS Powered MIDI Player by flashwave", "//flash.moe"),
  40. 40
    new AboutCredit("BASS, BASSENC and BASSMIDI by Un4seen", "//www.un4seen.com"),
  41. 41
    new AboutCredit("Silk Icons by famfamfam", "http://www.famfamfam.com/lab/icons/silk"),
  42. 42
    };
  43. 43
     
  44. 44
    public static void Display(IWin32Window owner, string tab = null) {
  45. 45
    using AboutWindow about = new(tab);
  46. 46
    about.ShowDialog(owner);
  47. 47
    }
  48. 48
     
  49. 49
    public AboutWindow(string tab = null) {
  50. 50
    Text = "About Musewave";
  51. 51
    ClientSize = new Size(500, 400);
  52. 52
    StartPosition = FormStartPosition.CenterParent;
  53. 53
     
  54. 54
    Controls.Add(Header = new() {
  55. 55
    Image = Resources.GetImage("Misc.about-v2-fg"),
  56. 56
    BackgroundImage = Resources.GetImage("Misc.about-v2-bg"),
  57. 57
    Size = new Size(640, 60),
  58. 58
    Location = new Point(0, 0),
  59. 59
    Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
  60. 60
    Dock = DockStyle.Top,
  61. 61
    });
  62. 62
     
  63. 63
    Controls.Add(Tabs = new() {
  64. 64
    Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right,
  65. 65
    Location = new Point(PADDING, PADDING + Header.Height),
  66. 66
    Size = new Size(ClientSize.Width - (PADDING * 2), ClientSize.Height - (PADDING * 2) - Header.Height),
  67. 67
    });
  68. 68
     
  69. 69
    Tabs.SelectedIndexChanged += Tabs_SelectedIndexChanged;
  70. 70
     
  71. 71
    Tabs.TabPages.Add(MainPage = new AboutMainPage());
  72. 72
     
  73. 73
    MainPage.AddButton("Close", (s, e) => Close());
  74. 74
    MainPage.AddButton("Website", (s, e) => MwProcess.OpenURL(HOME_URL));
  75. 75
    UpdateButton = MainPage.AddButton(UpdateButtonIdleText, UpdateButton_Click);
  76. 76
     
  77. 77
    foreach(AboutCredit credit in Credits)
  78. 78
    MainPage.AddCredit(credit);
  79. 79
     
  80. 80
    Tabs.TabPages.Add(ChangelogPage = new AboutChangelogPage {
  81. 81
    BackColor = Application.RenderWithVisualStyles ? SystemColors.ControlLightLight : SystemColors.Control,
  82. 82
    });
  83. 83
     
  84. 84
    ChangelogSection[] changelog;
  85. 85
    using(Stream stream = ResourceLoader.LoadEmbedded(CHANGELOG_FILE))
  86. 86
    changelog = ChangelogSection.Parse(stream);
  87. 87
     
  88. 88
    foreach(ChangelogSection section in changelog)
  89. 89
    ChangelogPage.AddSection(section);
  90. 90
     
  91. 91
    if(tab != null) {
  92. 92
    if(tab.Equals(ABOUT, StringComparison.OrdinalIgnoreCase))
  93. 93
    Tabs.SelectedTab = MainPage;
  94. 94
    else if(tab.Equals(CHANGELOG, StringComparison.OrdinalIgnoreCase))
  95. 95
    Tabs.SelectedTab = ChangelogPage;
  96. 96
    else if(tab.Equals(UPDATE, StringComparison.OrdinalIgnoreCase)) {
  97. 97
    AutoClickUpdateButton = true;
  98. 98
    Tabs.SelectedTab = MainPage;
  99. 99
    }
  100. 100
    }
  101. 101
    }
  102. 102
     
  103. 103
    private void AbortUpdateThread() {
  104. 104
    if(UpdateThread?.ThreadState == ThreadState.Running)
  105. 105
    UpdateThread.Abort();
  106. 106
    UpdateThread = null;
  107. 107
    }
  108. 108
     
  109. 109
    private void UpdateButton_Click(object sender, EventArgs ev) {
  110. 110
    if(!string.IsNullOrEmpty(UpdateUrl)) {
  111. 111
    MwProcess.OpenURL(UpdateUrl);
  112. 112
    return;
  113. 113
    }
  114. 114
     
  115. 115
    UpdateButton.Enabled = false;
  116. 116
    UpdateButton.Text = UPDATE_CHECKING;
  117. 117
     
  118. 118
    AbortUpdateThread();
  119. 119
    UpdateThread = new Thread(() => {
  120. 120
    try {
  121. 121
    if(MwUpdate.TryGetUpdateInfo(out MwUpdateInfo updateInfo)) {
  122. 122
    int diff = Program.Version.CompareTo(updateInfo.Version);
  123. 123
     
  124. 124
    if(diff < 0) {
  125. 125
    if(string.IsNullOrEmpty(updateInfo.Url)) {
  126. 126
    UpdateButtonIdleText = string.Format(UPDATE_NEWER_NO_URL, MwVersion.GetString(updateInfo.Version));
  127. 127
    } else {
  128. 128
    UpdateUrl = updateInfo.Url;
  129. 129
    UpdateButtonIdleText = string.Format(UPDATE_NEWER_YES_URL, MwVersion.GetString(updateInfo.Version));
  130. 130
    }
  131. 131
    } else if(diff > 0)
  132. 132
    UpdateButtonIdleText = UPDATE_OLDER;
  133. 133
    else
  134. 134
    UpdateButtonIdleText = UPDATE_LATEST;
  135. 135
     
  136. 136
    if(!string.IsNullOrEmpty(updateInfo.Text))
  137. 137
    Invoke(new Action(() => ShowMessageBox(updateInfo.Text, "Musewave Update Check", icon: MessageBoxIcon.Information)));
  138. 138
    } else
  139. 139
    UpdateButtonIdleText = UPDATE_FAIL;
  140. 140
    } finally {
  141. 141
    Invoke(new Action(() => {
  142. 142
    UpdateButton.Text = UpdateButtonIdleText;
  143. 143
    UpdateButton.Enabled = true;
  144. 144
     
  145. 145
    if(AutoClickUpdateButton) {
  146. 146
    if(string.IsNullOrEmpty(UpdateUrl))
  147. 147
    UpdateButton.Select();
  148. 148
    else
  149. 149
    UpdateButton.PerformClick();
  150. 150
    }
  151. 151
    }));
  152. 152
    }
  153. 153
    }) { IsBackground = true };
  154. 154
    UpdateThread.Start();
  155. 155
    }
  156. 156
     
  157. 157
    private void Tabs_SelectedIndexChanged(object sender, EventArgs ev) {
  158. 158
    TriggerResize();
  159. 159
    }
  160. 160
     
  161. 161
    protected override void OnFormClosing(FormClosingEventArgs e) {
  162. 162
    base.OnFormClosing(e);
  163. 163
     
  164. 164
    AbortUpdateThread();
  165. 165
    }
  166. 166
     
  167. 167
    protected override void OnShown(EventArgs e) {
  168. 168
    base.OnShown(e);
  169. 169
     
  170. 170
    TriggerResize();
  171. 171
     
  172. 172
    if(AutoClickUpdateButton)
  173. 173
    UpdateButton.PerformClick();
  174. 174
    }
  175. 175
     
  176. 176
    public void TriggerResize() {
  177. 177
    TabPage selectedTabRaw = Tabs.TabPages[Math.Max(0, Tabs.SelectedIndex)];
  178. 178
    if(selectedTabRaw is not AboutPage selectedTab)
  179. 179
    throw new NotSupportedException("Selected tab of About window tab must inherit AboutPage.");
  180. 180
     
  181. 181
    bool isResizable = selectedTab.IsResizeable;
  182. 182
     
  183. 183
    MaximumSize = Size.Empty;
  184. 184
     
  185. 185
    if(isResizable) {
  186. 186
    MaximizeBox = true;
  187. 187
    } else {
  188. 188
    MaximizeBox = false;
  189. 189
    if(WindowState == FormWindowState.Maximized)
  190. 190
    WindowState = FormWindowState.Normal;
  191. 191
    }
  192. 192
     
  193. 193
    Size desiredSize = selectedTab.DesiredSize + (Tabs.ClientSize - selectedTab.ClientSize);
  194. 194
     
  195. 195
    ResetMinimumSize();
  196. 196
    ClientSize = new Size(
  197. 197
    desiredSize.Width + (PADDING * 2),
  198. 198
    desiredSize.Height + Header.Height + (PADDING * 2)
  199. 199
    );
  200. 200
    SetCurrentSizeAsMinimum();
  201. 201
     
  202. 202
    if(!isResizable)
  203. 203
    MaximumSize = Size;
  204. 204
    }
  205. 205
    }
  206. 206
    }
  207. 207