Exemplo n.º 1
0
    def telector_show(bounding_rect_config: str = "",
                      mask_config: str = "",
                      target_mode: str = ""):
        """
        Locate and show labels on text. Can be given explicit search
        parameters or will pull them from settings.
        """

        global labels_ui
        if labels_ui is not None:
            labels_ui.hide()

        bounding_rect_config_ = \
            None if bounding_rect_config == "" else bounding_rect_config
        mask_config_ = \
            None if mask_config == "" else mask_config
        target_mode_ = \
            setting_target_mode.get() if target_mode == "" else target_mode
        bounding_rect = find_bounding_rect(bounding_rect_config_)

        target_groups = find_grouped_rects(bounding_rect, mask_config_)
        if target_mode_ == "lines":
            target_rects = [group["line_rect"] for group in target_groups]
        else:
            target_rects = [
                word_rect for group in target_groups
                for word_rect in group["word_rects"]
            ]

        use_underline_ui = 'user.telector_ui_underline' in registry.tags
        if use_underline_ui:
            labels_ui = marker_ui.UnderlineMarkerUi([
                marker_ui.UnderlineMarkerUi.Group(
                    label=label,
                    line_rect=TalonRect(bounding_rect.x + line_rect.x1,
                                        bounding_rect.y + line_rect.y1,
                                        line_rect.x2 - line_rect.x1,
                                        line_rect.y2 - line_rect.y1),
                    item_rects=[
                        TalonRect(bounding_rect.x + rect.x1,
                                  bounding_rect.y + rect.y1, rect.x2 - rect.x1,
                                  rect.y2 - rect.y1)
                        for rect in group["word_rects"]
                    ])
                for group, label in zip(target_groups, anchor_generator())
                for line_rect in (group["line_rect"], )
            ])
        else:
            labels_ui = marker_ui.MarkerUi(
                [
                    marker_ui.MarkerUi.Marker(target_region=TalonRect(
                        bounding_rect.x + rect.x1, bounding_rect.y + rect.y1,
                        rect.x2 - rect.x1, rect.y2 - rect.y1),
                                              label=label)
                    for rect, label in zip(target_rects, anchor_generator())
                ],
                offset_downward=setting_marker_ui_offset.get() == 1)
        labels_ui.show()
        ctx.tags = ["user.telector_showing"]
Exemplo n.º 2
0
def find_bounding_rect(config: str = None) -> TalonRect:
    """
    Finds a bounding box to search for text in either based on an
    explicit argument or on setting_bounding_box
    """

    bounding_box_setting = config if config is not None else setting_bounding_box.get(
    )

    if bounding_box_setting.startswith("active_window"):
        bits = bounding_box_setting.split(":")
        mods = bits[1].split(" ") if len(bits) > 1 else ["0", "0", "-0", "-0"]
        base_rect = find_active_window_rect()
        _calc_pos = calculate_relative

        x = _calc_pos(mods[0], base_rect.x, base_rect.x + base_rect.width)
        y = _calc_pos(mods[1], base_rect.y, base_rect.y + base_rect.height)
        rect = TalonRect(
            x,
            y,
            _calc_pos(mods[2], 0, base_rect.width) - int(mods[0]),
            _calc_pos(mods[3], 0, base_rect.height) - int(mods[1]),
        )

    return rect
Exemplo n.º 3
0
def find_active_window_rect() -> TalonRect:
    """
    The Talon active window rect detector is buggy under LInux. So allow getting it a
    different way.
    """

    if setting_win_rect_workaround.get() == 1:
        import subprocess
        active_window_id = subprocess.run(["xdotool", "getactivewindow"],
                                          capture_output=True,
                                          check=True).stdout.strip()
        active_window_geom = subprocess.run(
            ["xdotool", "getwindowgeometry", "--shell", active_window_id],
            capture_output=True,
            check=True).stdout
        val_map = {
            key.decode("utf8"): int(val)
            for line in active_window_geom.splitlines()
            for key, val in (line.split(b"="), )
        }

        return TalonRect(
            val_map["X"],
            val_map["Y"],
            val_map["WIDTH"],
            val_map["HEIGHT"],
        )
    else:
        return ui.active_window().rect
Exemplo n.º 4
0
def _debug_draw(canvas):
    global debug_bounding_rect, debug_grouped_rects

    paint = canvas.paint
    paint.stroke_width = 1
    paint.style = paint.Style.STROKE
    paint.color = 'blue'
    canvas.draw_rect(debug_bounding_rect)
    paint.color = 'red'
    for line in debug_grouped_rects:
        for rect in line["word_rects"]:
            # skia canvas positions are always relative to the screen
            canvas.draw_rect(
                TalonRect(debug_bounding_rect.x + rect.x1,
                          debug_bounding_rect.y + rect.y1, rect.x2 - rect.x1,
                          rect.y2 - rect.y1))