Skip to content

TUI - Clickable UI #12

@melihcelenk

Description

@melihcelenk

Make menu clickable by mouse.

python can be used.

also mc (sudo apt install mc) can be added
also broot or atuin can be added

Example


#!/usr/bin/env python3
import curses

def draw_button(window, start_y, start_x, width, label, is_focused):
    button_text = f"[ {label} ]"
    padding = max(0, width - len(button_text))
    content = button_text + " " * padding
    if is_focused:
        window.attron(curses.A_REVERSE)
    window.addstr(start_y, start_x, content[:width])
    if is_focused:
        window.attroff(curses.A_REVERSE)

def is_inside(y, x, top, left, width, height):
    return (y >= top and y < top + height) and (x >= left and x < left + width)

def main(stdscr):
    curses.curs_set(0)
    stdscr.nodelay(False)
    stdscr.keypad(True)

    # Fare olaylarını aç
    curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)

    # Basit düzen
    height, width = stdscr.getmaxyx()
    message = "Bir butona tıkla (Fare/Mouse). 'q' ile çık."
    stdscr.addstr(0, 0, message[:width])

    button1_top = 2
    button1_left = 2
    button1_width = 18
    button1_label = "Sayıyı Arttır"

    button2_top = 4
    button2_left = 2
    button2_width = 18
    button2_label = "Sayıyı Azalt"

    value_top = 7
    value_left = 2
    current_value = 0

    is_button1_focused = False
    is_button2_focused = False

    while True:
        stdscr.clear()
        height, width = stdscr.getmaxyx()
        stdscr.addstr(0, 0, message[:width])

        draw_button(stdscr, button1_top, button1_left, button1_width, button1_label, is_button1_focused)
        draw_button(stdscr, button2_top, button2_left, button2_width, button2_label, is_button2_focused)
        stdscr.addstr(value_top, value_left, f"Güncel değer: {current_value}")

        stdscr.refresh()

        key = stdscr.getch()

        if key == ord('q'):
            break

        if key == curses.KEY_MOUSE:
            try:
                mouse_id, mouse_x, mouse_y, mouse_z, mouse_state = curses.getmouse()
            except curses.error:
                continue

            is_button1_focused = is_inside(mouse_y, mouse_x, button1_top, button1_left, button1_width, 1)
            is_button2_focused = is_inside(mouse_y, mouse_x, button2_top, button2_left, button2_width, 1)

            is_left_click = (mouse_state & curses.BUTTON1_PRESSED) or (mouse_state & curses.BUTTON1_CLICKED)

            if is_left_click:
                if is_button1_focused:
                    current_value += 1
                elif is_button2_focused:
                    current_value -= 1

        else:
            # Klavye ile de butonlar arasında dolaşmak istersen burada ekleyebilirsin
            pass

if __name__ == "__main__":
    curses.wrapper(main)


Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

Status
Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions