private void RestoreWindows()
 {
     try
     {
         if (WindySerializationHelpers.RestoreWindows())
         {
             _trayIcon.ShowBalloonTip(5000,
                                      GetString("TipTitle_WindowsAutomaticallyRestored"),
                                      GetString("TipText_WindowsAutomaticallyRestored"),
                                      ToolTipIcon.Info);
         }
         else
         {
             _trayIcon.ShowBalloonTip(5000,
                                      GetString("TipTitle_NoWindowsToRestore"),
                                      GetString("TipText_NoWindowsToRestore"),
                                      ToolTipIcon.Info);
         }
     }
     catch (Exception ex)
     {
         _trayIcon.ShowBalloonTip(10000,
                                  GetString("TipTitle_CouldntRestoreWindows"),
                                  string.Format(GetString("TipText_WindowsNotAutomaticallyRestored"),
                                                ex.Message,
                                                ex.GetType()),
                                  ToolTipIcon.Error);
     }
 }
        private void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e)
        {
            var ds = WindySerializationHelpers.LoadDesktopState();

            if (CurrentDesktopLayoutMatches(ds))
            {
                RestoreWindows();
            }
        }
        private void Initialize()
        {
            WindySerializationHelpers.CleanUpStaleState();

            // set up the hotkey handlers first, so we can fail fast if they're already taken.
            try
            {
                _saveHotkey.Ctrl           = true;
                _saveHotkey.WindowsKey     = true;
                _saveHotkey.KeyCode        = Keys.S;
                _saveHotkey.Enabled        = true;
                _saveHotkey.HotkeyPressed += SaveHotkeyOnHotkeyPressed;

                _restoreHotkey.Ctrl           = true;
                _restoreHotkey.WindowsKey     = true;
                _restoreHotkey.KeyCode        = Keys.R;
                _restoreHotkey.Enabled        = true;
                _restoreHotkey.HotkeyPressed += RestoreHotkeyOnHotkeyPressed;
            }
            catch (HotkeyAlreadyInUseException)
            {
                MessageBox.Show(GetString("MessageBoxText_WindyFailedToStart_ShortcutsInUse"),
                                GetString("MessageBoxText_WindyFailedToStart"),
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                Environment.Exit(1);
            }

            _trayIcon      = new NotifyIcon();
            _trayIcon.Text = "Windy";
            _trayIcon.Icon = Resources.windy;

            _trayIcon.ContextMenuStrip = new ContextMenuStrip();
            _trayIcon.ContextMenuStrip.Items.AddRange(
                new ToolStripItem[]
            {
                new ToolStripMenuItem(GetString("MenuItem_SaveWindowLayout"), null, (sender, args) => SaveHotkeyOnHotkeyPressed(null, null))
                {
                    ShortcutKeyDisplayString = "Ctrl+Win+S"
                },
                new ToolStripMenuItem(GetString("MenuItem_RestoreWindowLayout"), null, (sender, args) => RestoreHotkeyOnHotkeyPressed(null, null))
                {
                    ShortcutKeyDisplayString = "Ctrl+Win+R"
                },
                new ToolStripSeparator(),
                _savedWindows,
                new ToolStripSeparator(),
                new ToolStripMenuItem(GetString("MenuItem_AboutWindy"), null, (sender, args) => (new AboutForm()).Show()),
                new ToolStripMenuItem(GetString("MenuItem_Exit"), null, (sender, args) => Application.Exit()),
            });
            PopulateSavedWindows();
        }
 private void SaveHotkeyOnHotkeyPressed(object sender, EventArgs eventArgs)
 {
     try
     {
         WindySerializationHelpers.SaveDesktopState();
         WindySerializationHelpers.SaveWindows();
         PopulateSavedWindows();
         _trayIcon.ShowBalloonTip(5000, GetString("TipTitle_WindowLayoutSaved"), GetString("TipText_WindowLayoutSaved"), ToolTipIcon.Info);
     }
     catch (Exception ex)
     {
         _trayIcon.ShowBalloonTip(10000,
                                  GetString("TipTitle_CouldntSaveWindows"),
                                  string.Format(GetString("TipText_CouldntSaveWindows"), ex.Message, ex.GetType()),
                                  ToolTipIcon.Error);
     }
 }
        public WindyApplicationContext()
        {
            Initialize();

            this.ThreadExit                      += Application_ApplicationExit;
            Application.ApplicationExit          += Application_ApplicationExit;
            SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging;

            _trayIcon.Visible     = true;
            _trayIcon.MouseClick += (sender, args) =>
            {
                // show only on a deliberate left click and not when they mash the mouse buttons or something
                if (args.Button == MouseButtons.Left)
                {
                    ShowWindyIsRunningTip();
                }
            };

            WindySerializationHelpers.SaveDesktopState();
            ShowWindyIsRunningTip();
        }
        private void RestoreHotkeyOnHotkeyPressed(object sender, EventArgs eventArgs)
        {
            var loaded = WindySerializationHelpers.LoadDesktopState();

            if (!CurrentDesktopLayoutMatches(loaded))
            {
                var res =
                    MessageBox.Show(
                        GetString("MessageBoxText_RestoringToNonMatchingScreenLayout"),
                        GetString("MessageBoxTitle_RestoringToNonMatchingScreenLayout"),
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning);

                if (res == DialogResult.No)
                {
                    return;
                }
            }

            RestoreWindows();
        }
        private void PopulateSavedWindows()
        {
            _savedWindows.DropDownItems.Clear();

            IEnumerable <Window> windows;

            if (WindySerializationHelpers.RestoreWindows(out windows))
            {
                _savedWindows.DropDownItems.AddRange(
                    windows.Select(
                        win =>
                        new ToolStripMenuItem(string.Format("{0} ({1}\xd7{2} @ {3},{4} - {5})", win.Title, win.Size.Width,
                                                            win.Size.Height, win.Location.X, win.Location.Y, win.State), null,
                                              (sender, args) =>
                {
                    try
                    {
                        WindyPInvokeWrappers.FocusWindow(win);
                    }
                    catch (Exception)
                    {
                        _trayIcon.ShowBalloonTip(10000,
                                                 GetString("TipTitle_CouldntShowWindow"),
                                                 GetString("TipText_CouldntShowWindow"),
                                                 ToolTipIcon.Error);
                    }
                })
                {
                    Enabled = true
                }).ToArray());
            }
            else
            {
                _savedWindows.DropDownItems.Add(new ToolStripMenuItem(GetString("NoSavedWindows"))
                {
                    Enabled = false
                });
            }
        }