99#include "pango.h"
1010#include "sway/config.h"
1111#include "sway/sway_text_node.h"
12+ #include "sway/output.h"
1213
1314struct cairo_buffer {
1415 struct wlr_buffer base ;
@@ -163,6 +164,41 @@ static void render_backing_buffer(struct text_buffer *buffer) {
163164 cairo_font_options_destroy (fo );
164165}
165166
167+ static void text_calc_size (struct text_buffer * buffer ) {
168+ struct sway_text_node * props = & buffer -> props ;
169+ props -> width = 0 ;
170+
171+ cairo_t * c = cairo_create (NULL );
172+ if (!c ) {
173+ sway_log (SWAY_ERROR , "cairo_t allocation failed" );
174+ return ;
175+ }
176+
177+ cairo_set_antialias (c , CAIRO_ANTIALIAS_BEST );
178+ for (int i = 0 ; i < root -> outputs -> length ; ++ i ) {
179+ struct sway_output * output = root -> outputs -> items [i ];
180+ if (!output -> wlr_output -> enabled ) {
181+ continue ;
182+ }
183+
184+ int size , baseline ;
185+ get_text_size (c , config -> font_description , & size , NULL ,
186+ & baseline , output -> wlr_output -> scale , props -> pango_markup , "%s" , buffer -> text );
187+
188+ size = ceil ((float )size / output -> wlr_output -> scale );
189+ if (props -> width < size ) {
190+ props -> width = size ;
191+ props -> baseline = (float )baseline / output -> wlr_output -> scale ;
192+ }
193+ }
194+ cairo_destroy (c );
195+
196+ printf ("%i" , props -> width );
197+
198+ wlr_scene_buffer_set_dest_size (buffer -> buffer_node ,
199+ get_text_width (props ), props -> height );
200+ }
201+
166202static void handle_outputs_update (struct wl_listener * listener , void * data ) {
167203 struct text_buffer * buffer = wl_container_of (listener , buffer , outputs_update );
168204 struct wlr_scene_outputs_update_event * event = data ;
@@ -194,6 +230,7 @@ static void handle_outputs_update(struct wl_listener *listener, void *data) {
194230 if (scale != buffer -> scale || subpixel != buffer -> subpixel ) {
195231 buffer -> scale = scale ;
196232 buffer -> subpixel = subpixel ;
233+ text_calc_size (buffer );
197234 render_backing_buffer (buffer );
198235 }
199236}
@@ -208,24 +245,6 @@ static void handle_destroy(struct wl_listener *listener, void *data) {
208245 free (buffer );
209246}
210247
211- static void text_calc_size (struct text_buffer * buffer ) {
212- struct sway_text_node * props = & buffer -> props ;
213-
214- cairo_t * c = cairo_create (NULL );
215- if (!c ) {
216- sway_log (SWAY_ERROR , "cairo_t allocation failed" );
217- return ;
218- }
219-
220- cairo_set_antialias (c , CAIRO_ANTIALIAS_BEST );
221- get_text_size (c , config -> font_description , & props -> width , NULL ,
222- & props -> baseline , 1 , props -> pango_markup , "%s" , buffer -> text );
223- cairo_destroy (c );
224-
225- wlr_scene_buffer_set_dest_size (buffer -> buffer_node ,
226- get_text_width (props ), props -> height );
227- }
228-
229248struct sway_text_node * sway_text_node_create (struct wlr_scene_tree * parent ,
230249 char * text , float color [4 ], bool pango_markup ) {
231250 struct text_buffer * buffer = calloc (1 , sizeof (* buffer ));
0 commit comments