示例#1
0
        public override void ForegroundChanged(IAppStateContext context, IntPtr handle, uint processId, string processPath)
        {
            if (context.ProcessPath == processPath)
            {
                // Get title
                string title = NativeMethods.GetWindowText(handle);

                // Get padded region
                NativeMethods.GetWindowRect(handle, out Win32Rect rect);
                context.WindowDimensions = new Dimensions
                {
                    Left   = rect.Left,
                    Top    = rect.Top,
                    Right  = rect.Right,
                    Bottom = rect.Bottom
                };

                // Update state
                context.Handle    = handle;
                context.ProcessId = processId;
                context.SetCurrentState(new LockedState());

                // Start hooks
                context.WindowHook.StartHook(handle);
                context.MouseHook.StartHook(context.Padding + context.WindowDimensions);
                context.MouseHook.RestrictMouseToRegion();

                // Update UI
                context.SendTitleChange(title);
                context.SendDimensionsChange(context.WindowDimensions);
                context.SendForegroundChange(true);
            }
        }
示例#2
0
        public override void Lock(IAppStateContext context, string path)
        {
            var nextState = new SpecificPath.UnlockedState();

            context.SetCurrentState(nextState);
            nextState.Lock(context, path);
        }
示例#3
0
        public override void Lock(IAppStateContext context, IntPtr handle)
        {
            var nextState = new SpecificWindow.UnlockedState();

            context.SetCurrentState(nextState);
            nextState.Lock(context, handle);
        }
示例#4
0
        // Constructor
        public AppSystem(IForegroundWindowHook fgHook, IWindowUpdateHook winHook, IMouseHook msHook)
        {
            _state = new InitialState();

            _stateContext = new AppStateContext
            {
                PathModeLocksToHandle = false,
                ForegroundHook        = fgHook,
                WindowHook            = winHook,
                MouseHook             = msHook,
                SetCurrentState       = (next) => SetCurrentState(next),
                SendLockStateChange   = () => LockStateChanged.Invoke(this, new Events.LockStateChangedEventArgs {
                    IsLocked = IsLocked
                }),
                SendPathChange = (path) => PathChanged.Invoke(this, new Events.PathChangedEventArgs {
                    Path = path
                }),
                SendTitleChange = (title) => TitleChanged.Invoke(this, new Events.TitleChangedEventArgs {
                    Title = title
                }),
                SendDimensionsChange = (dimensions) => DimensionsChanged.Invoke(this, new Events.DimensionsChangedEventArgs {
                    Dimensions = dimensions
                }),
                SendForegroundChange = (inForeground) => ForegroundChanged.Invoke(this, new Events.ForegroundStateChangedEventArgs {
                    InForeground = inForeground
                })
            };

            fgHook.ForegroundWindowChanged += ForegroundHook_ForegroundWindowChanged;
            winHook.WindowClosed           += WindowHook_WindowClosed;
            winHook.DimensionsChanged      += WindowHook_DimensionsChanged;
            winHook.TitleChanged           += WindowHook_TitleChanged;
        }
示例#5
0
        public override void ForegroundChanged(IAppStateContext context, IntPtr handle, uint processId, string processPath)
        {
            if (context.Handle != handle ||
                context.ProcessId != processId ||
                context.ProcessPath != processPath)
            {
                // Release mouse
                context.MouseHook.UnrestrictMouse();

                // If locks to handle, retain state and wait for handle
                if (context.PathModeLocksToHandle)
                {
                    // Update state
                    context.SetCurrentState(new WaitingHandleState());

                    // Update UI
                    context.SendForegroundChange(false);
                }
                else
                {
                    // Otherwise same result as closed window
                    WindowClosed(context);
                }
            }
        }
示例#6
0
        private object GetServiceInstance(Type serviceType, IAppStateContext stateContext, IServiceProvider serviceProvider)
        {
            if (serviceType == typeof(IAppStateContext))
            {
                return(stateContext);
            }
            else if (serviceType == typeof(IServiceProvider))
            {
                return(serviceProvider);
            }
            else if (serviceType == typeof(IAppState))
            {
                return(stateContext.State);
            }
            else if (serviceType == typeof(IAppView))
            {
                return(stateContext.View);
            }
            else if (serviceType == typeof(IAppStateManager))
            {
                return(stateContext.StateManager);
            }

            return(serviceProvider.GetService(serviceType));
        }
示例#7
0
        public IAppStateController CreateController(Type controllerType, IAppStateContext stateContext, IServiceProvider serviceProvider)
        {
            Debug.Assert(controllerType != null);
            Debug.Assert(stateContext != null);
            Debug.Assert(serviceProvider != null);

            try
            {
                var constructors = controllerType.GetConstructors(BindingFlags.Instance | BindingFlags.Public);

                if (constructors.Length > 0)
                {
                    var c          = constructors[0];
                    var parameters = c.GetParameters();
                    var args       = new object[parameters.Length];

                    for (int i = 0; i < args.Length; i++)
                    {
                        args[i] = GetServiceInstance(parameters[i].ParameterType, stateContext, serviceProvider);
                    }

                    return((IAppStateController)c.Invoke(args));
                }
                else
                {
                    return(Activator.CreateInstance(controllerType) as IAppStateController);
                }
            }
            catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }
        }
