public
Last active — forked from /App,xaml.cs

  • Download Gist
App,xaml.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace ordoFile
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
 
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
 
this.Properties["bgStartup"] = false;
 
if (e != null && e.Args.Count() > 0)
{
if(e.Args[0] == "-bg")
{
this.Properties["bgStartup"] = true;
}
}
 
Configs configs = new Configs();
PresetFilters presets = new PresetFilters();
OrganisationSyncer organisationSyncer = new OrganisationSyncer();
TrayApp trayApp = new TrayApp(organisationSyncer, configs);
Logger logger = new Logger();
BackgroundOrganiser backgroundOrganiser = new BackgroundOrganiser(new DirectoryModel());
ForegroundOrganiser foregroundOrganiser = new ForegroundOrganiser(new DirectoryModel());
 
DependencyFactory.Container.RegisterInstance<Configs>("configs", configs);
DependencyFactory.Container.RegisterInstance<PresetFilters>("presets", presets);
DependencyFactory.Container.RegisterInstance<OrganisationSyncer>("organisationSyncer", organisationSyncer);
DependencyFactory.Container.RegisterInstance<BackgroundOrganiser>("backgroundOrganiser", backgroundOrganiser);
DependencyFactory.Container.RegisterInstance<ForegroundOrganiser>("foregroundOrganiser", foregroundOrganiser);
DependencyFactory.Container.RegisterInstance<TrayApp>("trayApp", trayApp);
DependencyFactory.Container.RegisterInstance<Logger>("logger", logger);
 
MainViewModel mainViewModel = new MainViewModel(trayApp, organisationSyncer);
MainView mainView = new MainView(configs, trayApp, mainViewModel);
 
mainView.Show();
}
}
}
MainView.xaml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<Window x:Class="ordoFile.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ordoFile.ViewModels"
xmlns:vw="clr-namespace:ordoFile.Views"
Title="ordoFile"
Name="MainWindow"
Height="500"
Width="815">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Visibility" Value="{Binding WindowVisibility}" />
</Style>
</Window.Style>
<Grid Name="ContentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<vw:ForegroundOrganiserView Grid.RowSpan="2"/>
 
