示例#1
0
    def render_headers(self) -> None:
        header_labels = [
            self.displayer.column_header(column.variable)
            for column in self.columns
        ]
        header_lines = max((len(label.split('\n')) for label in header_labels),
                           default=1)

        ig.columns(len(self.columns) + 1)
        if len(self.columns) > 0:
            ig.set_column_width(-1, self.frame_column_width)
        ig.text('')
        ig.next_column()

        for index, column in enumerate(self.columns):
            initial_cursor_pos = ig.get_cursor_pos()
            ig.selectable(
                '##fs-col-' + str(id(self)) + '-' + str(id(column)),
                height=header_lines * ig.get_text_line_height(),
            )

            # TODO: Width adjusting
            ig.set_column_width(-1, column.width)

            if ig.begin_drag_drop_source():
                ig.text(header_labels[index])
                ig.set_drag_drop_payload('fs-col', str(index).encode('utf-8'))
                ig.end_drag_drop_source()

            if ig.begin_drag_drop_target():
                payload = ig.accept_drag_drop_payload('fs-col')
                if payload is not None:
                    source = int(payload.decode('utf-8'))
                    self._move_column(source, index)

                payload = ig.accept_drag_drop_payload('ve-var')
                if payload is not None:
                    self._insert_variable(index, Variable.from_bytes(payload))

                ig.end_drag_drop_target()

            if ig.is_item_hovered() and ig.is_mouse_clicked(2):
                self._remove_column(index)

            if ig.begin_popup_context_item('##fs-colctx-' + str(id(self)) +
                                           '-' + str(id(column))):
                if ig.selectable('Close')[0]:
                    self._remove_column(index)
                ig.end_popup_context_item()

            ig.set_cursor_pos(initial_cursor_pos)
            ig.text(header_labels[index])

            ig.next_column()
        ig.separator()
        ig.columns(1)
示例#2
0
def render_labeled_variable(
    id: str,
    label: str,
    variable: Variable,
    value: T,
    formatter: VariableFormatter,
    is_edited: bool,
    label_width=80,
    value_width=80,
) -> Tuple[Maybe[T], bool]:
    ig.push_id(id)

    ig.selectable(label + '##label', width=label_width)

    if ig.begin_drag_drop_source():
        ig.text(label)
        ig.set_drag_drop_payload('ve-var', variable.to_bytes())
        ig.end_drag_drop_source()

    ig.same_line()

    cell_width = value_width
    cell_height = ig.get_text_line_height(
    ) + 2 * ig.get_style().frame_padding[1]

    cell_cursor_pos = ig.get_cursor_pos()
    cell_cursor_pos = (
        cell_cursor_pos[0] + ig.get_window_position()[0] - ig.get_scroll_x(),
        cell_cursor_pos[1] + ig.get_window_position()[1] - ig.get_scroll_y(),
    )

    changed_data, _, _ = render_variable_value(
        'value',
        value,
        formatter,
        (cell_width, cell_height),
    )

    clear_edit = is_edited and ig.is_item_hovered() and ig.is_mouse_down(2)

    if is_edited:
        dl = ig.get_window_draw_list()
        spacing = ig.get_style().item_spacing
        spacing = (spacing[0] / 2, spacing[1] / 2)
        dl.add_rect(
            cell_cursor_pos[0] - spacing[0],
            cell_cursor_pos[1] - spacing[1],
            cell_cursor_pos[0] + cell_width + spacing[0] - 1,
            cell_cursor_pos[1] + cell_height + spacing[1] - 1,
            ig.get_color_u32_rgba(0.8, 0.6, 0, 1),
        )

    ig.pop_id()
    return changed_data, clear_edit
示例#3
0
 def render_value(label: str, value: object,
                  formatter: VariableFormatter) -> Optional[Any]:
     label_width = 60
     value_size = (
         60 if label == 'dyaw' else 80,
         ig.get_text_line_height() +
         2 * ig.get_style().frame_padding[1],
     )
     ig.push_item_width(label_width)
     ig.selectable(label, width=label_width)
     ig.pop_item_width()
     ig.same_line()
     new_value, _, _ = ui.render_variable_value('value-' + label, value,
                                                formatter, value_size)
     return None if new_value is None else new_value.value
