Skip to content

Commit 8a11402

Browse files
Fix brightness applet scroll
1 parent c558678 commit 8a11402

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

applets/brightness/brightness-applet.c

+40-30
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ static void gpm_applet_destroy_cb (GtkWidget *widget);
115115
* too long one will seem unresponsive. */
116116
#define GPM_BRIGHTNESS_APPLET_SLIDER_FREQUENCY 100
117117

118+
/* Brightness percentage changes for each input method */
119+
#define GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_BUTTON 1 /* plus/minus buttons in the popup */
120+
#define GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_ARROW 1 /* arrow keys when the popup is open */
121+
#define GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_SCROLL 5 /* scrolling over the indicator icon */
122+
#define GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_PAGE 10 /* page up/down keys when the popup is open */
123+
124+
118125
/**
119126
* gpm_applet_get_brightness:
120127
* Return value: Success value, or zero for failure
@@ -403,6 +410,26 @@ gpm_applet_update_popup_level (GpmBrightnessApplet *applet)
403410
gpm_applet_update_tooltip (applet);
404411
}
405412

413+
/**
414+
* gpm_applet_adjust_brightness:
415+
* @applet: Brightness applet instance
416+
*
417+
* Change brightness by a given percentage and keep the slider value in sync.
418+
**/
419+
static void
420+
gpm_applet_adjust_brightness (GpmBrightnessApplet *applet, int percent)
421+
{
422+
if (applet->level == -1) {
423+
return;
424+
}
425+
if (percent > 0) {
426+
applet->level = applet->level <= (100 - percent) ? applet->level + percent : 100;
427+
} else {
428+
applet->level = applet->level >= -percent ? applet->level + percent : 0;
429+
}
430+
gtk_range_set_value (GTK_RANGE(applet->slider), applet->level);
431+
}
432+
406433
/**
407434
* gpm_applet_plus_cb:
408435
* @widget: The sending widget (plus button)
@@ -413,11 +440,7 @@ gpm_applet_update_popup_level (GpmBrightnessApplet *applet)
413440
static gboolean
414441
gpm_applet_plus_cb (GtkWidget *w, GpmBrightnessApplet *applet)
415442
{
416-
if (applet->level < 100) {
417-
applet->level++;
418-
}
419-
applet->call_worked = gpm_applet_set_brightness (applet);
420-
gpm_applet_update_popup_level (applet);
443+
gpm_applet_adjust_brightness (applet, GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_BUTTON);
421444
return TRUE;
422445
}
423446

@@ -431,11 +454,7 @@ gpm_applet_plus_cb (GtkWidget *w, GpmBrightnessApplet *applet)
431454
static gboolean
432455
gpm_applet_minus_cb (GtkWidget *w, GpmBrightnessApplet *applet)
433456
{
434-
if (applet->level > 0) {
435-
applet->level--;
436-
}
437-
applet->call_worked = gpm_applet_set_brightness (applet);
438-
gpm_applet_update_popup_level (applet);
457+
gpm_applet_adjust_brightness (applet, -GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_BUTTON);
439458
return TRUE;
440459
}
441460

@@ -487,8 +506,6 @@ gpm_applet_slide_cb (GtkWidget *w, GpmBrightnessApplet *applet)
487506
static gboolean
488507
gpm_applet_key_press_cb (GtkWidget *popup, GdkEventKey *event, GpmBrightnessApplet *applet)
489508
{
490-
int i;
491-
492509
switch (event->keyval) {
493510
case GDK_KEY_KP_Enter:
494511
case GDK_KEY_ISO_Enter:
@@ -508,25 +525,21 @@ gpm_applet_key_press_cb (GtkWidget *popup, GdkEventKey *event, GpmBrightnessAppl
508525
}
509526
break;
510527
case GDK_KEY_Page_Up:
511-
for (i = 0;i < 10;i++) {
512-
gpm_applet_plus_cb (NULL, applet);
513-
}
528+
gpm_applet_adjust_brightness (applet, GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_PAGE);
514529
return TRUE;
515530
break;
516531
case GDK_KEY_Left:
517532
case GDK_KEY_Up:
518-
gpm_applet_plus_cb (NULL, applet);
533+
gpm_applet_adjust_brightness (applet, GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_ARROW);
519534
return TRUE;
520535
break;
521536
case GDK_KEY_Page_Down:
522-
for (i = 0;i < 10;i++) {
523-
gpm_applet_minus_cb (NULL, applet);
524-
}
537+
gpm_applet_adjust_brightness (applet, -GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_PAGE);
525538
return TRUE;
526539
break;
527540
case GDK_KEY_Right:
528541
case GDK_KEY_Down:
529-
gpm_applet_minus_cb (NULL, applet);
542+
gpm_applet_adjust_brightness (applet, -GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_ARROW);
530543
return TRUE;
531544
break;
532545
default:
@@ -549,18 +562,11 @@ gpm_applet_key_press_cb (GtkWidget *popup, GdkEventKey *event, GpmBrightnessAppl
549562
static gboolean
550563
gpm_applet_scroll_cb (GpmBrightnessApplet *applet, GdkEventScroll *event)
551564
{
552-
int i;
553-
554565
if (event->type == GDK_SCROLL) {
555566
if (event->direction == GDK_SCROLL_UP) {
556-
for (i = 0;i < 5;i++) {
557-
gpm_applet_plus_cb (NULL, applet);
558-
}
559-
567+
gpm_applet_adjust_brightness (applet, GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_SCROLL);
560568
} else {
561-
for (i = 0;i < 5;i++) {
562-
gpm_applet_minus_cb (NULL, applet);
563-
}
569+
gpm_applet_adjust_brightness (applet, -GPM_BRIGHTNESS_APPLET_PERCENT_CHANGE_SCROLL);
564570
}
565571
return TRUE;
566572
}
@@ -710,7 +716,7 @@ gpm_applet_popup_cb (GpmBrightnessApplet *applet, GdkEventButton *event)
710716
/* otherwise pop */
711717
applet->popped = TRUE;
712718

713-
/* create a new popup (initial or if panel parameters changed) */
719+
/* create a new popup (if panel parameters changed) */
714720
if (applet->popup == NULL) {
715721
gpm_applet_create_popup (applet);
716722
}
@@ -1026,6 +1032,10 @@ gpm_brightness_applet_init (GpmBrightnessApplet *applet)
10261032
mate_panel_applet_set_flags (MATE_PANEL_APPLET (applet), MATE_PANEL_APPLET_EXPAND_MINOR);
10271033
gtk_widget_set_events (GTK_WIDGET (applet), GDK_SCROLL_MASK);
10281034

1035+
/* Create popup (slider) upfront, so that we can keep it
1036+
* up to date from scroll events before it's ever opened. */
1037+
gpm_applet_create_popup (applet);
1038+
10291039
/* show */
10301040
gtk_widget_show_all (GTK_WIDGET(applet));
10311041

0 commit comments

Comments
 (0)