Commit 8b3a691a authored by Harish Fulara's avatar Harish Fulara

shortcuts: Make newly added shortcuts available

Closes #2211
parent 455114f0
Pipeline #16545 passed with stage
in 47 minutes and 47 seconds
......@@ -43,7 +43,7 @@ class ShortcutsManager(GObject.Object): # pylint: disable=too-many-instance-att
self.titles = {}
self.config_path = os.path.sep.join([xdg_config_home(),
"shortcuts.conf"])
self.__loaded = self.__load()
self.__loaded_actions = list(self.__load())
@property
def groups(self):
......@@ -53,17 +53,17 @@ class ShortcutsManager(GObject.Object): # pylint: disable=too-many-instance-att
def __load(self):
"""Loads the shortcuts from the config file and sets them.
Returns:
bool: Whether the config file exists.
Yields:
string: The shortcuts loaded from the config file.
"""
if not os.path.isfile(self.config_path):
return False
return
for line in open(self.config_path, "r"):
action_name, accelerators = line.split(":", 1)
accelerators = accelerators.strip("\n").split(",")
self.app.set_accels_for_action(action_name, accelerators)
return True
yield action_name
def save(self):
"""Saves the accelerators for each action to the config file.
......@@ -92,7 +92,7 @@ class ShortcutsManager(GObject.Object): # pylint: disable=too-many-instance-att
"""
self.default_accelerators[action] = accelerators
self.titles[action] = title
if not self.__loaded:
if action not in self.__loaded_actions:
self.app.set_accels_for_action(action, accelerators)
if title:
......@@ -163,6 +163,7 @@ class ShortcutsManager(GObject.Object): # pylint: disable=too-many-instance-att
self.__groups.append((position, action_prefix))
self.__groups.sort()
# pylint: disable=redefined-argument-from-local
def reset_accels(self, action=None):
"""Resets accelerators to their default values.
......
......@@ -17,7 +17,6 @@
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Test the keyboard shortcuts."""
import os
import tempfile
from unittest import mock
......@@ -71,17 +70,21 @@ class TestShortcutsManager(common.TestCase):
manager.register_group("general", "General group", position=0)
manager.add("prefix.action1", ["<Primary>P"], "Action one")
self.assertEqual(app.set_accels_for_action.call_count, 1)
# Save the shortcut to the config file.
manager.save()
app.set_accels_for_action.reset_mock()
# Create the temporary shortcuts.conf file
# and test that add is not calling set_accels_for_action()
open(os.path.sep.join([temp_dir, "shortcuts.conf"]), "w").close()
manager2 = ShortcutsManager(app)
# Previously saved shortcut is read from the config file
# and 'set_accels_for_action' is called.
self.assertEqual(app.set_accels_for_action.call_count, 1)
manager2.register_group("other", "Other group", position=0)
manager2.add("prefix.action4", ["<Primary>W"],
"Action addition not invoking set_accels_for_action")
# No. of calls to set_accels_for_action should be unchanged now
# because calling set_accels_for_action isn't allowed with .conf existing
app.set_accels_for_action.reset_mock()
manager2.add("prefix.action1", ["<Primary>P"], "Action one")
# Above shortcut has been already loaded from the config file
# and hence 'set_accels_for_action' is not called.
self.assertEqual(app.set_accels_for_action.call_count, 0)
manager2.add("prefix.action2", ["<Primary>W"], "Action two")
self.assertEqual(app.set_accels_for_action.call_count, 1)
def test_load_save(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment