using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Startup.Common.Browser;
using System;
using MonoMac.Foundation;
using MonoMac.AppKit;
namespace MediaBrowser.Server.Mac
{
public class MenuBarIcon
{
private NSMenuItem browseMenuItem;
private NSMenuItem configureMenuItem;
private NSMenuItem developerMenuItem;
private NSMenuItem quitMenuItem;
private NSMenuItem githubMenuItem;
private NSMenuItem apiMenuItem;
private NSMenuItem communityMenuItem;
public static MenuBarIcon Instance;
public MenuBarIcon (ILogger logger)
{
Instance = this;
Logger = logger;
}
public void ShowIcon() {
NSApplication.SharedApplication.BeginInvokeOnMainThread (CreateMenus);
}
private void CreateMenus() {
CreateNsMenu ();
}
public void Localize()
{
NSApplication.SharedApplication.BeginInvokeOnMainThread (() => {
var configManager = MainClass.AppHost.ServerConfigurationManager;
configManager.ConfigurationUpdated -= Instance_ConfigurationUpdated;
LocalizeText ();
configManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
});
}
private NSStatusItem statusItem;
private void CreateNsMenu() {
var menu = new NSMenu ();
statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
statusItem.Menu = menu;
statusItem.Image = NSImage.ImageNamed("statusicon");
statusItem.HighlightMode = true;
menu.RemoveAllItems ();
browseMenuItem = new NSMenuItem ("Browse Media Library", "b", delegate {
Browse (NSApplication.SharedApplication);
});
menu.AddItem (browseMenuItem);
configureMenuItem = new NSMenuItem ("Configure Media Browser", "c", delegate {
Configure (NSApplication.SharedApplication);
});
menu.AddItem (configureMenuItem);
developerMenuItem = new NSMenuItem ("Developer Resources");
menu.AddItem (developerMenuItem);
var developerMenu = new NSMenu ();
developerMenuItem.Submenu = developerMenu;
apiMenuItem = new NSMenuItem ("Api Documentation", "a", delegate {
ApiDocs (NSApplication.SharedApplication);
});
developerMenu.AddItem (apiMenuItem);
githubMenuItem = new NSMenuItem ("Github", "g", delegate {
Github (NSApplication.SharedApplication);
});
developerMenu.AddItem (githubMenuItem);
communityMenuItem = new NSMenuItem ("Visit Community", "v", delegate {
Community (NSApplication.SharedApplication);
});
menu.AddItem (communityMenuItem);
quitMenuItem = new NSMenuItem ("Quit", "q", delegate {
Quit (NSApplication.SharedApplication);
});
menu.AddItem (quitMenuItem);
}
private ILogger Logger{ get; set;}
private void Quit(NSObject sender)
{
MainClass.AppHost.Shutdown();
}
private void Community(NSObject sender)
{
BrowserLauncher.OpenCommunity(Logger);
}
private void Configure(NSObject sender)
{
BrowserLauncher.OpenDashboard(MainClass.AppHost, Logger);
}
private void Browse(NSObject sender)
{
BrowserLauncher.OpenWebClient(MainClass.AppHost, Logger);
}
private void Github(NSObject sender)
{
BrowserLauncher.OpenGithub(Logger);
}
private void ApiDocs(NSObject sender)
{
BrowserLauncher.OpenSwagger(MainClass.AppHost, Logger);
}
public void Terminate()
{
NSApplication.SharedApplication.InvokeOnMainThread (() => NSApplication.SharedApplication.Terminate(NSApplication.SharedApplication));
}
private string _uiCulture;
///
/// Handles the ConfigurationUpdated event of the Instance control.
///
/// The source of the event.
/// The instance containing the event data.
void Instance_ConfigurationUpdated(object sender, EventArgs e)
{
var configManager = MainClass.AppHost.ServerConfigurationManager;
if (!string.Equals(configManager.Configuration.UICulture, _uiCulture,
StringComparison.OrdinalIgnoreCase))
{
LocalizeText();
}
}
private void LocalizeText()
{
var configManager = MainClass.AppHost.ServerConfigurationManager;
_uiCulture = configManager.Configuration.UICulture;
NSApplication.SharedApplication.BeginInvokeOnMainThread (LocalizeInternal);
}
private void LocalizeInternal() {
var localization = MainClass.AppHost.LocalizationManager;
quitMenuItem.Title = localization.GetLocalizedString("LabelExit");
communityMenuItem.Title = localization.GetLocalizedString("LabelVisitCommunity");
githubMenuItem.Title = localization.GetLocalizedString("LabelGithub");
apiMenuItem.Title = localization.GetLocalizedString("LabelApiDocumentation");
developerMenuItem.Title = localization.GetLocalizedString("LabelDeveloperResources");
browseMenuItem.Title = localization.GetLocalizedString("LabelBrowseLibrary");
configureMenuItem.Title = localization.GetLocalizedString("LabelConfigureMediaBrowser");
}
}
}