public RegionSelectorViewModel( [NotNull] ISelectionAdornerViewModel selectionAdorner, [NotNull][Dependency(WellKnownSchedulers.UI)] IScheduler uiScheduler, [NotNull][Dependency(WellKnownSchedulers.Background)] IScheduler bgScheduler) { SelectionAdorner = selectionAdorner.AddTo(Anchors); windowSeeker = new TaskWindowSeeker { SkipNotVisibleWindows = true }; var refreshRequest = new Subject <Unit>(); SelectionAdorner.WhenAnyValue(x => x.MousePosition, x => x.Owner).ToUnit() .Merge(refreshRequest) .Select(x => new { SelectionAdorner.MousePosition, SelectionAdorner.Owner }) .Where(x => x.Owner != null) .Sample(ThrottlingPeriod, bgScheduler) .ObserveOn(uiScheduler) .Select(x => x.MousePosition.ToScreen(x.Owner)) .Select(x => new Rectangle(x.X, x.Y, 1, 1)) .Select(ToRegionResult) .Do(x => Log.Debug($"Selection candidate: {x}")) .Subscribe(x => SelectionCandidate = x) .AddTo(Anchors); refreshRequest .Subscribe(() => windowSeeker.Refresh()) .AddTo(Anchors); Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(1), bgScheduler).ToUnit() .Subscribe(refreshRequest) .AddTo(Anchors); }
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"); }