Example #1
0
        public EyeOverlayViewModel(
            [NotNull] [Dependency(WellKnownWindows.AllWindows)] IWindowTracker mainWindowTracker,
            [NotNull] IOverlayWindowController overlayWindowController,
            [NotNull] IAuraModelController auraModelController,
            [NotNull] IWindowListProvider windowListProvider,
            [NotNull] ISelectionAdornerViewModel selectionAdorner,
            [NotNull] [Dependency(WellKnownSchedulers.UI)] IScheduler uiScheduler)
        {
            using var sw = new BenchmarkTimer("Initialization", Log, nameof(EyeOverlayViewModel));
            SelectionAdorner = selectionAdorner.AddTo(Anchors);
            activeConfigEditorAnchors.AddTo(Anchors);
            this.mainWindowTracker = mainWindowTracker;
            this.overlayWindowController = overlayWindowController;
            this.auraModelController = auraModelController;
            this.windowListProvider = windowListProvider;
            MinSize = new Size(32, 32);
            SizeToContent = SizeToContent.Manual;
            Width = 400;
            Height = 400;
            Left = 200;
            Top = 200;
            IsUnlockable = true;
            Title = "EyeAuras";
            EnableHeader = false;
            thumbnailOpacity.SetDefaultValue(DefaultThumbnailOpacity);

            ResetRegionCommandExecuted();
            sw.Step("Basic properties initialized");
            
            WhenLoaded
                .Take(1)
                .Subscribe(ApplyConfig)
                .AddTo(Anchors);
            sw.Step("WhenLoaded executed");
            
            resetRegionCommand = CommandWrapper.Create(ResetRegionCommandExecuted, ResetRegionCommandCanExecute);
            selectRegionCommand = CommandWrapper.Create(SelectRegionCommandExecuted, SelectRegionCommandCanExecute);
            closeConfigEditorCommand = CommandWrapper.Create(CloseConfigEditorCommandExecuted);
            fitOverlayCommand = CommandWrapper.Create<double?>(FitOverlayCommandExecuted);
            setAttachedWindowCommand = CommandWrapper.Create<WindowHandle>(SetAttachedWindowCommandExecuted);
            setClickThroughCommand = CommandWrapper.Create<bool?>(SetClickThroughModeExecuted);
            DisableAuraCommand = CommandWrapper.Create(() => auraModelController.IsEnabled = false);
            CloseCommand = CommandWrapper.Create(CloseCommandExecuted, auraModelController.WhenAnyValue(x => x.CloseController).Select(CloseCommandCanExecute));
            ToggleLockStateCommand = CommandWrapper.Create(
                () =>
                {
                    if (IsLocked && UnlockWindowCommand.CanExecute(null))
                    {
                        UnlockWindowCommand.Execute(null);
                    }
                    else if (!IsLocked && LockWindowCommand.CanExecute(null))
                    {
                        LockWindowCommand.Execute(null);
                    }
                    else
                    {
                        throw new ApplicationException($"Something went wrong - invalid Overlay Lock state: {new {IsLocked, IsUnlockable, CanUnlock = UnlockWindowCommand.CanExecute(null), CanLock = LockWindowCommand.CanExecute(null)  }}");
                    }
                });

            auraModelController.WhenAnyValue(x => x.Name)
                .Where(x => !string.IsNullOrEmpty(x))
                .Subscribe(x => OverlayName = x)
                .AddTo(Anchors);

            this.RaiseWhenSourceValue(x => x.ActiveThumbnailOpacity, thumbnailOpacity, x => x.Value).AddTo(Anchors);
            this.RaiseWhenSourceValue(x => x.ThumbnailOpacity, this, x => x.ActiveThumbnailOpacity).AddTo(Anchors);
            this.RaiseWhenSourceValue(x => x.SourceBounds, Region, x => x.Bounds).AddTo(Anchors);

            isInEditMode = Observable.Merge(
                    this.WhenAnyProperty(x => x.IsInSelectMode, x => x.IsLocked))
                .Select(change => IsInSelectMode || !IsLocked)
                .ToPropertyHelper(this, x => x.IsInEditMode, uiScheduler)
                .AddTo(Anchors);

            this.WhenAnyValue(x => x.IsLocked)
                .Where(x => x && isInSelectMode)
                .Subscribe(() => IsInSelectMode = false)
                .AddTo(Anchors);

            aspectRatio = this.WhenAnyProperty(x => x.Bounds, x => x.ViewModelLocation)
                .Select(change => Width >= 0 && Height >= 0
                    ? Width / Height
                    : double.PositiveInfinity)
                .ToPropertyHelper(this, x => x.AspectRatio, uiScheduler)
                .AddTo(Anchors);
            sw.ResetStep();
            configEditorSupplier = new Lazy<OverlayConfigEditor>(() => CreateConfigEditor(this));
            sw.Step("Initialized Config editor");
        }
