mirror of
https://github.com/davatorium/rofi.git
synced 2025-02-03 15:34:54 -05:00
[FileBrowser] Reduce number of re-allocs.
This commit is contained in:
parent
6ec5bfba64
commit
e3860c89e8
2 changed files with 22 additions and 15 deletions
|
@ -1071,15 +1071,15 @@ This property sets the distance between the packed widgets (both horizontally an
|
|||
More dynamic spacing can be achieved by adding dummy widgets, for example to make one widget centered:
|
||||
|
||||
```
|
||||
|--------------------------------------------|
|
||||
| |-----------| |--------| |-----------| |
|
||||
| | dummy | | child | | dummy | |
|
||||
| | expand: y | | | | expand: y | |
|
||||
| | | | | | | |
|
||||
| | | | | | | |
|
||||
| | | | | | | |
|
||||
| |-----------| |--------| |-----------| |
|
||||
|--------------------------------------------|
|
||||
|----------------------------------------------------|
|
||||
| |---------------| |--------| |---------------| |
|
||||
| | dummy | | child | | dummy | |
|
||||
| | expand: true; | | | | expand: true; | |
|
||||
| | | | | | | |
|
||||
| | | | | | | |
|
||||
| | | | | | | |
|
||||
| |---------------| |--------| |---------------| |
|
||||
|----------------------------------------------------|
|
||||
```
|
||||
|
||||
If both dummy widgets are set to expand, `child` will be centered. Depending on the `expand` flag of child the
|
||||
|
|
|
@ -92,6 +92,7 @@ typedef struct {
|
|||
GFile *current_dir;
|
||||
FBFile *array;
|
||||
unsigned int array_length;
|
||||
unsigned int array_length_real;
|
||||
} FileBrowserModePrivateData;
|
||||
|
||||
/**
|
||||
|
@ -119,6 +120,7 @@ static void free_list(FileBrowserModePrivateData *pd) {
|
|||
g_free(pd->array);
|
||||
pd->array = NULL;
|
||||
pd->array_length = 0;
|
||||
pd->array_length_real = 0;
|
||||
}
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -205,6 +207,14 @@ static void set_time(FBFile *file) {
|
|||
// g_free(path);
|
||||
}
|
||||
|
||||
inline static void fb_resize_array(FileBrowserModePrivateData *pd) {
|
||||
if ((pd->array_length + 1) > pd->array_length_real) {
|
||||
pd->array_length_real += 256;
|
||||
pd->array =
|
||||
g_realloc(pd->array, (pd->array_length_real + 1) * sizeof(FBFile));
|
||||
}
|
||||
}
|
||||
|
||||
static void get_file_browser(Mode *sw) {
|
||||
FileBrowserModePrivateData *pd =
|
||||
(FileBrowserModePrivateData *)mode_get_private_data(sw);
|
||||
|
@ -218,8 +228,7 @@ static void get_file_browser(Mode *sw) {
|
|||
struct dirent *rd = NULL;
|
||||
while ((rd = readdir(dir)) != NULL) {
|
||||
if (g_strcmp0(rd->d_name, "..") == 0) {
|
||||
pd->array =
|
||||
g_realloc(pd->array, (pd->array_length + 1) * sizeof(FBFile));
|
||||
fb_resize_array(pd);
|
||||
// Rofi expects utf-8, so lets convert the filename.
|
||||
pd->array[pd->array_length].name = g_strdup("..");
|
||||
pd->array[pd->array_length].path = NULL;
|
||||
|
@ -244,8 +253,7 @@ static void get_file_browser(Mode *sw) {
|
|||
break;
|
||||
case DT_REG:
|
||||
case DT_DIR:
|
||||
pd->array =
|
||||
g_realloc(pd->array, (pd->array_length + 1) * sizeof(FBFile));
|
||||
fb_resize_array(pd);
|
||||
// Rofi expects utf-8, so lets convert the filename.
|
||||
pd->array[pd->array_length].name =
|
||||
g_filename_to_utf8(rd->d_name, -1, NULL, NULL, NULL);
|
||||
|
@ -266,8 +274,7 @@ static void get_file_browser(Mode *sw) {
|
|||
pd->array_length++;
|
||||
break;
|
||||
case DT_LNK:
|
||||
pd->array =
|
||||
g_realloc(pd->array, (pd->array_length + 1) * sizeof(FBFile));
|
||||
fb_resize_array(pd);
|
||||
// Rofi expects utf-8, so lets convert the filename.
|
||||
pd->array[pd->array_length].name =
|
||||
g_filename_to_utf8(rd->d_name, -1, NULL, NULL, NULL);
|
||||
|
|
Loading…
Add table
Reference in a new issue