Further improve file_browser.c

-Adds visible boundary to file icon box;
-Adds a 'cancel' button that closes the file dialog;
-Adds a 'save' button that should be used for the save() routine defined by the user;
This commit is contained in:
crazyBaboon 2022-03-26 18:28:49 +00:00 committed by GitHub
parent ceee8839ab
commit a1df0c4f57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,5 @@
nk_bool file_browser_is_open;
struct icons {
struct nk_image desktop;
struct nk_image home;
@ -325,6 +327,7 @@ file_browser_reload_directory_content(struct file_browser *browser, const char *
static void
file_browser_init(struct file_browser *browser, struct media *media)
{
file_browser_is_open = nk_true;
memset(browser, 0, sizeof(*browser));
browser->media = media;
{
@ -373,113 +376,127 @@ file_browser_run(struct file_browser *browser, struct nk_context *ctx)
struct media *media = browser->media;
struct nk_rect total_space;
if (nk_begin(ctx, "File Browser", nk_rect(50, 50, 600, 400),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
if (file_browser_is_open)
{
static float ratio[] = {0.25f, NK_UNDEFINED};
float spacing_x = ctx->style.window.spacing.x;
/* output path directory selector in the menubar */
ctx->style.window.spacing.x = 0;
nk_menubar_begin(ctx);
if (nk_begin(ctx, "File Browser", nk_rect(50, 50, 600, 400),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|NK_WINDOW_NO_SCROLLBAR|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
char *d = browser->directory;
char *begin = d + 1;
nk_layout_row_dynamic(ctx, 25, 6);
while (*d++) {
if (*d == '/') {
*d = '\0';
if (nk_button_label(ctx, begin)) {
*d++ = '/'; *d = '\0';
file_browser_reload_directory_content(browser, browser->directory);
break;
static float ratio[] = {0.25f, NK_UNDEFINED};
float spacing_x = ctx->style.window.spacing.x;
/* output path directory selector in the menubar */
ctx->style.window.spacing.x = 0;
nk_menubar_begin(ctx);
{
char *d = browser->directory;
char *begin = d + 1;
nk_layout_row_dynamic(ctx, 25, 6);
while (*d++) {
if (*d == '/') {
*d = '\0';
if (nk_button_label(ctx, begin)) {
*d++ = '/'; *d = '\0';
file_browser_reload_directory_content(browser, browser->directory);
break;
}
*d = '/';
begin = d + 1;
}
*d = '/';
begin = d + 1;
}
}
}
nk_menubar_end(ctx);
ctx->style.window.spacing.x = spacing_x;
nk_menubar_end(ctx);
ctx->style.window.spacing.x = spacing_x;
/* window layout */
total_space = nk_window_get_content_region(ctx);
nk_layout_row(ctx, NK_DYNAMIC, total_space.h, 2, ratio);
/* window layout */
total_space = nk_window_get_content_region(ctx);
nk_layout_row(ctx, NK_DYNAMIC, total_space.h - 40, 2, ratio);
nk_group_begin(ctx, "Special", NK_WINDOW_NO_SCROLLBAR);
{
struct nk_image home = media->icons.home;
struct nk_image desktop = media->icons.desktop;
struct nk_image computer = media->icons.computer;
nk_group_begin(ctx, "Special", NK_WINDOW_NO_SCROLLBAR);
{
struct nk_image home = media->icons.home;
struct nk_image desktop = media->icons.desktop;
struct nk_image computer = media->icons.computer;
nk_layout_row_dynamic(ctx, 40, 1);
if (nk_button_image_label(ctx, home, "home", NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, browser->home);
if (nk_button_image_label(ctx,desktop,"desktop",NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, browser->desktop);
if (nk_button_image_label(ctx,computer,"computer",NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, "/");
nk_group_end(ctx);
}
nk_layout_row_dynamic(ctx, 40, 1);
if (nk_button_image_label(ctx, home, "home", NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, browser->home);
if (nk_button_image_label(ctx,desktop,"desktop",NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, browser->desktop);
if (nk_button_image_label(ctx,computer,"computer",NK_TEXT_CENTERED))
file_browser_reload_directory_content(browser, "/");
nk_group_end(ctx);
}
/* output directory content window */
nk_group_begin(ctx, "Content", 0);
{
int index = -1;
size_t i = 0, j = 0;
size_t rows = 0, cols = 0;
size_t count = browser->dir_count + browser->file_count;
/* output directory content window */
nk_group_begin(ctx, "Content", NK_WINDOW_BORDER);
{
int index = -1;
size_t i = 0, j = 0;
size_t rows = 0, cols = 0;
size_t count = browser->dir_count + browser->file_count;
/* File icons layout */
cols = 2;
rows = count / cols;
static float ratio2[] = {0.08f, NK_UNDEFINED};
nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio2);
for (i = 0; i <= rows; i += 1) {
size_t n = j + cols;
for (; j < count && j < n; ++j) {
/* draw one column of icons */
if (j < browser->dir_count) {
/* draw and execute directory buttons */
if (nk_button_image(ctx,media->icons.directory))
index = (int)j;
/* File icons layout */
cols = 2;
rows = count / cols;
static float ratio2[] = {0.08f, NK_UNDEFINED};
nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio2);
for (i = 0; i <= rows; i += 1) {
size_t n = j + cols;
for (; j < count && j < n; ++j) {
/* draw one column of icons */
if (j < browser->dir_count) {
/* draw and execute directory buttons */
if (nk_button_image(ctx,media->icons.directory))
index = (int)j;
nk_label(ctx, browser->directories[j], NK_TEXT_LEFT);
} else {
/* draw and execute files buttons */
struct nk_image *icon;
size_t fileIndex = ((size_t)j - browser->dir_count);
icon = media_icon_for_file(media,browser->files[fileIndex]);
if (nk_button_image(ctx, *icon)) {
strncpy(browser->file, browser->directory, MAX_PATH_LEN);
n = strlen(browser->file);
strncpy(browser->file + n, browser->files[fileIndex], MAX_PATH_LEN - n);
ret = 1;
nk_label(ctx, browser->directories[j], NK_TEXT_LEFT);
} else {
/* draw and execute files buttons */
struct nk_image *icon;
size_t fileIndex = ((size_t)j - browser->dir_count);
icon = media_icon_for_file(media,browser->files[fileIndex]);
if (nk_button_image(ctx, *icon)) {
strncpy(browser->file, browser->directory, MAX_PATH_LEN);
n = strlen(browser->file);
strncpy(browser->file + n, browser->files[fileIndex], MAX_PATH_LEN - n);
ret = 1;
}
}
/* draw one column of labels */
if (j >= browser->dir_count) {
size_t t = j - browser->dir_count;
nk_label(ctx,browser->files[t],NK_TEXT_LEFT);
}
}
/* draw one column of labels */
if (j >= browser->dir_count) {
size_t t = j - browser->dir_count;
nk_label(ctx,browser->files[t],NK_TEXT_LEFT);
}
}
if (index != -1) {
size_t n = strlen(browser->directory);
strncpy(browser->directory + n, browser->directories[index], MAX_PATH_LEN - n);
n = strlen(browser->directory);
if (n < MAX_PATH_LEN - 1) {
browser->directory[n] = '/';
browser->directory[n+1] = '\0';
}
file_browser_reload_directory_content(browser, browser->directory);
}
nk_group_end(ctx);
}
if (index != -1) {
size_t n = strlen(browser->directory);
strncpy(browser->directory + n, browser->directories[index], MAX_PATH_LEN - n);
n = strlen(browser->directory);
if (n < MAX_PATH_LEN - 1) {
browser->directory[n] = '/';
browser->directory[n+1] = '\0';
}
file_browser_reload_directory_content(browser, browser->directory);
nk_layout_row_dynamic(ctx, 30, 5);
nk_label(ctx,"",NK_TEXT_LEFT);
nk_label(ctx,"",NK_TEXT_LEFT);
nk_label(ctx,"",NK_TEXT_LEFT);
if(nk_button_label(ctx, "Cancel"))
{
fprintf(stdout, "File dialog has been closed!\n");
file_browser_is_open = nk_false;
}
nk_group_end(ctx);
if(nk_button_label(ctx, "Save"))
fprintf(stdout, "Insert routine to open/save the file!\n");
}
nk_end(ctx);
}
nk_end(ctx);
return ret;
}