summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.timeline@1.2.13/Editor/Attributes/ShortcutAttribute.cs
blob: 756a09290ea74a9bf84ca08780bf921e7b8f30df (plain) (blame)
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
using System;
using System.Linq;
using UnityEditor.ShortcutManagement;
using UnityEngine;

namespace UnityEditor.Timeline
{
    [AttributeUsage(AttributeTargets.Class, Inherited = false)]
    class ShortcutAttribute : Attribute
    {
        readonly string m_Identifier;
        readonly string m_EventCommandName;
        readonly string m_MenuShortcut;

        public ShortcutAttribute(string identifier)
        {
            m_Identifier = identifier;
            m_EventCommandName = identifier;
        }

        public ShortcutAttribute(string identifier, string commandName)
        {
            m_Identifier = identifier;
            m_EventCommandName = commandName;
        }

        public ShortcutAttribute(KeyCode key, ShortcutModifiers modifiers = ShortcutModifiers.None)
        {
            m_MenuShortcut = new KeyCombination(key, modifiers).ToMenuShortcutString();
        }

        public string GetMenuShortcut()
        {
            if (m_MenuShortcut != null)
                return m_MenuShortcut;

            //find the mapped shortcut in the shortcut manager
            var shortcut = ShortcutIntegration.instance.directory.FindShortcutEntry(m_Identifier);
            if (shortcut != null && shortcut.combinations.Any())
            {
                return KeyCombination.SequenceToMenuString(shortcut.combinations);
            }

            return string.Empty;
        }

        public bool MatchesEvent(Event evt)
        {
            if (evt.type != EventType.ExecuteCommand)
                return false;
            return evt.commandName == m_EventCommandName;
        }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    class ShortcutPlatformOverrideAttribute : ShortcutAttribute
    {
        RuntimePlatform platform { get; }

        public ShortcutPlatformOverrideAttribute(RuntimePlatform platform, KeyCode key, ShortcutModifiers modifiers = ShortcutModifiers.None)
            : base(key, modifiers)
        {
            this.platform = platform;
        }

        public bool MatchesCurrentPlatform()
        {
            return Application.platform == platform;
        }
    }
}