@@ -55,11 +55,16 @@ void mainImage(out vec4 fragColor,
5555"#
5656. as_bytes ( ) ;
5757
58+ static mut SHADER : Option < gsk:: GLShader > = None ;
59+
5860mod imp {
5961 use super :: * ;
6062
6163 #[ derive( Default ) ]
6264 pub ( crate ) struct Background {
65+ pub ( super ) settings : OnceCell < gio:: Settings > ,
66+ pub ( super ) settings_handler : RefCell < Option < glib:: SignalHandlerId > > ,
67+
6368 pub ( super ) chat_theme : RefCell < Option < tdlib:: types:: ChatTheme > > ,
6469
6570 pub ( super ) background_texture : RefCell < Option < gdk:: Texture > > ,
@@ -92,6 +97,21 @@ mod imp {
9297
9398 let obj = self . obj ( ) ;
9499
100+ let settings = gio:: Settings :: new ( crate :: config:: APP_ID ) ;
101+ let settings_handler = settings. connect_changed (
102+ Some ( "theme-name" ) ,
103+ clone ! ( @weak obj => move |_, _| {
104+ let imp = obj. imp( ) ;
105+
106+ if imp. chat_theme. borrow( ) . is_none( ) {
107+ obj. refresh_theme( imp. dark. get( ) ) ;
108+ }
109+ } ) ,
110+ ) ;
111+
112+ self . settings . set ( settings) . unwrap ( ) ;
113+ self . settings_handler . replace ( Some ( settings_handler) ) ;
114+
95115 let pattern = gdk:: Texture :: from_resource ( "/app/drey/paper-plane/images/pattern.svg" ) ;
96116
97117 self . pattern . set ( pattern) . unwrap ( ) ;
@@ -140,6 +160,12 @@ mod imp {
140160
141161 self . animation . set ( animation) . unwrap ( ) ;
142162 }
163+
164+ fn dispose ( & self ) {
165+ if let Some ( settings) = self . settings . get ( ) {
166+ settings. disconnect ( self . settings_handler . take ( ) . unwrap ( ) ) ;
167+ }
168+ }
143169 }
144170
145171 impl WidgetImpl for Background {
@@ -432,6 +458,12 @@ impl Background {
432458 fn ensure_shader ( & self ) {
433459 let imp = self . imp ( ) ;
434460 if imp. shader . borrow ( ) . is_none ( ) {
461+ unsafe {
462+ if SHADER . is_some ( ) {
463+ imp. shader . replace ( SHADER . clone ( ) ) ;
464+ }
465+ }
466+
435467 let renderer = self . native ( ) . unwrap ( ) . renderer ( ) ;
436468
437469 let shader = gsk:: GLShader :: from_bytes ( & GRADIENT_SHADER . into ( ) ) ;
@@ -443,7 +475,11 @@ impl Background {
443475 self . add_css_class ( "fallback" ) ;
444476 }
445477 Ok ( _) => {
446- imp. shader . replace ( Some ( shader) ) ;
478+ imp. shader . replace ( Some ( shader. clone ( ) ) ) ;
479+
480+ unsafe {
481+ SHADER = Some ( shader) ;
482+ }
447483 }
448484 }
449485 } ;
@@ -458,11 +494,25 @@ impl Background {
458494 } ;
459495
460496 self . set_theme ( theme) ;
461-
462- // For some reason tdlib tells that light theme is dark
463- self . imp ( ) . dark . set ( dark) ;
464497 } else {
465- self . set_theme ( & hard_coded_themes ( dark) ) ;
498+ let chat_theme = self
499+ . ancestor ( crate :: Session :: static_type ( ) )
500+ . and_downcast :: < crate :: Session > ( )
501+ . map ( |s| s. default_chat_theme ( ) )
502+ . unwrap_or ( crate :: utils:: default_theme ( ) ) ;
503+
504+ if dark {
505+ self . set_theme ( & chat_theme. dark_settings ) ;
506+ } else {
507+ self . set_theme ( & chat_theme. light_settings ) ;
508+ }
509+ }
510+
511+ // For some reason tdlib tells that light theme is dark
512+ self . imp ( ) . dark . set ( dark) ;
513+
514+ if let Some ( animation) = self . imp ( ) . animation . get ( ) {
515+ animation. notify ( "value" ) ;
466516 }
467517 }
468518}
@@ -496,50 +546,3 @@ fn vec3_to_rgba(vec3: &graphene::Vec3) -> gdk::RGBA {
496546 let [ red, green, blue] = vec3. to_float ( ) ;
497547 gdk:: RGBA :: new ( red, green, blue, 1.0 )
498548}
499-
500- fn hard_coded_themes ( dark : bool ) -> tdlib:: types:: ThemeSettings {
501- fn theme (
502- dark : bool ,
503- bg_colors : Vec < i32 > ,
504- message_colors : Vec < i32 > ,
505- ) -> tdlib:: types:: ThemeSettings {
506- use tdlib:: enums:: BackgroundFill :: * ;
507- use tdlib:: enums:: BackgroundType :: Fill ;
508- use tdlib:: types:: * ;
509-
510- ThemeSettings {
511- background : Some ( Background {
512- is_default : true ,
513- is_dark : dark,
514- r#type : Fill ( BackgroundTypeFill {
515- fill : FreeformGradient ( BackgroundFillFreeformGradient { colors : bg_colors } ) ,
516- } ) ,
517- id : 0 ,
518- name : String :: new ( ) ,
519- document : None ,
520- } ) ,
521- accent_color : 0 ,
522- animate_outgoing_message_fill : false ,
523- outgoing_message_accent_color : 0 ,
524- outgoing_message_fill : FreeformGradient ( BackgroundFillFreeformGradient {
525- colors : message_colors,
526- } ) ,
527- }
528- }
529-
530- // tr tl bl br
531-
532- if dark {
533- theme (
534- dark,
535- vec ! [ 0xd6932e , 0xbc40db , 0x4280d7 , 0x614ed5 ] ,
536- vec ! [ 0x2d52ab , 0x4036a1 , 0x9f388d , 0x9d3941 ] ,
537- )
538- } else {
539- theme (
540- dark,
541- vec ! [ 0x94dae9 , 0x9aeddb , 0x94c3f6 , 0xac96f7 ] ,
542- vec ! [ 0xddecff , 0xe0ddfd , 0xdbffff , 0xddffdf ] ,
543- )
544- }
545- }
0 commit comments