<vw:BackgroundOrganiserView Grid.Row="2"/>
</Grid>
</Window>
MainView.xaml.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Security;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using ordoFile.Models;
using ordoFile.DataAccess;
using ordoFile.GUITools;
using ordoFile.Models.Organisers;
using ordoFile.ViewModels;
 
 
namespace ordoFile.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window
{
Configs _configs;
TrayApp _trayApp;
 
public MainView(Configs configs, TrayApp trayApp, MainViewModel viewModel)
{
_configs = configs;
_trayApp = trayApp;
this.DataContext = viewModel;
 
InitializeComponent();
}
 
/// <summary>
/// Override OnClosing to hide window rather than exiting application
/// when MainViews close button has been clicked. Application exiting
/// has been deferred to the TrayApp.
/// </summary>
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (_trayApp.WindowShouldMinimise)
{
e.Cancel = true;
_trayApp.ChangeGUIVisibility();
}
else
{
_configs.SaveConfigs();
}
 
base.OnClosing(e);
}
}
}
OrganisationSyncer.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ordoFile
{
/// <summary>
/// Class for ensuring foreground and background organisers do not
/// organise the same directory at the same time. Also synchronises
/// MainViews visibility state with actions in TrayApp.
/// </summary>
public class OrganisationSyncer
{
// Background/foreground organiser running states used
// for ensuring the same folder is not organised by both
// organisers
bool _backgroundOrganiserRunning,
_foregroundOrganiserRunning;
 
// Background/foreground organiser root directories used
// for ensuring the same folder is not organised by both
// organisers
string _backgroundDirectory,
_foregroundDirectory;
 
System.Windows.Visibility _windowVisibility = System.Windows.Visibility.Visible;
 
/// <summary>
/// Get or set the background directory path.
/// </summary>
public string BackgroundDirectory
{
get { return _backgroundDirectory; }
set { _backgroundDirectory = value; }
}
 
/// <summary>
/// Get or set the foreground directory path.
/// </summary>
public string ForegroundDirectory
{
get { return _foregroundDirectory; }
set { _foregroundDirectory = value; }
}
 
/// <summary>
/// Get or set the state of the background organiser
/// </summary>
public bool BackgroundOrganiserRunning
{
get { return _backgroundOrganiserRunning; }
set { _backgroundOrganiserRunning = value; }
}
 
/// <summary>
/// Get or set the state of the foreground organiser
/// </summary>
public bool ForegroundOrganiserRunning
{
get { return _foregroundOrganiserRunning; }
set { _foregroundOrganiserRunning = value; }
}
 
/// <summary>
/// Get or set the state of the foreground organiser
/// </summary>
public System.Windows.Visibility WindowVisibilty
{
get { return _windowVisibility; }
set { _windowVisibility = value; }
}
 
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be started.
/// </summary>
public event EventHandler OrganiseInBackground;
 
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be stopped.
/// </summary>
public event EventHandler StopBackgroundOrganisation;
 
/// <summary>
/// Method which, when called, raises the event for running
/// background organisation or the event for cancelling it
/// </summary>
/// <param name="shouldOrganiseInBackground">
/// Boolean value which decides whether or not start/stop events
/// are raised for background organisation
/// </param>
public void ShouldOrganiseInBackground(bool shouldOrganiseInBackground)
{
if (shouldOrganiseInBackground)
{
if (OrganiseInBackground != null)
{
OrganiseInBackground(this, EventArgs.Empty);
}
}
else
{
if (StopBackgroundOrganisation != null)
{
StopBackgroundOrganisation(this, EventArgs.Empty);
}
}
}
 
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be stopped.
/// </summary>
public event EventHandler ChangeOrganisationTextInTray;
 
/// <summary>
/// Method which, when called, raises the event for running
/// changing the organisation state text in the tray icon.
/// </summary>
public void ChangeOrganisationText()
{
if (ChangeOrganisationTextInTray != null)
{
ChangeOrganisationTextInTray(this, EventArgs.Empty);
}
}
 
/// <summary>
/// Handler for the event to be run when window visibility should be changed.
/// </summary>
public event EventHandler UpdateWindowVisibility;
 
/// <summary>
/// Method which, when called, raises the event for updating
/// the main window's visibility
/// </summary>
public void UpdateVisibility()
{
if (UpdateWindowVisibility != null)
{
UpdateWindowVisibility(this, EventArgs.Empty);
}
}
}
}
TrayApp.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ordoFile.DataAccess;
using ordoFile.Models.Organisers;
 
