"Fossies" - the Fresh Open Source Software Archive

Member "electron-4.2.12/atom/browser/ui/accelerator_util.cc" (16 Oct 2019, 3114 Bytes) of package /linux/www/electron-4.2.12.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "accelerator_util.cc" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 6.1.0_vs_7.0.0.

    1 // Copyright (c) 2013 GitHub, Inc.
    2 // Use of this source code is governed by the MIT license that can be
    3 // found in the LICENSE file.
    4 
    5 #include "atom/browser/ui/accelerator_util.h"
    6 
    7 #include <stdio.h>
    8 
    9 #include <string>
   10 #include <vector>
   11 
   12 #include "atom/common/keyboard_util.h"
   13 #include "base/stl_util.h"
   14 #include "base/strings/string_number_conversions.h"
   15 #include "base/strings/string_split.h"
   16 #include "base/strings/string_util.h"
   17 
   18 namespace accelerator_util {
   19 
   20 bool StringToAccelerator(const std::string& shortcut,
   21                          ui::Accelerator* accelerator) {
   22   if (!base::IsStringASCII(shortcut)) {
   23     LOG(ERROR) << "The accelerator string can only contain ASCII characters";
   24     return false;
   25   }
   26 
   27   std::vector<std::string> tokens = base::SplitString(
   28       shortcut, "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
   29 
   30   // Now, parse it into an accelerator.
   31   int modifiers = ui::EF_NONE;
   32   ui::KeyboardCode key = ui::VKEY_UNKNOWN;
   33   for (const auto& token : tokens) {
   34     bool shifted = false;
   35     ui::KeyboardCode code = atom::KeyboardCodeFromStr(token, &shifted);
   36     if (shifted)
   37       modifiers |= ui::EF_SHIFT_DOWN;
   38     switch (code) {
   39       // The token can be a modifier.
   40       case ui::VKEY_SHIFT:
   41         modifiers |= ui::EF_SHIFT_DOWN;
   42         break;
   43       case ui::VKEY_CONTROL:
   44         modifiers |= ui::EF_CONTROL_DOWN;
   45         break;
   46       case ui::VKEY_MENU:
   47         modifiers |= ui::EF_ALT_DOWN;
   48         break;
   49       case ui::VKEY_COMMAND:
   50         modifiers |= ui::EF_COMMAND_DOWN;
   51         break;
   52       case ui::VKEY_ALTGR:
   53         modifiers |= ui::EF_ALTGR_DOWN;
   54         break;
   55       // Or it is a normal key.
   56       default:
   57         key = code;
   58     }
   59   }
   60 
   61   if (key == ui::VKEY_UNKNOWN) {
   62     LOG(WARNING) << shortcut << " doesn't contain a valid key";
   63     return false;
   64   }
   65 
   66   *accelerator = ui::Accelerator(key, modifiers);
   67   return true;
   68 }
   69 
   70 void GenerateAcceleratorTable(AcceleratorTable* table,
   71                               atom::AtomMenuModel* model) {
   72   int count = model->GetItemCount();
   73   for (int i = 0; i < count; ++i) {
   74     atom::AtomMenuModel::ItemType type = model->GetTypeAt(i);
   75     if (type == atom::AtomMenuModel::TYPE_SUBMENU) {
   76       auto* submodel = model->GetSubmenuModelAt(i);
   77       GenerateAcceleratorTable(table, submodel);
   78     } else {
   79       ui::Accelerator accelerator;
   80       if (model->ShouldRegisterAcceleratorAt(i)) {
   81         if (model->GetAcceleratorAtWithParams(i, true, &accelerator)) {
   82           MenuItem item = {i, model};
   83           (*table)[accelerator] = item;
   84         }
   85       }
   86     }
   87   }
   88 }
   89 
   90 bool TriggerAcceleratorTableCommand(AcceleratorTable* table,
   91                                     const ui::Accelerator& accelerator) {
   92   if (base::ContainsKey(*table, accelerator)) {
   93     const accelerator_util::MenuItem& item = (*table)[accelerator];
   94     if (item.model->IsEnabledAt(item.position)) {
   95       const auto event_flags =
   96           accelerator.MaskOutKeyEventFlags(accelerator.modifiers());
   97       item.model->ActivatedAt(item.position, event_flags);
   98       return true;
   99     }
  100   }
  101   return false;
  102 }
  103 
  104 }  // namespace accelerator_util