Skip to content

Commit 904f4a7

Browse files
committed
dark mode with colors
1 parent 50a81e9 commit 904f4a7

File tree

3 files changed

+59
-38
lines changed

3 files changed

+59
-38
lines changed

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub const LOCALEDIR: &str = "/app/share/locale";
44
pub const PKGDATADIR: &str = "/app/share/geopard";
55
pub const PROFILE: &str = "Devel";
66
pub const RESOURCES_FILE: &str = concat!("/app/share/geopard", "/resources.gresource");
7-
pub const VERSION: &str = "1.0.0-alpha-bd80d58";
7+
pub const VERSION: &str = "1.0.0-alpha-50a81e9";
88

99
use once_cell::sync::Lazy;
1010
use serde::{Deserialize, Serialize};

src/draw_ctx.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ impl DrawCtx {
120120
.name(name)
121121
.build()
122122
}
123+
pub fn set_link_color(&self, color: &gtk::gdk::RGBA) {
124+
self.text_buffer.tag_table().lookup("a").unwrap().set_foreground_rgba(Some(color));
125+
}
123126
pub fn insert_heading(&self, text_iter: &mut gtk::TextIter, line: &str) {
124127
let n = line.chars().filter(|c| *c == '#').count();
125128
let line = line.trim_start_matches('#').trim_start();
@@ -185,7 +188,7 @@ impl DrawCtx {
185188
.weight(config.weight)
186189
.build();
187190

188-
tag.set_foreground(Some("#1c71d8"));
191+
tag.set_foreground_rgba(self.text_view.style_context().lookup_color("accent_color").as_ref());
189192
tag.set_underline(gtk::pango::Underline::Low);
190193

191194
Self::set_linkhandler(&tag, link.clone());

src/window.rs

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::cell::RefCell;
1515
use std::hash::{Hash, Hasher};
1616
use std::marker::PhantomData;
1717
use url::Url;
18+
use std::cell::Cell;
1819

1920
use crate::common::{bookmarks_url, glibctx, BOOKMARK_FILE_PATH};
2021
use 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

Comments
 (0)