示例#8
0
        public virtual void PaddingChanged(IAppStateContext context, Dimensions dimensions)
        {
            // Store new padding value
            context.Padding = dimensions;

            // Trigger an update using existing window dimensions
            WindowDimensionsChanged(context, context.WindowDimensions);
        }
示例#9
0
 public override void ForegroundChanged(IAppStateContext context, IntPtr handle, uint processId, string processPath)
 {
     if (context.Handle == handle &&
         context.ProcessId == processId &&
         context.ProcessPath == processPath)
     {
         context.MouseHook.RestrictMouseToRegion();
         context.SetCurrentState(new LockedState());
         context.SendForegroundChange(true);
     }
 }
示例#10
0
 public override void ForegroundChanged(IAppStateContext context, IntPtr handle, uint processId, string processPath)
 {
     if (context.Handle != handle ||
         context.ProcessId != processId ||
         context.ProcessPath != processPath)
     {
         context.MouseHook.UnrestrictMouse();
         context.SetCurrentState(new WaitingState());
         context.SendForegroundChange(false);
     }
 }
示例#11
0
        public override void WindowDimensionsChanged(IAppStateContext context, Dimensions dimensions)
        {
            // Store dimensions
            context.WindowDimensions = dimensions;

            // Create padded dimensions
            var paddedDimensions = context.Padding + context.WindowDimensions;

            // Update system and UI
            context.MouseHook.SetRegion(paddedDimensions);
            context.SendDimensionsChange(context.WindowDimensions);
        }
示例#12
0
        public override void Unlock(IAppStateContext context)
        {
            // Stop hooks
            context.ForegroundHook.StopHook();
            context.WindowHook.StopHook();
            context.MouseHook.StopHook();

            // Update state
            context.Handle      = default;
            context.ProcessId   = default;
            context.ProcessPath = default;
            context.SetCurrentState(new UnlockedState());

            // Update UI
            context.SendLockStateChange();
        }
示例#13
0
        public override void WindowClosed(IAppStateContext context)
        {
            // Stop hooks
            context.WindowHook.StopHook();
            context.MouseHook.StopHook();

            // Update state
            context.Handle    = default;
            context.ProcessId = default;
            context.SetCurrentState(new WaitingPathState());

            // Update UI
            context.SendTitleChange("Waiting for application");
            context.SendDimensionsChange(new Dimensions(0, 0, 0, 0));
            context.SendForegroundChange(false);
        }
示例#14
0
        public override void Lock(IAppStateContext context, IntPtr handle)
        {
            // Notify client the lock request failed
            if (!NativeMethods.IsWindow(handle))
            {
                context.SendLockStateChange();
                return;
            }

            // Get process id
            _ = NativeMethods.GetWindowThreadProcessId(handle, out uint procId);

            // Get process path
            string procPath = NativeMethods.GetFullProcessName((int)procId);

            // Get title
            string title = NativeMethods.GetWindowText(handle);

            // Get padded region
            // Upper-left and lower-right corners of the window
            NativeMethods.GetWindowRect(handle, out Win32Rect rect);
            context.WindowDimensions = new Dimensions
            {
                Left   = rect.Left,
                Top    = rect.Top,
                Right  = rect.Right,
                Bottom = rect.Bottom
            };

            // Update state
            context.Handle      = handle;
            context.ProcessId   = procId;
            context.ProcessPath = procPath;
            context.SetCurrentState(new WaitingState());

            // Start hooks
            context.ForegroundHook.StartHook();
            context.WindowHook.StartHook(handle);
            context.MouseHook.StartHook(context.Padding + context.WindowDimensions);

            // Update UI
            context.SendLockStateChange();
            context.SendTitleChange(title);
            context.SendPathChange(procPath);
            context.SendDimensionsChange(context.WindowDimensions);
        }
示例#15
0
        public override void Lock(IAppStateContext context, string path)
        {
            // Basic input checking
            if (string.IsNullOrWhiteSpace(path))
            {
                return;
            }

            // Set state
            context.Handle      = default;
            context.ProcessId   = default;
            context.ProcessPath = path;
            context.SetCurrentState(new WaitingPathState());

            // Start hooks
            context.ForegroundHook.StartHook();

            // Update UI
            context.SendLockStateChange();
            context.SendPathChange(path);
            context.SendTitleChange("Waiting for application");
        }
示例#16
0
 public virtual void WindowClosed(IAppStateContext context)
 {
 }
示例#17
0
 public virtual void Lock(IAppStateContext context, IntPtr handle)
 {
 }
示例#18
0
 public virtual void WindowDimensionsChanged(IAppStateContext context, Dimensions dimensions)
 {
 }
示例#19
0
 public virtual void Lock(IAppStateContext context, string path)
 {
 }
示例#20
0
 // No op
 public override void WindowClosed(IAppStateContext context)
 {
 }
示例#21
0
 public virtual void WindowTitleChanged(IAppStateContext context, string title)
 {
 }
示例#22
0
 public override void WindowTitleChanged(IAppStateContext context, string title)
 {
     // Update UI
     context.SendTitleChange(title);
 }
示例#23
0
 public virtual void Unlock(IAppStateContext context)
 {
 }
示例#24
0
 public virtual void ForegroundChanged(IAppStateContext context, IntPtr handle, uint processId, string processPath)
 {
 }
示例#25
0
 public TestController_ConstructorWithMultipleArguments(IServiceProvider sp, IAppStateContext c, object o)
 {
     ServiceProvider = sp;
     Context         = c;
     Obj             = o;
 }