Skip to content

Commit

Permalink
Merge pull request #6 from luxluth/feat/svg-png-icons-path
Browse files Browse the repository at this point in the history
Feat: svg png icons path
  • Loading branch information
luxluth authored Dec 18, 2024
2 parents 6c7419d + 529b56c commit a4aab81
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 79 deletions.
21 changes: 0 additions & 21 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,9 @@ impl Default for GeneralConf {
pub struct Config {
pub general: GeneralConf,
pub css: String,
pub css_path: PathBuf,
pub _conf_path: PathBuf,
}

impl Config {
fn reload_css(&mut self) {
let mut css = DEFAULT_CSS.to_string();
let css_path = self.css_path.clone();
if css_path.exists() {
if let Ok(mut f) = std::fs::File::open(&css_path) {
css = String::new();
let _ = f.read_to_string(&mut css);
}
}

self.css = css;
}

pub fn get_conf(conf_path: &PathBuf) -> GeneralConf {
let mut general = GeneralConf::default();
if let Ok(mut f) = std::fs::File::open(conf_path) {
Expand Down Expand Up @@ -112,10 +97,6 @@ impl Config {
return general;
}

pub fn reload(&mut self) {
self.reload_css();
}

pub fn parse(path: std::path::PathBuf) -> Self {
let mut css = DEFAULT_CSS.to_string();
let css_path = path.parent().unwrap().join("style.css");
Expand All @@ -133,8 +114,6 @@ impl Config {
return Self {
general: Self::get_conf(&path),
css,
css_path,
_conf_path: path,
};
}
}
Expand Down
36 changes: 31 additions & 5 deletions src/icons.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
use std::io::Read;

use crate::resources;
use gtk::gio;
use gtk::{
gdk_pixbuf::Pixbuf,
gio::{self, Icon},
};

pub fn load_image_resource(content: &[u8], size: (i32, i32)) -> Icon {
let cursor = gtk::gio::MemoryInputStream::from_bytes(&gtk::glib::Bytes::from(content));
Icon::from(
Pixbuf::from_stream_at_scale(
&cursor,
size.0,
size.1,
true,
None::<&gtk::gio::Cancellable>,
)
.unwrap(),
)
}

pub fn get_icon(name: &str) -> gio::Icon {
pub fn get_icon(name_or_path: &str) -> Icon {
let mut res = resources::ICON_MAP.get().write().unwrap();
if let Some(e) = res.get(name) {
if let Some(e) = res.get(name_or_path) {
return e.clone();
} else {
let icon = gio::Icon::from(gio::ThemedIcon::from_names(&[name]));
res.insert(name.to_string(), icon.clone());
if std::path::Path::new(name_or_path).exists() {
if let Ok(mut f) = std::fs::File::open(std::path::Path::new(name_or_path)) {
let mut buf = vec![];
let _ = f.read_to_end(&mut buf);
return load_image_resource(&buf, (512, 512));
}
}
let icon = Icon::from(gio::ThemedIcon::from_names(&[name_or_path]));
res.insert(name_or_path.to_string(), icon.clone());
return icon;
}
}
72 changes: 19 additions & 53 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::sync::Arc;
use tokio::runtime::Runtime;
use tokio::sync::RwLock;

use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};
use rust_i18n::t;
use search::SearchManager;
use tokio::runtime::Runtime;

mod app;
mod bus;
Expand All @@ -19,15 +16,10 @@ mod ui;

rust_i18n::i18n!("locales", fallback = "en");

async fn activate(
current_css_provider: Arc<RwLock<gtk::CssProvider>>,
config: Arc<RwLock<conf::Config>>,
app: &Application,
) {
fn activate(config: conf::Config, app: &Application) {
let settings = gtk::Settings::default().expect("Failed to create GTK settings.");
settings.set_gtk_icon_theme_name(Some(&config.read().await.general.theme));
settings.set_gtk_icon_theme_name(Some(&config.general.theme));

let rt = Runtime::new().expect("Unable to create Runtime");
let window = ApplicationWindow::builder()
.application(app)
.title("seekr")
Expand All @@ -52,34 +44,17 @@ async fn activate(
.hexpand(true)
.css_name("input")
.activates_default(true)
.placeholder_text(&config.read().await.general.search_placeholder)
.placeholder_text(&config.general.search_placeholder)
.build();

let represent_action = gtk::gio::SimpleAction::new("represent", None);
let conf_arc_clone = config.clone();
represent_action.connect_activate(glib::clone!(
#[weak]
window,
#[strong]
tomanager,
#[weak]
entry,
move |_, _| {
let _ = tomanager.send(search::SearchEvent::Represent);
let rt = Runtime::new().expect("Unable to create Runtime");
rt.block_on(async {
let mut conf = conf_arc_clone.write().await;
let ccp = current_css_provider.read().await;
conf.reload();
entry.set_placeholder_text(Some(&conf.general.search_placeholder.clone()));
let new_css_provider = load_css(conf.css.clone(), Some(ccp.clone()));
drop(ccp);
drop(conf);
{
let mut ccp = current_css_provider.write().await;
*ccp = new_css_provider;
}
});
window.present();
}
));
Expand Down Expand Up @@ -239,9 +214,8 @@ async fn activate(
entries_box.append(&title);
}

let cfg = rt.block_on(async { config.clone().read().await.clone() });
for entry in entries {
let button = ui::EntryButton(&cfg, entry, &tomanager);
let button = ui::EntryButton(&config, entry, &tomanager);
entries_box.append(&button);
}

Expand All @@ -267,7 +241,7 @@ async fn activate(
}
}

fn load_css(css: String, previous_provider: Option<gtk::CssProvider>) -> gtk::CssProvider {
fn load_css(css: String, previous_provider: Option<gtk::CssProvider>) {
let provider = gtk::CssProvider::new();
provider.load_from_string(&css);

Expand All @@ -283,38 +257,30 @@ fn load_css(css: String, previous_provider: Option<gtk::CssProvider>) -> gtk::Cs
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);

return provider;
}

fn main() {
let rt = Runtime::new().expect("Unable to create Runtime");
let _enter = rt.enter();
rust_i18n::set_locale(&locale::get_locale());
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_thread_ids(true)
.with_timer(tracing_subscriber::fmt::time::time())
.init();

let config = conf::Config::parse(conf::init_config_dir());
if bus::app_is_running() {
bus::send_represent_event();
} else {
let rt = Runtime::new().expect("Unable to create Runtime");
let _enter = rt.enter();
rust_i18n::set_locale(&locale::get_locale());
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_thread_ids(true)
.with_timer(tracing_subscriber::fmt::time::time())
.init();

let config = conf::Config::parse(conf::init_config_dir());

gtk::init().expect("Unable to init gtk");
let current_css_provider = load_css(config.css.clone(), None);
load_css(config.css.clone(), None);

let application = Application::new(Some(conf::APP_ID), Default::default());

application.connect_activate(move |app| {
rt.block_on(async {
activate(
Arc::new(RwLock::new(current_css_provider.clone())),
Arc::new(RwLock::new(config.clone())),
app,
)
.await
});
activate(config.clone(), app);
});

application.run();
Expand Down

0 comments on commit a4aab81

Please sign in to comment.