Пример #1
0
def get_action_for_key(action_contexts: typing.Sequence[str], key: UserInterface.Key) -> typing.Optional[Action]:
    for action_context in reversed(action_contexts):
        for action_id, action_shortcut_d in action_shortcuts.items():
            for key_action_context, key_sequence_str in action_shortcut_d.items():
                if action_context == key_action_context:
                    if isinstance(key_sequence_str, list):
                        key_sequences = [UserInterface.KeySequence(kss) for kss in key_sequence_str]
                    else:
                        key_sequences = [UserInterface.KeySequence(key_sequence_str)]
                    for key_sequence in key_sequences:
                        if key_sequence.matches(key) == UserInterface.KeySequenceMatch.EXACT:
                            return actions[action_id]
    return None
Пример #2
0
 def decode_font_metrics(self, font_metrics):
     from nion.ui import UserInterface
     return UserInterface.FontMetrics(width=font_metrics[0],
                                      height=font_metrics[1],
                                      ascent=font_metrics[2],
                                      descent=font_metrics[3],
                                      leading=font_metrics[4])
Пример #3
0
    def add_action_to_menu(
            self, menu: UserInterface.Menu, action_id: str,
            action_context: ActionContext) -> typing.Optional[Action]:
        action = actions.get(action_id)
        if action:
            key_sequence = action_shortcuts.get(action_id,
                                                dict()).get("window")
            assert menu is not None

            def perform_action() -> None:
                self.perform_action_in_context(action_id, action_context)

            def queue_perform_action() -> None:
                # delay execution to ensure menu closes properly
                # this would manifest itself by export dialog crashes in nionswift.
                self.queue_task(perform_action)

            menu_action = menu.add_menu_item(action.action_name,
                                             queue_perform_action,
                                             key_sequence=key_sequence,
                                             action_id=action_id)
            title = action.get_action_name(action_context)
            enabled = action and action.is_enabled(action_context)
            checked = action and action.is_checked(action_context)
            menu_action.apply_state(
                UserInterface.MenuItemState(title=title,
                                            enabled=enabled,
                                            checked=checked))
        return action
Пример #4
0
 def _apply_menu_state(self, action_id: str, action_context: ActionContext) -> None:
     menu_action = self.__document_window.get_menu_action(action_id)
     if menu_action and menu_action.action_id:
         action = actions.get(menu_action.action_id)
         if action:
             title = action.get_action_name(action_context)
             enabled = action and action.is_enabled(action_context)
             checked = action and action.is_checked(action_context)
             menu_action.apply_state(UserInterface.MenuItemState(title=title, enabled=enabled, checked=checked))
Пример #5
0
 def _get_menu_item_state(self, command_id: str) -> typing.Optional[UserInterface.MenuItemState]:
     # if there is a specific menu item state for the command_id, use it
     # otherwise, if the handle method exists, return an enabled menu item
     # otherwise, don't handle
     handle_method = "handle_" + command_id
     menu_item_state_method = "get_" + command_id + "_menu_item_state"
     if hasattr(self, menu_item_state_method):
         menu_item_state = getattr(self, menu_item_state_method)()
         if menu_item_state:
             return typing.cast(UserInterface.MenuItemState, menu_item_state)
     if hasattr(self, handle_method):
         return UserInterface.MenuItemState(title=None, enabled=True, checked=False)
     return None
Пример #6
0
 def _menu_about_to_show(self, menu: UserInterface.Menu) -> None:
     if self.app and self.app._menu_about_to_show(self, menu):
         pass
     elif menu.menu_id == "file":
         self._file_menu_about_to_show()
     elif menu.menu_id == "edit":
         self._edit_menu_about_to_show()
     elif menu.menu_id == "window":
         self._window_menu_about_to_show()
     # perform enable/disable/title for all menus
     action_context = self._get_action_context()
     for menu_action in menu.get_menu_actions():
         if menu_action.action_id:
             action = actions.get(menu_action.action_id)
             if action:
                 title = action.get_action_name(action_context)
                 enabled = action and action.is_enabled(action_context)
                 checked = action and action.is_checked(action_context)
                 menu_action.apply_state(UserInterface.MenuItemState(title=title, enabled=enabled, checked=checked))
Пример #7
0
 def test_get_font_metrics_sanity_check(self) -> None:
     # Test that TestUI.UserInterface.get_font_metrics returns a reasonable size
     # This test will need to be updated if 'make_font_metrics_for_tests' is modified
     self.assertEqual(
         self.ui.get_font_metrics("ignored", "This is a string"),
         UserInterfaceModule.FontMetrics(77, 13, 11, 2, 0))