Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add auto saving on app exit #168

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
tuanbass marked this conversation as resolved.
Show resolved Hide resolved

## 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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (state->config->auto_save) {
if (state->config->auto_save && state->output_file != NULL) {

Copy link
Contributor Author

@tuanbass tuanbass Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need to check NULL for state->output_file here, as in case output_file=NULL, in save_state_to_file_or_folder the file name is generated.

Actually, with the NULL check, at least in my case, the file is not auto saved.
I'm using swappy in sway using the command as specified in the README:

grim -g "$(slurp)" - | swappy -f -

every time I make a screenshot, then I close the swappy, the filename is NULL so the picture is not saved.

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
Loading