private void OnUIStateChanged(UIStateFlags newState) { // update state of UI elements NotifyIconComponent notifyIcon = this.notifyIcon; if (notifyIcon != null) { notifyIcon.ExitMenuItem.Enabled = ((newState & UIStateFlags.ExitEnabled) != 0); notifyIcon.StartMenuItem.Enabled = ((newState & UIStateFlags.StartEnabled) != 0); notifyIcon.StopMenuItem.Enabled = ((newState & UIStateFlags.StopEnabled) != 0); notifyIcon.SettingsMenuItem.Enabled = ((newState & UIStateFlags.SettingsEnabled) != 0); notifyIcon.AboutMenuItem.Enabled = ((newState & UIStateFlags.AboutEnabled) != 0); if (this.Command.IsProxyRunning) { notifyIcon.Icon = AssemblyResources.OnIcon; } else { notifyIcon.Icon = AssemblyResources.OffIcon; } } // notify if (this.UIStateChanged != null) { try { this.UIStateChanged(this, EventArgs.Empty); } catch { // continue } } return; }
internal App(Command command) : base() { // argument checks if (command == null) { throw new ArgumentNullException(nameof(command)); } // initialize members this.Command = command; this.UIState = UIStateFlags.InitialState; this.onIcon = null; this.offIcon = null; this.notifyIcon = null; this.mainWindow = null; return; }
protected override void OnStartup(StartupEventArgs e) { // process the base class level tasks base.OnStartup(e); // process this class level tasks this.ShutdownMode = ShutdownMode.OnExplicitShutdown; this.onIcon = BitmapFrame.Create(new Uri("pack://siteoforigin:,,,/Resources/OnIcon.ico")); this.offIcon = BitmapFrame.Create(new Uri("pack://siteoforigin:,,,/Resources/OffIcon.ico")); // setup task tray menu NotifyIconComponent notifyIcon = new NotifyIconComponent(); notifyIcon.StartMenuItem.Click += this.StartMenuItem_Click; notifyIcon.StopMenuItem.Click += this.StopMenuItem_Click; notifyIcon.OpenMenuItem.Click += this.OpenMenuItem_Click; notifyIcon.SettingsMenuItem.Click += this.SettingsMenuItem_Click; notifyIcon.AboutMenuItem.Click += this.AboutMenuItem_Click; notifyIcon.ExitMenuItem.Click += this.ExitMenuItem_Click; this.notifyIcon = notifyIcon; // misc this.Command.ProxyStateChanged += command_ProxyStateChanged; SetUIState(UIStateFlags.None); this.Command.DoInitialSetup(); if (this.Command.Settings.GUI.Start) { // start proxying with delay this.Command.ScheduleStartProxy(); } // UI state must be updated after the initial setup // otherwise another window can be opened from the context menu UpdateUIState(); return; }