Skip to content

Commit

Permalink
Always render choice boxes
Browse files Browse the repository at this point in the history
To make it easier for the user to see what the available options are,
also make the color for the choice boxes configurable.
  • Loading branch information
tmccombs committed Aug 23, 2020
1 parent d165731 commit 1dcef31
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct slurp_state {
uint32_t background;
uint32_t border;
uint32_t selection;
uint32_t choice;
} colors;

uint32_t border_weight;
Expand Down
7 changes: 6 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ static const char usage[] =
" -b #rrggbbaa Set background color.\n"
" -c #rrggbbaa Set border color.\n"
" -s #rrggbbaa Set selection color.\n"
" -B #rrggbbaa Set option box color.\n"
" -w n Set border weight.\n"
" -f s Set output format.\n"
" -p Select a single point.\n";
Expand Down Expand Up @@ -706,14 +707,15 @@ int main(int argc, char *argv[]) {
.background = 0xFFFFFF40,
.border = 0x000000FF,
.selection = 0x00000000,
.choice = 0xCCCCFF40,
},
.border_weight = 2,
.display_dimensions = false,
};

int opt;
char *format = "%x,%y %wx%h";
while ((opt = getopt(argc, argv, "hdb:c:s:w:pf:")) != -1) {
while ((opt = getopt(argc, argv, "hdb:c:s:B:w:pf:")) != -1) {
switch (opt) {
case 'h':
printf("%s", usage);
Expand All @@ -730,6 +732,9 @@ int main(int argc, char *argv[]) {
case 's':
state.colors.selection = parse_color(optarg);
break;
case 'o':
state.colors.choice = parse_color(optarg);
break;
case 'f':
format = optarg;
break;
Expand Down
42 changes: 28 additions & 14 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ static void set_source_u32(cairo_t *cairo, uint32_t color) {
(color >> (0 * 8) & 0xFF) / 255.0);
}

static void draw_rect(cairo_t *cairo, struct slurp_box *box, uint32_t color, int32_t scale) {
set_source_u32(cairo, color);
cairo_rectangle(cairo, box->x * scale, box->y * scale,
box->width * scale, box->height * scale);
}

static void adjust_box(struct slurp_box *box, struct slurp_output *output) {
box->x -= output->logical_geometry.x;
box->y -= output->logical_geometry.y;
}

void render(struct slurp_output *output) {
struct slurp_state *state = output->state;
struct pool_buffer *buffer = output->current_buffer;
Expand All @@ -24,18 +35,26 @@ void render(struct slurp_output *output) {
set_source_u32(cairo, state->colors.background);
cairo_paint(cairo);

// Draw option boxes from input
struct slurp_box *choice_box;
wl_list_for_each(choice_box, &state->boxes, link) {
if (box_intersect(&output->logical_geometry,
choice_box)) {
struct slurp_box b = *choice_box;
adjust_box(&b, output);
draw_rect(cairo, &b, state->colors.choice, scale);
cairo_fill(cairo);
}
}

struct slurp_seat *seat;
wl_list_for_each(seat, &state->seats, link) {
struct slurp_selection *current_selection =
seat->touch_selection.has_selection ?
&seat->touch_selection :
&seat->pointer_selection;

if (!seat->wl_pointer) {
continue;
}

if (!current_selection->has_selection) {
if (!seat->wl_pointer || !current_selection->has_selection) {
continue;
}

Expand All @@ -44,19 +63,14 @@ void render(struct slurp_output *output) {
continue;
}
struct slurp_box b = current_selection->selection;
b.x -= output->logical_geometry.x;
b.y -= output->logical_geometry.y;
adjust_box(&b, output);

// Draw border
set_source_u32(cairo, state->colors.selection);
cairo_rectangle(cairo, b.x * scale, b.y * scale,
b.width * scale, b.height * scale);
draw_rect(cairo, &b, state->colors.selection, scale);
cairo_fill(cairo);

set_source_u32(cairo, state->colors.border);
// Draw border
cairo_set_line_width(cairo, state->border_weight * scale);
cairo_rectangle(cairo, b.x * scale, b.y * scale,
b.width * scale, b.height * scale);
draw_rect(cairo, &b, state->colors.border, scale);
cairo_stroke(cairo);

if (state->display_dimensions) {
Expand Down
4 changes: 4 additions & 0 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ held, the selection is moved instead of being resized.
*-s* _color_
Set selection color. See *COLORS* for more detail.

*-o* _color_
Set color for highlighting predefined rectangles from standard input when not
selected.

*-w* _weight_
Set border weight.

Expand Down

0 comments on commit 1dcef31

Please sign in to comment.