Skip to content

Commit

Permalink
Update grid constructor to get reference to parent. Add dropdown exam…
Browse files Browse the repository at this point in the history
…ple. Update tests
  • Loading branch information
jwlodek committed Apr 26, 2023
1 parent 2f6d2d7 commit 55df84f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
21 changes: 21 additions & 0 deletions examples/controls/dropdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""The most basic possible use case for py_cui
@author: Jakub Wlodek
@created: 12-Aug-2019
"""

# Import the lib
import py_cui

# create the CUI object. Will have a 3 by 3 grid with indexes from 0,0 to 2,2
root = py_cui.PyCUI(3, 3)

# Add a label to the center of the CUI in the 1,1 grid position
dropdown = root.add_custom_widget(py_cui.widgets.DropdownMenu, 'Example Dropdown', 1, 1, 1, 1, 1, 0, 10)
textbox = root.add_text_box('Test Text Box', 0, 1, 1, 1, 1, 0, "Hi")

for i in range(15):
dropdown.add_item(f'Test{i}')

# Start/Render the CUI
root.start()
2 changes: 1 addition & 1 deletion py_cui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(

# Initialize grid, renderer, and widget dict
self._grid = py_cui.grid.Grid(
num_rows, num_cols, self._height, self._width, self._logger
self, num_rows, num_cols, self._height, self._width, self._logger
)
self._stdscr: Any = None
self._refresh_timeout = -1
Expand Down
3 changes: 2 additions & 1 deletion py_cui/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Grid:
"""


def __init__(self, num_rows: int, num_columns: int, height: int, width: int, logger: 'py_cui.debug.PyCUILogger'):
def __init__(self, parent, num_rows: int, num_columns: int, height: int, width: int, logger: 'py_cui.debug.PyCUILogger'):
"""Constructor for the Grid class
Parameters
Expand All @@ -46,6 +46,7 @@ def __init__(self, num_rows: int, num_columns: int, height: int, width: int, log
The width in characters of the terminal window
"""

self._parent = parent
self._num_rows = num_rows
self._num_columns = num_columns
self._height = height
Expand Down
2 changes: 1 addition & 1 deletion py_cui/widget_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, num_rows: int, num_cols: int, logger: 'py_cui.debug.PyCUILogg
status_bars_height = self._root.title_bar.get_height() + self._root.status_bar.get_height()
self._height = self._height - status_bars_height - 2

self._grid = py_cui.grid.Grid(num_rows, num_cols, self._height, self._width, logger)
self._grid = py_cui.grid.Grid(root, num_rows, num_cols, self._height, self._width, logger)

self._selected_widget: Optional[int] = None
self._logger = logger
Expand Down
7 changes: 7 additions & 0 deletions py_cui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def __init__(self, id, title: str, grid: 'py_cui.grid.Grid', row: int, column: i
self._context_menu = None


def _get_parent_ui(self):
"""Function used to get reference to parent UI instance for interfacing with popups and context menus
"""

return self._grid._parent


def add_key_command(self, key: Union[int, List[int]], command: Callable[[],Any]) -> None:
"""Maps a keycode to a function that will be executed when in focus mode
Expand Down
21 changes: 10 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@
def LOGGER():
return dbg.PyCUILogger('PYCUI TEST')

@pytest.fixture
def PYCUI():

def _PYCUI(rows, cols, height, width):
return py_cui.PyCUI(rows, cols, simulated_terminal=[height, width])

return _PYCUI


@pytest.fixture
def RENDERER(request, LOGGER):
return py_cui.renderer.Renderer(None, None, LOGGER)


@pytest.fixture
def GRID(request, LOGGER):
def GRID(request, PYCUI, LOGGER):

def _GRID(rows, cols, height, width):

return py_cui.grid.Grid(rows, cols, height, width, LOGGER)
return py_cui.grid.Grid(PYCUI, rows, cols, height, width, LOGGER)

return _GRID

Expand All @@ -43,15 +51,6 @@ def _CUSTOMWIDGET(id, name, row, col, rowspan, colspan):
return _CUSTOMWIDGET


@pytest.fixture
def PYCUI():

def _PYCUI(rows, cols, height, width):
return py_cui.PyCUI(rows, cols, simulated_terminal=[height, width])

return _PYCUI


@pytest.fixture
def WIDGETSET(request, LOGGER):

Expand Down

0 comments on commit 55df84f

Please sign in to comment.