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 Sep 8, 2020
1 parent 8dc2a66 commit a9a8a36
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 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
17 changes: 13 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "slurp.h"
#include "render.h"

#define BG_COLOR 0xFFFFFF40
#define BORDER_COLOR 0x000000FF
#define SELECTION_COLOR 0x00000000

static void noop() {
// This space intentionally left blank
}
Expand Down Expand Up @@ -637,6 +641,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"
" -o Select a display output.\n"
Expand Down Expand Up @@ -736,9 +741,10 @@ int main(int argc, char *argv[]) {

struct slurp_state state = {
.colors = {
.background = 0xFFFFFF40,
.border = 0x000000FF,
.selection = 0x00000000,
.background = BG_COLOR,
.border = BORDER_COLOR,
.selection = SELECTION_COLOR,
.choice = BG_COLOR,
},
.border_weight = 2,
.display_dimensions = false,
Expand All @@ -747,7 +753,7 @@ int main(int argc, char *argv[]) {
int opt;
char *format = "%x,%y %wx%h\n";
bool output_boxes = false;
while ((opt = getopt(argc, argv, "hdb:c:s:w:pof:")) != -1) {
while ((opt = getopt(argc, argv, "hdb:c:s:B:w:pof:")) != -1) {
switch (opt) {
case 'h':
printf("%s", usage);
Expand All @@ -764,6 +770,9 @@ int main(int argc, char *argv[]) {
case 's':
state.colors.selection = parse_color(optarg);
break;
case 'B':
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 box_layout_to_output(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;
box_layout_to_output(&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;
box_layout_to_output(&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.

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

*-w* _weight_
Set border weight.

Expand Down

0 comments on commit a9a8a36

Please sign in to comment.