@@ -15,6 +15,7 @@ use std::cell::RefCell;
1515use std:: hash:: { Hash , Hasher } ;
1616use std:: marker:: PhantomData ;
1717use url:: Url ;
18+ use std:: cell:: Cell ;
1819
1920use crate :: common:: { bookmarks_url, glibctx, BOOKMARK_FILE_PATH } ;
2021use crate :: config;
@@ -51,6 +52,7 @@ pub mod imp {
5152 pub ( crate ) scroll_ctrl : gtk:: EventControllerScroll ,
5253 pub ( crate ) action_previous : RefCell < Option < gio:: SimpleAction > > ,
5354 pub ( crate ) action_next : RefCell < Option < gio:: SimpleAction > > ,
55+ pub ( crate ) style_provider : RefCell < gtk:: CssProvider > ,
5456 }
5557
5658 impl Window {
@@ -136,6 +138,11 @@ impl Window {
136138 let imp = this. imp ( ) ;
137139 imp. config . replace ( config) ;
138140
141+ gtk:: StyleContext :: add_provider_for_display (
142+ & gdk:: Display :: default ( ) . unwrap ( ) ,
143+ & * imp. style_provider . borrow ( ) ,
144+ gtk:: STYLE_PROVIDER_PRIORITY_APPLICATION ,
145+ ) ;
139146 this. bind_signals ( ) ;
140147 this. squeezer_changed ( ) ;
141148 this. setup_actions_signals ( ) ;
@@ -213,23 +220,13 @@ impl Window {
213220 "notify::url" ,
214221 false ,
215222 clone ! ( @weak self as this => @default -panic, move |_| {
216- let mut s = std:: collections:: hash_map:: DefaultHasher :: new( ) ;
217- let url = this. imp( ) . url. borrow( ) ;
218- let url = if let Ok ( domain) = Url :: parse( & url) {
219- if let Some ( domain) = domain. domain( ) {
220- domain. to_string( )
221- } else {
222- url. to_string( )
223- }
224- } else {
225- url. to_string( )
226- } ;
227- url. hash( & mut s) ;
228- let h = s. finish( ) ;
229- Self :: set_special_color_from_hash( h) ;
223+ this. set_special_color_from_hash( ) ;
230224 None
231225 } ) ,
232226 ) ;
227+ adw:: StyleManager :: default ( ) . connect_dark_notify ( clone ! ( @weak self as this => @default -panic, move |_| {
228+ this. set_special_color_from_hash( ) ;
229+ } ) ) ;
233230 }
234231 fn add_tab ( & self ) -> adw:: TabPage {
235232 let imp = self . imp ( ) ;
@@ -381,30 +378,51 @@ impl Window {
381378 fn inner_tab ( & self , tab : & adw:: TabPage ) -> Tab {
382379 tab. child ( ) . downcast ( ) . unwrap ( )
383380 }
384- fn set_special_color_from_hash ( hash : u64 ) {
381+ fn set_special_color_from_hash ( & self ) {
382+ let imp = self . imp ( ) ;
383+ let url = imp. url . borrow ( ) ;
384+ let url = if let Ok ( domain) = Url :: parse ( & url) {
385+ if let Some ( domain) = domain. domain ( ) {
386+ domain. to_string ( )
387+ } else {
388+ url. to_string ( )
389+ }
390+ } else {
391+ url. to_string ( )
392+ } ;
393+ let hash = {
394+ let mut s = std:: collections:: hash_map:: DefaultHasher :: new ( ) ;
395+ url. hash ( & mut s) ;
396+ s. finish ( )
397+ } ;
385398 let hue = hash % 360 ;
386- let stylesheet = format ! (
387- "
388- @define-color view_bg_color hsl({hue}, 100%, 99%);
389- @define-color view_fg_color hsl({hue}, 100%, 12%);
390- @define-color window_bg_color hsl({hue}, 100%, 99%);
391- @define-color window_fg_color hsl({hue}, 100%, 12%);
392- @define-color headerbar_bg_color hsl({hue}, 100%, 96%);
393- @define-color headerbar_fg_color hsl({hue}, 100%, 12%);
394- " ,
395- ) ;
396- Self :: add_stylesheet ( & stylesheet) ;
397- }
398- fn add_stylesheet ( stylesheet : & str ) {
399- // TODO: Adding a provider and keeping it in memory forever
400- // is a memory leak. Fortunately, it's small. Yes, I should fix this
399+ let stylesheet =
400+ if adw:: StyleManager :: default ( ) . is_dark ( ) {
401+ format ! ( "
402+ @define-color view_bg_color hsl({hue}, 20%, 8%);
403+ @define-color view_fg_color hsl({hue}, 100%, 98%);
404+ @define-color window_bg_color hsl({hue}, 20%, 8%);
405+ @define-color window_fg_color hsl({hue}, 100%, 98%);
406+ @define-color headerbar_bg_color hsl({hue}, 80%, 10%);
407+ @define-color headerbar_fg_color hsl({hue}, 100%, 98%);
408+ " )
409+ } else {
410+ format ! (
411+ "
412+ @define-color view_bg_color hsl({hue}, 100%, 99%);
413+ @define-color view_fg_color hsl({hue}, 100%, 12%);
414+ @define-color window_bg_color hsl({hue}, 100%, 99%);
415+ @define-color window_fg_color hsl({hue}, 100%, 12%);
416+ @define-color headerbar_bg_color hsl({hue}, 100%, 96%);
417+ @define-color headerbar_fg_color hsl({hue}, 100%, 12%);
418+ "
419+ )
420+ } ;
401421
402- let provider = gtk:: CssProvider :: new ( ) ;
403- provider. load_from_data ( stylesheet. as_bytes ( ) ) ;
404- gtk:: StyleContext :: add_provider_for_display (
405- & gdk:: Display :: default ( ) . unwrap ( ) ,
406- & provider,
407- gtk:: STYLE_PROVIDER_PRIORITY_APPLICATION ,
422+ imp. style_provider . borrow ( ) . load_from_data ( stylesheet. as_bytes ( ) ) ;
423+ // FIXME: Should add a method on `Tab`...
424+ self . current_tab ( ) . imp ( ) . draw_ctx . borrow ( ) . as_ref ( ) . unwrap ( ) . set_link_color (
425+ & self . style_context ( ) . lookup_color ( "accent_color" ) . unwrap ( )
408426 ) ;
409427 }
410428
0 commit comments