Exemplo n.º 1
0
        public BackupAndRestorePosition(IObservable <EventType> events, PositionWindowsByProcessCollector collector, PostionRestoreOperator restoreOperator, IReadOnlyCollection <string> excludedProcesses, INotificationBus bus)
        {
            _collector = collector;

            var options = new PositionWindowsByProcessCollectorOptions(
                window => string.IsNullOrWhiteSpace(window.Title)
                //|| window.Rectangle.IsEmpty
                //|| window.Rectangle.IsOffScreen() since there is a monitor left to main window, this does not work
                || window.Placement.showCmd == User32.ShowState.SW_HIDE ||
                !window.IsVisible
                ,
                process => excludedProcesses.Any(n => process.ProcessName.Contains(n))
                );

            _subscription = events
                            .Do(e =>
            {
                if (e == EventType.Backup)
                {
                    _positionWindowsByProcesses = _collector.Collect(options);
                    bus.Emit(new BackupNotification(_positionWindowsByProcesses));
                    Console.WriteLine($"{DateTime.Now} - BACKUP");
                }
                else
                {
                    restoreOperator.Restore(_positionWindowsByProcesses);
                    Console.WriteLine($"{DateTime.Now} - RESTORE");
                }
            })
                            .Subscribe();
        }
 private static IObservable <BackupAndRestorePosition.EventType> CreateFromSessionSwitch(INotificationBus bus)
 {
     return(Observable.FromEventPattern <SessionSwitchEventHandler, SessionSwitchEventArgs>(
                ev => SystemEvents.SessionSwitch += ev,
                ev => SystemEvents.SessionSwitch -= ev)
            .Select(eventPattern => eventPattern.EventArgs.Reason)
            .Where(reason => reason == SessionSwitchReason.SessionLock || reason == SessionSwitchReason.SessionUnlock)
            .Select(reason =>
     {
         if (reason == SessionSwitchReason.SessionLock)
         {
             bus.Emit(new BackupOrRestore("Backup triggered by SessionLock event"));
             return BackupAndRestorePosition.EventType.Backup;
         }
         else
         {
             bus.Emit(new BackupOrRestore("Restore triggered by SessionUnlock event"));
             return BackupAndRestorePosition.EventType.Restore;
         }
     }));
 }
 private static IObservable <BackupAndRestorePosition.EventType> CreateFromPowerMode(INotificationBus bus)
 {
     return(Observable.FromEventPattern <PowerModeChangedEventHandler, PowerModeChangedEventArgs>(
                ev => SystemEvents.PowerModeChanged += ev,
                ev => SystemEvents.PowerModeChanged -= ev
                )
            .Select(eventPattern => eventPattern.EventArgs.Mode)
            .Where(mode => mode != PowerModes.StatusChange)
            .Select(mode =>
     {
         if (mode == PowerModes.Suspend)
         {
             bus.Emit(new BackupOrRestore("Backup triggered by Suspend event"));
             return BackupAndRestorePosition.EventType.Backup;
         }
         else
         {
             bus.Emit(new BackupOrRestore("Restore triggered by Power Event event"));
             return BackupAndRestorePosition.EventType.Restore;
         }
     }));
 }