Skip to content

Commit

Permalink
HotCorner and ContextMenu
Browse files Browse the repository at this point in the history
Added Enable/Disable HotCorner
Added Tray Context Menu
  • Loading branch information
BaalEvan committed Jul 31, 2015
1 parent e8390be commit 7c3b057
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
64 changes: 56 additions & 8 deletions FloatingClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public partial class MainWindow
private NotifyIcon notifyIcon;
private DispatcherTimer refreshDispatcher;
public static MainWindow current;
public static bool IsVisible;
public static bool windowIsVisible;
public static bool HotCornerEnabled;

/// <summary>
/// Initialize Application and Main Window
Expand All @@ -32,12 +33,21 @@ public MainWindow()
InitializeRefreshDispatcher();
WaitToFullMinuteAndRefresh();
new HotKey(Key.C, KeyModifier.Alt, key => ShowClock());
EnableHotCorner(true);

TrayIcon();

MouseHook._hookID = MouseHook.SetHook(MouseHook._proc);

}

private void EnableHotCorner(bool enable)
{
if (enable)
MouseHook._hookID = MouseHook.SetHook(MouseHook._proc);
else MouseHook.UnhookWindowsHookEx(MouseHook._hookID);
HotCornerEnabled = enable;
}

/// <summary>
/// Prepare Clock to Show
/// </summary>
Expand Down Expand Up @@ -106,14 +116,52 @@ private void SetPositionOnCurrentDisplay()
private void TrayIcon()
{
notifyIcon = new NotifyIcon();
notifyIcon.Click += notifyIcon_Click;
// notifyIcon.Click += notifyIcon_Click;
ContextMenu m_menu;



m_menu = new ContextMenu();
var ActiveHotCornerItem = new MenuItem("Activate HotCorner", ChangeHotCornerActiveState);
ActiveHotCornerItem.Checked = HotCornerEnabled;
var OptionsItem = new MenuItem("Options", OpenOptionWindow);
OptionsItem.Enabled = false;
var ExitItem = new MenuItem("Exit", CloseWindow);
m_menu.MenuItems.Add(0, (ActiveHotCornerItem));
m_menu.MenuItems.Add(1, (OptionsItem));
m_menu.MenuItems.Add(2, (ExitItem));
notifyIcon.ContextMenu = m_menu;


var streamResourceInfo = Application.GetResourceStream(new Uri("pack://application:,,,/clock.ico"));
if (streamResourceInfo != null)
notifyIcon.Icon = new Icon(streamResourceInfo.Stream);


notifyIcon.Visible = true;



notifyIcon.ShowBalloonTip(5, "Hello " + Environment.UserName,
"Press Alt+C to show Clock\nRight Click on Tray to Close", ToolTipIcon.Info);
}

private void OpenOptionWindow(object sender, EventArgs e)
{

}

private void ChangeHotCornerActiveState(object sender, EventArgs e)
{
EnableHotCorner(!HotCornerEnabled);
(sender as MenuItem).Checked =HotCornerEnabled;
}

private void CloseWindow(object sender, EventArgs e)
{
Close();
}

/// <summary>
/// Closing app after Right Click
/// </summary>
Expand All @@ -137,9 +185,9 @@ private void InitializeAnimationIn()
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 5);
Application.Current.MainWindow.Visibility = Visibility.Visible;

IsVisible = true;
windowIsVisible = true;
dispatcherTimer.Start();

}

/// <summary>
Expand All @@ -152,7 +200,7 @@ private static void OpacityFadeIn(object sender, EventArgs e)
if (Application.Current.MainWindow.Opacity < 0.95)
Application.Current.MainWindow.Opacity += 0.05;
else
((DispatcherTimer) sender).Stop();
((DispatcherTimer)sender).Stop();
}

/// <summary>
Expand All @@ -165,7 +213,7 @@ private void Window_Deactivated(object sender, EventArgs e)
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 15);

dispatcherTimer.Start();
IsVisible = false;
windowIsVisible = false;
refreshDispatcher.Stop();
}

Expand All @@ -180,7 +228,7 @@ private static void OpacityFadeOut(object sender, EventArgs e)
Application.Current.MainWindow.Opacity -= 0.1;
else
{
((DispatcherTimer) sender).Stop();
((DispatcherTimer)sender).Stop();
Application.Current.MainWindow.Visibility = Visibility.Collapsed;
}
}
Expand Down
2 changes: 1 addition & 1 deletion FloatingClock/MouseHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IntPtr SetHook(LowLevelMouseProc proc)
/// <returns></returns>
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (MainWindow.IsVisible || nCode < 0) return CallNextHookEx(_hookID, nCode, wParam, lParam);
if (MainWindow.windowIsVisible || nCode < 0) return CallNextHookEx(_hookID, nCode, wParam, lParam);
var hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
var activeScreen = Screen.FromPoint(Control.MousePosition);
if (hookStruct.pt.x >= activeScreen.Bounds.X + activeScreen.Bounds.Width - 25)
Expand Down

0 comments on commit 7c3b057

Please sign in to comment.