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

This commit is contained in:
UnknownShadow200 2022-11-15 22:30:49 +11:00
parent 9bbea3a4b7
commit 2e04505df0
5 changed files with 22 additions and 8 deletions

View File

@ -811,7 +811,7 @@ static void OnContextRecreated(void* obj) {
void EnvRenderer_SetMode(int flags) { void EnvRenderer_SetMode(int flags) {
EnvRenderer_Legacy = flags & ENV_LEGACY; EnvRenderer_Legacy = flags & ENV_LEGACY;
EnvRenderer_Minimal = flags & ENV_MINIMAL; EnvRenderer_Minimal = flags & ENV_MINIMAL;
OnContextRecreated(NULL); OnContextRecreated(NULL);
} }
int EnvRenderer_CalcFlags(const cc_string* mode) { int EnvRenderer_CalcFlags(const cc_string* mode) {

View File

@ -1096,14 +1096,19 @@ static void DecodeMachineID(char* tmp, int len, cc_uint32* key) {
} }
#if defined CC_BUILD_LINUX #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) { 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]; char tmp[MACHINEID_LEN];
struct Stream s; struct Stream s;
cc_result res; 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); res = Stream_Read(&s, tmp, MACHINEID_LEN);
if (!res) DecodeMachineID(tmp, MACHINEID_LEN, key); if (!res) DecodeMachineID(tmp, MACHINEID_LEN, key);

View File

@ -551,8 +551,8 @@ static void ShowDialogCore(const char* title, const char* msg) {
MessageBoxA(win_handle, msg, title, 0); MessageBoxA(win_handle, msg, title, 0);
} }
static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback callback, static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback callback, cc_bool load,
const char* const* fileExts, cc_bool load) { const char* const* fileExts, const cc_string* defaultName) {
cc_string path; char pathBuffer[NATIVE_STR_LEN]; cc_string path; char pathBuffer[NATIVE_STR_LEN];
WCHAR str[MAX_PATH] = { 0 }; WCHAR str[MAX_PATH] = { 0 };
OPENFILENAMEW ofn = { 0 }; OPENFILENAMEW ofn = { 0 };
@ -560,6 +560,7 @@ static cc_result OpenSaveFileDialog(const cc_string* filters, FileDialogCallback
BOOL ok; BOOL ok;
int i; int i;
Platform_EncodeUtf16(str, defaultName);
Platform_EncodeUtf16(filter, filters); Platform_EncodeUtf16(filter, filters);
ofn.lStructSize = sizeof(ofn); ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = win_handle; ofn.hwndOwner = win_handle;
@ -608,7 +609,7 @@ cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) {
} }
String_Append(&filters, '\0'); 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) { 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_Format1(&filters, "*%c", fileExts[i]);
String_Append(&filters, '\0'); String_Append(&filters, '\0');
} }
return OpenSaveFileDialog(&filters, args->Callback, fileExts, false); return OpenSaveFileDialog(&filters, args->Callback, false, fileExts, &args->defaultName);
} }
static HDC draw_DC; static HDC draw_DC;

View File

@ -6,6 +6,7 @@
#include "Bitmap.h" #include "Bitmap.h"
#include "Options.h" #include "Options.h"
#include "Errors.h" #include "Errors.h"
#include "Utils.h"
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/XKBlib.h> #include <X11/XKBlib.h>
@ -1034,6 +1035,11 @@ cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) {
} }
String_AppendConst(&path, " --save --confirm-overwrite"); 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'; path.buffer[path.length] = '\0';
return OpenSaveFileDialog(path.buffer, args->Callback, fileExts[0]); return OpenSaveFileDialog(path.buffer, args->Callback, fileExts[0]);
} }

View File

@ -561,6 +561,8 @@ cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) {
NSString* str; NSString* str;
const char* src; const char* src;
int len, i; int len, i;
// TODO: Use args->defaultName, but only macOS 10.6
NSMutableArray* types = GetOpenSaveFilters(args->filters); NSMutableArray* types = GetOpenSaveFilters(args->filters);
[dlg setAllowedFileTypes:types]; [dlg setAllowedFileTypes:types];