File: key_combo_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (223 lines) | stat: -rw-r--r-- 7,118 bytes parent folder | download | duplicates (6)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/capture_mode/key_combo_view.h"

#include <iterator>
#include <memory>
#include <string>
#include <vector>

#include "ash/accelerators/keyboard_code_util.h"
#include "ash/capture_mode/key_item_view.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"

namespace ash {

namespace {

constexpr auto kBetweenKeyItemSpace = 8;

// Returns the vector icons for keys that have icons on the keyboard.
const gfx::VectorIcon* GetVectorIconForDemoTools(ui::KeyboardCode key_code) {
  switch (key_code) {
    case ui::VKEY_COMMAND:
      return GetSearchOrLauncherVectorIcon();
    case ui::VKEY_ASSISTANT:
      return &kCaptureModeDemoToolsAssistantIcon;
    case ui::VKEY_SETTINGS:
      return &kCaptureModeDemoToolsMenuIcon;
    default:
      return GetVectorIconForKeyboardCode(key_code);
  }
}

std::unique_ptr<KeyItemView> CreateKeyItemView(ui::KeyboardCode key_code) {
  std::unique_ptr key_item_view = std::make_unique<KeyItemView>(key_code);
  const gfx::VectorIcon* vector_icon = GetVectorIconForDemoTools(key_code);
  if (vector_icon) {
    key_item_view->SetIcon(*vector_icon);
  } else {
    std::u16string key_item_string =
        GetStringForKeyboardCode(key_code, /*remap_positional_key=*/false);
    key_item_view->SetText(key_item_string);
  }
  return key_item_view;
}

ui::KeyboardCode DecodeModifier(int modifier) {
  switch (modifier) {
    case ui::EF_CONTROL_DOWN:
      return ui::VKEY_CONTROL;
    case ui::EF_ALT_DOWN:
      return ui::VKEY_MENU;
    case ui::EF_SHIFT_DOWN:
      return ui::VKEY_SHIFT;
    case ui::EF_COMMAND_DOWN:
      return ui::VKEY_COMMAND;
    default:
      return ui::VKEY_UNKNOWN;
  }
}

bool operator>(const ui::KeyboardCode lhs, const ui::KeyboardCode rhs) {
  auto digitize_modifier_key_code = [&](ui::KeyboardCode key_code) {
    switch (key_code) {
      case ui::VKEY_CONTROL:
        return 0;
      case ui::VKEY_MENU:
        return 1;
      case ui::VKEY_SHIFT:
        return 2;
      case ui::VKEY_COMMAND:
        return 3;
      default:
        return 1000;
    }
  };

  return digitize_modifier_key_code(lhs) > digitize_modifier_key_code(rhs);
}

}  // namespace

// -----------------------------------------------------------------------------
// ModifiersContainerView:

// The container view that hosts the modifiers key item views.
class ModifiersContainerView : public views::View {
  METADATA_HEADER(ModifiersContainerView, views::View)

 public:
  explicit ModifiersContainerView() {
    views::BoxLayout* layout_manager =
        SetLayoutManager(std::make_unique<views::BoxLayout>(
            views::BoxLayout::Orientation::kHorizontal, gfx::Insets::VH(0, 0),
            kBetweenKeyItemSpace));
    layout_manager->set_cross_axis_alignment(
        views::BoxLayout::CrossAxisAlignment::kCenter);
  }

  ModifiersContainerView(const ModifiersContainerView&) = delete;
  ModifiersContainerView& operator=(const ModifiersContainerView&) = delete;
  ~ModifiersContainerView() override = default;

  void RebuildModifiersContainerView(int new_modifiers) {
    // Use XOR to filter out the modifiers that changed, i.e. the 1s in the
    // `diff` bit fields that correspond to the modifiers that changed.
    const int diff = current_modifiers_ ^ new_modifiers;
    for (const auto modifier : {ui::EF_CONTROL_DOWN, ui::EF_ALT_DOWN,
                                ui::EF_SHIFT_DOWN, ui::EF_COMMAND_DOWN}) {
      if ((modifier & diff) == 0) {
        continue;
      }

      const ui::KeyboardCode key_code = DecodeModifier(modifier);

      // Use AND to decide whether we want to do adding or removal operation. If
      // this `modifier` is set in the `new_modifiers` that means it has been
      // recently pressed, which means a new `KeyItemView` needs to be added.
      // Otherwise, it means a key has been recently released, and we should
      // remove its reoccoresponding `KeyItemView`.
      if ((modifier & new_modifiers) != 0) {
        AddModifier(key_code);
      } else {
        RemoveModifier(key_code);
      }
    }

    current_modifiers_ = new_modifiers;
  }

 private:
  void RemoveModifier(ui::KeyboardCode key_code) {
    for (views::View* child : children()) {
      if (static_cast<KeyItemView*>(child)->key_code() == key_code) {
        RemoveChildViewT(child);
        return;
      }
    }
  }

  void AddModifier(ui::KeyboardCode key_code) {
    // We're trying to find the view whose `key_code()` is greater than the
    // given `key_code` so that we can insert a new `KeyItemView` at that index,
    // or at the end if no such view was found. This keeps the modifiers sorted
    // as `Ctrl < Alt < Shift < Search`.
    const auto iter =
        std::ranges::find_if(children(), [key_code](views::View* child) {
          return static_cast<KeyItemView*>(child)->key_code() > key_code;
        });

    AddChildViewAt(CreateKeyItemView(key_code),
                   std::distance(children().begin(), iter));
  }

  int current_modifiers_ = 0;
};

BEGIN_METADATA(ModifiersContainerView)
END_METADATA

// -----------------------------------------------------------------------------
// KeyComboView:

KeyComboView::KeyComboView() {
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
      kBetweenKeyItemSpace));
}

KeyComboView::~KeyComboView() = default;

void KeyComboView::RefreshView(int modifiers,
                               ui::KeyboardCode last_non_modifier_key) {
  if (modifiers_ != modifiers) {
    modifiers_ = modifiers;
    if (!modifiers_container_view_) {
      modifiers_container_view_ = AddChildViewAt(
          std::make_unique<ModifiersContainerView>(), /*index=*/0);
    }

    modifiers_container_view_->RebuildModifiersContainerView(modifiers_);
  }

  if (last_non_modifier_key != last_non_modifier_key_) {
    last_non_modifier_key_ = last_non_modifier_key;
    if (non_modifier_view_) {
      RemoveChildViewT(non_modifier_view_.get());
      non_modifier_view_ = nullptr;
    }

    if (last_non_modifier_key != ui::VKEY_UNKNOWN) {
      non_modifier_view_ =
          AddChildView(CreateKeyItemView(last_non_modifier_key_));

      // Ensure to trigger a relayout so that the newly added
      // `non_modifier_view_` will be visible.
      InvalidateLayout();
    }
  }
}

std::vector<ui::KeyboardCode> KeyComboView::GetModifierKeycodeVector() const {
  std::vector<ui::KeyboardCode> key_codes;
  std::ranges::for_each(
      modifiers_container_view_->children(), [&](views::View* view) {
        key_codes.push_back(static_cast<KeyItemView*>(view)->key_code());
      });
  return key_codes;
}

BEGIN_METADATA(KeyComboView)
END_METADATA

}  // namespace ash