Skip to content

Commit

Permalink
Defer files opened by fopenFiles on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Mar 1, 2021
1 parent 4b8e37e commit d4683f9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
8 changes: 7 additions & 1 deletion inc/app.h
Expand Up @@ -11,14 +11,20 @@ typedef struct app_ {
struct theme_* theme;
int draw_mode; // shaded or wireframe
int draw_proj; // orthographic or perspective

unsigned deferred_count;
char** deferred_files;
} app_t;

/* Calls instance_run on every instance */
bool app_run(app_t* app);

/* Triggered from the UI */
/* Synchronously loads a file (triggered from UI menu items) */
struct instance_* app_open(app_t* app, const char* filename);

/* Loads a file when control returns to the event loop */
void app_defer_open(app_t* app, const char* filename);

/* Changes the view mode for the whole application */
void app_view_shaded(app_t* app);
void app_view_wireframe(app_t* app);
Expand Down
8 changes: 6 additions & 2 deletions platform/darwin.mm
Expand Up @@ -103,8 +103,12 @@ -(void)onAboutMenu {

void fopenFiles(id self, SEL _cmd, NSApplication* application,
NSArray<NSString *>* openFiles) {
// We defer loading files until control hits the main event loop. This
// prevents an issue when someone starts the app by dragging a file onto
// its icon, which would otherwise call fopenFiles within the first call
// to glfwCreateWindow, hanging until the app is re-focused.
for (NSString* t in openFiles) {
app_open(GLUE->app, [t UTF8String]);
app_defer_open(GLUE->app, [t UTF8String]);
}
}

Expand Down Expand Up @@ -148,7 +152,7 @@ void fopenFiles(id self, SEL _cmd, NSApplication* application,
window_new("", 1.0f, 1.0f);

// If no file was opened, then load the default
if (app->instance_count == 0) {
if (app->instance_count == 0 && !app->deferred_files) {
app_open(app, ":/sphere");
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/app.c
Expand Up @@ -34,6 +34,15 @@ instance_t* app_open(app_t* app, const char* filename) {
return instance;
}

void app_defer_open(app_t* app, const char* filename) {
const size_t len = strlen(filename);
char* f = calloc(1, len + 1);
strcpy(f, filename);
app->deferred_files = realloc(app->deferred_files,
sizeof(char*) * app->deferred_count + 1);
app->deferred_files[app->deferred_count++] = f;
}

void app_view_shaded(app_t* app) {
app->draw_mode = DRAW_SHADED;
for (unsigned i=0; i < app->instance_count; ++i) {
Expand Down Expand Up @@ -82,8 +91,20 @@ instance_t* app_get_front(app_t* app) {
}

bool app_run(app_t* app) {
unsigned i=0;
/* On some platforms, we defer loading of files until the event loop
* begins running, so handle them all here. */
if (app->deferred_files) {
for (unsigned i=0; i < app->deferred_count; ++i) {
app_open(app, app->deferred_files[i]);
free(app->deferred_files[i]);
}
free(app->deferred_files);
app->deferred_files = NULL;
app->deferred_count = 0;
}

bool needs_redraw = false;
unsigned i = 0;
while (i < app->instance_count) {
needs_redraw |= instance_draw(app->instances[i], app->theme);
if (glfwWindowShouldClose(app->instances[i]->window)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Expand Up @@ -14,6 +14,8 @@ int main(int argc, char** argv) {
.instances_size=0,
.theme=NULL,
.draw_mode=DRAW_SHADED,
.deferred_count=0,
.deferred_files=NULL,
};
app.theme = theme_new_solarized();

Expand Down

0 comments on commit d4683f9

Please sign in to comment.