Skip to content

Commit

Permalink
feat(ui): life is full of colors and joy
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 26, 2019
1 parent 2bdab68 commit a8c8be3
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 12 deletions.
9 changes: 9 additions & 0 deletions include/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void copy_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void save_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void clear_clicked_handler(GtkWidget *widget, struct swappy_state *state);

void color_red_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void color_green_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void color_blue_clicked_handler(GtkWidget *widget, struct swappy_state *state);

void color_custom_clicked_handler(GtkWidget *widget,
struct swappy_state *state);
void color_custom_color_set_handler(GtkWidget *widget,
struct swappy_state *state);
13 changes: 13 additions & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ struct swappy_box {
int32_t height;
};

struct swappy_state_painting {
double r;
double g;
double b;
double a;
double w;
};

struct swappy_state_ui {
GtkWindow *window;
GtkWidget *area;
Expand All @@ -79,6 +87,9 @@ struct swappy_state_ui {
GtkRadioButton *rectangle;
GtkRadioButton *ellipse;
GtkRadioButton *arrow;

GtkRadioButton *red;
GtkColorButton *custom;
};

struct swappy_state {
Expand Down Expand Up @@ -114,6 +125,8 @@ struct swappy_state {
GSList *redo_paints;
struct swappy_paint *temp_paint;

struct swappy_state_painting painting;

int argc;
char **argv;
};
Expand Down
16 changes: 16 additions & 0 deletions res/style/swappy.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
.drawing .text-button {
font-family: "FontAwesome 5 Free Solid";
}

.color-box image {
border-radius: 50px;
}

.color-box .color-red image {
background-color: rgb(255, 0, 0);
}

.color-box .color-green image {
background-color: rgb(0, 255, 0);
}

.color-box .color-blue image {
background-color: rgb(0, 0, 255);
}
146 changes: 145 additions & 1 deletion res/swappy.ui
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<property name="image">edit-redo</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="redo_clicked_handler" swapped="no"/>
<accelerator key="y" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
<accelerator key="z" signal="clicked" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
<accelerator key="y" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
</object>
<packing>
<property name="expand">True</property>
Expand Down Expand Up @@ -233,6 +233,7 @@
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">15</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkRadioButton" id="brush">
Expand Down Expand Up @@ -324,6 +325,149 @@
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="brush-box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="valign">baseline</property>
<property name="spacing">10</property>
<child>
<object class="GtkRadioButton" id="color-red-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<property name="draw_indicator">False</property>
<signal name="clicked" handler="color_red_clicked_handler" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<style>
<class name="color-red"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="color-green-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<property name="draw_indicator">False</property>
<property name="group">color-red-button</property>
<signal name="clicked" handler="color_green_clicked_handler" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<style>
<class name="color-green"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="color-blue-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<property name="draw_indicator">False</property>
<property name="group">color-red-button</property>
<signal name="clicked" handler="color_blue_clicked_handler" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<style>
<class name="color-blue"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="color-button-custom">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<property name="margin_left">20</property>
<property name="draw_indicator">False</property>
<property name="group">color-red-button</property>
<signal name="clicked" handler="color_custom_clicked_handler" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-color-picker</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkColorButton" id="custom-color-button">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<signal name="color-set" handler="color_custom_color_set_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<style>
<class name="color-box"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="resize">False</property>
Expand Down
58 changes: 57 additions & 1 deletion src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ static void switch_mode_to_arrow(struct swappy_state *state) {
state->mode = SWAPPY_PAINT_MODE_ARROW;
}

static void action_update_color_state(struct swappy_state *state, double r,
double g, double b) {
state->painting.r = r;
state->painting.g = g;
state->painting.b = b;
}

void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
switch_mode_to_brush(state);
}
Expand Down Expand Up @@ -319,9 +326,47 @@ void draw_area_button_release_handler(GtkWidget *widget, GdkEventButton *event,
}
}

void color_red_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_update_color_state(state, 1, 0, 0);
gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false);
}

void color_green_clicked_handler(GtkWidget *widget,
struct swappy_state *state) {
action_update_color_state(state, 0, 1, 0);
gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false);
}

void color_blue_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_update_color_state(state, 0, 0, 1);
gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false);
}

void color_custom_clicked_handler(GtkWidget *widget,
struct swappy_state *state) {
gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), true);
}

void color_group_set_inactive(gpointer data, gpointer user_data) {
GtkToggleButton *button = GTK_TOGGLE_BUTTON(data);

gtk_toggle_button_set_active(button, false);
}

void color_custom_color_set_handler(GtkWidget *widget,
struct swappy_state *state) {
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);

state->painting.r = color.red;
state->painting.g = color.green;
state->painting.b = color.blue;
state->painting.a = color.alpha;
}

static void apply_css(GtkWidget *widget, GtkStyleProvider *provider) {
gtk_style_context_add_provider(gtk_widget_get_style_context(widget), provider,
1);
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
if (GTK_IS_CONTAINER(widget)) {
gtk_container_forall(GTK_CONTAINER(widget), (GtkCallback)apply_css,
provider);
Expand Down Expand Up @@ -371,6 +416,11 @@ static bool load_layout(struct swappy_state *state) {
GtkRadioButton *arrow =
GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "arrow"));

state->ui->red =
GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "color-red-button"));
state->ui->custom =
GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button"));

// gtk_popover_set_relative_to(ui, area);
gtk_widget_set_size_request(area, geometry->width, geometry->height);

Expand Down Expand Up @@ -462,6 +512,12 @@ bool application_init(struct swappy_state *state) {
g_signal_connect(state->app, "command-line", G_CALLBACK(command_line_handler),
state);

state->painting.r = 1;
state->painting.g = 0;
state->painting.b = 0;
state->painting.a = 1;
state->painting.w = 2;

return true;
}

Expand Down
26 changes: 16 additions & 10 deletions src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
struct swappy_paint *paint = g_new(struct swappy_paint, 1);
struct swappy_point *brush;

double r = state->painting.r;
double g = state->painting.g;
double b = state->painting.b;
double a = state->painting.a;
double w = state->painting.w;

paint->type = type;

switch (type) {
case SWAPPY_PAINT_MODE_BRUSH:
paint->can_draw = true;

paint->content.brush.r = 1;
paint->content.brush.g = 0;
paint->content.brush.b = 0;
paint->content.brush.a = 1;
paint->content.brush.w = 2;
paint->content.brush.r = r;
paint->content.brush.g = g;
paint->content.brush.b = b;
paint->content.brush.a = a;
paint->content.brush.w = w;

brush = g_new(struct swappy_point, 1);
brush->x = x;
Expand All @@ -60,11 +66,11 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,

paint->content.shape.from.x = x;
paint->content.shape.from.y = y;
paint->content.shape.r = 1;
paint->content.shape.g = 0;
paint->content.shape.b = 0;
paint->content.shape.a = 1;
paint->content.shape.w = 2;
paint->content.shape.r = r;
paint->content.shape.g = g;
paint->content.shape.b = b;
paint->content.shape.a = a;
paint->content.shape.w = w;
paint->content.shape.type = type;
break;
default:
Expand Down

0 comments on commit a8c8be3

Please sign in to comment.