namespace ordoFile
{
public partial class TrayApp : Form
{
// Icon for tray app
NotifyIcon _trayIcon = new NotifyIcon();
 
//Menu for tray app
ContextMenu _trayMenu = new ContextMenu();
 
// Menu items to be placed in context menu
MenuItem _showGUI, _bgState, _exit;
 
// Reading and saving of config settings
Configs _configs;
 
// Ensures background/foreground organiser do not organise
// the same folder; also syncs view states between GUI and
// tray app
OrganisationSyncer _organisationSyncer;
 
// Represent states for whether or not the MainWindow should
// be hidden, and whether or not it close button should minimise
// window or exit application
bool _hideWindow,
_windowShouldMinimise;
 
public TrayApp(OrganisationSyncer organisationSyncer, Configs configs)
{
_organisationSyncer = organisationSyncer;
_configs = configs;
 
OnInitialise();
 
InitializeComponent();
}
 
/// <summary>
/// Returns wether or the window should be minimised
/// when MainWindow's close button is clicked
/// </summary>
public bool WindowShouldMinimise
{
get { return _windowShouldMinimise; }
}
 
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
 
// Hide form window.
this.Visible = false;
this.ShowInTaskbar = false;
}
 
void OnInitialise()
{
// Pass the event used for changing the _bgState display text
// to organisation syncer, which will be raised on input
// in the GUI
_organisationSyncer.ChangeOrganisationTextInTray += ChangeOrganisationText;
 
// Set the application to minimise when close button
// of main window is clicked
_windowShouldMinimise = true;
 
// Set whether or not the GUI should be hidden on startup
_hideWindow = ((bool) System.Windows.Application.Current.Properties["bgStartup"]);
 
if (_hideWindow)
{
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Hidden;
_organisationSyncer.UpdateVisibility();
}
 
// Instantiate tray menu, and menu items to be added to it
_showGUI = new MenuItem();
_showGUI.Text = (_hideWindow) ? "Show Window" : "Hide Window";
_showGUI.Click += (a, e) => { ChangeGUIVisibility(); };
 
_bgState = new MenuItem();
_bgState.Text = (_organisationSyncer.BackgroundOrganiserRunning) ? "Stop Organising" : "Start Organising";
_bgState.Enabled = _configs.BGDirectoryExists;
_bgState.Click += (a, e) => { CheckOrganisationState(); };
 
_exit = new MenuItem();
_exit.Text = "Exit";
_exit.Click += (a, e) => { OnExit(); };
 
_trayMenu.MenuItems.Add(0, _showGUI);
_trayMenu.MenuItems.Add(1, _bgState);
_trayMenu.MenuItems.Add(2, _exit);
 
_trayIcon.Icon = new Icon("icon.ico", 38, 42);
_trayIcon.Text = "ordoFile";
_trayIcon.DoubleClick += (a, b) => { ChangeGUIVisibility(); };
_trayIcon.ContextMenu = _trayMenu;
_trayIcon.Visible = true;
}
 
/// <summary>
/// Method which sets visibility of GUI and tray menu text
/// </summary>
public void ChangeGUIVisibility()
{
if (_organisationSyncer.WindowVisibilty == System.Windows.Visibility.Visible)
{
_showGUI.Text = "Show window";
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Hidden;
_organisationSyncer.UpdateVisibility();
}
else
{
_showGUI.Text = "Hide window";
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Visible;
_organisationSyncer.UpdateVisibility();
}
}
 
/// <summary>
/// Check organisation current organisation state, start or
/// stop organisation depending on whether or not background
/// directory exists then update menu item with appropriate text
/// </summary>
void CheckOrganisationState()
{
if (!_organisationSyncer.BackgroundOrganiserRunning)
{
if (_configs.BGDirectoryExists)
{
_bgState.Text = "Stop Organising";
_organisationSyncer.ShouldOrganiseInBackground(true);
}
}
else
{
_bgState.Text = "Start Organising";
_organisationSyncer.ShouldOrganiseInBackground(false);
}
}
 
/// <summary>
/// Event to update organisation state text in context menu
/// depending on current organisation state.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ChangeOrganisationText(object sender, EventArgs e)
{
if (_organisationSyncer.BackgroundOrganiserRunning)
{
_bgState.Text = "Stop Organising";
}
else
{
_bgState.Text = "Start Organising";
}
}
 
/// <summary>
/// Checks to run on exit of application.
/// </summary>
void OnExit()
{
_organisationSyncer.ShouldOrganiseInBackground(false);
_configs.SaveConfigs();
_windowShouldMinimise = false;
System.Windows.Application.Current.Shutdown();
this.Close();
}
 
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
_trayIcon.Dispose();
}
 
if (isDisposing && (components != null))
{
components.Dispose();
}
 
base.Dispose(isDisposing);
}
}
}
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
 
namespace ordoFile.ViewModels
{
public class MainViewModel : ViewModelBase
{
TrayApp _trayApp;
 
OrganisationSyncer _organisationSyncer;
 
Visibility _windowVisibility;
 
public MainViewModel(TrayApp trayApp, OrganisationSyncer organisationSyncer)
{
_trayApp = trayApp;
_organisationSyncer = organisationSyncer;
OnInitialise();
}
 
///
public Visibility WindowVisibility
{
get { return _windowVisibility; }
set
{
_windowVisibility = value;
OnPropertyChanged("WindowVisibility");
}
}
 
void OnInitialise()
{
WindowVisibility = _organisationSyncer.WindowVisibilty;
// Add method to event handler in OrganisationSyncer
// that will be called when UI visibility is to be updated.
_organisationSyncer.UpdateWindowVisibility += CheckVisibilityStatus;
}
 
/// <summary>
/// Set property to visibility value held in OrganisationSyncer
/// instance.
/// </summary>
void CheckVisibilityStatus(object sender, EventArgs e)
{
WindowVisibility = _organisationSyncer.WindowVisibilty;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.