Skip to content

Commit

Permalink
feat: auto saving on quit app
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanbass committed Mar 15, 2024
1 parent 09a4e99 commit 32e72ce
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ text_font=sans-serif
paint_mode=brush
early_exit=false
fill_shape=false
auto_save=false
```

- `save_dir` is where swappshots will be saved, can contain env variables, when it does not exist, swappy attempts to create it first, but does not abort if directory creation fails
Expand All @@ -62,6 +63,7 @@ fill_shape=false
- `paint_mode` is the mode activated at application start (must be one of: brush|text|rectangle|ellipse|arrow|blur, matching is case-insensitive)
- `early_exit` is used to make the application exit after saving the picture or copying it to the clipboard
- `fill_shape` is used to toggle shape filling (for the rectangle and ellipsis tools) on or off upon startup
- `auto_save` is used to toggle auto saving on exit the application

## Keyboard Shortcuts

Expand Down
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define CONFIG_PAINT_MODE_DEFAULT SWAPPY_PAINT_MODE_BRUSH
#define CONFIG_EARLY_EXIT_DEFAULT false
#define CONFIG_FILL_SHAPE_DEFAULT false
#define CONFIG_AUTO_SAVE_DEFAULT false

void config_load(struct swappy_state *state);
void config_free(struct swappy_state *state);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ struct swappy_config {
guint32 text_size;
char *text_font;
gboolean early_exit;
gboolean auto_save;
};

struct swappy_state {
Expand Down
2 changes: 1 addition & 1 deletion src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ static void save_state_to_file_or_folder(struct swappy_state *state,
}

static void maybe_save_output_file(struct swappy_state *state) {
if (state->output_file != NULL) {
if (state->config->auto_save) {
save_state_to_file_or_folder(state, state->output_file);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static void print_config(struct swappy_config *config) {
g_info("paint_mode: %d", config->paint_mode);
g_info("early_exit: %d", config->early_exit);
g_info("fill_shape: %d", config->fill_shape);
g_info("auto_save: %d", config->auto_save);
}

static char *get_default_save_dir() {
Expand Down Expand Up @@ -81,6 +82,7 @@ static void load_config_from_file(struct swappy_config *config,
gchar *paint_mode = NULL;
gboolean early_exit;
gboolean fill_shape;
gboolean auto_save;
GError *error = NULL;

if (file == NULL) {
Expand Down Expand Up @@ -232,6 +234,16 @@ static void load_config_from_file(struct swappy_config *config,
error = NULL;
}

auto_save = g_key_file_get_boolean(gkf, group, "auto_save", &error);

if (error == NULL) {
config->auto_save = auto_save;
} else {
g_info("auto_save is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

g_key_file_free(gkf);
}

Expand All @@ -249,6 +261,7 @@ static void load_default_config(struct swappy_config *config) {
config->paint_mode = CONFIG_PAINT_MODE_DEFAULT;
config->early_exit = CONFIG_EARLY_EXIT_DEFAULT;
config->fill_shape = CONFIG_FILL_SHAPE_DEFAULT;
config->auto_save = CONFIG_AUTO_SAVE_DEFAULT;
}

void config_load(struct swappy_state *state) {
Expand Down

0 comments on commit 32e72ce

Please sign in to comment.