mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:37:35 +00:00
LibGUI: Allow Shortcuts to have a mouse button associated with them
A Shortcut can now be either have a keyboard key, or a mouse button, along with any modifiers. Decided to add an extra type field instead of subclassing, which means callers will have to be a little careful before accessing a particular input method's "key", but it keeps things simple for now.
This commit is contained in:
parent
8b9b836a0e
commit
a252c3e058
2 changed files with 62 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Geordie Hall <me@geordiehall.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -24,10 +25,17 @@ String Shortcut::to_string() const
|
||||||
if (m_modifiers & Mod_Super)
|
if (m_modifiers & Mod_Super)
|
||||||
parts.append("Super");
|
parts.append("Super");
|
||||||
|
|
||||||
if (auto* key_name = key_code_to_string(m_key))
|
if (m_type == Type::Keyboard) {
|
||||||
parts.append(key_name);
|
if (auto* key_name = key_code_to_string(m_keyboard_key))
|
||||||
else
|
parts.append(key_name);
|
||||||
parts.append("(Invalid)");
|
else
|
||||||
|
parts.append("(Invalid)");
|
||||||
|
} else {
|
||||||
|
if (m_mouse_button != MouseButton::None)
|
||||||
|
parts.append(String::formatted("Mouse {}", mouse_button_to_string(m_mouse_button)));
|
||||||
|
else
|
||||||
|
parts.append("(Invalid)");
|
||||||
|
}
|
||||||
|
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.join('+', parts);
|
builder.join('+', parts);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Geordie Hall <me@geordiehall.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -8,6 +9,7 @@
|
||||||
|
|
||||||
#include <AK/Traits.h>
|
#include <AK/Traits.h>
|
||||||
#include <Kernel/API/KeyCode.h>
|
#include <Kernel/API/KeyCode.h>
|
||||||
|
#include <LibGUI/Event.h>
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
|
@ -15,30 +17,62 @@ class Shortcut {
|
||||||
public:
|
public:
|
||||||
Shortcut() = default;
|
Shortcut() = default;
|
||||||
Shortcut(u8 modifiers, KeyCode key)
|
Shortcut(u8 modifiers, KeyCode key)
|
||||||
: m_modifiers(modifiers)
|
: m_type(Type::Keyboard)
|
||||||
, m_key(key)
|
, m_modifiers(modifiers)
|
||||||
|
, m_keyboard_key(key)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
Shortcut(KeyCode key)
|
Shortcut(KeyCode key)
|
||||||
: m_modifiers(0)
|
: m_type(Type::Keyboard)
|
||||||
, m_key(key)
|
, m_modifiers(0)
|
||||||
|
, m_keyboard_key(key)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Shortcut(u8 modifiers, MouseButton button)
|
||||||
|
: m_type(Type::Mouse)
|
||||||
|
, m_modifiers(modifiers)
|
||||||
|
, m_mouse_button(button)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Shortcut(MouseButton button)
|
||||||
|
: m_type(Type::Mouse)
|
||||||
|
, m_modifiers(0)
|
||||||
|
, m_mouse_button(button)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_valid() const { return m_key != KeyCode::Key_Invalid; }
|
enum class Type : u8 {
|
||||||
u8 modifiers() const { return m_modifiers; }
|
Keyboard,
|
||||||
KeyCode key() const { return m_key; }
|
Mouse,
|
||||||
|
};
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
|
Type type() const { return m_type; }
|
||||||
|
bool is_valid() const { return m_type == Type::Keyboard ? (m_keyboard_key != Key_Invalid) : (m_mouse_button != MouseButton::None); }
|
||||||
|
u8 modifiers() const { return m_modifiers; }
|
||||||
|
|
||||||
|
KeyCode key() const
|
||||||
|
{
|
||||||
|
VERIFY(m_type == Type::Keyboard);
|
||||||
|
return m_keyboard_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseButton mouse_button() const
|
||||||
|
{
|
||||||
|
VERIFY(m_type == Type::Mouse);
|
||||||
|
return m_mouse_button;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator==(Shortcut const& other) const
|
bool operator==(Shortcut const& other) const
|
||||||
{
|
{
|
||||||
return m_modifiers == other.m_modifiers
|
return m_modifiers == other.m_modifiers && m_type == other.m_type && m_keyboard_key == other.m_keyboard_key && m_mouse_button == other.m_mouse_button;
|
||||||
&& m_key == other.m_key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Type m_type { Type::Keyboard };
|
||||||
u8 m_modifiers { 0 };
|
u8 m_modifiers { 0 };
|
||||||
KeyCode m_key { KeyCode::Key_Invalid };
|
KeyCode m_keyboard_key { KeyCode::Key_Invalid };
|
||||||
|
MouseButton m_mouse_button { MouseButton::None };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -49,7 +83,12 @@ template<>
|
||||||
struct Traits<GUI::Shortcut> : public GenericTraits<GUI::Shortcut> {
|
struct Traits<GUI::Shortcut> : public GenericTraits<GUI::Shortcut> {
|
||||||
static unsigned hash(const GUI::Shortcut& shortcut)
|
static unsigned hash(const GUI::Shortcut& shortcut)
|
||||||
{
|
{
|
||||||
return pair_int_hash(shortcut.modifiers(), shortcut.key());
|
auto base_hash = pair_int_hash(shortcut.modifiers(), (u32)shortcut.type());
|
||||||
|
if (shortcut.type() == GUI::Shortcut::Type::Keyboard) {
|
||||||
|
return pair_int_hash(base_hash, shortcut.key());
|
||||||
|
} else {
|
||||||
|
return pair_int_hash(base_hash, shortcut.mouse_button());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue