MenuBarIcon.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Localization;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Startup.Common.Browser;
  6. using System;
  7. using MonoMac.Foundation;
  8. using MonoMac.AppKit;
  9. namespace MediaBrowser.Server.Mac
  10. {
  11. public class MenuBarIcon
  12. {
  13. private NSMenuItem browseMenuItem;
  14. private NSMenuItem configureMenuItem;
  15. private NSMenuItem developerMenuItem;
  16. private NSMenuItem quitMenuItem;
  17. private NSMenuItem githubMenuItem;
  18. private NSMenuItem apiMenuItem;
  19. private NSMenuItem communityMenuItem;
  20. public static MenuBarIcon Instance;
  21. public MenuBarIcon (ILogger logger)
  22. {
  23. Instance = this;
  24. Logger = logger;
  25. }
  26. public void ShowIcon() {
  27. NSApplication.SharedApplication.BeginInvokeOnMainThread (CreateMenus);
  28. }
  29. private void CreateMenus() {
  30. CreateNsMenu ();
  31. }
  32. public void Localize()
  33. {
  34. NSApplication.SharedApplication.BeginInvokeOnMainThread (() => {
  35. var configManager = MainClass.AppHost.ServerConfigurationManager;
  36. configManager.ConfigurationUpdated -= Instance_ConfigurationUpdated;
  37. LocalizeText ();
  38. configManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  39. });
  40. }
  41. private NSStatusItem statusItem;
  42. private void CreateNsMenu() {
  43. var menu = new NSMenu ();
  44. statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
  45. statusItem.Menu = menu;
  46. statusItem.Image = NSImage.ImageNamed("statusicon");
  47. statusItem.HighlightMode = true;
  48. menu.RemoveAllItems ();
  49. browseMenuItem = new NSMenuItem ("Browse Media Library", "b", delegate {
  50. Browse (NSApplication.SharedApplication);
  51. });
  52. menu.AddItem (browseMenuItem);
  53. configureMenuItem = new NSMenuItem ("Configure Media Browser", "c", delegate {
  54. Configure (NSApplication.SharedApplication);
  55. });
  56. menu.AddItem (configureMenuItem);
  57. developerMenuItem = new NSMenuItem ("Developer Resources");
  58. menu.AddItem (developerMenuItem);
  59. var developerMenu = new NSMenu ();
  60. developerMenuItem.Submenu = developerMenu;
  61. apiMenuItem = new NSMenuItem ("Api Documentation", "a", delegate {
  62. ApiDocs (NSApplication.SharedApplication);
  63. });
  64. developerMenu.AddItem (apiMenuItem);
  65. githubMenuItem = new NSMenuItem ("Github", "g", delegate {
  66. Github (NSApplication.SharedApplication);
  67. });
  68. developerMenu.AddItem (githubMenuItem);
  69. communityMenuItem = new NSMenuItem ("Visit Community", "v", delegate {
  70. Community (NSApplication.SharedApplication);
  71. });
  72. menu.AddItem (communityMenuItem);
  73. quitMenuItem = new NSMenuItem ("Quit", "q", delegate {
  74. Quit (NSApplication.SharedApplication);
  75. });
  76. menu.AddItem (quitMenuItem);
  77. }
  78. private ILogger Logger{ get; set;}
  79. private void Quit(NSObject sender)
  80. {
  81. MainClass.AppHost.Shutdown();
  82. }
  83. private void Community(NSObject sender)
  84. {
  85. BrowserLauncher.OpenCommunity(Logger);
  86. }
  87. private void Configure(NSObject sender)
  88. {
  89. BrowserLauncher.OpenDashboard(MainClass.AppHost, Logger);
  90. }
  91. private void Browse(NSObject sender)
  92. {
  93. BrowserLauncher.OpenWebClient(MainClass.AppHost, Logger);
  94. }
  95. private void Github(NSObject sender)
  96. {
  97. BrowserLauncher.OpenGithub(Logger);
  98. }
  99. private void ApiDocs(NSObject sender)
  100. {
  101. BrowserLauncher.OpenSwagger(MainClass.AppHost, Logger);
  102. }
  103. public void Terminate()
  104. {
  105. NSApplication.SharedApplication.InvokeOnMainThread (() => NSApplication.SharedApplication.Terminate(NSApplication.SharedApplication));
  106. }
  107. private string _uiCulture;
  108. /// <summary>
  109. /// Handles the ConfigurationUpdated event of the Instance control.
  110. /// </summary>
  111. /// <param name="sender">The source of the event.</param>
  112. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  113. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  114. {
  115. var configManager = MainClass.AppHost.ServerConfigurationManager;
  116. if (!string.Equals(configManager.Configuration.UICulture, _uiCulture,
  117. StringComparison.OrdinalIgnoreCase))
  118. {
  119. LocalizeText();
  120. }
  121. }
  122. private void LocalizeText()
  123. {
  124. var configManager = MainClass.AppHost.ServerConfigurationManager;
  125. _uiCulture = configManager.Configuration.UICulture;
  126. NSApplication.SharedApplication.BeginInvokeOnMainThread (LocalizeInternal);
  127. }
  128. private void LocalizeInternal() {
  129. var localization = MainClass.AppHost.LocalizationManager;
  130. quitMenuItem.Title = localization.GetLocalizedString("LabelExit");
  131. communityMenuItem.Title = localization.GetLocalizedString("LabelVisitCommunity");
  132. githubMenuItem.Title = localization.GetLocalizedString("LabelGithub");
  133. apiMenuItem.Title = localization.GetLocalizedString("LabelApiDocumentation");
  134. developerMenuItem.Title = localization.GetLocalizedString("LabelDeveloperResources");
  135. browseMenuItem.Title = localization.GetLocalizedString("LabelBrowseLibrary");
  136. configureMenuItem.Title = localization.GetLocalizedString("LabelConfigureMediaBrowser");
  137. }
  138. }
  139. }