File: command.h

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 (104 lines) | stat: -rw-r--r-- 4,036 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_ACCELERATORS_COMMAND_H_
#define UI_BASE_ACCELERATORS_COMMAND_H_

#include <map>
#include <string>
#include <string_view>

#include "base/functional/callback_forward.h"
#include "base/values.h"
#include "ui/base/accelerators/accelerator.h"

namespace ui {

// Denotes the reason why parsing a string to a valid global accelerator fails.
enum class AcceleratorParseError {
  // The platform key used must be for a known platform.
  kUnsupportedPlatform,
  // The string sent to the parser cannot be parsed into a valid global
  // accelerator.
  kMalformedInput,
  // Media keys may not have modifiers.
  kMediaKeyWithModifier
};

class COMPONENT_EXPORT(UI_BASE) Command {
 public:
  using AcceleratorParseErrorCallback =
      base::OnceCallback<void(ui::AcceleratorParseError)>;

  Command() = default;
  Command(std::string_view command_name,
          std::u16string_view description,
          bool global);
  Command(const Command& other) = default;
  virtual ~Command() = default;

  // The platform value for the Command.
  static std::string CommandPlatform();

  // Parse a string as an accelerator. If the accelerator is unparsable then
  // a generic ui::Accelerator object will be returns (with key_code Unknown).
  static ui::Accelerator StringToAccelerator(std::string_view accelerator);

  // Returns the string representation of an accelerator without localizing the
  // shortcut text (like accelerator::GetShortcutText() does).
  static std::string AcceleratorToString(const ui::Accelerator& accelerator);

  // Accessors:
  const std::string& command_name() const { return command_name_; }
  const ui::Accelerator& accelerator() const { return accelerator_; }
  const std::u16string& description() const { return description_; }
  bool global() const { return global_; }

  // Setter:
  void set_command_name(std::string_view command_name) {
    command_name_ = command_name;
  }
  void set_accelerator(const ui::Accelerator& accelerator) {
    accelerator_ = accelerator;
  }
  void set_description(std::u16string_view description) {
    description_ = description;
  }
  void set_global(bool global) { global_ = global; }

 protected:
  // Parse an |accelerator| for a given platform (specified by |platform_key|)
  // and return the result as a ui::Accelerator if successful, or VKEY_UNKNOWN
  // if not. |should_parse_media_keys| specifies whether media keys are to be
  // considered for parsing. |error_callback| is called when there is an issue
  // with parsing |accelerator| and the reason why parsing failed is passed in.
  // |allow_ctrl_alt| parses the accelerator even if the accelerator contains
  // the control and alt (command and option) key combination. Usually this is
  // not preferred because that key combination produces a special key in
  // certain languages. However, this restriction may be loosened if the user
  // is aware and explicitly binds this key combination. See article for more
  // information:
  // https://devblogs.microsoft.com/oldnewthing/20040329-00/?p=40003
  // Note: If the parsing rules here are changed, make sure to
  // update the corresponding shortcut_input.ts validation, which validates the
  // user input for the CrShortcutInput WebUI component.
  static ui::Accelerator ParseImpl(std::string_view accelerator,
                                   std::string_view platform_key,
                                   bool should_parse_media_keys,
                                   AcceleratorParseErrorCallback error_callback,
                                   bool allow_ctrl_alt = false);

 private:
  std::string command_name_;
  ui::Accelerator accelerator_;
  std::u16string description_;
  bool global_ = false;
};

// A mapping of command name (std::string) to a command object.
using CommandMap = std::map<std::string, Command>;

}  // namespace ui

#endif  // UI_BASE_ACCELERATORS_COMMAND_H_