CHANGES: update v4.4.2 release notes draft for #1368
[git-cola.git] / cola / widgets / common.py
blobb103a51ba903b69e5a59602f817f3181236f8efe
1 import functools
3 from ..i18n import N_
4 from .. import cmds
5 from .. import hotkeys
6 from .. import icons
7 from .. import qtutils
8 from .. import utils
11 def cmd_action(parent, cmd, context, func, *keys):
12 """Wrap a standard Command object in a QAction
14 This function assumes that :func:`func()` takes no arguments,
15 that `cmd` has a :func:`name()` method, and that the `cmd`
16 constructor takes a single argument, as returned by `func`.
18 """
19 return qtutils.add_action(
20 parent, cmd.name(), lambda: cmds.do(cmd, context, func()), *keys
24 def default_app_action(context, parent, func):
25 """Open paths with the OS-default app -> QAction"""
26 action = cmd_action(
27 parent, cmds.OpenDefaultApp, context, func, hotkeys.PRIMARY_ACTION
29 action.setIcon(icons.default_app())
30 return action
33 def edit_action(context, parent, *keys):
34 """Launch an editor -> QAction"""
35 action = qtutils.add_action_with_tooltip(
36 parent,
37 cmds.LaunchEditor.name(),
38 N_('Edit selected paths'),
39 cmds.run(cmds.LaunchEditor, context),
40 hotkeys.EDIT,
41 *keys
43 action.setIcon(icons.edit())
44 return action
47 def parent_dir_action(context, parent, func):
48 """Open the parent directory of paths -> QAction"""
49 hotkey = hotkeys.SECONDARY_ACTION
50 action = cmd_action(parent, cmds.OpenParentDir, context, func, hotkey)
51 action.setIcon(icons.folder())
52 return action
55 def worktree_dir_action(context, parent, *keys):
56 """Open the repository worktree -> QAction"""
57 # lambda: None is a no-op.
58 action = cmd_action(parent, cmds.OpenWorktree, context, lambda: None, *keys)
59 action.setIcon(icons.folder())
60 return action
63 def refresh_action(context, parent):
64 """Refresh the repository state -> QAction"""
65 return qtutils.add_action(
66 parent, cmds.Refresh.name(), cmds.run(cmds.Refresh, context), hotkeys.REFRESH
70 def terminal_action(context, parent, func=None, hotkey=None):
71 """Launch a terminal -> QAction"""
72 action = None
73 if cmds.LaunchTerminal.is_available(context):
74 if func is None:
75 func = functools.partial(lambda: None)
76 action = cmd_action(
77 parent,
78 cmds.LaunchTerminal,
79 context,
80 lambda: utils.select_directory(func()),
82 action.setIcon(icons.terminal())
83 if hotkey is not None:
84 action.setShortcut(hotkey)
85 return action