Ejemplo n.º 1
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);
        }