fix: various bug fixes in desktop components

This commit is contained in:
2026-05-07 20:00:17 +02:00
parent ecf6edb826
commit 69fc184a4e
12 changed files with 311 additions and 85 deletions
@@ -8,6 +8,19 @@
namespace filemanager {
static bool path_is_same_or_descendant(const char* path, const char* root) {
if (!path || !root) return false;
int root_len = montauk::slen(root);
while (root_len > 0 && root[root_len - 1] == '/') root_len--;
if (root_len <= 0) return false;
for (int i = 0; i < root_len; i++) {
if (path[i] == '\0' || path[i] != root[i]) return false;
}
return path[root_len] == '\0' || path[root_len] == '/';
}
void filemanager_do_copy(FileManagerState* fm) {
if (fm->selected < 0 || fm->selected >= fm->entry_count) return;
if (filemanager_is_computer_view(fm) || filemanager_is_virtual_view(fm) ||
@@ -36,11 +49,13 @@ void filemanager_do_paste(FileManagerState* fm) {
const char* basename = path_basename(fm->clipboard_path);
char dst[512];
filemanager_build_fullpath(dst, 512, fm->current_path, basename);
if (montauk::streq(dst, fm->clipboard_path)) return;
// Check if source is a directory by trying to readdir it
const char* probe[1];
int probe_count = montauk::readdir(fm->clipboard_path, probe, 1);
bool src_is_dir = (probe_count >= 0);
if (src_is_dir && path_is_same_or_descendant(dst, fm->clipboard_path)) return;
bool ok;
if (src_is_dir)
@@ -243,7 +243,7 @@ void filemanager_read_dir(FileManagerState* fm) {
// Sort: directories first, then alphabetical (case-insensitive)
for (int i = 1; i < fm->entry_count; i++) {
char tmp_name[64];
char tmp_name[128];
int tmp_type = fm->entry_types[i];
int tmp_size = fm->entry_sizes[i];
bool tmp_isdir = fm->is_dir[i];
@@ -385,9 +385,11 @@ bool filemanager_delete_recursive(const char* path) {
}
bool filemanager_copy_file(const char* src, const char* dst) {
if (!src || !dst || montauk::streq(src, dst)) return false;
int sfd = montauk::open(src);
if (sfd < 0) return false;
int size = (int)montauk::getsize(sfd);
uint64_t size = montauk::getsize(sfd);
// Create destination
int dfd = montauk::fcreate(dst);
@@ -396,33 +398,46 @@ bool filemanager_copy_file(const char* src, const char* dst) {
return false;
}
bool ok = true;
if (size > 0) {
// Cap at 4 MB to avoid exhausting memory
if (size > 4 * 1024 * 1024) {
montauk::close(sfd);
montauk::close(dfd);
montauk::fdelete(dst);
return false;
}
uint8_t* buf = (uint8_t*)montauk::malloc(size);
static constexpr uint64_t COPY_CHUNK = 64 * 1024;
uint64_t buf_size = size < COPY_CHUNK ? size : COPY_CHUNK;
uint8_t* buf = (uint8_t*)montauk::malloc(buf_size);
if (!buf) {
montauk::close(sfd);
montauk::close(dfd);
montauk::fdelete(dst);
return false;
}
montauk::read(sfd, buf, 0, size);
montauk::fwrite(dfd, buf, 0, size);
uint64_t off = 0;
while (off < size) {
uint64_t todo64 = size - off;
if (todo64 > buf_size) todo64 = buf_size;
int todo = (int)todo64;
int got = montauk::read(sfd, buf, off, todo64);
if (got != todo) { ok = false; break; }
int wrote = montauk::fwrite(dfd, buf, off, todo64);
if (wrote != todo) { ok = false; break; }
off += todo64;
}
montauk::mfree(buf);
}
montauk::close(sfd);
montauk::close(dfd);
if (!ok) {
montauk::fdelete(dst);
return false;
}
return true;
}
// Recursive directory copy
bool filemanager_copy_dir_recursive(const char* src, const char* dst) {
if (!src || !dst || montauk::streq(src, dst)) return false;
montauk::fmkdir(dst);
const char* names[64];
@@ -130,12 +130,14 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
if (grad_option.contains(mx, cy)) {
s.bg_gradient = true;
s.bg_image = false;
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
if (solid_option.contains(mx, cy)) {
s.bg_gradient = false;
s.bg_image = false;
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -146,6 +148,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
wallpaper_scan_home(st->desktop->home_dir, &st->wp_files);
st->wp_scanned = true;
}
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -162,6 +165,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
str_append(fullpath, st->wp_files.names[i], 256);
wallpaper_load(&s, fullpath,
st->desktop->screen_w, st->desktop->screen_h);
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -173,6 +177,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
int idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
if (idx >= 0) {
s.bg_grad_top = bg_palette[idx];
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -181,6 +186,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
if (idx >= 0) {
s.bg_grad_bottom = bg_palette[idx];
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -189,6 +195,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
int idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
if (idx >= 0) {
s.bg_solid = bg_palette[idx];
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -199,6 +206,7 @@ bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, in
int idx = mtk::hit_swatch_row(mx, cy, x + 110, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
if (idx >= 0) {
s.panel_color = panel_palette[idx];
desktop_mark_background_dirty(st->desktop);
settings_persist(st);
return true;
}
@@ -11,6 +11,8 @@
#include <gui/mtk.hpp>
#include <montauk/config.h>
void desktop_mark_background_dirty(DesktopState* ds);
namespace settings_app {
struct SettingsState {