示例#4
0
    def render(id: str) -> None:
        nonlocal error

        if error is not None:
            message = error.strip()
            ig.text('Wafel has crashed. Cause:')
            lines = message.split('\n')
            ig.input_text_multiline(
                '##error-msg',
                message,
                len(message) + 1,
                max(map(len, lines)) * 10,
                (len(lines) + 1) * ig.get_text_line_height() + 6,
            )

            ig.dummy(10, 10)

            if ig.button('Exit'):
                log.info('Aborted')
                sys.exit(1)
            ig.same_line()
            if ig.button('Try to save'):
                if view.ask_save_filename():
                    view.save()
            # ig.same_line()
            # if view is not None and ig.button('Try to continue (mad lads only)'):
            #   view.reload_ui()
            #   error = None
            return

        try:
            if hasattr(model, 'pipeline'):
                log.timer.get_num_copies = lambda: model.pipeline.num_copies(
                ) if config.dev_mode else 0
                log.timer.get_num_updates = lambda: model.pipeline.num_advances(
                ) if config.dev_mode else 0

            log.timer.begin_frame()
            ig.try_render(lambda: do_render(id))
        except:
            error = traceback.format_exc()
            log.error('Caught: ' + error)
        finally:
            log.timer.end_frame()
示例#5
0
def render_input_text_with_error(
    id: str,
    value: str,
    buffer_size: int,
    width: int,
    validate: Callable[[str], T],
) -> Maybe[T]:
    ig.push_id(id)

    cursor_pos = ig.get_cursor_pos()
    cursor_pos = (
        ig.get_window_position()[0] + cursor_pos[0],
        ig.get_window_position()[1] + cursor_pos[1] - ig.get_scroll_y(),
    )

    ig.push_item_width(width)
    changed, new_value = ig.input_text('##input', value, buffer_size)
    ig.pop_item_width()

    result_value = None
    if changed:
        try:
            result_value = Just(validate(new_value))
        except:
            # TODO: Show error message
            dl = ig.get_window_draw_list()
            dl.add_rect(
                cursor_pos[0],
                cursor_pos[1],
                cursor_pos[0] + width,
                cursor_pos[1] + ig.get_text_line_height() +
                2 * ig.get_style().frame_padding[1],
                ig.get_color_u32_rgba(1, 0, 0, 1),
            )
            new_value = value

    ig.pop_id()
    return result_value
示例#6
0
def _render_text(
    value: T,
    formatter: VariableFormatter,
    size: Tuple[int, int],
    highlight: bool,
) -> Tuple[Maybe[T], bool, bool]:
    editing = use_state('editing', False)
    initial_focus = use_state('initial-focus', False)

    if not editing.value:
        clicked, _ = ig.selectable(
            dcast(str, formatter.output(value)) + '##text',
            highlight,
            width=size[0],
            height=size[1],
            flags=ig.SELECTABLE_ALLOW_DOUBLE_CLICK,
        )

        if clicked:
            if ig.is_mouse_double_clicked():
                editing.value = True
                initial_focus.value = False

        pressed = ig.is_item_hovered() and ig.is_mouse_clicked()

        return None, clicked, pressed

    cursor_pos = ig.get_cursor_pos()
    cursor_pos = (
        ig.get_window_position()[0] + cursor_pos[0],
        ig.get_window_position()[1] + cursor_pos[1] - ig.get_scroll_y(),
    )

    ig.push_item_width(size[0])
    value_text = dcast(str, formatter.output(value))
    buffer_size = len(value_text) + ig.get_clipboard_length() + 1000
    _, input = ig.input_text('##text-edit', value_text, buffer_size)
    ig.pop_item_width()

    if not initial_focus.value:
        ig.set_keyboard_focus_here(-1)
        initial_focus.value = True
    elif not ig.is_item_active():
        editing.value = False

    try:
        input_value = formatter.input(input)
        assert type(input_value) is type(value)
        if input_value != value:
            return Just(cast(T, input_value)), False, False
    except:
        # TODO: Show error message
        dl = ig.get_window_draw_list()
        dl.add_rect(
            cursor_pos[0],
            cursor_pos[1],
            cursor_pos[0] + size[0],
            cursor_pos[1] + ig.get_text_line_height() +
            2 * ig.get_style().frame_padding[1],
            ig.get_color_u32_rgba(1, 0, 0, 1),
        )

    return None, False, False