Example #2
0
        public WindowSelectorViewModel(
            [NotNull] IWindowListProvider windowListProvider,
            [NotNull][Dependency(WellKnownSchedulers.UI)] IScheduler uiScheduler)
        {
            WindowList = windowListProvider.WindowList;

            this.WhenAnyValue(x => x.WindowTitle)
            .WithPrevious((prev, curr) => new { prev, curr })
            .DistinctUntilChanged()
            .Where(x => !string.IsNullOrEmpty(x.prev) && x.curr == null)
            .Subscribe(x => WindowTitle = x.prev)
            .AddTo(Anchors);

            windowListProvider.WindowList.ToObservableChangeSet()
            .ToUnit()
            .Merge(this.WhenAnyValue(x => x.TargetWindow).ToUnit())
            .Throttle(ThrottlingPeriod)
            .ObserveOn(uiScheduler)
            .Subscribe(x => MatchingWindowList = BuildMatches(windowListProvider.WindowList))
            .AddTo(Anchors);

            this.WhenAnyValue(x => x.ActiveWindow)
            .Where(x => x != null)
            .Where(x => !IsMatch(x, TargetWindow))
            .Subscribe(
                x =>
            {
                var newTargetWindow = new WindowMatchParams
                {
                    Title  = x.Title,
                    Handle = x.Handle
                };
                Log.Debug($"Selected non-matching Overlay source, changing TargetWindow, {TargetWindow} => {newTargetWindow}");
                TargetWindow = newTargetWindow;
            })
            .AddTo(Anchors);

            this.WhenAnyValue(x => x.MatchingWindowList)
            .Where(items => !items.Contains(ActiveWindow))
            .Select(items => items.FirstOrDefault(x => x.Handle == ActiveWindow?.Handle || x.Handle == WindowHandle) ?? items.FirstOrDefault())
            .Where(x => !Equals(ActiveWindow, x))
            .Subscribe(
                x =>
            {
                Log.Debug(
                    $"Setting new Overlay Window(target: {TargetWindow}): {(ActiveWindow == null ? "null" : ActiveWindow.ToString())} => {(x == null ? "null" : x.ToString())}\n\t{MatchingWindowList.DumpToTable()}");
                ActiveWindow = x;
            })
            .AddTo(Anchors);

            enableOverlaySelector = this.WhenAnyProperty(x => x.MatchingWindowList)
                                    .Select(change => MatchingWindowList.Length > 1)
                                    .ToPropertyHelper(this, x => x.EnableOverlaySelector)
                                    .AddTo(Anchors);

            this.WhenAnyValue(x => x.TargetWindow)
            .Subscribe(
                x =>
            {
                WindowTitle        = x.Title;
                WindowTitleIsRegex = x.IsRegex;
                WindowHandle       = x.Handle;
            })
            .AddTo(Anchors);

            this.WhenAnyValue(x => x.WindowTitle, x => x.WindowTitleIsRegex, x => x.WindowHandle)
            .Select(
                x => new WindowMatchParams
            {
                Title   = WindowTitle,
                IsRegex = WindowTitleIsRegex,
                Handle  = WindowHandle
            })
            .DistinctUntilChanged()
            .Throttle(ThrottlingPeriod)
            .ObserveOn(uiScheduler)
            .Subscribe(x => TargetWindow = x)
            .AddTo(Anchors);

            SetWindowTitleCommand = CommandWrapper.Create <WindowHandle>(SetWindowTitleCommandExecuted);
        }