-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from JasonWei512/feature/today-coding-time-de…
…velop Display "total time today" in status bar
- Loading branch information
Showing
13 changed files
with
566 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.VisualStudio.Shell; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
|
||
namespace WakaTime.ExtensionUtils | ||
{ | ||
internal class StatusbarControl : TextBlock | ||
{ | ||
private const string Icon = "🕑"; | ||
|
||
private readonly Brush _normalBackground = new SolidColorBrush(Colors.Transparent); | ||
private readonly Brush _hoverBackground = new SolidColorBrush(Colors.White) { Opacity = 0.2 }; | ||
|
||
public StatusbarControl() | ||
{ | ||
Text = Icon; | ||
Foreground = new SolidColorBrush(Colors.White); | ||
Background = _normalBackground; | ||
|
||
VerticalAlignment = VerticalAlignment.Center; | ||
Margin = new Thickness(7, 0, 7, 0); | ||
Padding = new Thickness(7, 0, 7, 0); | ||
|
||
MouseEnter += (s, e) => | ||
{ | ||
Cursor = Cursors.Hand; | ||
Background = _hoverBackground; | ||
}; | ||
|
||
MouseLeave += (s, e) => | ||
{ | ||
Cursor = Cursors.Arrow; | ||
Background = _normalBackground; | ||
}; | ||
|
||
MouseLeftButtonUp += (s, e) => | ||
{ | ||
// Open WakaTime in browser | ||
System.Diagnostics.Process.Start("https://wakatime.com/"); | ||
}; | ||
} | ||
|
||
public void SetText(string text) | ||
{ | ||
ThreadHelper.ThrowIfNotOnUIThread(); | ||
Text = string.IsNullOrEmpty(text) ? Icon : $"{Icon} {text}"; | ||
} | ||
|
||
public void SetToolTip(string toolTip) | ||
{ | ||
ThreadHelper.ThrowIfNotOnUIThread(); | ||
ToolTip = toolTip; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Microsoft.VisualStudio.Shell; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Media; | ||
using Task = System.Threading.Tasks.Task; | ||
|
||
namespace WakaTime.ExtensionUtils | ||
{ | ||
internal static class StatusbarInjector | ||
{ | ||
private static Panel _panel; | ||
|
||
private static DependencyObject FindChild(DependencyObject parent, string childName) | ||
{ | ||
if (parent == null) | ||
{ | ||
return null; | ||
} | ||
|
||
int childrenCount = VisualTreeHelper.GetChildrenCount(parent); | ||
|
||
for (int i = 0; i < childrenCount; i++) | ||
{ | ||
DependencyObject child = VisualTreeHelper.GetChild(parent, i); | ||
|
||
if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName) | ||
{ | ||
return frameworkElement; | ||
} | ||
|
||
child = FindChild(child, childName); | ||
|
||
if (child != null) | ||
{ | ||
return child; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static async Task EnsureUIAsync() | ||
{ | ||
while (_panel is null) | ||
{ | ||
_panel = FindChild(Application.Current.MainWindow, "StatusBarPanel") as DockPanel; | ||
if (_panel is null) | ||
{ | ||
// Start window is showing. Need to wait for status bar render. | ||
await Task.Delay(5000); | ||
} | ||
} | ||
} | ||
|
||
public static async Task InjectControlAsync(FrameworkElement element) | ||
{ | ||
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | ||
await EnsureUIAsync(); | ||
|
||
// Place the element to the left of all elements on the right side of the status bar | ||
element.SetValue(DockPanel.DockProperty, Dock.Right); | ||
int index = GetLastDockRightElementIndex(_panel.Children) + 1; | ||
_panel.Children.Insert(index, element); | ||
} | ||
|
||
private static int GetLastDockRightElementIndex(UIElementCollection elements) | ||
{ | ||
int lastDockRightElementIndex = 0; | ||
|
||
int elementCount = elements.Count; | ||
for (int i = 0; i < elementCount; i++) | ||
{ | ||
object element = elements[i]; | ||
|
||
if (element is DependencyObject dependencyObject && | ||
dependencyObject.GetValue(DockPanel.DockProperty) is Dock dockProperty && | ||
dockProperty == Dock.Right) | ||
{ | ||
lastDockRightElementIndex = i; | ||
} | ||
} | ||
|
||
return lastDockRightElementIndex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.VisualStudio.Shell; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
|
||
namespace WakaTime.ExtensionUtils | ||
{ | ||
internal class StatusbarControl : TextBlock | ||
{ | ||
private const string Icon = "🕑"; | ||
|
||
private readonly Brush _normalBackground = new SolidColorBrush(Colors.Transparent); | ||
private readonly Brush _hoverBackground = new SolidColorBrush(Colors.White) { Opacity = 0.2 }; | ||
|
||
public StatusbarControl() | ||
{ | ||
Text = Icon; | ||
Foreground = new SolidColorBrush(Colors.White); | ||
Background = _normalBackground; | ||
|
||
VerticalAlignment = VerticalAlignment.Center; | ||
Margin = new Thickness(7, 0, 7, 0); | ||
Padding = new Thickness(7, 0, 7, 0); | ||
|
||
MouseEnter += (s, e) => | ||
{ | ||
Cursor = Cursors.Hand; | ||
Background = _hoverBackground; | ||
}; | ||
|
||
MouseLeave += (s, e) => | ||
{ | ||
Cursor = Cursors.Arrow; | ||
Background = _normalBackground; | ||
}; | ||
|
||
MouseLeftButtonUp += (s, e) => | ||
{ | ||
// Open WakaTime in browser | ||
System.Diagnostics.Process.Start("https://wakatime.com/"); | ||
}; | ||
} | ||
|
||
public void SetText(string text) | ||
{ | ||
ThreadHelper.ThrowIfNotOnUIThread(); | ||
Text = string.IsNullOrEmpty(text) ? Icon : $"{Icon} {text}"; | ||
} | ||
|
||
public void SetToolTip(string toolTip) | ||
{ | ||
ThreadHelper.ThrowIfNotOnUIThread(); | ||
ToolTip = toolTip; | ||
} | ||
} | ||
} |
Oops, something went wrong.