private void SetupControls() { //Setup elements Title = Config.ApplicationName; TitleLabel.Content = Title; //Setup drag behavior ToolbarLayout.MouseDown += (object sender, MouseButtonEventArgs args) => { DragMove(); }; //Setup close behavior CloseButton.Click += (object sender, RoutedEventArgs args) => { WindowState = WindowState.Minimized; }; KeyDown += (object sender, KeyEventArgs args) => { if (args.Key == Key.Escape) { WindowState = WindowState.Minimized; } }; IsVisibleChanged += (object sender, DependencyPropertyChangedEventArgs args) => { if (!IsVisible) { try { if (DialogWindow != null && DialogWindow.IsVisible) { DialogWindow.Close(); DialogWindow = null; } } catch (Exception e) { } } else { UpdateList(); } }; //Setup events RunButton.Click += (object sender, RoutedEventArgs args) => { try { if (BackgroundTask != null) { BackgroundTask.Run(); } } catch (Exception e) { } }; AddTaskButton.Click += (object sender, RoutedEventArgs args) => { try { if (DialogWindow != null && DialogWindow.IsVisible) { DialogWindow.Close(); DialogWindow = null; } InputDialogWindow dialogWindow = new InputDialogWindow("Enter task name"); this.DialogWindow = dialogWindow; dialogWindow.Owner = this; dialogWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner; dialogWindow.OnMessageEvent += (string message) => { if (message == null || message.Length < 0) { return; } if (TaskStorage == null) { return; } lock (TaskStorage) { if (TaskStorage.StorageMap.ContainsKey(message)) { return; } Task task = new Task(); task.Id = message; //Import task data ImportTaskWindow importTaskDialog = new ImportTaskWindow(task); importTaskDialog.Owner = DialogWindow != null ? DialogWindow : this; importTaskDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner; importTaskDialog.OnSelectEvent += (Task newTask) => { if (newTask != null) { newTask.ApplyFieldsTo(task, true); } }; importTaskDialog.ShowDialog(); //Set new task if (task != null && task.Id != null && task.Id.Length > 0) { TaskStorage.Put(task.Id, task); } UpdateList(); } }; dialogWindow.ShowDialog(); } catch (Exception e) { } }; OpenRegistryManagerButton.Click += (object sender, RoutedEventArgs args) => { try { if (DialogWindow != null && DialogWindow.IsVisible) { DialogWindow.Close(); DialogWindow = null; } RegisterWindow dialogWindow = new RegisterWindow(); this.DialogWindow = dialogWindow; dialogWindow.Owner = this; dialogWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner; dialogWindow.ShowDialog(); } catch (Exception e) { Console.WriteLine(e); } }; //Load UpdateList(); }
public ManagerWindow() { //Initialize StartHelper.Initialize(); //Initialize storages ConfigStorage = ConfigStorage.Global; TaskStorage = TaskStorage.Global; //Initialize Window InitializeComponent(); //Hide window on startup Hide(); //Create context menu for notify icon ContextMenu contextMenu = new ContextMenu(); bool hasExitOption = false; if (ConfigStorage != null) { hasExitOption = ConfigStorage.GetBool(Config.ConfigEnableExitOptionKey, Config.ConfigDefaultEnableExitOptionValue); } if (hasExitOption) { MenuItem menuExitItem = new MenuItem(); menuExitItem.Text = "Exit"; menuExitItem.Click += (object sender, EventArgs e) => { if (IconHelper != null) { IconHelper.Destroy(); } if (BackgroundTask != null) { BackgroundTask.Stop(); } //Exit application if (System.Windows.Forms.Application.MessageLoop) { // WinForms app System.Windows.Forms.Application.Exit(); } else { // Console app Environment.Exit(1); } }; contextMenu.MenuItems.Add(menuExitItem); } //Create notification icon in taskbar NotifyIconHelper.Data data = new NotifyIconHelper.Data(); data.ContextMenu = contextMenu; if (ConfigStorage != null) { data.ShowAlways = ConfigStorage.GetBool(Config.ConfigShowIconAlwaysKey, Config.ConfigDefaultShowIconAlwaysValue); data.DestroyOnClose = false; data.NoClickEvent = !ConfigStorage.GetBool(Config.ConfigEnableGuiKey, Config.ConfigDefaultEnableGuiValue); data.NoMenu = false; data.NoBalloonTip = !ConfigStorage.GetBool(Config.ConfigEnableBalloonTipKey, Config.ConfigDefaultEnableBalloonTipValue); } data.Icon = Properties.Resources.ic_application; data.BalloonTipTitle = Title; data.BalloonTipText = "The app has been minimised. Click the tray icon to show."; data.Text = Title; IconHelper = new NotifyIconHelper(this, data); IconHelper.Create(); //Create background worker BackgroundTask = new BackgroundTask(); BackgroundTask.Start(); //Setup controls SetupControls(); }