diff --git a/Carol/Controls/HyperlinkTextField.cs b/Carol/Controls/HyperlinkTextField.cs index 7f386ea..8e1ff1b 100644 --- a/Carol/Controls/HyperlinkTextField.cs +++ b/Carol/Controls/HyperlinkTextField.cs @@ -1,10 +1,21 @@ -using System; +/* + * Custom Text field control that works as a hyperlink + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.ComponentModel; using AppKit; using Foundation; namespace Carol.Controls { + // DesignTimeVisible(true) makes sure that the class is visible in Xcode at design time [Register("HyperlinkTextField"), DesignTimeVisible(true)] public class HyperlinkTextField : NSTextField { @@ -23,6 +34,7 @@ public String Href set => href = value; } + #region Constructors public HyperlinkTextField(IntPtr p) : base(p) { @@ -32,6 +44,7 @@ public HyperlinkTextField() { } + #endregion public override void AwakeFromNib() { @@ -39,6 +52,7 @@ public override void AwakeFromNib() AttributedStringValue = new NSAttributedString(StringValue, new NSStringAttributes() { + //You can change the color of link after uncommenting the following //ForegroundColor = NSColor.Blue, UnderlineStyle = NSUnderlineStyle.Single.GetHashCode() }); diff --git a/Carol/Controls/PopoverView.cs b/Carol/Controls/PopoverView.cs index 19eb6dc..4c62369 100644 --- a/Carol/Controls/PopoverView.cs +++ b/Carol/Controls/PopoverView.cs @@ -1,13 +1,26 @@ -using System; +/* + * Custom Popover control with adjustable background color + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.ComponentModel; using AppKit; using Foundation; namespace Carol.Controls { + // DesignTimeVisible(true) makes sure that the class is visible in Xcode at design time [Register("PopoverView"), DesignTimeVisible(true)] public class PopoverView : NSView { + + #region Constructors public PopoverView() { @@ -17,6 +30,7 @@ public PopoverView(IntPtr p) : base(p) { } + #endregion public override void ViewDidMoveToWindow() { @@ -29,6 +43,8 @@ public override void ViewDidMoveToWindow() { WantsLayer = true }; + + //You can change the background color of popover below. Here it takes the color as rgba backgroundView.Layer.BackgroundColor = new CoreGraphics.CGColor(0.07f, 0.07f, 0.07f, 1.0f); backgroundView.AutoresizingMask = (NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable); frameView.AddSubview(backgroundView, NSWindowOrderingMode.Below, frameView); diff --git a/Carol/Helpers/EventMonitor.cs b/Carol/Helpers/EventMonitor.cs index fed1646..de8c876 100644 --- a/Carol/Helpers/EventMonitor.cs +++ b/Carol/Helpers/EventMonitor.cs @@ -1,4 +1,14 @@ -using AppKit; +/* + * Helper class to close the popover automatically on an external event + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using AppKit; using Foundation; namespace Carol.Helpers { @@ -8,6 +18,7 @@ public class EventMonitor NSEventMask mask; GlobalEventHandler handler; + #region Constructors public EventMonitor() { @@ -18,17 +29,25 @@ public EventMonitor(NSEventMask mask, GlobalEventHandler handler) this.mask = mask; this.handler = handler; } + #endregion + // Destructor ~EventMonitor() { Stop(); } + /// + /// Start monitoring events of a given mask + /// public void Start() { monitor = NSEvent.AddGlobalMonitorForEventsMatchingMask(mask, handler) as NSObject; } + /// + /// Stop monitoring events and release the resources + /// public void Stop() { if (monitor != null) diff --git a/Carol/Helpers/LyricsHelper.cs b/Carol/Helpers/LyricsHelper.cs index 576ac7e..5c60884 100644 --- a/Carol/Helpers/LyricsHelper.cs +++ b/Carol/Helpers/LyricsHelper.cs @@ -1,4 +1,14 @@ -using System; +/* + * Helper class to get the lyrics of a track + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.Net.Http; using System.Threading.Tasks; using Carol.Models; @@ -18,6 +28,12 @@ public LyricsHelper() apikey = SecretsReader.GetSecrets(); } + /// + /// Method to get the track id of currently playing track + /// + /// Trackid. + /// Track Name. + /// Artist Name. public async Task GetTrackId(string track, string artist) { var uri = new Uri(String.Format("https://api.musixmatch.com/ws/1.1/track.search?q_track={0}&q_artist={1}&apikey={2}", track, artist, apikey)); @@ -29,6 +45,13 @@ public async Task GetTrackId(string track, string artist) } } + /// + /// Method to get the lyrics of a track + /// + /// Lyrics of the track. + /// Track. + /// Artist. + /// Action. public async Task GetLyrics(string track, string artist, Action onSuccess) { await GetTrackId(track, artist); diff --git a/Carol/Helpers/SecretsReader.cs b/Carol/Helpers/SecretsReader.cs index 20dd980..3d41a9e 100644 --- a/Carol/Helpers/SecretsReader.cs +++ b/Carol/Helpers/SecretsReader.cs @@ -1,4 +1,14 @@ -using System; +/* + * Helper class to retrieve API key from Secrets.json fole + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.IO; using Newtonsoft.Json.Linq; @@ -8,6 +18,10 @@ public static class SecretsReader { private static JObject secrets; + /// + /// Gets the API key + /// + /// API Key public static string GetSecrets() { secrets = JObject.Parse(GetSecretKey()); diff --git a/Carol/Helpers/StatusBarController.cs b/Carol/Helpers/StatusBarController.cs index 8baed7f..5292b1d 100644 --- a/Carol/Helpers/StatusBarController.cs +++ b/Carol/Helpers/StatusBarController.cs @@ -1,4 +1,14 @@ -using AppKit; +/* + * Helper class to create and maintain a Status Bar Item + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using AppKit; using Foundation; namespace Carol.Helpers @@ -14,6 +24,7 @@ public class StatusBarController : NSObject NSStoryboard storyboard; NSWindowController windowController; + #region Constructors public StatusBarController() { @@ -45,7 +56,9 @@ public StatusBarController(NSPopover popover, string image) storyboard = NSStoryboard.FromName("Main", null); windowController = storyboard.InstantiateControllerWithIdentifier("AboutWindow") as NSWindowController; } + #endregion + //Destructor ~StatusBarController() { ViewController.AboutMenuItemClicked -= HandleAboutMenuItemClicked; @@ -73,12 +86,21 @@ void HidePopover(NSObject sender) eventMonitor.Stop(); } + /// + /// Hides popover on external mouse click + /// + /// Event. void MouseEventHandler(NSEvent _event) { if (popover.Shown) HidePopover(_event); } + /// + /// Handles the about menu item click. + /// + /// Sender. + /// E. void HandleAboutMenuItemClicked(object sender, System.EventArgs e) { HidePopover(sender as NSObject); @@ -91,6 +113,11 @@ void HandleAboutMenuItemClicked(object sender, System.EventArgs e) windowController.ShowWindow(sender as NSObject); } + /// + /// Handles the quit button click. + /// + /// Sender. + /// E. void HandleQuitButtonClicked(object sender, System.EventArgs e) { HidePopover(sender as NSObject); diff --git a/Carol/Models/TrackLyrics.cs b/Carol/Models/TrackLyrics.cs index 1d4b864..bc538f7 100644 --- a/Carol/Models/TrackLyrics.cs +++ b/Carol/Models/TrackLyrics.cs @@ -1,4 +1,14 @@ -using System; +/* + * Model for lyrics of a track + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.Collections.Generic; namespace Carol.Models diff --git a/Carol/Models/Tracks.cs b/Carol/Models/Tracks.cs index 981cb90..0989a8f 100644 --- a/Carol/Models/Tracks.cs +++ b/Carol/Models/Tracks.cs @@ -1,4 +1,14 @@ -using System; +/* + * Model for track + * + * Author - Anagh Sharma + * http://www.anaghsharma.com + * + * 2017 + * + */ + +using System; using System.Collections.Generic; namespace Carol.Models diff --git a/Carol/ViewController.cs b/Carol/ViewController.cs index 4d51e15..6631110 100644 --- a/Carol/ViewController.cs +++ b/Carol/ViewController.cs @@ -37,17 +37,22 @@ public override void ViewDidLoad() // Do any additional setup after loading the view. lyricsHelper = new LyricsHelper(); LyricsTextView.BackgroundColor = NSColor.Clear; + + // Media Player is the Visual Effect View with Blur and Vibrancy MediaPlayer.WantsLayer = true; MediaPlayer.Material = NSVisualEffectMaterial.Dark; MediaPlayer.BlendingMode = NSVisualEffectBlendingMode.WithinWindow; MediaPlayer.Layer.CornerRadius = 4.0f; + // Progress bar shows how much of lyrics have you covered. It works with scrollview progress = ProgressBar.Frame; + //Adding observer of Scroll view change in Notification Center. It helps to update the width of progress bar MainScroll.ContentView.PostsBoundsChangedNotifications = true; NSNotificationCenter.DefaultCenter.AddObserver(this, new ObjCRuntime.Selector("boundsChange:"), NSView.BoundsChangedNotification, MainScroll.ContentView); + #region Settings Menu settingsMenu = new NSMenu(); hoverarea = new NSTrackingArea(SettingsButton.Bounds, NSTrackingAreaOptions.MouseEnteredAndExited | NSTrackingAreaOptions.ActiveAlways, this, null); SettingsButton.AddTrackingArea(hoverarea); @@ -61,28 +66,11 @@ public override void ViewDidLoad() settingsMenu.AddItem(about); settingsMenu.AddItem(NSMenuItem.SeparatorItem); settingsMenu.AddItem(quit); + #endregion cursor = NSCursor.CurrentSystemCursor; } - [Export("boundsChange:")] - public void BoundsDidChangeNotification(NSObject sender) - { - var notification = sender as NSNotification; - var view = notification.Object as NSView; - var position = view.Bounds.Location.Y; - - var width = (position * 100) / (containerHeight - MainScroll.Bounds.Height); - if (width > 4 && width <= 100) - progress.Width = width; - else if (width < 0) - progress.Width = 4; - else if (width > 100) - progress.Width = 100; - ProgressBar.Frame = progress; - } - - public override NSObject RepresentedObject { get @@ -104,6 +92,7 @@ public override void ViewDidAppear() script = new NSAppleScript(getCurrentSongScript); result = script.ExecuteAndReturnError(out errors); + //The NumberofItems property is being used to handle different use cases. Check GetCurrentSong.txt in Scripts folder to know more if (result.NumberOfItems == 3) { var artist = result.DescriptorAtIndex(1).StringValue; @@ -157,10 +146,30 @@ public override void ViewDidAppear() LyricsTextView.Value = "Something went wrong. It happens."; } + //Observer method called to update the progress bar whenever scrolling occurs + [Export("boundsChange:")] + public void BoundsDidChangeNotification(NSObject sender) + { + var notification = sender as NSNotification; + var view = notification.Object as NSView; + var position = view.Bounds.Location.Y; + + var width = (position * 100) / (containerHeight - MainScroll.Bounds.Height); + if (width > 4 && width <= 100) + progress.Width = width; + else if (width < 0) + progress.Width = 4; + else if (width > 100) + progress.Width = 100; + ProgressBar.Frame = progress; + } + + partial void SettingsButtonClick(NSObject sender) { var current = NSApplication.SharedApplication.CurrentEvent; + //Check if the app is in login items of macOS or not var checkLoginItemsScript = File.ReadAllText("Scripts/LoginCheck.txt"); script = new NSAppleScript(checkLoginItemsScript); result = script.ExecuteAndReturnError(out errors); @@ -176,6 +185,7 @@ partial void SettingsButtonClick(NSObject sender) NSMenu.PopUpContextMenu(settingsMenu, current, sender as NSView); } + //Method to handle Launch at Login functionality [Export("launch:")]
 void Launch(NSObject sender)
 { if (!isLoginItem) { @@ -188,13 +198,15 @@ partial void SettingsButtonClick(NSObject sender) var removeFromLoginScript = File.ReadAllText("Scripts/LoginRemove.txt"); script = new NSAppleScript(removeFromLoginScript); script.ExecuteAndReturnError(out errors); - }
 }
 + }
 } +
 //Delegating the About Menu Item click event to Helpers/StatusBarController.cs [Export("about:")] void About(NSObject sender) { AboutMenuItemClicked?.Invoke(this, null); } + //Delegating the Quit Menu Item click event to Helpers/StatusBarController.cs [Export("quit:")]
 void Quit(NSObject sender)
 {
 QuitButtonClicked?.Invoke(this, null);
 } //Method override to change cursor to pointing hand on Mouse Enter (Hover)