Exemplo n.º 1
0
        public AllTagsDialog(
            IChangeNotification changeNotification,
            IDialogViewModel viewModel,
            IEnumerable <string> availableTags,
            string initiallyFocusedTag
            )
        {
            this.viewModel = viewModel;
            InitializeComponent();

            checkedListBox1.BeginUpdate();
            int focusedTagIndex = -1;

            foreach (var t in availableTags)
            {
                var idx = checkedListBox1.Items.Add(t);
                if (initiallyFocusedTag != null && t == initiallyFocusedTag)
                {
                    focusedTagIndex = idx;
                }
            }
            checkedListBox1.EndUpdate();
            if (focusedTagIndex >= 0)
            {
                checkedListBox1.SelectedIndex = focusedTagIndex;
                checkedListBox1.TopIndex      = focusedTagIndex;
            }

            var listUpdater = Updaters.Create(
                () => viewModel.SelectedTags,
                () => viewModel.IsEditingFormula,
                (selected, editingFormula) =>
            {
                checkedListBox1.Tag = "ignore events";
                checkedListBox1.BeginUpdate();
                foreach (var t in availableTags.ZipWithIndex())
                {
                    checkedListBox1.SetItemChecked(t.Key, selected.Contains(t.Value));
                }
                checkedListBox1.Enabled = !editingFormula;
                checkedListBox1.EndUpdate();
                checkedListBox1.Tag = null;
            }
                );

            Color getLinkColor(MessageSeverity sev) =>
            sev == MessageSeverity.Error ? Color.Red :
            sev == MessageSeverity.Warning ? Color.DarkOrange :
            SystemColors.WindowText;

            var formulaUpdater = Updaters.Create(
                () => viewModel.Formula,
                () => viewModel.IsEditingFormula,
                () => viewModel.FormulaStatus,
                (formula, editing, status) =>
            {
                formulaTextBox.Text             = formula;
                formulaTextBox.ReadOnly         = !editing;
                checkAllLinkLabel.Enabled       = !editing;
                checkNoneLinkLabel.Enabled      = !editing;
                okButton.Enabled                = !editing;
                formulaLinkLabel.Text           = editing ? "done" : "edit";
                var(statusText, statusSeverity) = status;
                UIUtils.SetLinkContents(formulaStatusLinkLabel, statusText);
                formulaLinkLabel.Enabled         = statusSeverity != MessageSeverity.Error;
                formulaStatusLinkLabel.ForeColor = getLinkColor(statusSeverity);
            }
                );
            var formulaFocusSideEffect = Updaters.Create(
                () => viewModel.IsEditingFormula,
                editing =>
            {
                if (editing && formulaTextBox.CanFocus)
                {
                    formulaTextBox.Focus();
                }
            }
                );
            var updateSuggestions = Updaters.Create(
                () => viewModel.FormulaSuggesions,
                value =>
            {
                var(list, selectedItem)  = value;
                suggestionsPanel.Visible = !list.IsEmpty;
                suggestionsPanel.Controls.Clear();
                suggestionsPanel.Controls.AddRange(list.Select((str, idx) =>
                {
                    var lbl = new Label()
                    {
                        Text      = str,
                        AutoSize  = true,
                        Left      = 4,
                        Top       = 2 + idx * (formulaLinkLabel.Height + 3),
                        ForeColor = idx == selectedItem ? SystemColors.HighlightText : SystemColors.ControlText,
                        BackColor = idx == selectedItem ? SystemColors.Highlight : suggestionsPanel.BackColor
                    };
                    lbl.MouseDown += (s, e) => viewModel.OnSuggestionClicked(idx);
                    return(lbl);
                }).ToArray());
                if (selectedItem != null)
                {
                    suggestionsPanel.ScrollControlIntoView(suggestionsPanel.Controls[selectedItem.Value]);
                }
            }
                );
            var listStatusUpdater = Updaters.Create(
                () => viewModel.TagsListStatus,
                (status) =>
            {
                var(statusText, statusSeverity) = status;
                UIUtils.SetLinkContents(tagsStatusLinkLabel, statusText);
                tagsStatusLinkLabel.ForeColor = getLinkColor(statusSeverity);
            }
                );

            subscription = changeNotification.CreateSubscription(() =>
            {
                listUpdater();
                formulaUpdater();
                formulaFocusSideEffect();
                updateSuggestions();
                listStatusUpdater();
            });

            detectFormulaCursorPositionChange = Updaters.Create(
                () => formulaTextBox.SelectionStart,
                _ => changeNotification.Post()
                );

            checkedListBox1.ItemCheck += (sender, e) =>
            {
                if (checkedListBox1.Tag != null)
                {
                    return;
                }
                var tag = (string)checkedListBox1.Items[e.Index];
                if (e.NewValue == CheckState.Checked)
                {
                    viewModel.OnUseTagClicked(tag);
                }
                else
                {
                    viewModel.OnUnuseTagClicked(tag);
                }
            };
            cancelButton.Click += (sender, e) => viewModel.OnCancelDialog();
            okButton.Click     += (sender, e) => viewModel.OnConfirmDialog();
        }