Skip to content

Commit

Permalink
Merge pull request #8 from mousebyte/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mousebyte authored Feb 3, 2019
2 parents da69f70 + 5063a27 commit 09b1ba4
Show file tree
Hide file tree
Showing 38 changed files with 1,352 additions and 381 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/Properties/LogophiSettings.cs
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<setting name="DataDirectory" serializeAs="String">
<value />
</setting>
<setting name="EnableHotkey" serializeAs="String">
<value>False</value>
</setting>
<setting name="Hotkey" serializeAs="String">
<value>None</value>
</setting>
</MouseNet.Logophi.Properties.Settings>
</userSettings>
</configuration>
160 changes: 97 additions & 63 deletions AppContext.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using MouseNet.Logophi.Forms;
using Microsoft.Win32;
using MouseNet.Logophi.Properties;
using MouseNet.Logophi.Views.Presentation;
using MouseNet.Logophi.Thesaurus;
using MouseNet.Logophi.Utilities;

namespace MouseNet.Logophi
{
internal class AppContext : ApplicationContext
{
private readonly BookmarksFormPresenter
_bookmarksFormPresenter;

private readonly MainFormPresenter _mainFormPresenter;
private readonly PresentationAgent _agent;
private readonly GlobalHotkey _hotkey = new GlobalHotkey();
private readonly Settings _settings = Settings.Default;
private readonly Browser _thesaurus;
private readonly NotifyIcon _trayIcon;
private Keys _registeredHotkey = Keys.None;

public AppContext()
{
Application.ApplicationExit += OnApplicationExit;
_settings.PropertyChanged += OnSettingsPropertyChanged;
SetupDirectories();
_mainFormPresenter = new MainFormPresenter(
Settings.Default.DataDirectory,
Settings.Default.PersistentCache,
Settings.Default.SaveHistory);
_bookmarksFormPresenter = new BookmarksFormPresenter(
_mainFormPresenter.Thesaurus,
_mainFormPresenter.Search);
var openMenuItem = new ToolStripMenuItem {Text = @"Open"};
openMenuItem.Click += OnOpen;
var exitMenuItem = new ToolStripMenuItem {Text = @"Exit"};
Expand All @@ -44,86 +40,124 @@ public AppContext()
}
};
_trayIcon.DoubleClick += OnOpen;
PresentMainForm();
_hotkey.HotkeyPressed += OnHotkeyPressed;
RegisterHotkey();
_thesaurus = new Browser(_settings.DataDirectory,
_settings.PersistentCache,
_settings.SaveHistory);
_thesaurus.History.MaxItems = (int) _settings.MaxHistory;
_agent = new PresentationAgent(_thesaurus);
Activate();
}

public void Activate()
{
_agent.PresentMainForm();
}

private void OnHotkeyPressed
(object sender,
HotkeyEventArgs e)
{
Activate();
}

