Skip to content

Commit 76c201c

Browse files
feat: Add night mode and reorganize buttons (#359)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 29d7f9d commit 76c201c

File tree

14 files changed

+176
-39
lines changed

14 files changed

+176
-39
lines changed

.github/workflows/ci_cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111

1212
env:
1313
MAIN_PYTHON_VERSION: '3.13'
14-
RESET_IMAGE_CACHE: 0
14+
RESET_IMAGE_CACHE: 1
1515
PACKAGE_NAME: ansys-tools-visualization-interface
1616
DOCUMENTATION_CNAME: visualization-interface.tools.docs.pyansys.com
1717
IN_GITHUB_ACTIONS: true

doc/changelog.d/359.maintenance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Feat: Add night mode and reorganize buttons

src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import ansys.tools.visualization_interface
3434
from ansys.tools.visualization_interface.backends._base import BaseBackend
3535
from ansys.tools.visualization_interface.backends.pyvista.pyvista_interface import PyVistaInterface
36+
from ansys.tools.visualization_interface.backends.pyvista.widgets.dark_mode import DarkModeButton
3637
from ansys.tools.visualization_interface.backends.pyvista.widgets.displace_arrows import (
3738
CameraPanDirection,
3839
DisplacementArrow,
@@ -209,6 +210,7 @@ def enable_widgets(self, dark_mode: bool = False) -> None:
209210
self._widgets.append(MeshSliderWidget(self, dark_mode))
210211
self._widgets.append(HideButton(self, dark_mode))
211212
self._widgets.append(PickRotCenterButton(self, dark_mode))
213+
self._widgets.append(DarkModeButton(self, dark_mode))
212214

213215
def add_widget(self, widget: Union[PlotterWidget, List[PlotterWidget]]):
214216
"""Add one or more custom widgets to the plotter.
595 Bytes
Loading
495 Bytes
Loading
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Provides the dark mode button widget for the PyAnsys plotter."""
23+
from pathlib import Path
24+
from typing import TYPE_CHECKING
25+
26+
from vtk import vtkActor, vtkButtonWidget, vtkPNGReader
27+
28+
from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget
29+
30+
if TYPE_CHECKING:
31+
from ansys.tools.visualization_interface.backends.pyvista.pyvista import Plotter
32+
33+
34+
class DarkModeButton(PlotterWidget):
35+
"""Provides the dark mode widget for the Visualization Interface Tool ``Plotter`` class.
36+
37+
Parameters
38+
----------
39+
plotter_helper : PlotterHelper
40+
Plotter to add the dark mode widget to.
41+
dark_mode : bool, optional
42+
Whether to activate the dark mode or not.
43+
44+
"""
45+
46+
def __init__(self, plotter: "Plotter", dark_mode: bool = False) -> None:
47+
"""Initialize the ``DarkModeButton`` class."""
48+
# Call PlotterWidget ctor
49+
super().__init__(plotter._pl.scene)
50+
self._dark_mode = dark_mode
51+
# Initialize variables
52+
self._actor: vtkActor = None
53+
self._plotter = plotter
54+
self._button: vtkButtonWidget = self._plotter._pl.scene.add_checkbox_button_widget(
55+
self.callback, position=(43, 10), size=30, border_size=3
56+
)
57+
self.update()
58+
59+
def callback(self, state: bool) -> None:
60+
"""Remove or add the dark mode widget actor upon click.
61+
62+
Parameters
63+
----------
64+
state : bool
65+
Whether the state of the button, which is inherited from PyVista, is active.
66+
67+
"""
68+
if not self._dark_mode:
69+
self._plotter.scene.set_background("black")
70+
for widget in self._plotter._widgets:
71+
widget._dark_mode = True
72+
widget.update()
73+
74+
# Using internal method to force the render to update
75+
self._plotter.scene.iren._mouse_right_button_click()
76+
else:
77+
self._plotter.scene.set_background("white")
78+
for widget in self._plotter._widgets:
79+
widget._dark_mode = False
80+
widget.update()
81+
82+
# Using internal method to force the render to update
83+
self._plotter.scene.iren._mouse_right_button_click()
84+
85+
def update(self) -> None:
86+
"""Define the dark mode widget button parameters."""
87+
if self._dark_mode:
88+
is_inv = "_inv"
89+
else:
90+
is_inv = ""
91+
92+
show_vr = self._button.GetRepresentation()
93+
show_vison_icon_file = Path(
94+
Path(__file__).parent / "_images" / f"dark_mode{is_inv}.png"
95+
)
96+
show_visoff_icon_file = Path(
97+
Path(__file__).parent / "_images" / f"dark_mode{is_inv}.png"
98+
)
99+
show_r_on = vtkPNGReader()
100+
show_r_on.SetFileName(show_vison_icon_file)
101+
show_r_on.Update()
102+
image_on = show_r_on.GetOutput()
103+
104+
show_r_off = vtkPNGReader()
105+
show_r_off.SetFileName(show_visoff_icon_file)
106+
show_r_off.Update()
107+
image_off = show_r_off.GetOutput()
108+
109+
110+
show_vr.SetButtonTexture(0, image_off)
111+
show_vr.SetButtonTexture(1, image_on)

src/ansys/tools/visualization_interface/backends/pyvista/widgets/displace_arrows.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
class CameraPanDirection(Enum):
3131
"""Provides an enum with the available movement directions of the camera."""
3232

33-
XUP = 0, "upxarrow", (5, 170)
34-
XDOWN = 1, "downarrow", (5, 130)
35-
YUP = 2, "upyarrow", (35, 170)
36-
YDOWN = 3, "downarrow", (35, 130)
37-
ZUP = 4, "upzarrow", (65, 170)
38-
ZDOWN = 5, "downarrow", (65, 130)
33+
XUP = 0, "upxarrow", (5, 230)
34+
XDOWN = 1, "downarrow", (5, 190)
35+
YUP = 2, "upyarrow", (35, 230)
36+
YDOWN = 3, "downarrow", (35, 190)
37+
ZUP = 4, "upzarrow", (65, 230)
38+
ZDOWN = 5, "downarrow", (65, 190)
3939

4040

4141
class DisplacementArrow(Button):

src/ansys/tools/visualization_interface/backends/pyvista/widgets/measure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
5252
self._actor: vtkActor = None
5353
self.plotter_helper = plotter_helper
5454
self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget(
55-
self.callback, position=(10, 60), size=30, border_size=3
55+
self.callback, position=(5, 160), size=30, border_size=3
5656
)
5757
self.update()
5858

src/ansys/tools/visualization_interface/backends/pyvista/widgets/mesh_slider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
5252
self._widget_actor: vtkActor = None
5353
self.plotter_helper = plotter_helper
5454
self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget(
55-
self.callback, position=(45, 60), size=30, border_size=3
55+
self.callback, position=(37, 160), size=30, border_size=3
5656
)
5757
self._mb = None
5858
self._mesh_actor_list = []

src/ansys/tools/visualization_interface/backends/pyvista/widgets/pick_rotation_center.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None:
5252
self._actor: vtkActor = None
5353
self.plotter_helper = plotter_helper
5454
self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget(
55-
self.callback, position=(45, 10), size=30, border_size=3
55+
self.callback, position=(37, 128), size=30, border_size=3
5656
)
5757
self.update()
5858

0 commit comments

Comments
 (0)