File: toggle_view_visibility_action.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (107 lines) | stat: -rw-r--r-- 3,230 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" An action that toggles a view's visibility (ie. hides/shows it). """


from pyface.workbench.api import IView
from traits.api import Delegate, Instance


from .workbench_action import WorkbenchAction


class ToggleViewVisibilityAction(WorkbenchAction):
    """ An action that toggles a view's visibility (ie. hides/shows it). """

    # 'Action' interface ---------------------------------------------------

    # The action's unique identifier (may be None).
    id = Delegate("view", modify=True)

    # The action's name (displayed on menus/tool bar tools etc).
    name = Delegate("view", modify=True)

    # The action's style.
    style = "toggle"

    # 'ViewAction' interface -----------------------------------------------

    # The view that we toggle the visibility for.
    view = Instance(IView)

    # ------------------------------------------------------------------------
    # 'Action' interface.
    # ------------------------------------------------------------------------

    def destroy(self):
        """ Called when the action is no longer required. """

        if self.view is not None:
            self._remove_view_listeners(self.view)

    def perform(self, event):
        """ Perform the action. """

        self._toggle_view_visibility(self.view)

        return

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    # Trait change handlers ------------------------------------------------

    def _view_changed(self, old, new):
        """ Static trait change handler. """

        if old is not None:
            self._remove_view_listeners(old)

        if new is not None:
            self._add_view_listeners(new)

        self._refresh_checked()

        return

    # Methods -------------------------------------------------------------#

    def _add_view_listeners(self, view):
        """ Add listeners for trait events on a view. """

        view.observe(self._refresh_checked, "visible")
        view.observe(self._refresh_checked, "window")

    def _remove_view_listeners(self, view):
        """ Add listeners for trait events on a view. """

        view.observe(self._refresh_checked, "visible", remove=True)
        view.observe(self._refresh_checked, "window", remove=True)

    def _refresh_checked(self, event=None):
        """ Refresh the checked state of the action. """

        self.checked = (
            self.view is not None
            and self.view.window is not None
            and self.view.visible
        )

    def _toggle_view_visibility(self, view):
        """ Toggle the visibility of a view. """

        if view.visible:
            view.hide()

        else:
            view.show()

        return