13
13
#include "swaybar/ipc.h"
14
14
#include "swaybar/render.h"
15
15
#include "swaybar/status_line.h"
16
- #include "log.h"
17
16
#if HAVE_TRAY
18
17
#include "swaybar/tray/tray.h"
19
18
#endif
20
19
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
21
20
22
21
static const int WS_HORIZONTAL_PADDING = 5 ;
23
22
static const double WS_VERTICAL_PADDING = 1.5 ;
24
- static const double BORDER_WIDTH = 1 ;
23
+ static const int BORDER_WIDTH = 1 ;
25
24
26
25
struct render_context {
27
26
cairo_t * cairo ;
@@ -541,62 +540,74 @@ static uint32_t render_status_line(struct render_context *ctx, double *x) {
541
540
return 0 ;
542
541
}
543
542
544
- static uint32_t render_binding_mode_indicator (struct render_context * ctx ,
545
- double x ) {
543
+ static struct box_size render_box (struct render_context * ctx , double x ,
544
+ struct box_colors colors , const char * label , bool pango_markup ) {
546
545
struct swaybar_output * output = ctx -> output ;
547
- const char * mode = output -> bar -> mode ;
548
- if (!mode ) {
549
- return 0 ;
550
- }
551
-
552
- cairo_t * cairo = ctx -> cairo ;
553
546
struct swaybar_config * config = output -> bar -> config ;
547
+ cairo_t * cairo = ctx -> cairo ;
548
+
554
549
int text_width , text_height ;
555
550
get_text_size (cairo , config -> font_description , & text_width , & text_height , NULL ,
556
- 1 , output -> bar -> mode_pango_markup ,
557
- "%s" , mode );
551
+ 1 , pango_markup , "%s" , label );
558
552
559
- int ws_vertical_padding = WS_VERTICAL_PADDING ;
560
- int ws_horizontal_padding = WS_HORIZONTAL_PADDING ;
561
- int border_width = BORDER_WIDTH ;
553
+ uint32_t width = text_width + WS_HORIZONTAL_PADDING * 2 + BORDER_WIDTH * 2 ;
554
+ if (width < config -> workspace_min_width ) {
555
+ width = config -> workspace_min_width ;
556
+ }
562
557
563
- uint32_t ideal_height = text_height + ws_vertical_padding * 2
564
- + border_width * 2 ;
558
+ uint32_t ideal_height = text_height + WS_VERTICAL_PADDING * 2
559
+ + BORDER_WIDTH * 2 ;
565
560
uint32_t ideal_surface_height = ideal_height ;
566
561
if (!output -> bar -> config -> height &&
567
562
output -> height < ideal_surface_height ) {
568
- return ideal_surface_height ;
569
- }
570
- uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2 ;
571
- if (width < config -> workspace_min_width ) {
572
- width = config -> workspace_min_width ;
563
+ return (struct box_size ) {
564
+ .width = width ,
565
+ .height = ideal_surface_height ,
566
+ };
573
567
}
574
568
575
569
uint32_t height = output -> height ;
576
570
cairo_set_operator (cairo , CAIRO_OPERATOR_SOURCE );
577
- cairo_set_source_u32 (cairo , config -> colors . binding_mode .background );
578
- ctx -> background_color = config -> colors . binding_mode .background ;
579
- ctx -> has_transparency |= (config -> colors . binding_mode .background & 0xFF ) != 0xFF ;
571
+ cairo_set_source_u32 (cairo , colors .background );
572
+ ctx -> background_color = colors .background ;
573
+ ctx -> has_transparency |= (colors .background & 0xFF ) != 0xFF ;
580
574
cairo_rectangle (cairo , x , 0 , width , height );
581
575
cairo_fill (cairo );
582
576
583
- cairo_set_source_u32 (cairo , config -> colors . binding_mode .border );
584
- cairo_rectangle (cairo , x , 0 , width , border_width );
577
+ cairo_set_source_u32 (cairo , colors .border );
578
+ cairo_rectangle (cairo , x , 0 , width , BORDER_WIDTH );
585
579
cairo_fill (cairo );
586
- cairo_rectangle (cairo , x , 0 , border_width , height );
580
+ cairo_rectangle (cairo , x , 0 , BORDER_WIDTH , height );
587
581
cairo_fill (cairo );
588
- cairo_rectangle (cairo , x + width - border_width , 0 , border_width , height );
582
+ cairo_rectangle (cairo , x + width - BORDER_WIDTH , 0 , BORDER_WIDTH , height );
589
583
cairo_fill (cairo );
590
- cairo_rectangle (cairo , x , height - border_width , width , border_width );
584
+ cairo_rectangle (cairo , x , height - BORDER_WIDTH , width , BORDER_WIDTH );
591
585
cairo_fill (cairo );
592
586
593
587
double text_y = height / 2.0 - text_height / 2.0 ;
594
- cairo_set_source_u32 (cairo , config -> colors . binding_mode .text );
588
+ cairo_set_source_u32 (cairo , colors .text );
595
589
cairo_move_to (cairo , x + width / 2 - text_width / 2 , (int )floor (text_y ));
596
- choose_text_aa_mode (ctx , config -> colors .binding_mode .text );
597
- render_text (cairo , config -> font_description , 1 , output -> bar -> mode_pango_markup ,
598
- "%s" , mode );
599
- return output -> height ;
590
+ choose_text_aa_mode (ctx , colors .text );
591
+ render_text (cairo , config -> font_description , 1 , pango_markup ,
592
+ "%s" , label );
593
+
594
+ return (struct box_size ) {
595
+ .width = width ,
596
+ .height = output -> height ,
597
+ };
598
+ }
599
+
600
+ static uint32_t render_binding_mode_indicator (struct render_context * ctx ,
601
+ double x ) {
602
+ struct swaybar_output * output = ctx -> output ;
603
+ const char * mode = output -> bar -> mode ;
604
+ if (!mode ) {
605
+ return 0 ;
606
+ }
607
+
608
+ struct box_size size = render_box (ctx , x , output -> bar -> config -> colors .binding_mode ,
609
+ mode , output -> bar -> mode_pango_markup );
610
+ return size .height ;
600
611
}
601
612
602
613
static enum hotspot_event_handling workspace_hotspot_callback (
@@ -618,6 +629,7 @@ static uint32_t render_workspace_button(struct render_context *ctx,
618
629
struct swaybar_workspace * ws , double * x ) {
619
630
struct swaybar_output * output = ctx -> output ;
620
631
struct swaybar_config * config = output -> bar -> config ;
632
+
621
633
struct box_colors box_colors ;
622
634
if (ws -> urgent ) {
623
635
box_colors = config -> colors .urgent_workspace ;
@@ -629,66 +641,21 @@ static uint32_t render_workspace_button(struct render_context *ctx,
629
641
box_colors = config -> colors .inactive_workspace ;
630
642
}
631
643
632
- uint32_t height = output -> height ;
633
-
634
- cairo_t * cairo = ctx -> cairo ;
635
- int text_width , text_height ;
636
- get_text_size (cairo , config -> font_description , & text_width , & text_height , NULL ,
637
- 1 , config -> pango_markup , "%s" , ws -> label );
638
-
639
- int ws_vertical_padding = WS_VERTICAL_PADDING ;
640
- int ws_horizontal_padding = WS_HORIZONTAL_PADDING ;
641
- int border_width = BORDER_WIDTH ;
642
-
643
- uint32_t ideal_height = ws_vertical_padding * 2 + text_height
644
- + border_width * 2 ;
645
- uint32_t ideal_surface_height = ideal_height ;
646
- if (!output -> bar -> config -> height &&
647
- output -> height < ideal_surface_height ) {
648
- return ideal_surface_height ;
649
- }
650
-
651
- uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2 ;
652
- if (width < config -> workspace_min_width ) {
653
- width = config -> workspace_min_width ;
654
- }
655
-
656
- cairo_set_operator (cairo , CAIRO_OPERATOR_SOURCE );
657
- cairo_set_source_u32 (cairo , box_colors .background );
658
- ctx -> background_color = box_colors .background ;
659
- ctx -> has_transparency |= (box_colors .background & 0xFF ) != 0xFF ;
660
- cairo_rectangle (cairo , * x , 0 , width , height );
661
- cairo_fill (cairo );
662
-
663
- cairo_set_source_u32 (cairo , box_colors .border );
664
- cairo_rectangle (cairo , * x , 0 , width , border_width );
665
- cairo_fill (cairo );
666
- cairo_rectangle (cairo , * x , 0 , border_width , height );
667
- cairo_fill (cairo );
668
- cairo_rectangle (cairo , * x + width - border_width , 0 , border_width , height );
669
- cairo_fill (cairo );
670
- cairo_rectangle (cairo , * x , height - border_width , width , border_width );
671
- cairo_fill (cairo );
672
-
673
- double text_y = height / 2.0 - text_height / 2.0 ;
674
- cairo_set_source_u32 (cairo , box_colors .text );
675
- cairo_move_to (cairo , * x + width / 2 - text_width / 2 , (int )floor (text_y ));
676
- choose_text_aa_mode (ctx , box_colors .text );
677
- render_text (cairo , config -> font_description , 1 , config -> pango_markup ,
678
- "%s" , ws -> label );
644
+ struct box_size size = render_box (ctx , * x , box_colors ,
645
+ ws -> label , config -> pango_markup );
679
646
680
647
struct swaybar_hotspot * hotspot = calloc (1 , sizeof (struct swaybar_hotspot ));
681
648
hotspot -> x = * x ;
682
649
hotspot -> y = 0 ;
683
- hotspot -> width = width ;
684
- hotspot -> height = height ;
650
+ hotspot -> width = size . width ;
651
+ hotspot -> height = size . height ;
685
652
hotspot -> callback = workspace_hotspot_callback ;
686
653
hotspot -> destroy = free ;
687
654
hotspot -> data = strdup (ws -> name );
688
655
wl_list_insert (& output -> hotspots , & hotspot -> link );
689
656
690
- * x += width ;
691
- return output -> height ;
657
+ * x += size . width ;
658
+ return size . height ;
692
659
}
693
660
694
661
static uint32_t render_to_cairo (struct render_context * ctx ) {
0 commit comments