Files
MontaukOS/programs/src/files/properties.cpp
T

1254 lines
43 KiB
C++

/*
* properties.cpp
* MTK file properties dialog for the Files file manager
* Copyright (c) 2026 Daniel Hammer
*/
#include "filemanager_internal.hpp"
#include <gui/mtk/hosts.hpp>
namespace filemanager {
struct FilePropertiesDialogState {
DesktopState* desktop;
Color accent;
int mouse_x;
int mouse_y;
char name[128];
char type[64];
char location[256];
char path[512];
char size[32];
char contains[32];
char modified[24];
bool has_path;
bool has_size;
bool has_contains;
bool has_modified;
bool owns_icon;
bool fallback_is_dir;
SvgIcon icon;
};
struct FileDeleteConfirmDialogState {
DesktopState* desktop;
FileManagerState* owner;
Color accent;
int mouse_x;
int mouse_y;
char name[128]; // display name (single item) or summary ("3 items")
char path[512]; // primary path (used for single-item subtitle)
char title[96]; // custom heading (Empty Trash); empty = derived
bool is_dir; // single item: folder or file wording
SvgIcon icon;
// Multi-item targets (dynamically allocated). count == 1 keeps the original
// single-folder UX.
char (*paths)[512];
int count;
};
static void props_copy(char* dst, int dst_len, const char* src) {
if (!dst || dst_len <= 0) return;
if (!src) src = "";
montauk::strncpy(dst, src, dst_len - 1);
dst[dst_len - 1] = '\0';
}
static Rect props_close_button_rect(int content_w, int content_h) {
return {content_w - 96, content_h - 42, 80, 28};
}
static void props_close_self(DesktopState* ds, void* app_data) {
if (!ds) return;
for (int i = 0; i < ds->window_count; i++) {
if (ds->windows[i].app_data == app_data) {
desktop_close_window(ds, i);
return;
}
}
}
void props_center_dialog(DesktopState* ds,
const void* owner_app_data,
int w,
int h,
int* out_x,
int* out_y) {
if (!out_x || !out_y) return;
int wx = 180;
int wy = 120;
if (ds) {
for (int i = 0; i < ds->window_count; i++) {
if (ds->windows[i].app_data == owner_app_data) {
wx = ds->windows[i].frame.x + (ds->windows[i].frame.w - w) / 2;
wy = ds->windows[i].frame.y + (ds->windows[i].frame.h - h) / 2;
break;
}
}
if (wx < 8) wx = 8;
if (wy < PANEL_HEIGHT + 8) wy = PANEL_HEIGHT + 8;
if (wx + w > ds->screen_w) wx = ds->screen_w - w;
if (wy + h > ds->screen_h) wy = ds->screen_h - h;
}
if (wx < 0) wx = 0;
if (wy < 0) wy = 0;
*out_x = wx;
*out_y = wy;
}
static void props_drive_path(int drive, char* out, int out_len) {
if (!out || out_len <= 0) return;
if (drive < 0) {
out[0] = '\0';
return;
}
snprintf(out, out_len, "%d:/", drive);
}
static const char* props_type_label(const FileManagerState* fm, int idx) {
if (!fm || idx < 0 || idx >= fm->entry_count) return "Item";
switch (fm->entry_types[idx]) {
case FM_ENTRY_DIR: return "Folder";
case FM_ENTRY_EXEC: return "Executable";
case FM_ENTRY_DRIVE: return "Drive";
case FM_ENTRY_HOME: return "Home Folder";
case FM_ENTRY_APPS_ROOT: return "Apps Folder";
case FM_ENTRY_SPECIAL_DIR: return "Folder";
case FM_ENTRY_VIRTUAL_ITEM: return "Application";
case FM_ENTRY_FILE:
default:
return fm->is_dir[idx] ? "Folder" : "File";
}
}
static void props_build_location(const FileManagerState* fm, char* out, int out_len) {
if (!fm) {
props_copy(out, out_len, "");
return;
}
if (filemanager_is_computer_view(fm)) {
props_copy(out, out_len, "Computer");
} else if (filemanager_is_virtual_view(fm)) {
props_copy(out, out_len, filemanager_virtual_view_label(fm->virtual_view));
} else {
props_copy(out, out_len, fm->current_path);
}
}
static void props_build_path(const FileManagerState* fm, int idx, char* out, int out_len) {
if (!fm || idx < 0 || idx >= fm->entry_count || !out || out_len <= 0) return;
out[0] = '\0';
DesktopState* ds = fm->desktop;
int type = fm->entry_types[idx];
if (!filemanager_is_computer_view(fm) && !filemanager_is_virtual_view(fm)) {
filemanager_build_fullpath(out, out_len, fm->current_path, fm->entry_names[idx]);
return;
}
if (filemanager_is_computer_view(fm)) {
if (type == FM_ENTRY_HOME && ds) {
props_copy(out, out_len, ds->home_dir);
} else if (type == FM_ENTRY_SPECIAL_DIR && ds) {
props_copy(out, out_len, ds->home_dir);
int plen = montauk::slen(out);
if (plen > 0 && out[plen - 1] != '/') str_append(out, "/", out_len);
str_append(out, fm->entry_names[idx], out_len);
} else if (type == FM_ENTRY_DRIVE) {
props_drive_path(fm->drive_indices[idx], out, out_len);
} else if (type == FM_ENTRY_APPS_ROOT) {
props_copy(out, out_len, "virtual://apps");
}
return;
}
if (type == FM_ENTRY_VIRTUAL_ITEM && idx < fm->virtual_entry_count) {
const FileManagerVirtualEntry& entry = fm->virtual_entries[idx];
if (ds && entry.desktop_item_index >= 0
&& entry.desktop_item_index < ds->desktop_item_count) {
const DesktopItem& item = ds->desktop_items[entry.desktop_item_index];
if (item.launch_kind == DESKTOP_ITEM_LAUNCH_EXECUTABLE) {
props_copy(out, out_len, item.binary_path);
} else {
props_copy(out, out_len, item.id);
}
}
}
}
static void props_set_shared_icon(FilePropertiesDialogState* st, const SvgIcon* icon) {
if (!st || !icon || !icon->pixels) return;
st->icon = *icon;
st->owns_icon = false;
}
static void props_load_owned_icon(FilePropertiesDialogState* st, const char* path) {
if (!st || !path || !path[0]) return;
SvgIcon icon = svg_load(path, 48, 48, colors::ICON_COLOR);
if (!icon.pixels) return;
st->icon = icon;
st->owns_icon = true;
}
static void props_choose_icon(FilePropertiesDialogState* st,
const FileManagerState* fm,
int idx) {
if (!st || !fm || idx < 0 || idx >= fm->entry_count) return;
DesktopState* ds = fm->desktop;
int type = fm->entry_types[idx];
st->fallback_is_dir = fm->is_dir[idx] || type == FM_ENTRY_DIR ||
type == FM_ENTRY_SPECIAL_DIR || type == FM_ENTRY_HOME ||
type == FM_ENTRY_APPS_ROOT || type == FM_ENTRY_DRIVE;
int sfi = -1;
if (type == FM_ENTRY_SPECIAL_DIR) sfi = fm->drive_indices[idx];
else if (ds && type == FM_ENTRY_DIR) sfi = special_folder_index(fm->entry_names[idx]);
if (sfi >= 0 && ds && ds->icon_special_folder_lg[sfi].pixels) {
props_set_shared_icon(st, &ds->icon_special_folder_lg[sfi]);
} else if (type == FM_ENTRY_VIRTUAL_ITEM && idx < fm->virtual_entry_count) {
const FileManagerVirtualEntry& entry = fm->virtual_entries[idx];
if (ds && entry.desktop_item_index >= 0
&& entry.desktop_item_index < ds->desktop_item_count) {
props_load_owned_icon(
st, ds->desktop_items[entry.desktop_item_index].icon_path);
}
} else if (ds && type == FM_ENTRY_APPS_ROOT && ds->icon_apps_lg.pixels) {
props_set_shared_icon(st, &ds->icon_apps_lg);
} else if (ds && type == FM_ENTRY_HOME && ds->icon_home_folder_lg.pixels) {
props_set_shared_icon(st, &ds->icon_home_folder_lg);
} else if (ds && type == FM_ENTRY_DRIVE && ds->icon_drive_lg.pixels) {
props_set_shared_icon(st, &ds->icon_drive_lg);
} else if (ds && type == FM_ENTRY_DIR && ds->icon_folder_lg.pixels) {
props_set_shared_icon(st, &ds->icon_folder_lg);
} else if (ds && type == FM_ENTRY_EXEC && ds->icon_exec_lg.pixels) {
props_set_shared_icon(st, &ds->icon_exec_lg);
} else if (const SvgIcon* type_icon = type == FM_ENTRY_FILE
? filemanager_file_type_icon(fm->entry_names[idx], true) : nullptr) {
props_set_shared_icon(st, type_icon);
} else if (ds && ds->icon_file_lg.pixels) {
props_set_shared_icon(st, &ds->icon_file_lg);
}
}
static bool props_entry_is_directory_like(const FileManagerState* fm, int idx) {
if (!fm || idx < 0 || idx >= fm->entry_count) return false;
int type = fm->entry_types[idx];
return fm->is_dir[idx] ||
type == FM_ENTRY_DIR ||
type == FM_ENTRY_HOME ||
type == FM_ENTRY_SPECIAL_DIR ||
type == FM_ENTRY_DRIVE;
}
static void props_populate(FilePropertiesDialogState* st,
const FileManagerState* fm,
int idx) {
props_copy(st->name, sizeof(st->name), fm->entry_names[idx]);
props_copy(st->type, sizeof(st->type), props_type_label(fm, idx));
props_build_location(fm, st->location, sizeof(st->location));
props_build_path(fm, idx, st->path, sizeof(st->path));
st->has_path = st->path[0] != '\0';
if (!props_entry_is_directory_like(fm, idx) && !filemanager_is_computer_view(fm) &&
!filemanager_is_virtual_view(fm)) {
format_size(st->size, fm->entry_sizes[idx]);
st->has_size = true;
}
// Modified time (files and directories), when the filesystem recorded one.
if (!filemanager_is_computer_view(fm) && !filemanager_is_virtual_view(fm) &&
fm->entry_mtimes[idx] > 0) {
format_datetime(st->modified, sizeof(st->modified),
fm->entry_mtimes[idx], montauk::gettz());
st->has_modified = true;
}
if (props_entry_is_directory_like(fm, idx) && st->has_path) {
const char* names[256];
int count = montauk::readdir(st->path, names, 256);
if (count >= 0) {
if (count >= 256) snprintf(st->contains, sizeof(st->contains), "256+ items");
else snprintf(st->contains, sizeof(st->contains), "%d item%s",
count, count == 1 ? "" : "s");
st->has_contains = true;
}
}
props_choose_icon(st, fm, idx);
}
static void props_draw_icon(Canvas& c, FilePropertiesDialogState* st, int x, int y) {
if (st->icon.pixels) {
c.icon(x + (48 - st->icon.width) / 2,
y + (48 - st->icon.height) / 2,
st->icon);
return;
}
Color fallback = st->fallback_is_dir
? Color::from_rgb(0xFF, 0xBD, 0x2E)
: Color::from_rgb(0x90, 0x90, 0x90);
c.fill_rounded_rect(x, y, 48, 48, 4, fallback);
}
static int props_draw_row(Canvas& c,
const mtk::Theme& theme,
int x,
int y,
int label_w,
const char* label,
const char* value) {
int fh = system_font_height();
c.text(x, y, label, theme.text_muted);
char fit[512];
filemanager_fit_text(value, fit, sizeof(fit), c.w - x - label_w - 16);
c.text(x + label_w, y, fit, theme.text);
return y + fh + 8;
}
static void props_on_draw(Window* win, Framebuffer& fb) {
(void)fb;
FilePropertiesDialogState* st = (FilePropertiesDialogState*)win->app_data;
if (!st) return;
Canvas c(win);
mtk::Theme theme = mtk::make_theme(st->accent);
c.fill(theme.window_bg);
int pad = 16;
int fh = system_font_height();
props_draw_icon(c, st, pad, pad + 2);
char title[128];
filemanager_fit_text(st->name, title, sizeof(title), c.w - 88);
c.text(80, pad + 4, title, theme.text);
c.text(80, pad + 4 + fh + 4, st->type, theme.text_muted);
int y = pad + 68;
mtk::draw_separator(c, pad, y, c.w - 2 * pad, theme);
y += 14;
y = props_draw_row(c, theme, pad, y, 82, "Name", st->name);
y = props_draw_row(c, theme, pad, y, 82, "Type", st->type);
y = props_draw_row(c, theme, pad, y, 82, "Location", st->location);
if (st->has_path)
y = props_draw_row(c, theme, pad, y, 82, "Path", st->path);
if (st->has_size)
y = props_draw_row(c, theme, pad, y, 82, "Size", st->size);
if (st->has_contains)
y = props_draw_row(c, theme, pad, y, 82, "Contains", st->contains);
if (st->has_modified)
y = props_draw_row(c, theme, pad, y, 82, "Modified", st->modified);
Rect close = props_close_button_rect(c.w, c.h);
mtk::draw_button(c, close, "Close", mtk::BUTTON_PRIMARY,
mtk::widget_state(false, close.contains(st->mouse_x, st->mouse_y), true),
theme);
}
static void props_on_mouse(Window* win, MouseEvent& ev) {
FilePropertiesDialogState* st = (FilePropertiesDialogState*)win->app_data;
if (!st) return;
mtk::DesktopHost host(win);
int mx = 0;
int my = 0;
if (!host.map_mouse(ev, &mx, &my)) return;
st->mouse_x = mx;
st->mouse_y = my;
Rect close = props_close_button_rect(win->content_w, win->content_h);
if (ev.left_pressed() && close.contains(mx, my))
props_close_self(st->desktop, st);
}
static void props_on_key(Window* win, const montauk::abi::KeyEvent& key) {
FilePropertiesDialogState* st = (FilePropertiesDialogState*)win->app_data;
if (!st || !key.pressed) return;
if (key.scancode == 0x01 || key.ascii == '\n' || key.ascii == '\r')
props_close_self(st->desktop, st);
}
static void props_on_close(Window* win) {
FilePropertiesDialogState* st = (FilePropertiesDialogState*)win->app_data;
if (!st) return;
if (st->owns_icon) svg_free(st->icon);
montauk::mfree(st);
win->app_data = nullptr;
}
// Subtitle wording, shared between the width calculation at creation time and
// the draw path so the two never drift out of sync.
static const char* const kDeleteSubMulti =
"They will be deleted for good, folders with their contents.";
static const char* const kDeleteSubSingle =
"The folder and all of its contents will be deleted for good.";
static const char* const kDeleteSubFile =
"It will be deleted for good. This can't be undone.";
static const char* delete_confirm_subtitle(const FileDeleteConfirmDialogState* st) {
if (st->count > 1) return kDeleteSubMulti;
return st->is_dir ? kDeleteSubSingle : kDeleteSubFile;
}
// Pick a dialog width wide enough that the subtitle (drawn at x=80 with a 16px
// right margin) fits without being clipped. Falls back to the original 380px
// minimum and is capped so an absurd subtitle cannot create a huge window.
static int delete_confirm_dialog_width(const char* sub) {
int needed = 80 + text_width(sub) + 16;
int w = 380;
if (needed > w) w = needed;
if (w > 560) w = 560;
return w;
}
static void delete_confirm_button_rects(int content_w,
int content_h,
Rect* delete_btn,
Rect* cancel_btn) {
int btn_w = 92;
int btn_h = 30;
int gap = 8;
int y = content_h - 44;
int cancel_x = content_w - 16 - btn_w;
int delete_x = cancel_x - gap - btn_w;
if (delete_btn) *delete_btn = {delete_x, y, btn_w, btn_h};
if (cancel_btn) *cancel_btn = {cancel_x, y, btn_w, btn_h};
}
static FileManagerState* delete_confirm_live_owner(FileDeleteConfirmDialogState* st) {
if (!st || !st->desktop || !st->owner) return nullptr;
for (int i = 0; i < st->desktop->window_count; i++) {
Window& win = st->desktop->windows[i];
if (win.app_data == st->owner && win.on_draw == filemanager_on_draw)
return st->owner;
}
return nullptr;
}
static void delete_confirm_close(FileDeleteConfirmDialogState* st) {
if (!st) return;
props_close_self(st->desktop, st);
}
static void delete_confirm_apply(FileDeleteConfirmDialogState* st) {
if (!st) return;
FileManagerState* owner = delete_confirm_live_owner(st);
int n = st->count > 0 ? st->count : (st->path[0] ? 1 : 0);
// A single small file deletes inline, without a progress dialog flash.
// Anything bigger frees many blocks, which can stall the desktop.
bool inline_ok = false;
if (n == 1 && !st->is_dir) {
montauk::abi::FileStat stt;
const char* p = st->count > 0 ? st->paths[0] : st->path;
inline_ok = montauk::stat(p, &stt) == 0 && stt.size <= FILEMANAGER_DELETE_INLINE_MAX_BYTES;
}
// Delete on a worker thread with a progress dialog. The job copies the path
// list, so it stays valid after this confirm dialog is freed.
bool started = false;
if (owner && n > 0 && !inline_ok) {
const char** arr = (const char**)montauk::malloc((uint64_t)n * sizeof(const char*));
if (arr) {
for (int i = 0; i < n; i++)
arr[i] = (st->count > 0) ? st->paths[i] : st->path;
started = filemanager_start_delete_job(owner, arr, n);
montauk::mfree(arr);
}
}
if (!started) {
// Fallback: delete synchronously (e.g. another operation is running).
bool any = false;
int failed = 0;
const char* first_failed_path = nullptr;
montauk::StatusCode first_error = montauk::Success;
for (int i = 0; i < n; i++) {
const char* p = (st->count > 0) ? st->paths[i] : st->path;
if (!p || !p[0]) continue;
montauk::StatusCode status = filemanager_delete_item(p);
if (status == montauk::Success) {
any = true;
} else if (failed++ == 0) {
first_failed_path = p;
first_error = status;
}
}
if (owner && any) {
filemanager_clear_multi_selection(owner);
filemanager_read_dir(owner);
}
if (failed > 0) {
char heading[192];
if (failed == 1)
snprintf(heading, sizeof(heading), "Couldn't delete \"%s\"",
path_basename(first_failed_path));
else
snprintf(heading, sizeof(heading), "Couldn't delete %d of %d items", failed, n);
filemanager_show_error(st->desktop, st->owner, heading,
first_failed_path, first_error);
}
}
if (owner && owner->delete_confirm_dialog == st)
owner->delete_confirm_dialog = nullptr;
delete_confirm_close(st);
}
static void delete_confirm_on_draw(Window* win, Framebuffer& fb) {
(void)fb;
FileDeleteConfirmDialogState* st = (FileDeleteConfirmDialogState*)win->app_data;
if (!st) return;
Canvas c(win);
mtk::Theme theme = mtk::make_theme(st->accent);
c.fill(theme.window_bg);
int pad = 16;
int fh = system_font_height();
if (st->icon.pixels) {
c.icon(pad + (48 - st->icon.width) / 2,
22 + (48 - st->icon.height) / 2,
st->icon);
} else {
c.fill_rounded_rect(pad, 22, 48, 48, 4, Color::from_rgb(0xFF, 0xBD, 0x2E));
}
char title[160];
if (st->title[0])
snprintf(title, sizeof(title), "%s", st->title);
else if (st->count > 1)
snprintf(title, sizeof(title), "Permanently delete %d items?", st->count);
else if (st->is_dir)
snprintf(title, sizeof(title), "Permanently delete folder \"%s\"?", st->name);
else
snprintf(title, sizeof(title), "Permanently delete \"%s\"?", st->name);
char title_fit[160];
filemanager_fit_text(title, title_fit, sizeof(title_fit), c.w - 88);
c.text(80, 22, title_fit, theme.text);
const char* sub = delete_confirm_subtitle(st);
char sub_fit[160];
filemanager_fit_text(sub, sub_fit, sizeof(sub_fit), c.w - 96);
c.text(80, 22 + fh + 8, sub_fit, theme.text_muted);
if (st->count <= 1) {
char path_fit[512];
filemanager_fit_text(st->path, path_fit, sizeof(path_fit), c.w - 96);
c.text(80, 22 + fh * 2 + 16, path_fit, theme.text_muted);
}
Rect delete_btn;
Rect cancel_btn;
delete_confirm_button_rects(c.w, c.h, &delete_btn, &cancel_btn);
mtk::draw_button(c, delete_btn, "Delete", mtk::BUTTON_DANGER,
mtk::widget_state(false, delete_btn.contains(st->mouse_x, st->mouse_y), true),
theme);
mtk::draw_button(c, cancel_btn, "Cancel", mtk::BUTTON_SECONDARY,
mtk::widget_state(false, cancel_btn.contains(st->mouse_x, st->mouse_y), true),
theme);
}
static void delete_confirm_on_mouse(Window* win, MouseEvent& ev) {
FileDeleteConfirmDialogState* st = (FileDeleteConfirmDialogState*)win->app_data;
if (!st) return;
mtk::DesktopHost host(win);
int mx = 0;
int my = 0;
if (!host.map_mouse(ev, &mx, &my)) return;
Rect delete_btn;
Rect cancel_btn;
delete_confirm_button_rects(win->content_w, win->content_h, &delete_btn, &cancel_btn);
bool old_delete = delete_btn.contains(st->mouse_x, st->mouse_y);
bool old_cancel = cancel_btn.contains(st->mouse_x, st->mouse_y);
bool new_delete = delete_btn.contains(mx, my);
bool new_cancel = cancel_btn.contains(mx, my);
st->mouse_x = mx;
st->mouse_y = my;
if (old_delete != new_delete || old_cancel != new_cancel)
host.invalidate();
if (ev.left_pressed()) {
if (new_delete) {
delete_confirm_apply(st);
} else if (new_cancel) {
delete_confirm_close(st);
}
}
}
static void delete_confirm_on_key(Window* win, const montauk::abi::KeyEvent& key) {
FileDeleteConfirmDialogState* st = (FileDeleteConfirmDialogState*)win->app_data;
if (!st || !key.pressed) return;
if (key.ascii == '\n' || key.ascii == '\r') {
delete_confirm_apply(st);
} else if (key.scancode == 0x01) {
delete_confirm_close(st);
}
}
static void delete_confirm_on_close(Window* win) {
FileDeleteConfirmDialogState* st = (FileDeleteConfirmDialogState*)win->app_data;
if (!st) return;
FileManagerState* owner = delete_confirm_live_owner(st);
if (owner && owner->delete_confirm_dialog == st)
owner->delete_confirm_dialog = nullptr;
if (st->paths) montauk::mfree(st->paths);
montauk::mfree(st);
win->app_data = nullptr;
}
void filemanager_show_directory_delete_confirmation(FileManagerState* fm, int target_idx) {
if (!fm || !fm->desktop || target_idx < 0 || target_idx >= fm->entry_count)
return;
if (!fm->is_dir[target_idx] || filemanager_is_computer_view(fm) ||
filemanager_is_virtual_view(fm) || fm->entry_types[target_idx] == FM_ENTRY_DRIVE)
return;
DesktopState* ds = fm->desktop;
if (fm->delete_confirm_dialog) {
for (int i = 0; i < ds->window_count; i++) {
if (ds->windows[i].app_data == fm->delete_confirm_dialog) {
desktop_raise_window(ds, i);
return;
}
}
fm->delete_confirm_dialog = nullptr;
}
FileDeleteConfirmDialogState* st =
(FileDeleteConfirmDialogState*)montauk::malloc(sizeof(FileDeleteConfirmDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(FileDeleteConfirmDialogState));
st->desktop = ds;
st->owner = fm;
st->accent = ds->settings.accent_color;
props_copy(st->name, sizeof(st->name), fm->entry_names[target_idx]);
filemanager_build_fullpath(st->path, sizeof(st->path),
fm->current_path, fm->entry_names[target_idx]);
st->is_dir = true;
int sfi = -1;
if (fm->entry_types[target_idx] == FM_ENTRY_SPECIAL_DIR)
sfi = fm->drive_indices[target_idx];
else if (fm->entry_types[target_idx] == FM_ENTRY_DIR)
sfi = special_folder_index(fm->entry_names[target_idx]);
if (sfi >= 0 && ds->icon_special_folder_lg[sfi].pixels) {
st->icon = ds->icon_special_folder_lg[sfi];
} else if (ds->icon_folder_lg.pixels) {
st->icon = ds->icon_folder_lg;
}
int w = delete_confirm_dialog_width(kDeleteSubSingle);
int h = 180;
int wx = 0;
int wy = 0;
props_center_dialog(ds, fm, w, h, &wx, &wy);
int idx = desktop_create_window(ds, "Delete Folder", wx, wy, w, h);
if (idx < 0) {
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
fm->delete_confirm_dialog = st;
win->app_data = st;
win->on_draw = delete_confirm_on_draw;
win->on_mouse = delete_confirm_on_mouse;
win->on_key = delete_confirm_on_key;
win->on_close = delete_confirm_on_close;
}
void filemanager_show_bulk_delete_confirmation(FileManagerState* fm,
const int* indices, int count) {
if (!fm || !fm->desktop || !indices || count <= 0) return;
if (filemanager_is_computer_view(fm) || filemanager_is_virtual_view(fm)) return;
DesktopState* ds = fm->desktop;
if (fm->delete_confirm_dialog) {
for (int i = 0; i < ds->window_count; i++) {
if (ds->windows[i].app_data == fm->delete_confirm_dialog) {
desktop_raise_window(ds, i);
return;
}
}
fm->delete_confirm_dialog = nullptr;
}
FileDeleteConfirmDialogState* st =
(FileDeleteConfirmDialogState*)montauk::malloc(sizeof(FileDeleteConfirmDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(FileDeleteConfirmDialogState));
st->desktop = ds;
st->owner = fm;
st->accent = ds->settings.accent_color;
st->paths = (char(*)[512])montauk::malloc((uint64_t)count * 512);
if (!st->paths) {
montauk::mfree(st);
return;
}
int out = 0;
for (int i = 0; i < count; i++) {
int idx = indices[i];
if (idx < 0 || idx >= fm->entry_count) continue;
if (fm->entry_types[idx] == FM_ENTRY_DRIVE) continue;
filemanager_build_fullpath(st->paths[out], 512,
fm->current_path, fm->entry_names[idx]);
out++;
}
if (out == 0) {
montauk::mfree(st->paths);
montauk::mfree(st);
return;
}
st->count = out;
// For the single-item case, populate name/path so the dialog renders the
// original "Delete folder X?" wording.
if (out == 1) {
int idx = indices[0];
props_copy(st->name, sizeof(st->name), fm->entry_names[idx]);
montauk::strncpy(st->path, st->paths[0], sizeof(st->path) - 1);
st->is_dir = fm->is_dir[idx];
} else {
snprintf(st->name, sizeof(st->name), "%d items", out);
}
const SvgIcon* type_icon = out == 1 && fm->entry_types[indices[0]] == FM_ENTRY_FILE
? filemanager_file_type_icon(fm->entry_names[indices[0]], true) : nullptr;
const SvgIcon& icon = type_icon ? *type_icon
: (out == 1 && !st->is_dir) ? ds->icon_file_lg : ds->icon_folder_lg;
if (icon.pixels) st->icon = icon;
int w = delete_confirm_dialog_width(delete_confirm_subtitle(st));
int h = 180;
int wx = 0;
int wy = 0;
props_center_dialog(ds, fm, w, h, &wx, &wy);
const char* win_title = out > 1 ? "Delete Items" : st->is_dir ? "Delete Folder" : "Delete File";
int idx = desktop_create_window(ds, win_title, wx, wy, w, h);
if (idx < 0) {
montauk::mfree(st->paths);
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
fm->delete_confirm_dialog = st;
win->app_data = st;
win->on_draw = delete_confirm_on_draw;
win->on_mouse = delete_confirm_on_mouse;
win->on_key = delete_confirm_on_key;
win->on_close = delete_confirm_on_close;
}
void filemanager_show_paths_delete_confirmation(FileManagerState* fm, const char* title,
const char* const* paths, int count) {
if (!fm || !fm->desktop || !paths || count <= 0) return;
DesktopState* ds = fm->desktop;
if (fm->delete_confirm_dialog) {
for (int i = 0; i < ds->window_count; i++) {
if (ds->windows[i].app_data == fm->delete_confirm_dialog) {
desktop_raise_window(ds, i);
return;
}
}
fm->delete_confirm_dialog = nullptr;
}
FileDeleteConfirmDialogState* st =
(FileDeleteConfirmDialogState*)montauk::malloc(sizeof(FileDeleteConfirmDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(FileDeleteConfirmDialogState));
st->desktop = ds;
st->owner = fm;
st->accent = ds->settings.accent_color;
st->paths = (char(*)[512])montauk::malloc((uint64_t)count * 512);
if (!st->paths) {
montauk::mfree(st);
return;
}
for (int i = 0; i < count; i++) montauk::strncpy(st->paths[i], paths[i], 512);
st->count = count;
props_copy(st->title, sizeof(st->title), title);
props_copy(st->name, sizeof(st->name), path_basename(paths[0]));
montauk::strncpy(st->path, paths[0], sizeof(st->path));
// One item: describe it as a folder or a file.
st->is_dir = count == 1 && filemanager_path_is_dir(paths[0]);
if (ds->icon_trash_full_lg.pixels) st->icon = ds->icon_trash_full_lg;
int w = delete_confirm_dialog_width(delete_confirm_subtitle(st));
int h = 180;
int wx = 0;
int wy = 0;
props_center_dialog(ds, fm, w, h, &wx, &wy);
int idx = desktop_create_window(ds, title, wx, wy, w, h);
if (idx < 0) {
montauk::mfree(st->paths);
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
fm->delete_confirm_dialog = st;
win->app_data = st;
win->on_draw = delete_confirm_on_draw;
win->on_mouse = delete_confirm_on_mouse;
win->on_key = delete_confirm_on_key;
win->on_close = delete_confirm_on_close;
}
// ===== File operation error dialog =====
struct FileErrorDialogState {
DesktopState* desktop;
Color accent;
int mouse_x;
int mouse_y;
char heading[192]; // what failed, e.g. "Couldn't delete "foo""
char path[256]; // the item involved (optional)
char cause[4][160]; // wrapped GetStatusExplanationText() lines
int cause_lines;
SvgIcon icon; // owned
};
static constexpr int kErrorDialogW = 440;
static constexpr int kErrorTextX = 80;
static constexpr int kErrorTop = 22;
// Greedy word wrap of `src` into at most `max_lines` lines no wider than
// `max_w`. A word that cannot fit on a line of its own, and any text left over
// for the last line, is ellipsized.
static int error_wrap_text(const char* src, char (*lines)[160], int max_lines, int max_w) {
int count = 0;
int pos = 0;
while (src[pos] && count < max_lines) {
while (src[pos] == ' ') pos++;
if (!src[pos]) break;
if (count == max_lines - 1) {
filemanager_fit_text(src + pos, lines[count++], 160, max_w);
break;
}
// Extend the line one word at a time while it still fits.
char buf[160];
int best = -1;
int end = pos;
for (;;) {
while (src[end] && src[end] != ' ') end++;
int n = end - pos;
if (n > 159) break;
montauk::memcpy(buf, src + pos, n);
buf[n] = '\0';
if (text_width(buf) > max_w) break;
best = end;
if (!src[end]) break;
end++;
}
if (best < 0) {
// The first word alone is too wide: truncate it and skip past it.
int word_end = pos;
while (src[word_end] && src[word_end] != ' ') word_end++;
int n = word_end - pos;
if (n > 159) n = 159;
montauk::memcpy(buf, src + pos, n);
buf[n] = '\0';
filemanager_fit_text(buf, lines[count++], 160, max_w);
pos = word_end;
} else {
int n = best - pos;
montauk::memcpy(lines[count], src + pos, n);
lines[count][n] = '\0';
count++;
pos = best;
}
}
return count;
}
// Top-down text layout shared by the draw path and the window sizing.
static int error_path_y(int fh) { return kErrorTop + fh + 8; }
static int error_cause_y(const FileErrorDialogState* st, int fh) {
return st->path[0] ? error_path_y(fh) + fh + 12 : error_path_y(fh);
}
static Rect error_ok_button_rect(int content_w, int content_h) {
return {content_w - 96, content_h - 42, 80, 28};
}
static void error_on_draw(Window* win, Framebuffer& fb) {
(void)fb;
FileErrorDialogState* st = (FileErrorDialogState*)win->app_data;
if (!st) return;
Canvas c(win);
mtk::Theme theme = mtk::make_theme(st->accent);
c.fill(theme.window_bg);
int pad = 16;
int fh = system_font_height();
int text_w = c.w - kErrorTextX - pad;
if (st->icon.pixels) {
c.icon(pad + (48 - st->icon.width) / 2,
kErrorTop + (48 - st->icon.height) / 2,
st->icon);
} else {
c.fill_rounded_rect(pad, kErrorTop, 48, 48, 24, theme.danger);
c.text(pad + (48 - text_width("!")) / 2, kErrorTop + (48 - fh) / 2, "!",
theme.danger_fg);
}
char fit[192];
filemanager_fit_text(st->heading, fit, sizeof(fit), text_w);
c.text(kErrorTextX, kErrorTop, fit, theme.text);
if (st->path[0]) {
filemanager_fit_text(st->path, fit, sizeof(fit), text_w);
c.text(kErrorTextX, error_path_y(fh), fit, theme.text_muted);
}
int y = error_cause_y(st, fh);
for (int i = 0; i < st->cause_lines; i++) {
c.text(kErrorTextX, y, st->cause[i], theme.text);
y += fh + 6;
}
Rect ok = error_ok_button_rect(c.w, c.h);
mtk::draw_button(c, ok, "OK", mtk::BUTTON_PRIMARY,
mtk::widget_state(false, ok.contains(st->mouse_x, st->mouse_y), true),
theme);
}
static void error_on_mouse(Window* win, MouseEvent& ev) {
FileErrorDialogState* st = (FileErrorDialogState*)win->app_data;
if (!st) return;
mtk::DesktopHost host(win);
int mx = 0;
int my = 0;
if (!host.map_mouse(ev, &mx, &my)) return;
Rect ok = error_ok_button_rect(win->content_w, win->content_h);
bool old_hover = ok.contains(st->mouse_x, st->mouse_y);
bool new_hover = ok.contains(mx, my);
st->mouse_x = mx;
st->mouse_y = my;
if (old_hover != new_hover) host.invalidate();
if (ev.left_pressed() && new_hover)
props_close_self(st->desktop, st);
}
static void error_on_key(Window* win, const montauk::abi::KeyEvent& key) {
FileErrorDialogState* st = (FileErrorDialogState*)win->app_data;
if (!st || !key.pressed) return;
if (key.scancode == 0x01 || key.ascii == '\n' || key.ascii == '\r')
props_close_self(st->desktop, st);
}
static void error_on_close(Window* win) {
FileErrorDialogState* st = (FileErrorDialogState*)win->app_data;
if (!st) return;
if (st->icon.pixels) svg_free(st->icon);
montauk::mfree(st);
win->app_data = nullptr;
}
void filemanager_show_error(DesktopState* ds, const void* owner_app_data,
const char* heading, const char* path,
montauk::StatusCode status) {
if (!ds) return;
FileErrorDialogState* st =
(FileErrorDialogState*)montauk::malloc(sizeof(FileErrorDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(FileErrorDialogState));
st->desktop = ds;
st->accent = ds->settings.accent_color;
props_copy(st->heading, sizeof(st->heading), heading ? heading : "The operation failed");
props_copy(st->path, sizeof(st->path), path);
int content_w = kErrorDialogW - 2 * gui::BORDER_WIDTH;
if (status != montauk::Success) {
st->cause_lines = error_wrap_text(montauk::GetStatusExplanationText(status),
st->cause, 4, content_w - kErrorTextX - 16);
}
st->icon = svg_load("0:/os/icons/dialog-error.svg", 48, 48, colors::ICON_COLOR);
int fh = system_font_height();
int text_bottom = error_cause_y(st, fh) + st->cause_lines * (fh + 6);
int body_bottom = kErrorTop + 48;
if (text_bottom > body_bottom) body_bottom = text_bottom;
int content_h = body_bottom + 18 /*gap*/ + 28 /*button*/ + 14 /*bottom margin*/;
int w = kErrorDialogW;
int h = content_h + gui::TITLEBAR_HEIGHT + gui::BORDER_WIDTH;
int wx = 0;
int wy = 0;
props_center_dialog(ds, owner_app_data, w, h, &wx, &wy);
int idx = desktop_create_window(ds, "Error", wx, wy, w, h);
if (idx < 0) {
if (st->icon.pixels) svg_free(st->icon);
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
win->app_data = st;
win->on_draw = error_on_draw;
win->on_mouse = error_on_mouse;
win->on_key = error_on_key;
win->on_close = error_on_close;
}
// ===== Multi-item Properties dialog =====
struct MultiPropertiesDialogState {
DesktopState* desktop;
Color accent;
int mouse_x;
int mouse_y;
int total_count;
int file_count;
int folder_count;
char title[64]; // "5 items"
char breakdown[64]; // "3 files, 2 folders"
char location[256];
char size[64]; // sum of file sizes; "Unknown" when only folders
bool has_size;
SvgIcon icon; // shared, not owned
};
static void multi_props_on_draw(Window* win, Framebuffer& fb) {
(void)fb;
MultiPropertiesDialogState* st = (MultiPropertiesDialogState*)win->app_data;
if (!st) return;
Canvas c(win);
mtk::Theme theme = mtk::make_theme(st->accent);
c.fill(theme.window_bg);
int pad = 16;
int fh = system_font_height();
if (st->icon.pixels) {
c.icon(pad + (48 - st->icon.width) / 2,
pad + 2 + (48 - st->icon.height) / 2,
st->icon);
} else {
c.fill_rounded_rect(pad, pad + 2, 48, 48, 4, Color::from_rgb(0x90, 0x90, 0x90));
}
c.text(80, pad + 4, st->title, theme.text);
c.text(80, pad + 4 + fh + 4, st->breakdown, theme.text_muted);
int y = pad + 68;
mtk::draw_separator(c, pad, y, c.w - 2 * pad, theme);
y += 14;
y = props_draw_row(c, theme, pad, y, 92, "Items", st->title);
y = props_draw_row(c, theme, pad, y, 92, "Contents", st->breakdown);
y = props_draw_row(c, theme, pad, y, 92, "Location", st->location);
if (st->has_size)
y = props_draw_row(c, theme, pad, y, 92, "Total size", st->size);
Rect close = props_close_button_rect(c.w, c.h);
mtk::draw_button(c, close, "Close", mtk::BUTTON_PRIMARY,
mtk::widget_state(false, close.contains(st->mouse_x, st->mouse_y), true),
theme);
}
static void multi_props_on_mouse(Window* win, MouseEvent& ev) {
MultiPropertiesDialogState* st = (MultiPropertiesDialogState*)win->app_data;
if (!st) return;
mtk::DesktopHost host(win);
int mx = 0;
int my = 0;
if (!host.map_mouse(ev, &mx, &my)) return;
bool old_hover = props_close_button_rect(win->content_w, win->content_h)
.contains(st->mouse_x, st->mouse_y);
st->mouse_x = mx;
st->mouse_y = my;
Rect close = props_close_button_rect(win->content_w, win->content_h);
bool new_hover = close.contains(mx, my);
if (old_hover != new_hover) host.invalidate();
if (ev.left_pressed() && new_hover)
props_close_self(st->desktop, st);
}
static void multi_props_on_key(Window* win, const montauk::abi::KeyEvent& key) {
MultiPropertiesDialogState* st = (MultiPropertiesDialogState*)win->app_data;
if (!st || !key.pressed) return;
if (key.scancode == 0x01 || key.ascii == '\n' || key.ascii == '\r')
props_close_self(st->desktop, st);
}
static void multi_props_on_close(Window* win) {
MultiPropertiesDialogState* st = (MultiPropertiesDialogState*)win->app_data;
if (!st) return;
montauk::mfree(st);
win->app_data = nullptr;
}
void filemanager_show_multi_properties(FileManagerState* fm,
const int* indices, int count) {
if (!fm || !fm->desktop || !indices || count <= 0) return;
DesktopState* ds = fm->desktop;
MultiPropertiesDialogState* st =
(MultiPropertiesDialogState*)montauk::malloc(sizeof(MultiPropertiesDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(MultiPropertiesDialogState));
st->desktop = ds;
st->accent = ds->settings.accent_color;
uint64_t total_size = 0;
bool any_file = false;
for (int i = 0; i < count; i++) {
int idx = indices[i];
if (idx < 0 || idx >= fm->entry_count) continue;
st->total_count++;
if (fm->is_dir[idx]) {
st->folder_count++;
} else {
st->file_count++;
total_size += fm->entry_sizes[idx];
any_file = true;
}
}
if (st->total_count == 0) {
montauk::mfree(st);
return;
}
snprintf(st->title, sizeof(st->title), "%d item%s",
st->total_count, st->total_count == 1 ? "" : "s");
if (st->file_count > 0 && st->folder_count > 0) {
snprintf(st->breakdown, sizeof(st->breakdown), "%d file%s, %d folder%s",
st->file_count, st->file_count == 1 ? "" : "s",
st->folder_count, st->folder_count == 1 ? "" : "s");
} else if (st->file_count > 0) {
snprintf(st->breakdown, sizeof(st->breakdown), "%d file%s",
st->file_count, st->file_count == 1 ? "" : "s");
} else {
snprintf(st->breakdown, sizeof(st->breakdown), "%d folder%s",
st->folder_count, st->folder_count == 1 ? "" : "s");
}
props_build_location(fm, st->location, sizeof(st->location));
if (any_file) {
format_size(st->size, total_size);
if (st->folder_count > 0)
str_append(st->size, " (folders not counted)", sizeof(st->size));
st->has_size = true;
}
// Pick a representative icon: prefer folder if any folders, else file.
if (st->folder_count > 0 && ds->icon_folder_lg.pixels)
st->icon = ds->icon_folder_lg;
else if (ds->icon_file_lg.pixels)
st->icon = ds->icon_file_lg;
int w = 440;
int h = 280;
int wx = 0;
int wy = 0;
props_center_dialog(ds, fm, w, h, &wx, &wy);
char title[64];
snprintf(title, sizeof(title), "%d Items Properties", st->total_count);
int idx = desktop_create_window(ds, title, wx, wy, w, h);
if (idx < 0) {
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
win->app_data = st;
win->on_draw = multi_props_on_draw;
win->on_mouse = multi_props_on_mouse;
win->on_key = multi_props_on_key;
win->on_close = multi_props_on_close;
}
void filemanager_show_properties(FileManagerState* fm, int target_idx) {
if (!fm || target_idx < 0 || target_idx >= fm->entry_count || !fm->desktop)
return;
DesktopState* ds = fm->desktop;
FilePropertiesDialogState* st =
(FilePropertiesDialogState*)montauk::malloc(sizeof(FilePropertiesDialogState));
if (!st) return;
montauk::memset(st, 0, sizeof(FilePropertiesDialogState));
st->desktop = ds;
st->accent = ds->settings.accent_color;
props_populate(st, fm, target_idx);
int w = 420;
int h = 330;
int wx = 0;
int wy = 0;
props_center_dialog(ds, fm, w, h, &wx, &wy);
char title[64];
snprintf(title, sizeof(title), "%s Properties", st->name);
int idx = desktop_create_window(ds, title, wx, wy, w, h);
if (idx < 0) {
if (st->owns_icon) svg_free(st->icon);
montauk::mfree(st);
return;
}
Window* win = &ds->windows[idx];
win->app_data = st;
win->on_draw = props_on_draw;
win->on_mouse = props_on_mouse;
win->on_key = props_on_key;
win->on_close = props_on_close;
}
} // namespace filemanager