potocpav / python-concur

Concur UI Framework for Python
MIT License
49 stars 2 forks source link

Add hint to doc how to get horizontal separators in c.columns() #30

Closed sla-te closed 3 years ago

sla-te commented 3 years ago

https://github.com/potocpav/python-concur/blob/5cc92d2ffc57ed896d2aced048be54a53c42199e/concur/widgets.py#L570

import concur as c

def app():
    return c.orr([c.columns([
        [c.text("x1"), c.text("x3"), c.text("x4"), c.text("x"), c.separator()],
        [c.text("1"), c.text("2"), c.text("3"), c.text("4")],
        [c.text("1"), c.text("2"), c.text("3"), c.text("4")],
        [c.text("1"), c.text("2"), c.text("3"), c.text("4")],
    ], "start", True)])

if __name__ == "__main__":
    c.main(app(), "Columns")

drops

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\project\flder\concur_demos\extra\columns.py", line 18, in <module>
    c.main(app(), "Columns")
  File "C:\Users\user\PycharmProjects\project\flder\concur_demos\extra\columns.py", line 9, in app
    return c.orr([c.columns([
  File "C:\Users\user\PycharmProjects\project\venv\lib\site-packages\concur\widgets.py", line 570, in columns
    assert len(e) == n_columns
AssertionError

If we comment out the assertion it works fine.

EDIT: In fact it doesn't work fine if we do that, the rows will be out of order. How do we properly add horizontal separators? EDIT2: Ah, we can do [c.separator(), c.nothing(), c.nothing(), c.nothing()] would add that to the docs

potocpav commented 3 years ago

The "columns" function has a really ugly API. I didn't think of a better one - do you have some ideas?

sla-te commented 3 years ago
def columns(elems, identifier=None, vert_borders=True, hor_borders=True, widths=None) -> Widget:
    """
    Table, using the imgui columns API.
    :param List[List[Widget]] elems: 2D array of widgets. A list containing another list for
    each row. len() of the first row defines the amount of columns, that will be rendered.
    :param identifier: TODO
    :param bool hor_borders: Toggle horizontal borders on/off.
    :param List[Union[int,float]] widths: is an optional vector of column widths in pixels. May
    contain  None values.
    :param bool vert_borders: Toggle horizontal borders on/off.
    :return: orr() wrapping the column widget.
    :rtype: WidgetWrapper
    """

    # TODO Move import to top of script
    from concur.core import nothing
    n_columns = len(elems[0])

    if widths is None:
        widths = []

    if hor_borders:
        sep = [separator(), *[nothing() for _ in range(n_columns - 1)]]
        n, i = 1, 1
        while i < len(elems):
            elems.insert(i, sep)
            i += (n + 1)

    accum = [lift(imgui.columns, n_columns, identifier, vert_borders)]
    for i, w in enumerate(widths):
        if w is not None:
            accum.append(lift(imgui.set_column_width, i, w))
    for row in elems:
        assert len(row) == n_columns
        for widget in row:
            accum.append(widget)
            accum.append(lift(imgui.next_column))
    accum.append(lift(imgui.columns, 1))
    return orr(accum)

https://github.com/potocpav/python-concur/pull/31