File: menu_property_list.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (153 lines) | stat: -rw-r--r-- 5,216 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dbus/menu/menu_property_list.h"

#include <string>
#include <utility>

#include "base/containers/contains.h"
#include "base/memory/ref_counted_memory.h"
#include "base/notimplemented.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/menu_label_accelerator_util_linux.h"
#include "ui/base/models/image_model.h"
#include "ui/base/models/menu_model.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/gfx/image/image.h"

namespace {

std::string ToDBusKeySym(ui::KeyboardCode code) {
  const uint16_t c =
      DomCodeToUsLayoutCharacter(UsLayoutKeyboardCodeToDomCode(code), 0);
  if (!c) {
    return std::string();
  }
  return base::UTF16ToUTF8(std::u16string(1, c));
}

std::vector<DbusString> GetDbusMenuShortcut(ui::Accelerator accelerator) {
  auto dbus_key_sym = ToDBusKeySym(accelerator.key_code());
  if (dbus_key_sym.empty()) {
    return {};
  }

  std::vector<DbusString> parts;
  if (accelerator.IsCtrlDown()) {
    parts.emplace_back("Control");
  }
  if (accelerator.IsAltDown()) {
    parts.emplace_back("Alt");
  }
  if (accelerator.IsShiftDown()) {
    parts.emplace_back("Shift");
  }
  if (accelerator.IsCmdDown()) {
    parts.emplace_back("Super");
  }
  parts.emplace_back(dbus_key_sym);
  return parts;
}

}  // namespace

MenuItemProperties ComputeMenuPropertiesForMenuItem(ui::MenuModel* menu,
                                                    size_t i) {
  // Properties should only be set if they differ from the default values.
  MenuItemProperties properties;

  // The dbusmenu interface has no concept of a "sublabel", "minor text", or
  // "minor icon" like MenuModel has.  Ignore these rather than trying to
  // merge them with the regular label and icon.
  std::u16string label = menu->GetLabelAt(i);
  if (!label.empty()) {
    properties["label"] = MakeDbusVariant(DbusString(
        ui::ConvertAcceleratorsFromWindowsStyle(base::UTF16ToUTF8(label))));
  }

  if (!menu->IsEnabledAt(i)) {
    properties["enabled"] = MakeDbusVariant(DbusBoolean(false));
  }
  if (!menu->IsVisibleAt(i)) {
    properties["visible"] = MakeDbusVariant(DbusBoolean(false));
  }

  ui::ImageModel icon = menu->GetIconAt(i);
  if (icon.IsImage()) {
    properties["icon-data"] =
        MakeDbusVariant(DbusByteArray(icon.GetImage().As1xPNGBytes()));
  }

  ui::Accelerator accelerator;
  if (menu->GetAcceleratorAt(i, &accelerator)) {
    auto parts = GetDbusMenuShortcut(accelerator);
    if (!parts.empty()) {
      properties["shortcut"] = MakeDbusVariant(
          MakeDbusArray(DbusArray<DbusString>(std::move(parts))));
    }
  }

  switch (menu->GetTypeAt(i)) {
    case ui::MenuModel::TYPE_COMMAND:
    case ui::MenuModel::TYPE_HIGHLIGHTED:
    case ui::MenuModel::TYPE_TITLE:
      // Nothing special to do.
      break;
    case ui::MenuModel::TYPE_CHECK:
    case ui::MenuModel::TYPE_RADIO:
      properties["toggle-type"] = MakeDbusVariant(DbusString(
          menu->GetTypeAt(i) == ui::MenuModel::TYPE_CHECK ? "checkmark"
                                                          : "radio"));
      properties["toggle-state"] =
          MakeDbusVariant(DbusInt32(menu->IsItemCheckedAt(i) ? 1 : 0));
      break;
    case ui::MenuModel::TYPE_SEPARATOR:
      // The dbusmenu interface doesn't have multiple types of separators like
      // MenuModel.  Just use a regular separator in all cases.
      properties["type"] = MakeDbusVariant(DbusString("separator"));
      break;
    case ui::MenuModel::TYPE_BUTTON_ITEM:
      // This type of menu represents a row of buttons, but the dbusmenu
      // interface has no equivalent of this.  Ignore these items for now
      // since there's currently no uses of it that plumb into this codepath.
      // If there are button menu items in the future, we'd have to fake them
      // with multiple menu items.
      NOTIMPLEMENTED();
      break;
    case ui::MenuModel::TYPE_SUBMENU:
    case ui::MenuModel::TYPE_ACTIONABLE_SUBMENU:
      properties["children-display"] = MakeDbusVariant(DbusString("submenu"));
      break;
  }

  return properties;
}

void ComputeMenuPropertyChanges(const MenuItemProperties& old_properties,
                                const MenuItemProperties& new_properties,
                                MenuPropertyList* item_updated_props,
                                MenuPropertyList* item_removed_props) {
  // Compute updated and removed properties.
  for (const auto& pair : old_properties) {
    const std::string& key = pair.first;
    auto new_it = new_properties.find(key);
    if (new_it != new_properties.end()) {
      if (new_it->second != pair.second) {
        item_updated_props->push_back(key);
      }
    } else {
      item_removed_props->push_back(key);
    }
  }
  // Compute added properties.
  for (const auto& pair : new_properties) {
    const std::string& key = pair.first;
    if (!base::Contains(old_properties, key)) {
      item_updated_props->push_back(key);
    }
  }
}