From 2e04505df0957d0d6806fcb301dc86a25a1cf519 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 15 Nov 2022 22:30:49 +1100 Subject: [PATCH] Save file dialog on Windows and Linux now also defaults to showing text entered in input save menu input field, also fallback to reading from /etc/machine-id for getting machine ID on Linux for e.g. Flatpaks --- src/EnvRenderer.c | 2 +- src/Platform_Posix.c | 11 ++++++++--- src/Window_Win.c | 9 +++++---- src/Window_X11.c | 6 ++++++ src/interop_cocoa.m | 2 ++ 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index c4a71d91f..8b685c8b0 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -811,7 +811,7 @@ static void OnContextRecreated(void* obj) { void EnvRenderer_SetMode(int flags) { EnvRenderer_Legacy = flags & ENV_LEGACY; EnvRenderer_Minimal = flags & ENV_MINIMAL; - OnContextRecreated(NULL); + OnContextRecreated(NULL); } int EnvRenderer_CalcFlags(const cc_string* mode) { diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 38baa5fd7..a89ffd97c 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -1096,14 +1096,19 @@ static void DecodeMachineID(char* tmp, int len, cc_uint32* key) { } #if defined CC_BUILD_LINUX -/* Read /var/lib/dbus/machine-id for the key */ +/* Read /var/lib/dbus/machine-id or /etc/machine-id for the key */ static cc_result GetMachineID(cc_uint32* key) { - const cc_string idFile = String_FromConst("/var/lib/dbus/machine-id"); + const cc_string idFile = String_FromConst("/var/lib/dbus/machine-id"); + const cc_string altFile = String_FromConst("/etc/machine-id"); char tmp[MACHINEID_LEN]; struct Stream s; cc_result res; - if ((res = Stream_OpenFile(&s, &idFile))) return res; + /* Some machines only have dbus id, others only have etc id */ + res = Stream_OpenFile(&s, &idFile); + if (res) res = Stream_OpenFile(&s, &altFile); + if (res) return res; + res = Stream_Read(&s, tmp, MACHINEID_LEN); if (!res) DecodeMachineID(tmp, MACHINEID_LEN, key); diff --git a/src/Window_Win.c b/src/Window_Win.c index 60dccb151..df2b93448 100644 --- a/src/Window_Win.c +++ b/src/Window_Win.c @@ -551,8 +551,8 @@ static void ShowDialogCore(const char* title, const char* msg) { MessageBoxA(win_handle, msg, title, 0); } -static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback callback, - const char* const* fileExts, cc_bool load) { +static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback callback, cc_bool load, + const char* const* fileExts, const cc_string* defaultName) { cc_string path; char pathBuffer[NATIVE_STR_LEN]; WCHAR str[MAX_PATH] = { 0 }; OPENFILENAMEW ofn = { 0 }; @@ -560,6 +560,7 @@ static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback BOOL ok; int i; + Platform_EncodeUtf16(str, defaultName); Platform_EncodeUtf16(filter, filters); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = win_handle; @@ -608,7 +609,7 @@ cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) { } String_Append(&filters, '\0'); - return OpenSaveFileDialog(&filters, args->Callback, fileExts, true); + return OpenSaveFileDialog(&filters, args->Callback, true, fileExts, &String_Empty); } cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { @@ -626,7 +627,7 @@ cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { String_Format1(&filters, "*%c", fileExts[i]); String_Append(&filters, '\0'); } - return OpenSaveFileDialog(&filters, args->Callback, fileExts, false); + return OpenSaveFileDialog(&filters, args->Callback, false, fileExts, &args->defaultName); } static HDC draw_DC; diff --git a/src/Window_X11.c b/src/Window_X11.c index 32dbbc1f5..9ce11669e 100644 --- a/src/Window_X11.c +++ b/src/Window_X11.c @@ -6,6 +6,7 @@ #include "Bitmap.h" #include "Options.h" #include "Errors.h" +#include "Utils.h" #include #include #include @@ -1034,6 +1035,11 @@ cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { } String_AppendConst(&path, " --save --confirm-overwrite"); + /* TODO: Utf8 encode filename */ + if (args->defaultName.length) { + String_Format1(&path, " --filename='%s'", &args->defaultName); + } + path.buffer[path.length] = '\0'; return OpenSaveFileDialog(path.buffer, args->Callback, fileExts[0]); } diff --git a/src/interop_cocoa.m b/src/interop_cocoa.m index fcf6c6ac1..c7ab56a31 100644 --- a/src/interop_cocoa.m +++ b/src/interop_cocoa.m @@ -561,6 +561,8 @@ cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { NSString* str; const char* src; int len, i; + + // TODO: Use args->defaultName, but only macOS 10.6 NSMutableArray* types = GetOpenSaveFilters(args->filters); [dlg setAllowedFileTypes:types];