1
0

ChangelogWindow.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Linq;
  2. using Content.Client.Administration.Managers;
  3. using Content.Client.Stylesheets;
  4. using Content.Client.UserInterface.Controls;
  5. using Content.Client.UserInterface.Systems.EscapeMenu;
  6. using Content.Shared.Administration;
  7. using JetBrains.Annotations;
  8. using Robust.Client.AutoGenerated;
  9. using Robust.Client.UserInterface;
  10. using Robust.Client.UserInterface.XAML;
  11. using Robust.Shared;
  12. using Robust.Shared.Configuration;
  13. using Robust.Shared.Console;
  14. namespace Content.Client.Changelog
  15. {
  16. [GenerateTypedNameReferences]
  17. public sealed partial class ChangelogWindow : FancyWindow
  18. {
  19. [Dependency] private readonly ChangelogManager _changelog = default!;
  20. [Dependency] private readonly IClientAdminManager _adminManager = default!;
  21. [Dependency] private readonly IConfigurationManager _cfg = default!;
  22. public ChangelogWindow()
  23. {
  24. RobustXamlLoader.Load(this);
  25. WindowTitle.AddStyleClass(StyleBase.StyleClassLabelHeading);
  26. Stylesheet = IoCManager.Resolve<IStylesheetManager>().SheetSpace;
  27. }
  28. protected override void Opened()
  29. {
  30. base.Opened();
  31. _changelog.SaveNewReadId();
  32. PopulateChangelog();
  33. }
  34. protected override void EnteredTree()
  35. {
  36. base.EnteredTree();
  37. _adminManager.AdminStatusUpdated += OnAdminStatusUpdated;
  38. }
  39. protected override void ExitedTree()
  40. {
  41. base.ExitedTree();
  42. _adminManager.AdminStatusUpdated -= OnAdminStatusUpdated;
  43. }
  44. private void OnAdminStatusUpdated()
  45. {
  46. TabsUpdated();
  47. }
  48. private async void PopulateChangelog()
  49. {
  50. // Changelog is not kept in memory so load it again.
  51. var changelogs = await _changelog.LoadChangelog();
  52. Tabs.DisposeAllChildren();
  53. var i = 0;
  54. foreach (var changelog in changelogs)
  55. {
  56. var tab = new ChangelogTab { AdminOnly = changelog.AdminOnly };
  57. tab.PopulateChangelog(changelog);
  58. Tabs.AddChild(tab);
  59. Tabs.SetTabTitle(i++, Loc.GetString($"changelog-tab-title-{changelog.Name}"));
  60. }
  61. VersionLabel.Text = _changelog.GetClientVersion();
  62. TabsUpdated();
  63. }
  64. private void TabsUpdated()
  65. {
  66. var tabs = Tabs.Children.OfType<ChangelogTab>().ToArray();
  67. var isAdmin = _adminManager.IsAdmin(true);
  68. var visibleTabs = 0;
  69. int? firstVisible = null;
  70. for (var i = 0; i < tabs.Length; i++)
  71. {
  72. var tab = tabs[i];
  73. if (!tab.AdminOnly || isAdmin)
  74. {
  75. Tabs.SetTabVisible(i, true);
  76. visibleTabs++;
  77. firstVisible ??= i;
  78. }
  79. else
  80. {
  81. Tabs.SetTabVisible(i, false);
  82. }
  83. }
  84. Tabs.TabsVisible = visibleTabs > 1;
  85. // Current tab became invisible, select the first one that is visible
  86. if (!Tabs.GetTabVisible(Tabs.CurrentTab) && firstVisible != null)
  87. {
  88. Tabs.CurrentTab = firstVisible.Value;
  89. }
  90. // We are only displaying one tab, hide its header
  91. if (!Tabs.TabsVisible && firstVisible != null)
  92. {
  93. Tabs.SetTabVisible(firstVisible.Value, false);
  94. }
  95. }
  96. }
  97. [UsedImplicitly, AnyCommand]
  98. public sealed class ChangelogCommand : IConsoleCommand
  99. {
  100. public string Command => "changelog";
  101. public string Description => "Opens the changelog";
  102. public string Help => "Usage: changelog";
  103. public void Execute(IConsoleShell shell, string argStr, string[] args)
  104. {
  105. IoCManager.Resolve<IUserInterfaceManager>().GetUIController<ChangelogUIController>().OpenWindow();
  106. }
  107. }
  108. }