Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am having problems using scroll in my media file list display. #989

Open
baobluebird opened this issue Jan 16, 2025 · 0 comments
Open

Comments

@baobluebird
Copy link

baobluebird commented Jan 16, 2025

This is my current code for show list media file, i'm trying way to scroll 25 row per page, I tried every method but got error, seems the problem is i dont know which scroll guide to follow so i can apply it
my code:
`
std::string MediaFileManagerView::displayAllMediaFile(MediaFileManager MediaFileManager) {
system("clear");

auto media_files = MediaFileManager.getAllMediaFile();

if (media_files.empty()) {
    auto screen = ScreenInteractive::TerminalOutput();
    auto empty_message = Renderer([&] {
        return vbox({
            text("No media files to display.") | bold | center,
        });
    });
    screen.Loop(empty_message);
    return "";
}

const int rows_per_page = 25;
int current_page = 0;
int total_pages = std::ceil((double)media_files.size() / rows_per_page);
std::string error_message;
std::string selected_filename;
int selected_row = -1;  
int hovered_row = -1;   
std::string input_filename;
bool is_exit_hovered = false;
auto screen = ScreenInteractive::TerminalOutput();

auto create_table_view = [&]() {
std::vector<Element> table_rows;

table_rows.push_back(
    hbox({
        text("STT") | bold | size(WIDTH, EQUAL, 5) | border,
        text("File Name") | bold | size(WIDTH, EQUAL, 20) | border,
        text("File Type") | bold | size(WIDTH, EQUAL, 10) | border,
        text("File Duration") | bold | size(WIDTH, EQUAL, 15) | border,
        text("File Size") | bold | size(WIDTH, EQUAL, 15) | border,
        text("File Path") | bold | size(WIDTH, EQUAL, 60) | border,
    })
);

int start_index = current_page * rows_per_page;
int end_index = std::min(start_index + rows_per_page, (int)media_files.size());

for (int i = start_index; i < end_index; ++i) {
    auto file = media_files[i];
    bool is_hovered = (i == hovered_row);
    bool is_selected = (i == selected_row);

    table_rows.push_back(
        hbox({
            text(std::to_string(i + 1)) | size(WIDTH, EQUAL, 5) | border,
            text(file->getName()) | size(WIDTH, EQUAL, 20) | border,
            text(file->getType()) | size(WIDTH, EQUAL, 10) | border,
            text(file->getDuration()) | size(WIDTH, EQUAL, 15) | border,
            text(std::to_string(file->getSize()) + " bytes") | size(WIDTH, EQUAL, 15) | border,
            text(file->getPath()) | size(WIDTH, EQUAL, 60) | border,
        }) | (is_hovered ? inverted : nothing) 
          | (is_selected ? bold : nothing)
    );
}

// Hiển thị nút Exit với phản hồi hover
auto exit_button = text("Exit (Simulate 0)") | bold | center | border |
                   (is_exit_hovered ? color(Color::Red) : nothing);

return vbox({
    text("----- All Media Files -----") | bold | center | color(Color::Blue),
    text("Page " + std::to_string(current_page + 1) + " of " + std::to_string(total_pages)) | center,
    separator(),
    vbox(std::move(table_rows)),
    separator(),
    exit_button | center,  // Hiển thị nút Exit
    separator(),
    text("Use UP/DOWN to navigate, LEFT/RIGHT to change pages, ENTER to select.") | dim | center,
    text("Hover over a row with mouse or click to select.") | dim | center,
    text("Enter filename below (or input 0 to exit):") | dim | center,
    text(input_filename) | border | center, 
    text("Selected File: " + selected_filename) | bold | center,
    text(error_message) | color(Color::Red) | center, 
});

};

auto main_view = Renderer([&]() {
    return create_table_view();
});

main_view = CatchEvent(main_view, [&](Event event) {
if (event.is_mouse()) {
    int row_height = 3;  // Chiều cao mỗi hàng
    int exit_y_position = rows_per_page + 1;  // Vị trí của nút Exit
    int y_offset = (event.mouse().y - 5) / row_height;  // Chia tọa độ y cho chiều cao mỗi hàng

    if (event.mouse().motion == Mouse::Moved) {
        // Kiểm tra nếu chuột hover vào bảng
        if (y_offset >= 0 && y_offset < rows_per_page) {
            hovered_row = y_offset + current_page * rows_per_page;
            is_exit_hovered = false;  // Không hover nút Exit
        }
        // Kiểm tra nếu chuột hover vào nút Exit
        else if (y_offset == exit_y_position) {
            hovered_row = -1;
            is_exit_hovered = true;  // Hover nút Exit
        } else {
            hovered_row = -1;
            is_exit_hovered = false;  // Không hover
        }
        return true;
    }

    // Xử lý click chuột trái
    if (event.mouse().button == Mouse::Left && event.mouse().motion == Mouse::Pressed) {
        // Nếu click vào một hàng
        if (hovered_row >= 0 && hovered_row < (int)media_files.size()) {
            selected_row = hovered_row;
            selected_filename = media_files[selected_row]->getName();
            screen.ExitLoopClosure()();
        }
        // Nếu click vào nút Exit
        else if (is_exit_hovered) {
            selected_filename = "";
            screen.ExitLoopClosure()();  // Thoát chương trình
            return true;
        }
        return true;
    }
}

// Xử lý các phím Arrow
if (event == Event::ArrowUp) {
    if (selected_row > current_page * rows_per_page) {
        --selected_row;  // Di chuyển lên hàng trước
        hovered_row = selected_row;
        error_message.clear();
    } else {
        error_message = "Already at the top of the page!";
    }
    return true;
}

if (event == Event::ArrowDown) {
    if (selected_row < std::min((current_page + 1) * rows_per_page - 1, (int)media_files.size() - 1)) {
        ++selected_row;  // Di chuyển xuống hàng tiếp theo
        hovered_row = selected_row;
        error_message.clear();
    } else {
        error_message = "Already at the bottom of the page!";
    }
    return true;
}

if (event == Event::ArrowLeft) {
    if (current_page > 0) {
        --current_page;  // Quay lại trang trước
        selected_row = current_page * rows_per_page;  // Cập nhật hàng được chọn
        hovered_row = selected_row;
        error_message.clear();
    } else {
        error_message = "Already on the first page!";
    }
    return true;
}

if (event == Event::ArrowRight) {
    if (current_page < total_pages - 1) {
        ++current_page;  // Chuyển sang trang tiếp theo
        selected_row = current_page * rows_per_page;  // Cập nhật hàng được chọn
        hovered_row = selected_row;
        error_message.clear();
    } else {
        error_message = "Already on the last page!";
    }
    return true;
}

// Giữ nguyên các sự kiện bàn phím khác
if (event.is_character()) {
    char c = event.character()[0];
    if (std::isalnum(c) || c == '.' || c == '_' || c == '-' || c == ' ' || c == '(' || c == ')') {
        input_filename += c;
    }
    return true;
}

if (event == Event::Backspace && !input_filename.empty()) {
    input_filename.pop_back();
    return true;
}

if (event == Event::Return) {
    if (!input_filename.empty()) {
        if (input_filename == "0") {
            selected_filename = "";
            screen.ExitLoopClosure()();
            return true;
        } else if (MediaFileManager.checkFileExists(input_filename)) {
            selected_filename = input_filename;
            screen.ExitLoopClosure()();
            return true;
        } else {
            error_message = "File not found!";
            input_filename.clear();  // Xóa dữ liệu nhập khi file không tồn tại
        }
    } else if (selected_row >= 0 && selected_row < (int)media_files.size()) {
        selected_filename = media_files[selected_row]->getName();
        screen.ExitLoopClosure()();
        return true;
    } else {
        error_message = "No file selected!";
    }
    return true;
}

return false;

});
screen.Loop(main_view);
return selected_filename;
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant