summaryrefslogtreecommitdiff
path: root/chromium/ui/keyboard/resources/elements/kb-shift-key.html
blob: 686a59c5125914fc96be4cf560976ffd5ec128a7 (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
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
<!--
  -- Copyright 2013 The Chromium Authors. All rights reserved.
  -- Use of this source code is governed by a BSD-style license that can be
  -- found in the LICENSE file.
  -->

<polymer-element name="kb-shift-key"
    attributes="lowerCaseKeysetId upperCaseKeysetId"
    class="shift dark" char="Shift" extends="kb-key">
  <script>
    (function () {

      /**
       * The possible states of the shift key.
       * Unlocked is the default state. Locked for capslocked, pressed is a
       * key-down and tapped for a key-down followed by an immediate key-up.
       * @const
       * @type {Enum}
       */
      var KEY_STATES = {
        PRESSED: "pressed", // Key-down on shift key.
        LOCKED: "locked", // Key is capslocked.
        UNLOCKED: "unlocked", // Default state.
        TAPPED: "tapped" // Key-down followed by key-up.
      };

      /**
       * Uses a closure to define one long press timer among all shift keys
       * regardless of the layout they are in.
       * @type {function}
       */
      var shiftLongPressTimer = undefined;

      /**
       * The current state of the shift key.
       * @type {Enum}
       */
      var state = KEY_STATES.UNLOCKED;

      Polymer('kb-shift-key', {
        /**
         * Defines how capslock effects keyset transition. We always transition
         * from the lowerCaseKeysetId to the upperCaseKeysetId if capslock is on.
         * @type {string}
         */
        lowerCaseKeysetId: 'lower',
        upperCaseKeysetId: 'upper',

        up: function(event) {
          if (state == KEY_STATES.PRESSED) {
            state = KEY_STATES.TAPPED;
            // TODO(rsadam@): Add chording logic here.
          }
          this.super();
        },

        down: function(event) {
          // First transition state so that populateDetails generates
          // correct data.
          switch (state) {
            case KEY_STATES.UNLOCKED:
              state = KEY_STATES.PRESSED;
              break;
            case KEY_STATES.TAPPED:
            case KEY_STATES.LOCKED:
              state = KEY_STATES.UNLOCKED;
              break;
            case KEY_STATES.PRESSED:
              // We pressed another shift key at the same time,
              // so ignore second press.
              break;
            default:
              console.error("Undefined shift key state: " + state);
              break;
          }
          // Trigger parent behaviour.
          this.super();
          this.fire('enable-sel');
          // Populate double click transition details.
          var detail = {};
          detail.char = this.char || this.textContent;
          detail.toKeyset = this.upperCaseKeysetId;
          detail.nextKeyset = undefined;
          detail.callback = this.onDoubleClick;
          this.fire('enable-dbl', detail);
        },

        generateLongPressTimer: function() {
          return this.asyncMethod(function() {
            var detail = this.populateDetails();
            if (state == KEY_STATES.LOCKED) {
              // We don't care about the longpress if we are already
              // capitalized.
              return;
            } else {
              state = KEY_STATES.LOCKED;
              detail.toKeyset = this.upperCaseKeysetId;
              detail.nextKeyset = undefined;
            }
            this.fire('key-longpress', detail);
          }, null, LONGPRESS_DELAY_MSEC);
        },

        /**
         * Callback function for when a double click is triggered.
         */
        onDoubleClick: function() {
          state = KEY_STATES.LOCKED;
        },

        /**
         * Notifies shift key that a non-control key was pressed down.
         * A control key is defined as one of shift, control or alt.
         */
        onNonControlKeyDown: function() {
           // TODO(rsadam@): add logic for chording here.
           switch (state) {
             case (KEY_STATES.TAPPED):
               state = KEY_STATES.UNLOCKED;
               break;
             case (KEY_STATES.PRESSED):
               // Enter chording mode. First disable longpress timer.
               clearTimeout(this.shiftLongPressTimer);
               break;
             default:
               break;
           }
         },

        /**
         * Callback function for when a space is pressed after punctuation.
         * @return {Object} The keyset transitions the keyboard should make.
         */
        onSpaceAfterPunctuation: function() {
           var detail = {};
           detail.toKeyset = this.upperCaseKeysetId;
           detail.nextKeyset = this.lowerCaseKeysetId;
           state = KEY_STATES.TAPPED;
           return detail;
        },

        populateDetails: function() {
          var detail = this.super();
          switch(state) {
            case(KEY_STATES.LOCKED):
              detail.toKeyset = this.upperCaseKeysetId;
              break;
            case(KEY_STATES.UNLOCKED):
              detail.toKeyset = this.lowerCaseKeysetId;
              break;
            case(KEY_STATES.PRESSED):
              detail.toKeyset = this.upperCaseKeysetId;
              break;
            case(KEY_STATES.TAPPED):
              detail.toKeyset = this.upperCaseKeysetId;
              detail.nextKeyset = this.lowerCaseKeysetId;
              break;
          }
          return detail;
        },

        /**
         *  Resets the shift key state.
         */
        reset: function() {
          state = KEY_STATES.UNLOCKED;
        },

        /**
         * Overrides longPressTimer for the shift key.
         */
        get longPressTimer() {
          return shiftLongPressTimer;
        },

        set longPressTimer(timer) {
          shiftLongPressTimer = timer;
        },

        get state() {
          return state;
        }
      });
    })();
  </script>
</polymer-element>