Skip to content

Commit

Permalink
feat(config): have overridable defaults
Browse files Browse the repository at this point in the history
Defaults:

 - `save_dir`
 - `line_size`
 - `text_font`
 - `text_size`

Closes #1
  • Loading branch information
jtheoof committed Jan 13, 2020
1 parent 7f2f6da commit f0d6b08
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 42 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ Grab a swappshot from a specific window under Sway, using `swaymsg` and `jq`:
grim -g "$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)" - | swappy -f -
```

## Config

The config file is located at `$XDG_CONFIG_HOME/swappy/config` or at `$HOME/.config/swappy/config`.

The file follows the GLib `conf` format. See the `man` page for details. There is example config file [here](example/config).

The following lines can be used as swappy's default:

```
[Default]
save_dir=$HOME/Desktop
line_size=5
text_size=20
text_font=sans-serif
```

- `save_dir` is where swappshots will be saved, can contain env variables and must exist in your filesystem
- `line_size` is the default line size (must be between 1 and 50)
- `text_size` is the default text size (must be between 10 and 50)
- `text_font` is the font used to render text, its format is pango friendly

## Keyboard Shortcuts

- `Ctrl+b`: Toggle Paint Panel
Expand Down
5 changes: 5 additions & 0 deletions example/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Default]
save_dir=$HOME/Desktop
line_size=5
text_size=20
text_font=sans-serif
7 changes: 6 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include "swappy.h"

bool config_get_storage_path(struct swappy_state *state);
#define CONFIG_LINE_SIZE_DEFAULT 5
#define CONFIG_TEXT_FONT_DEFAULT "sans-serif"
#define CONFIG_TEXT_SIZE_DEFAULT 20

void config_load(struct swappy_state *state);
void config_free(struct swappy_state *state);
21 changes: 13 additions & 8 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

#define GEOMETRY_PATTERN "xx,yy wwxhh"

#define SWAPPY_STROKE_SIZE_DEFAULT 5
#define SWAPPY_STROKE_SIZE_MIN 1
#define SWAPPY_STROKE_SIZE_MAX 50
#define SWAPPY_LINE_SIZE_MIN 1
#define SWAPPY_LINE_SIZE_MAX 50

#define SWAPPY_TEXT_FONT_DEFAULT "serif"
#define SWAPPY_TEXT_SIZE_DEFAULT 20
#define SWAPPY_TEXT_SIZE_MIN 10
#define SWAPPY_TEXT_SIZE_MAX 50

Expand Down Expand Up @@ -49,6 +46,7 @@ struct swappy_paint_text {
double b;
double a;
double s;
gchar *font;
gchar *text;
size_t cursor;
struct swappy_point from;
Expand Down Expand Up @@ -124,7 +122,7 @@ struct swappy_state_ui {
GtkRadioButton *custom;
GtkColorButton *color;

GtkButton *stroke_size;
GtkButton *line_size;
GtkButton *text_size;
};

Expand Down Expand Up @@ -170,17 +168,24 @@ struct swappy_wayland {
#endif
};

struct swappy_config {
char *config_file;
char *save_dir;
guint32 line_size;
guint32 text_size;
char *text_font;
};

struct swappy_state {
GtkApplication *app;

struct swappy_state_ui *ui;
struct swappy_config *config;
struct swappy_wayland *wl;

cairo_surface_t *cairo_surface;
GList *patterns; // List of cairo_pattern_t

char *storage_path;

enum swappy_paint_type mode;

/* Options */
Expand Down
40 changes: 23 additions & 17 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "buffer.h"
#include "clipboard.h"
#include "config.h"
#include "file.h"
#include "notification.h"
#include "paint.h"
Expand All @@ -22,7 +23,7 @@ static void update_ui_undo_redo(struct swappy_state *state) {
}

static void update_ui_stroke_size_widget(struct swappy_state *state) {
GtkButton *button = GTK_BUTTON(state->ui->stroke_size);
GtkButton *button = GTK_BUTTON(state->ui->line_size);
char label[255];
snprintf(label, 255, "%.0lf", state->settings.w);
gtk_button_set_label(button, label);
Expand Down Expand Up @@ -115,15 +116,15 @@ static void action_stroke_size_decrease(struct swappy_state *state) {

state->settings.w -= step;

if (state->settings.w < SWAPPY_STROKE_SIZE_MIN) {
state->settings.w = SWAPPY_STROKE_SIZE_MIN;
if (state->settings.w < SWAPPY_LINE_SIZE_MIN) {
state->settings.w = SWAPPY_LINE_SIZE_MIN;
}

update_ui_stroke_size_widget(state);
}

static void action_stroke_size_reset(struct swappy_state *state) {
state->settings.w = SWAPPY_STROKE_SIZE_DEFAULT;
state->settings.w = state->config->line_size;

update_ui_stroke_size_widget(state);
}
Expand All @@ -132,8 +133,8 @@ static void action_stroke_size_increase(struct swappy_state *state) {
guint step = state->settings.w >= 10 ? 5 : 1;
state->settings.w += step;

if (state->settings.w > SWAPPY_STROKE_SIZE_MAX) {
state->settings.w = SWAPPY_STROKE_SIZE_MAX;
if (state->settings.w > SWAPPY_LINE_SIZE_MAX) {
state->settings.w = SWAPPY_LINE_SIZE_MAX;
}

update_ui_stroke_size_widget(state);
Expand All @@ -150,7 +151,7 @@ static void action_text_size_decrease(struct swappy_state *state) {
update_ui_text_size_widget(state);
}
static void action_text_size_reset(struct swappy_state *state) {
state->settings.t = SWAPPY_TEXT_SIZE_DEFAULT;
state->settings.t = state->config->text_size;
update_ui_text_size_widget(state);
}
static void action_text_size_increase(struct swappy_state *state) {
Expand Down Expand Up @@ -188,14 +189,14 @@ void application_finish(struct swappy_state *state) {
paint_free_all(state);
buffer_free_all(state);
cairo_surface_destroy(state->cairo_surface);
g_free(state->storage_path);
g_free(state->file_str);
g_free(state->geometry_str);
g_free(state->geometry);
g_free(state->ui);
g_object_unref(state->app);

wayland_finish(state);
config_free(state);
}

static void action_save_area_to_file(struct swappy_state *state) {
Expand All @@ -213,7 +214,7 @@ static void action_save_area_to_file(struct swappy_state *state) {
c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
char path[MAX_PATH];
snprintf(path, MAX_PATH, "%s/%s %s.png", state->storage_path, "Swappshot",
snprintf(path, MAX_PATH, "%s/%s %s.png", state->config->save_dir, "Swappshot",
c_time_string);
gdk_pixbuf_savev(pixbuf, path, "png", NULL, NULL, &error);

Expand Down Expand Up @@ -561,7 +562,7 @@ static bool load_layout(struct swappy_state *state) {
state->ui->color =
GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button"));

state->ui->stroke_size =
state->ui->line_size =
GTK_BUTTON(gtk_builder_get_object(builder, "stroke-size-button"));
state->ui->text_size =
GTK_BUTTON(gtk_builder_get_object(builder, "text-size-button"));
Expand Down Expand Up @@ -616,9 +617,21 @@ static gboolean is_file_from_stdin(const char *file) {
return (strcmp(file, "-") == 0);
}

static void init_settings(struct swappy_state *state) {
state->settings.r = 1;
state->settings.g = 0;
state->settings.b = 0;
state->settings.a = 1;
state->settings.w = state->config->line_size;
state->settings.t = state->config->text_size;
}

static gint command_line_handler(GtkApplication *app,
GApplicationCommandLine *cmdline,
struct swappy_state *state) {
config_load(state);
init_settings(state);

if (!wayland_init(state)) {
g_warning(
"error while initializing wayland objects, can only be used in file "
Expand Down Expand Up @@ -688,13 +701,6 @@ bool application_init(struct swappy_state *state) {
g_signal_connect(state->app, "command-line", G_CALLBACK(command_line_handler),
state);

state->settings.r = 1;
state->settings.g = 0;
state->settings.b = 0;
state->settings.a = 1;
state->settings.w = SWAPPY_STROKE_SIZE_DEFAULT;
state->settings.t = SWAPPY_TEXT_SIZE_DEFAULT;

return true;
}

Expand Down
Loading

0 comments on commit f0d6b08

Please sign in to comment.