private void SetupDirectories()
{
if (Settings.Default.DataDirectory == string.Empty)
if (_settings.DataDirectory == string.Empty)
{
Settings.Default.DataDirectory = Path.Combine(
_settings.DataDirectory = Path.Combine(
Environment.GetFolderPath(
Environment
.SpecialFolder.LocalApplicationData),
Resources.AppName);
Settings.Default.Save();
_settings.Save();
}

if (!Directory.Exists(Settings.Default.DataDirectory))
Directory.CreateDirectory(
Settings.Default.DataDirectory);
if (!Directory.Exists(_settings.DataDirectory))
Directory.CreateDirectory(_settings.DataDirectory);
}

private void PresentMainForm()
{
if (_mainFormPresenter.IsPresenting) return;
var form = new MainForm();
//? Possibly move these event handlers somewhere else
form.ViewBookmarksClicked += OnViewBookmarksClicked;
form.GithubProjectClicked += OnGithubProjectClicked;
form.AboutClicked += OnAboutClicked;
form.ExitClicked +=
(sender,
args) => Application.Exit();
_mainFormPresenter.Present(form);
}

private void OnAboutClicked
private void OnApplicationExit
(object sender,
EventArgs e)
{
var form = new AboutForm();
form.ShowDialog((IWin32Window) _mainFormPresenter.View);
_agent.CloseMainForm();
}

private void OnApplicationExit
private void OnOpen
(object sender,
EventArgs e)
{
if (_mainFormPresenter.IsPresenting)
_mainFormPresenter.View.Close();
_trayIcon.Visible = false;
_trayIcon.Dispose();
Activate();
}

private void OnGithubProjectClicked
(object sender,
EventArgs e)
private void RegisterHotkey()
{
Process.Start(Resources.GithubUrl);
if (_settings.Hotkey == Keys.None
|| _settings.Hotkey == _registeredHotkey)
return;
if (_registeredHotkey != Keys.None)
_hotkey.UnregisterHotkey(0);
_hotkey.RegisterHotkey(_settings.Hotkey);
_registeredHotkey = _settings.Hotkey;
}

private void OnOpen
(object sender,
EventArgs e)
private void UnregisterHotkey()
{
PresentMainForm();
if (_registeredHotkey == Keys.None) return;
_hotkey.UnregisterHotkey(0);
_registeredHotkey = Keys.None;
}

private void OnViewBookmarksClicked
private void OnSettingsPropertyChanged
(object sender,
EventArgs e)
PropertyChangedEventArgs e)
{
if (!_mainFormPresenter.IsPresenting
|| _bookmarksFormPresenter.IsPresenting) return;
var form = new BookmarksForm();
_mainFormPresenter.Thesaurus.BookmarkRemoved +=
(o,
s) => form.Items.Remove(s);
_mainFormPresenter.Thesaurus.BookmarkAdded +=
(o,
s) => form.Items.Add(s);
_bookmarksFormPresenter.Present(form);
switch (e.PropertyName)
{
case "PersistentCache":
_thesaurus.PersistentCache =
_settings.PersistentCache;
break;
case "SaveHistory":
_thesaurus.History.PersistentHistory =
_settings.SaveHistory;
break;
case "MaxHistory":
_thesaurus.History.MaxItems =
(int) _settings.MaxHistory;
break;
case "EnableHotkey":
if (_settings.EnableHotkey)
RegisterHotkey();
else UnregisterHotkey();
break;
case "AutoRun":
var key = OpenAutoRunKey();
if (key == null) return;
if (_settings.AutoRun)
key.SetValue(Resources.AppName, Application.ExecutablePath);
else if (key.GetValue(Resources.AppName) != null)
key.DeleteValue(Resources.AppName);
break;
}
}

private static RegistryKey OpenAutoRunKey() =>
Registry.CurrentUser.OpenSubKey(
Resources.AutoRunKey,
true);

protected override void Dispose
(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_agent?.Dispose();
_trayIcon.Visible = false;
_trayIcon?.Dispose();
_hotkey?.Dispose();
}
}
}
22 changes: 19 additions & 3 deletions Forms/BookmarksForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,35 @@ public BookmarksForm()
InitializeComponent();
}

public BookmarksForm
(IEnumerable bookmarks)
{
InitializeComponent();
foreach (var bkmark in bookmarks)
Items.Add(bkmark);
}

public IList Items => _cBookmarksList.Items;
public event EventHandler<string> BookmarkRemoved;
public event EventHandler<string> BookmarkActivated;

public void Show
(object parent)
{
if (!(parent is IWin32Window window)) return;
base.Show(window);
}

public event EventHandler<ViewEventArgs> ViewEventActivated;

private void InvokeBookmarkActivated
(object sender,
MouseEventArgs args)
{
var i = _cBookmarksList.IndexFromPoint(args.Location);
if (i != ListBox.NoMatches)
BookmarkActivated?.Invoke(
ViewEventActivated?.Invoke(
this,
(string) _cBookmarksList.Items[i]);
new ViewEventArgs(Items[i]));
}

private void InvokeBookmarkRemoved
Expand Down
55 changes: 55 additions & 0 deletions Forms/HotkeyEdit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MouseNet.Logophi.Forms
{
public class HotkeyEdit : TextBox
{
private Keys _modifiers;
private const string CtrlKey = "Ctrl +";
private const string ShiftKey = "Shift +";
private const string AltKey = "Alt +";
public Keys Hotkey { get; set; }

public Keys Modifiers {
get => _modifiers;
set => _modifiers = value;
}

public HotkeyEdit()
{

}

private void OnPreviewKeyDown
(object sender,
PreviewKeyDownEventArgs e)
{
Text = string.Empty;
if (e.Modifiers == Keys.None || e.Modifiers == Keys.Shift)
{
Text = @"None";
return;
}

_modifiers = e.Modifiers;
if ((e.Modifiers & Keys.Control) == Keys.Control)
Text += CtrlKey;
if ((e.Modifiers & Keys.Shift) == Keys.Shift)
Text += ShiftKey;
if ((e.Modifiers & Keys.Alt) == Keys.Alt)
Text += AltKey;
}

private void OnKeyDown
(object sender,
KeyEventArgs e)
{
e.Handled = true;
}
}
}
Loading

0 comments on commit 09b1ba4

Please sign in to comment.