mirror of
https://github.com/FelixKratz/SketchyBar
synced 2024-11-26 21:30:17 +00:00
refactor slider and only handle drag internally -> fire only one event on slider release
This commit is contained in:
parent
5a59ebb2da
commit
b3342faa98
4 changed files with 44 additions and 42 deletions
|
@ -175,24 +175,11 @@ void bar_item_needs_update(struct bar_item* bar_item) {
|
|||
}
|
||||
|
||||
void bar_item_on_drag(struct bar_item* bar_item, CGPoint point) {
|
||||
if (!bar_item->has_slider) return;
|
||||
|
||||
float delta = point.x - bar_item->slider.background.bounds.origin.x;
|
||||
if (delta < 0) delta = 0;
|
||||
uint32_t percentage = min(delta
|
||||
/ bar_item->slider.background.bounds.size.width
|
||||
* 100.f + 0.5f, 100);
|
||||
|
||||
if (percentage == bar_item->slider.prev_drag_percentage) return;
|
||||
bar_item->slider.prev_drag_percentage = percentage;
|
||||
char perc_str[8];
|
||||
snprintf(perc_str, 8, "%d", percentage);
|
||||
env_vars_set(&bar_item->signal_args.env_vars,
|
||||
string_copy("PERCENTAGE"),
|
||||
string_copy(perc_str) );
|
||||
|
||||
if (bar_item->update_mask & UPDATE_MOUSE_CLICKED)
|
||||
bar_item_update(bar_item, COMMAND_SUBSCRIBE_MOUSE_CLICKED, true, NULL);
|
||||
if (bar_item->has_slider) {
|
||||
if (slider_handle_drag(&bar_item->slider, point)) {
|
||||
bar_item_needs_update(bar_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifier, CGPoint point) {
|
||||
|
@ -207,19 +194,17 @@ void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifi
|
|||
string_copy(get_modifier_description(modifier)));
|
||||
|
||||
if (bar_item->has_slider) {
|
||||
if (CGRectContainsPoint(bar_item->slider.background.bounds, point)) {
|
||||
float delta = point.x - bar_item->slider.background.bounds.origin.x;
|
||||
if (delta < 0) delta = 0;
|
||||
uint32_t percentage = min(delta
|
||||
/ bar_item->slider.background.bounds.size.width
|
||||
* 100.f + 0.5f, 100);
|
||||
|
||||
if (bar_item->slider.is_dragged
|
||||
|| CGRectContainsPoint(bar_item->slider.background.bounds, point)) {
|
||||
slider_handle_drag(&bar_item->slider, point);
|
||||
char perc_str[8];
|
||||
snprintf(perc_str, 8, "%d", percentage);
|
||||
|
||||
snprintf(perc_str, 8, "%d", bar_item->slider.percentage);
|
||||
env_vars_set(&bar_item->signal_args.env_vars,
|
||||
string_copy("PERCENTAGE"),
|
||||
string_copy(perc_str) );
|
||||
bar_item->slider.prev_drag_percentage = NO_DRAG;
|
||||
bar_item->slider.is_dragged = false;
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -176,6 +176,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED) {
|
|||
|
||||
debug("item: %s\n", bar_item ? bar_item->name : "NULL");
|
||||
bar_item_on_drag(bar_item, point_in_window_coords);
|
||||
bar_manager_refresh(&g_bar_manager, false);
|
||||
|
||||
CFRelease(context);
|
||||
return EVENT_SUCCESS;
|
||||
|
|
39
src/slider.c
39
src/slider.c
|
@ -2,12 +2,6 @@
|
|||
#include "bar_manager.h"
|
||||
#include "animation.h"
|
||||
|
||||
static bool slider_set_percentage(struct slider* slider, uint32_t percentage) {
|
||||
if (percentage == slider->percentage) return false;
|
||||
slider->percentage = min(percentage, 100);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool slider_set_width(struct slider* slider, uint32_t width) {
|
||||
if (width == slider->background.bounds.size.width) return false;
|
||||
slider->background.bounds.size.width = width;
|
||||
|
@ -20,10 +14,31 @@ static bool slider_set_foreground_color(struct slider* slider, uint32_t color) {
|
|||
return background_set_color(&slider->foreground, color);
|
||||
}
|
||||
|
||||
static bool slider_set_percentage(struct slider* slider, uint32_t percentage) {
|
||||
if (percentage == slider->percentage) return false;
|
||||
slider->percentage = max(min(percentage, 100), 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t slider_get_percentage_for_point(struct slider* slider, CGPoint point) {
|
||||
float delta = point.x - slider->background.bounds.origin.x;
|
||||
if (delta < 0) delta = 0;
|
||||
uint32_t percentage = delta / slider->background.bounds.size.width * 100.f
|
||||
+ 0.5f;
|
||||
|
||||
return min(percentage, 100);
|
||||
}
|
||||
|
||||
bool slider_handle_drag(struct slider* slider, CGPoint point) {
|
||||
uint32_t percentage = slider_get_percentage_for_point(slider, point);
|
||||
slider->is_dragged = true;
|
||||
return slider_set_percentage(slider, percentage);
|
||||
}
|
||||
|
||||
void slider_init(struct slider* slider) {
|
||||
slider->percentage = 0;
|
||||
slider->prev_drag_percentage = NO_DRAG;
|
||||
slider->background.bounds.size.width = 100;
|
||||
slider->is_dragged = false;
|
||||
|
||||
slider->foreground_color = 0xff0000ff;
|
||||
text_init(&slider->knob);
|
||||
|
@ -108,10 +123,12 @@ bool slider_parse_sub_domain(struct slider* slider, FILE* rsp, struct token prop
|
|||
bool needs_refresh = false;
|
||||
if (token_equals(property, PROPERTY_PERCENTAGE)) {
|
||||
struct token token = get_token(&message);
|
||||
ANIMATE(slider_set_percentage,
|
||||
slider,
|
||||
slider->percentage,
|
||||
token_to_uint32t(token));
|
||||
if (!slider->is_dragged) {
|
||||
ANIMATE(slider_set_percentage,
|
||||
slider,
|
||||
slider->percentage,
|
||||
token_to_uint32t(token));
|
||||
}
|
||||
}
|
||||
else if (token_equals(property, PROPERTY_HIGHLIGHT_COLOR)) {
|
||||
struct token token = get_token(&message);
|
||||
|
|
|
@ -3,14 +3,11 @@
|
|||
#include "background.h"
|
||||
#include "text.h"
|
||||
|
||||
#define NO_DRAG -1
|
||||
|
||||
struct slider {
|
||||
bool is_dragged;
|
||||
uint32_t percentage;
|
||||
uint32_t foreground_color;
|
||||
|
||||
int32_t prev_drag_percentage;
|
||||
|
||||
struct text knob;
|
||||
struct background background;
|
||||
struct background foreground;
|
||||
|
@ -21,7 +18,9 @@ void slider_clear_pointers(struct slider* slider);
|
|||
void slider_setup(struct slider* slider, uint32_t width);
|
||||
void slider_calculate_bounds(struct slider* slider, uint32_t x, uint32_t y);
|
||||
void slider_draw(struct slider* slider, CGContextRef context);
|
||||
bool slider_handle_drag(struct slider* slider, CGPoint point);
|
||||
|
||||
uint32_t slider_get_percentage_for_point(struct slider* slider, CGPoint point);
|
||||
uint32_t slider_get_length(struct slider* slider);
|
||||
|
||||
void slider_destroy(struct slider* slider);
|
||||
|
|
Loading…
Reference in a new issue