summaryrefslogtreecommitdiffstats
path: root/src/core/autofill_popup_controller.cpp
blob: 260b1ca8a2951cd9b3612cd7b87b2d2fe5ddecfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "autofill_popup_controller.h"
#include "autofill_popup_controller_p.h"

#include "components/autofill/core/browser/ui/autofill_popup_delegate.h"
#include "components/autofill/core/browser/ui/suggestion.h"

namespace QtWebEngineCore {

AutofillPopupController::AutofillPopupController(AutofillPopupControllerPrivate *dd)
{
    Q_ASSERT(dd);
    d.reset(dd);
}

AutofillPopupController::~AutofillPopupController() { }

void AutofillPopupController::setCurrentIndex(const QModelIndex &index)
{
    if (m_currentIndex == index)
        return;

    m_currentIndex = index;

    if (m_currentIndex.isValid()) {
        const autofill::Suggestion &suggestion = d->suggestions[m_currentIndex.row()];
        d->delegate->DidSelectSuggestion(suggestion.value, suggestion.frontend_id, suggestion.backend_id);
    }

    Q_EMIT currentIndexChanged(index);
}

void AutofillPopupController::selectPreviousSuggestion()
{
    if (!m_currentIndex.isValid()) {
        setCurrentIndex(m_model.index(m_model.rowCount() - 1, 0));
        return;
    }

    if (m_currentIndex.row() == 0) {
        selectLastSuggestion();
        return;
    }

    setCurrentIndex(m_model.index(m_currentIndex.row() - 1, 0));
}

void AutofillPopupController::selectNextSuggestion()
{
    if (!m_currentIndex.isValid()) {
        setCurrentIndex(m_model.index(0, 0));
        return;
    }

    if (m_currentIndex.row() == m_model.rowCount() - 1) {
        selectFirstSuggestion();
        return;
    }

    setCurrentIndex(m_model.index(m_currentIndex.row() + 1, 0));
}

void AutofillPopupController::selectFirstSuggestion()
{
    setCurrentIndex(m_model.index(0, 0));
}

void AutofillPopupController::selectLastSuggestion()
{
    setCurrentIndex(m_model.index(m_model.rowCount() - 1, 0));
}

void AutofillPopupController::acceptSuggestion()
{
    if (!m_currentIndex.isValid())
        return;

    const int index = m_currentIndex.row();
    const autofill::Suggestion &suggestion = d->suggestions[index];
    d->delegate->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id,
                                     suggestion.backend_id, index);
}

void AutofillPopupController::notifyPopupShown()
{
    d->delegate->OnPopupShown();
}

void AutofillPopupController::notifyPopupHidden()
{
    d->delegate->OnPopupHidden();
}

void AutofillPopupController::selectSuggestion(int index)
{
    if (index < 0)
        setCurrentIndex(QModelIndex());
    else
        setCurrentIndex(m_model.index(index, 0));
}

void AutofillPopupController::updateModel()
{
    QStringList values;
    for (size_t i = 0; i < d->suggestions.size(); ++i) {
        values.append(QString::fromStdU16String(d->suggestions[i].value));
    }
    m_model.setStringList(values);
    setCurrentIndex(QModelIndex());
}

} // namespace QtWebEngineCore